blob: 57c7829c88fe7f9c1f20dd72c984ca4f0c260cb3 [file] [log] [blame]
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ART_COMPILER_OPTIMIZING_NODES_H_
18#define ART_COMPILER_OPTIMIZING_NODES_H_
19
David Brazdil8d5b8b22015-03-24 10:51:52 +000020#include "base/arena_containers.h"
Mathieu Chartierb666f482015-02-18 14:33:14 -080021#include "base/arena_object.h"
Calin Juravle27df7582015-04-17 19:12:31 +010022#include "dex/compiler_enums.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000023#include "entrypoints/quick/quick_entrypoints_enum.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000024#include "handle.h"
25#include "handle_scope.h"
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000026#include "invoke_type.h"
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +010027#include "locations.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000028#include "mirror/class.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010029#include "offsets.h"
Ian Rogerse63db272014-07-15 15:36:11 -070030#include "primitive.h"
Nicolas Geoffray0e336432014-02-26 18:24:38 +000031#include "utils/arena_bit_vector.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000032#include "utils/growable_array.h"
33
34namespace art {
35
David Brazdil1abb4192015-02-17 18:33:36 +000036class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000037class HBasicBlock;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010038class HCurrentMethod;
David Brazdil8d5b8b22015-03-24 10:51:52 +000039class HDoubleConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010040class HEnvironment;
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +010041class HFakeString;
David Brazdil8d5b8b22015-03-24 10:51:52 +000042class HFloatConstant;
David Brazdilfc6a86a2015-06-26 10:33:45 +000043class HGraphBuilder;
David Brazdil8d5b8b22015-03-24 10:51:52 +000044class HGraphVisitor;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000045class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000046class HIntConstant;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000047class HInvoke;
David Brazdil8d5b8b22015-03-24 10:51:52 +000048class HLongConstant;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +000049class HNullConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010050class HPhi;
Nicolas Geoffray3c049742014-09-24 18:10:46 +010051class HSuspendCheck;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010052class LiveInterval;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000053class LocationSummary;
Nicolas Geoffraydb216f42015-05-05 17:02:20 +010054class SlowPathCode;
David Brazdil8d5b8b22015-03-24 10:51:52 +000055class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000056
57static const int kDefaultNumberOfBlocks = 8;
58static const int kDefaultNumberOfSuccessors = 2;
59static const int kDefaultNumberOfPredecessors = 2;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010060static const int kDefaultNumberOfDominatedBlocks = 1;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000061static const int kDefaultNumberOfBackEdges = 1;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000062
Calin Juravle9aec02f2014-11-18 23:06:35 +000063static constexpr uint32_t kMaxIntShiftValue = 0x1f;
64static constexpr uint64_t kMaxLongShiftValue = 0x3f;
65
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010066static constexpr uint32_t kUnknownFieldIndex = static_cast<uint32_t>(-1);
67
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +010068static constexpr InvokeType kInvalidInvokeType = static_cast<InvokeType>(-1);
69
Dave Allison20dfc792014-06-16 20:44:29 -070070enum IfCondition {
71 kCondEQ,
72 kCondNE,
73 kCondLT,
74 kCondLE,
75 kCondGT,
76 kCondGE,
77};
78
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010079class HInstructionList {
80 public:
81 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
82
83 void AddInstruction(HInstruction* instruction);
84 void RemoveInstruction(HInstruction* instruction);
85
David Brazdilc3d743f2015-04-22 13:40:50 +010086 // Insert `instruction` before/after an existing instruction `cursor`.
87 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
88 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
89
Roland Levillain6b469232014-09-25 10:10:38 +010090 // Return true if this list contains `instruction`.
91 bool Contains(HInstruction* instruction) const;
92
Roland Levillainccc07a92014-09-16 14:48:16 +010093 // Return true if `instruction1` is found before `instruction2` in
94 // this instruction list and false otherwise. Abort if none
95 // of these instructions is found.
96 bool FoundBefore(const HInstruction* instruction1,
97 const HInstruction* instruction2) const;
98
Nicolas Geoffray276d9da2015-02-02 18:24:11 +000099 bool IsEmpty() const { return first_instruction_ == nullptr; }
100 void Clear() { first_instruction_ = last_instruction_ = nullptr; }
101
102 // Update the block of all instructions to be `block`.
103 void SetBlockOfInstructions(HBasicBlock* block) const;
104
105 void AddAfter(HInstruction* cursor, const HInstructionList& instruction_list);
106 void Add(const HInstructionList& instruction_list);
107
David Brazdil2d7352b2015-04-20 14:52:42 +0100108 // Return the number of instructions in the list. This is an expensive operation.
109 size_t CountSize() const;
110
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100111 private:
112 HInstruction* first_instruction_;
113 HInstruction* last_instruction_;
114
115 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000116 friend class HGraph;
117 friend class HInstruction;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100118 friend class HInstructionIterator;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100119 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100120
121 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
122};
123
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000124// Control-flow graph of a method. Contains a list of basic blocks.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700125class HGraph : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000126 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100127 HGraph(ArenaAllocator* arena,
128 const DexFile& dex_file,
129 uint32_t method_idx,
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100130 bool should_generate_constructor_barrier,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700131 InstructionSet instruction_set,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100132 InvokeType invoke_type = kInvalidInvokeType,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100133 bool debuggable = false,
134 int start_instruction_id = 0)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000135 : arena_(arena),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000136 blocks_(arena, kDefaultNumberOfBlocks),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100137 reverse_post_order_(arena, kDefaultNumberOfBlocks),
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100138 linear_order_(arena, kDefaultNumberOfBlocks),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700139 entry_block_(nullptr),
140 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100141 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100142 number_of_vregs_(0),
143 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000144 temporaries_vreg_slots_(0),
Mark Mendell1152c922015-04-24 17:06:35 -0400145 has_bounds_checks_(false),
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000146 debuggable_(debuggable),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000147 current_instruction_id_(start_instruction_id),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100148 dex_file_(dex_file),
149 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100150 invoke_type_(invoke_type),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100151 in_ssa_form_(false),
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100152 should_generate_constructor_barrier_(should_generate_constructor_barrier),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700153 instruction_set_(instruction_set),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000154 cached_null_constant_(nullptr),
155 cached_int_constants_(std::less<int32_t>(), arena->Adapter()),
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000156 cached_float_constants_(std::less<int32_t>(), arena->Adapter()),
157 cached_long_constants_(std::less<int64_t>(), arena->Adapter()),
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100158 cached_double_constants_(std::less<int64_t>(), arena->Adapter()),
159 cached_current_method_(nullptr) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000160
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000161 ArenaAllocator* GetArena() const { return arena_; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100162 const GrowableArray<HBasicBlock*>& GetBlocks() const { return blocks_; }
Roland Levillain93445682014-10-06 19:24:02 +0100163 HBasicBlock* GetBlock(size_t id) const { return blocks_.Get(id); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000164
David Brazdil69ba7b72015-06-23 18:27:30 +0100165 bool IsInSsaForm() const { return in_ssa_form_; }
166
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000167 HBasicBlock* GetEntryBlock() const { return entry_block_; }
168 HBasicBlock* GetExitBlock() const { return exit_block_; }
David Brazdilc7af85d2015-05-26 12:05:55 +0100169 bool HasExitBlock() const { return exit_block_ != nullptr; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000170
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000171 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
172 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000173
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000174 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100175
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000176 // Try building the SSA form of this graph, with dominance computation and loop
177 // recognition. Returns whether it was successful in doing all these steps.
178 bool TryBuildingSsa() {
179 BuildDominatorTree();
Nicolas Geoffrayd3350832015-03-12 11:16:23 +0000180 // The SSA builder requires loops to all be natural. Specifically, the dead phi
181 // elimination phase checks the consistency of the graph when doing a post-order
182 // visit for eliminating dead phis: a dead phi can only have loop header phi
183 // users remaining when being visited.
184 if (!AnalyzeNaturalLoops()) return false;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000185 TransformToSsa();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100186 in_ssa_form_ = true;
Nicolas Geoffrayd3350832015-03-12 11:16:23 +0000187 return true;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000188 }
189
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100190 void ComputeDominanceInformation();
191 void ClearDominanceInformation();
192
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000193 void BuildDominatorTree();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000194 void TransformToSsa();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100195 void SimplifyCFG();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000196
Nicolas Geoffrayf5370122014-12-02 11:51:19 +0000197 // Analyze all natural loops in this graph. Returns false if one
198 // loop is not natural, that is the header does not dominate the
199 // back edge.
200 bool AnalyzeNaturalLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100201
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000202 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
203 void InlineInto(HGraph* outer_graph, HInvoke* invoke);
204
Mingyao Yang3584bce2015-05-19 16:01:59 -0700205 // Need to add a couple of blocks to test if the loop body is entered and
206 // put deoptimization instructions, etc.
207 void TransformLoopHeaderForBCE(HBasicBlock* header);
208
David Brazdil2d7352b2015-04-20 14:52:42 +0100209 // Removes `block` from the graph.
210 void DeleteDeadBlock(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000211
David Brazdilfc6a86a2015-06-26 10:33:45 +0000212 // Splits the edge between `block` and `successor` while preserving the
213 // indices in the predecessor/successor lists. If there are multiple edges
214 // between the blocks, the lowest indices are used.
215 // Returns the new block which is empty and has the same dex pc as `successor`.
216 HBasicBlock* SplitEdge(HBasicBlock* block, HBasicBlock* successor);
217
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100218 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
219 void SimplifyLoop(HBasicBlock* header);
220
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000221 int32_t GetNextInstructionId() {
222 DCHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000223 return current_instruction_id_++;
224 }
225
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000226 int32_t GetCurrentInstructionId() const {
227 return current_instruction_id_;
228 }
229
230 void SetCurrentInstructionId(int32_t id) {
231 current_instruction_id_ = id;
232 }
233
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100234 uint16_t GetMaximumNumberOfOutVRegs() const {
235 return maximum_number_of_out_vregs_;
236 }
237
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000238 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
239 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100240 }
241
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100242 void UpdateMaximumNumberOfOutVRegs(uint16_t other_value) {
243 maximum_number_of_out_vregs_ = std::max(maximum_number_of_out_vregs_, other_value);
244 }
245
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000246 void UpdateTemporariesVRegSlots(size_t slots) {
247 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100248 }
249
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000250 size_t GetTemporariesVRegSlots() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100251 DCHECK(!in_ssa_form_);
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000252 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100253 }
254
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100255 void SetNumberOfVRegs(uint16_t number_of_vregs) {
256 number_of_vregs_ = number_of_vregs;
257 }
258
259 uint16_t GetNumberOfVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100260 DCHECK(!in_ssa_form_);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100261 return number_of_vregs_;
262 }
263
264 void SetNumberOfInVRegs(uint16_t value) {
265 number_of_in_vregs_ = value;
266 }
267
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100268 uint16_t GetNumberOfLocalVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100269 DCHECK(!in_ssa_form_);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100270 return number_of_vregs_ - number_of_in_vregs_;
271 }
272
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100273 const GrowableArray<HBasicBlock*>& GetReversePostOrder() const {
274 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100275 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100276
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100277 const GrowableArray<HBasicBlock*>& GetLinearOrder() const {
278 return linear_order_;
279 }
280
Mark Mendell1152c922015-04-24 17:06:35 -0400281 bool HasBoundsChecks() const {
282 return has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800283 }
284
Mark Mendell1152c922015-04-24 17:06:35 -0400285 void SetHasBoundsChecks(bool value) {
286 has_bounds_checks_ = value;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800287 }
288
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100289 bool ShouldGenerateConstructorBarrier() const {
290 return should_generate_constructor_barrier_;
291 }
292
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000293 bool IsDebuggable() const { return debuggable_; }
294
David Brazdil8d5b8b22015-03-24 10:51:52 +0000295 // Returns a constant of the given type and value. If it does not exist
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000296 // already, it is created and inserted into the graph. This method is only for
297 // integral types.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000298 HConstant* GetConstant(Primitive::Type type, int64_t value);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000299 HNullConstant* GetNullConstant();
David Brazdil8d5b8b22015-03-24 10:51:52 +0000300 HIntConstant* GetIntConstant(int32_t value) {
301 return CreateConstant(value, &cached_int_constants_);
302 }
303 HLongConstant* GetLongConstant(int64_t value) {
304 return CreateConstant(value, &cached_long_constants_);
305 }
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000306 HFloatConstant* GetFloatConstant(float value) {
307 return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_);
308 }
309 HDoubleConstant* GetDoubleConstant(double value) {
310 return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_);
311 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000312
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100313 HCurrentMethod* GetCurrentMethod();
314
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000315 HBasicBlock* FindCommonDominator(HBasicBlock* first, HBasicBlock* second) const;
David Brazdil2d7352b2015-04-20 14:52:42 +0100316
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100317 const DexFile& GetDexFile() const {
318 return dex_file_;
319 }
320
321 uint32_t GetMethodIdx() const {
322 return method_idx_;
323 }
324
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100325 InvokeType GetInvokeType() const {
326 return invoke_type_;
327 }
328
Mark Mendellc4701932015-04-10 13:18:51 -0400329 InstructionSet GetInstructionSet() const {
330 return instruction_set_;
331 }
332
David Brazdil2d7352b2015-04-20 14:52:42 +0100333 private:
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000334 void VisitBlockForDominatorTree(HBasicBlock* block,
335 HBasicBlock* predecessor,
336 GrowableArray<size_t>* visits);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100337 void FindBackEdges(ArenaBitVector* visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000338 void VisitBlockForBackEdges(HBasicBlock* block,
339 ArenaBitVector* visited,
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100340 ArenaBitVector* visiting);
Roland Levillainfc600dc2014-12-02 17:16:31 +0000341 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100342 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000343
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000344 template <class InstructionType, typename ValueType>
345 InstructionType* CreateConstant(ValueType value,
346 ArenaSafeMap<ValueType, InstructionType*>* cache) {
347 // Try to find an existing constant of the given value.
348 InstructionType* constant = nullptr;
349 auto cached_constant = cache->find(value);
350 if (cached_constant != cache->end()) {
351 constant = cached_constant->second;
352 }
353
354 // If not found or previously deleted, create and cache a new instruction.
Nicolas Geoffrayf78848f2015-06-17 11:57:56 +0100355 // Don't bother reviving a previously deleted instruction, for simplicity.
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000356 if (constant == nullptr || constant->GetBlock() == nullptr) {
357 constant = new (arena_) InstructionType(value);
358 cache->Overwrite(value, constant);
359 InsertConstant(constant);
360 }
361 return constant;
362 }
363
David Brazdil8d5b8b22015-03-24 10:51:52 +0000364 void InsertConstant(HConstant* instruction);
365
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000366 // Cache a float constant into the graph. This method should only be
367 // called by the SsaBuilder when creating "equivalent" instructions.
368 void CacheFloatConstant(HFloatConstant* constant);
369
370 // See CacheFloatConstant comment.
371 void CacheDoubleConstant(HDoubleConstant* constant);
372
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000373 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000374
375 // List of blocks in insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000376 GrowableArray<HBasicBlock*> blocks_;
377
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100378 // List of blocks to perform a reverse post order tree traversal.
379 GrowableArray<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000380
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100381 // List of blocks to perform a linear order tree traversal.
382 GrowableArray<HBasicBlock*> linear_order_;
383
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000384 HBasicBlock* entry_block_;
385 HBasicBlock* exit_block_;
386
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100387 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100388 uint16_t maximum_number_of_out_vregs_;
389
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100390 // The number of virtual registers in this method. Contains the parameters.
391 uint16_t number_of_vregs_;
392
393 // The number of virtual registers used by parameters of this method.
394 uint16_t number_of_in_vregs_;
395
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000396 // Number of vreg size slots that the temporaries use (used in baseline compiler).
397 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100398
Mark Mendell1152c922015-04-24 17:06:35 -0400399 // Has bounds checks. We can totally skip BCE if it's false.
400 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800401
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000402 // Indicates whether the graph should be compiled in a way that
403 // ensures full debuggability. If false, we can apply more
404 // aggressive optimizations that may limit the level of debugging.
405 const bool debuggable_;
406
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000407 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000408 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000409
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100410 // The dex file from which the method is from.
411 const DexFile& dex_file_;
412
413 // The method index in the dex file.
414 const uint32_t method_idx_;
415
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100416 // If inlined, this encodes how the callee is being invoked.
417 const InvokeType invoke_type_;
418
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100419 // Whether the graph has been transformed to SSA form. Only used
420 // in debug mode to ensure we are not using properties only valid
421 // for non-SSA form (like the number of temporaries).
422 bool in_ssa_form_;
423
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100424 const bool should_generate_constructor_barrier_;
425
Mathieu Chartiere401d142015-04-22 13:56:20 -0700426 const InstructionSet instruction_set_;
427
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000428 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000429 HNullConstant* cached_null_constant_;
430 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000431 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000432 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000433 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000434
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100435 HCurrentMethod* cached_current_method_;
436
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000437 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100438 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000439 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000440 DISALLOW_COPY_AND_ASSIGN(HGraph);
441};
442
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700443class HLoopInformation : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000444 public:
445 HLoopInformation(HBasicBlock* header, HGraph* graph)
446 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100447 suspend_check_(nullptr),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100448 back_edges_(graph->GetArena(), kDefaultNumberOfBackEdges),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100449 // Make bit vector growable, as the number of blocks may change.
450 blocks_(graph->GetArena(), graph->GetBlocks().Size(), true) {}
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100451
452 HBasicBlock* GetHeader() const {
453 return header_;
454 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000455
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000456 void SetHeader(HBasicBlock* block) {
457 header_ = block;
458 }
459
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100460 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
461 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
462 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
463
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000464 void AddBackEdge(HBasicBlock* back_edge) {
465 back_edges_.Add(back_edge);
466 }
467
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100468 void RemoveBackEdge(HBasicBlock* back_edge) {
469 back_edges_.Delete(back_edge);
470 }
471
David Brazdil46e2a392015-03-16 17:31:52 +0000472 bool IsBackEdge(const HBasicBlock& block) const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100473 for (size_t i = 0, e = back_edges_.Size(); i < e; ++i) {
David Brazdil46e2a392015-03-16 17:31:52 +0000474 if (back_edges_.Get(i) == &block) return true;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100475 }
476 return false;
477 }
478
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000479 size_t NumberOfBackEdges() const {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000480 return back_edges_.Size();
481 }
482
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100483 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100484
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100485 const GrowableArray<HBasicBlock*>& GetBackEdges() const {
486 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100487 }
488
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100489 // Returns the lifetime position of the back edge that has the
490 // greatest lifetime position.
491 size_t GetLifetimeEnd() const;
492
493 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
494 for (size_t i = 0, e = back_edges_.Size(); i < e; ++i) {
495 if (back_edges_.Get(i) == existing) {
496 back_edges_.Put(i, new_back_edge);
497 return;
498 }
499 }
500 UNREACHABLE();
Nicolas Geoffray57902602015-04-21 14:28:41 +0100501 }
502
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100503 // Finds blocks that are part of this loop. Returns whether the loop is a natural loop,
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100504 // that is the header dominates the back edge.
505 bool Populate();
506
David Brazdila4b8c212015-05-07 09:59:30 +0100507 // Reanalyzes the loop by removing loop info from its blocks and re-running
508 // Populate(). If there are no back edges left, the loop info is completely
509 // removed as well as its SuspendCheck instruction. It must be run on nested
510 // inner loops first.
511 void Update();
512
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100513 // Returns whether this loop information contains `block`.
514 // Note that this loop information *must* be populated before entering this function.
515 bool Contains(const HBasicBlock& block) const;
516
517 // Returns whether this loop information is an inner loop of `other`.
518 // Note that `other` *must* be populated before entering this function.
519 bool IsIn(const HLoopInformation& other) const;
520
521 const ArenaBitVector& GetBlocks() const { return blocks_; }
522
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000523 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000524 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000525
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000526 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100527 // Internal recursive implementation of `Populate`.
528 void PopulateRecursive(HBasicBlock* block);
529
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000530 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100531 HSuspendCheck* suspend_check_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000532 GrowableArray<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100533 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000534
535 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
536};
537
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100538static constexpr size_t kNoLifetime = -1;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100539static constexpr uint32_t kNoDexPc = -1;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100540
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000541// A block in a method. Contains the list of instructions represented
542// as a double linked list. Each block knows its predecessors and
543// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100544
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700545class HBasicBlock : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000546 public:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100547 explicit HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000548 : graph_(graph),
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000549 predecessors_(graph->GetArena(), kDefaultNumberOfPredecessors),
550 successors_(graph->GetArena(), kDefaultNumberOfSuccessors),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000551 loop_information_(nullptr),
552 dominator_(nullptr),
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100553 dominated_blocks_(graph->GetArena(), kDefaultNumberOfDominatedBlocks),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100554 block_id_(-1),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100555 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100556 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000557 lifetime_end_(kNoLifetime),
558 is_catch_block_(false) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000559
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100560 const GrowableArray<HBasicBlock*>& GetPredecessors() const {
561 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000562 }
563
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100564 const GrowableArray<HBasicBlock*>& GetSuccessors() const {
565 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000566 }
567
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100568 const GrowableArray<HBasicBlock*>& GetDominatedBlocks() const {
569 return dominated_blocks_;
570 }
571
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100572 bool IsEntryBlock() const {
573 return graph_->GetEntryBlock() == this;
574 }
575
576 bool IsExitBlock() const {
577 return graph_->GetExitBlock() == this;
578 }
579
David Brazdil46e2a392015-03-16 17:31:52 +0000580 bool IsSingleGoto() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000581 bool IsSingleTryBoundary() const;
582
583 // Returns true if this block emits nothing but a jump.
584 bool IsSingleJump() const {
585 HLoopInformation* loop_info = GetLoopInformation();
586 return (IsSingleGoto() || IsSingleTryBoundary())
587 // Back edges generate a suspend check.
588 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
589 }
David Brazdil46e2a392015-03-16 17:31:52 +0000590
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000591 void AddBackEdge(HBasicBlock* back_edge) {
592 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000593 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000594 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100595 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000596 loop_information_->AddBackEdge(back_edge);
597 }
598
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000599 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000600 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000601
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000602 int GetBlockId() const { return block_id_; }
603 void SetBlockId(int id) { block_id_ = id; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000604
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000605 HBasicBlock* GetDominator() const { return dominator_; }
606 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100607 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.Add(block); }
David Brazdil2d7352b2015-04-20 14:52:42 +0100608 void RemoveDominatedBlock(HBasicBlock* block) { dominated_blocks_.Delete(block); }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000609 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
610 for (size_t i = 0, e = dominated_blocks_.Size(); i < e; ++i) {
611 if (dominated_blocks_.Get(i) == existing) {
612 dominated_blocks_.Put(i, new_block);
613 return;
614 }
615 }
616 LOG(FATAL) << "Unreachable";
617 UNREACHABLE();
618 }
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100619 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000620
621 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100622 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000623 }
624
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100625 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
626 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100627 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100628 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +0100629 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
630 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000631
632 void AddSuccessor(HBasicBlock* block) {
633 successors_.Add(block);
634 block->predecessors_.Add(this);
635 }
636
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100637 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
638 size_t successor_index = GetSuccessorIndexOf(existing);
639 DCHECK_NE(successor_index, static_cast<size_t>(-1));
640 existing->RemovePredecessor(this);
641 new_block->predecessors_.Add(this);
642 successors_.Put(successor_index, new_block);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000643 }
644
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000645 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
646 size_t predecessor_index = GetPredecessorIndexOf(existing);
647 DCHECK_NE(predecessor_index, static_cast<size_t>(-1));
648 existing->RemoveSuccessor(this);
649 new_block->successors_.Add(this);
650 predecessors_.Put(predecessor_index, new_block);
651 }
652
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100653 // Insert `this` between `predecessor` and `successor. This method
654 // preserves the indicies, and will update the first edge found between
655 // `predecessor` and `successor`.
656 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
657 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
658 DCHECK_NE(predecessor_index, static_cast<size_t>(-1));
659 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
660 DCHECK_NE(successor_index, static_cast<size_t>(-1));
661 successor->predecessors_.Put(predecessor_index, this);
662 predecessor->successors_.Put(successor_index, this);
663 successors_.Add(successor);
664 predecessors_.Add(predecessor);
665 }
666
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100667 void RemovePredecessor(HBasicBlock* block) {
668 predecessors_.Delete(block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100669 }
670
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000671 void RemoveSuccessor(HBasicBlock* block) {
672 successors_.Delete(block);
673 }
674
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100675 void ClearAllPredecessors() {
676 predecessors_.Reset();
677 }
678
679 void AddPredecessor(HBasicBlock* block) {
680 predecessors_.Add(block);
681 block->successors_.Add(this);
682 }
683
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100684 void SwapPredecessors() {
Nicolas Geoffrayc83d4412014-09-18 16:46:20 +0100685 DCHECK_EQ(predecessors_.Size(), 2u);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100686 HBasicBlock* temp = predecessors_.Get(0);
687 predecessors_.Put(0, predecessors_.Get(1));
688 predecessors_.Put(1, temp);
689 }
690
David Brazdil769c9e52015-04-27 13:54:09 +0100691 void SwapSuccessors() {
692 DCHECK_EQ(successors_.Size(), 2u);
693 HBasicBlock* temp = successors_.Get(0);
694 successors_.Put(0, successors_.Get(1));
695 successors_.Put(1, temp);
696 }
697
David Brazdilfc6a86a2015-06-26 10:33:45 +0000698 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100699 for (size_t i = 0, e = predecessors_.Size(); i < e; ++i) {
700 if (predecessors_.Get(i) == predecessor) {
701 return i;
702 }
703 }
704 return -1;
705 }
706
David Brazdilfc6a86a2015-06-26 10:33:45 +0000707 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100708 for (size_t i = 0, e = successors_.Size(); i < e; ++i) {
709 if (successors_.Get(i) == successor) {
710 return i;
711 }
712 }
713 return -1;
714 }
715
David Brazdilfc6a86a2015-06-26 10:33:45 +0000716 HBasicBlock* GetSinglePredecessor() const {
717 DCHECK_EQ(GetPredecessors().Size(), 1u);
718 return GetPredecessors().Get(0);
719 }
720
721 HBasicBlock* GetSingleSuccessor() const {
722 DCHECK_EQ(GetSuccessors().Size(), 1u);
723 return GetSuccessors().Get(0);
724 }
725
726 // Returns whether the first occurrence of `predecessor` in the list of
727 // predecessors is at index `idx`.
728 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
729 DCHECK_EQ(GetPredecessors().Get(idx), predecessor);
730 return GetPredecessorIndexOf(predecessor) == idx;
731 }
732
733 // Returns whether successor at index `idx` is an exception handler.
734 bool IsExceptionalSuccessor(size_t idx) const;
735
736 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +0100737 // created, latter block. Note that this method will add the block to the
738 // graph, create a Goto at the end of the former block and will create an edge
739 // between the blocks. It will not, however, update the reverse post order or
740 // loop information.
David Brazdilfc6a86a2015-06-26 10:33:45 +0000741 HBasicBlock* SplitBefore(HInstruction* cursor);
742
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000743 // Split the block into two blocks just after `cursor`. Returns the newly
744 // created block. Note that this method just updates raw block information,
745 // like predecessors, successors, dominators, and instruction list. It does not
746 // update the graph, reverse post order, loop information, nor make sure the
747 // blocks are consistent (for example ending with a control flow instruction).
748 HBasicBlock* SplitAfter(HInstruction* cursor);
749
750 // Merge `other` at the end of `this`. Successors and dominated blocks of
751 // `other` are changed to be successors and dominated blocks of `this`. Note
752 // that this method does not update the graph, reverse post order, loop
753 // information, nor make sure the blocks are consistent (for example ending
754 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +0100755 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000756
757 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
758 // of `this` are moved to `other`.
759 // Note that this method does not update the graph, reverse post order, loop
760 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +0000761 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000762 void ReplaceWith(HBasicBlock* other);
763
David Brazdil2d7352b2015-04-20 14:52:42 +0100764 // Merge `other` at the end of `this`. This method updates loops, reverse post
765 // order, links to predecessors, successors, dominators and deletes the block
766 // from the graph. The two blocks must be successive, i.e. `this` the only
767 // predecessor of `other` and vice versa.
768 void MergeWith(HBasicBlock* other);
769
770 // Disconnects `this` from all its predecessors, successors and dominator,
771 // removes it from all loops it is included in and eventually from the graph.
772 // The block must not dominate any other block. Predecessors and successors
773 // are safely updated.
774 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +0000775
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000776 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +0100777 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100778 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +0100779 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +0100780 // Replace instruction `initial` with `replacement` within this block.
781 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
782 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100783 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100784 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +0000785 // RemoveInstruction and RemovePhi delete a given instruction from the respective
786 // instruction list. With 'ensure_safety' set to true, it verifies that the
787 // instruction is not in use and removes it from the use lists of its inputs.
788 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
789 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +0100790 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100791
792 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +0100793 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100794 }
795
Roland Levillain6b879dd2014-09-22 17:13:44 +0100796 bool IsLoopPreHeaderFirstPredecessor() const {
797 DCHECK(IsLoopHeader());
798 DCHECK(!GetPredecessors().IsEmpty());
799 return GetPredecessors().Get(0) == GetLoopInformation()->GetPreHeader();
800 }
801
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100802 HLoopInformation* GetLoopInformation() const {
803 return loop_information_;
804 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000805
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000806 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100807 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000808 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100809 void SetInLoop(HLoopInformation* info) {
810 if (IsLoopHeader()) {
811 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +0100812 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100813 loop_information_ = info;
814 } else if (loop_information_->Contains(*info->GetHeader())) {
815 // Block is currently part of an outer loop. Make it part of this inner loop.
816 // Note that a non loop header having a loop information means this loop information
817 // has already been populated
818 loop_information_ = info;
819 } else {
820 // Block is part of an inner loop. Do not update the loop information.
821 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
822 // at this point, because this method is being called while populating `info`.
823 }
824 }
825
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000826 // Raw update of the loop information.
827 void SetLoopInformation(HLoopInformation* info) {
828 loop_information_ = info;
829 }
830
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100831 bool IsInLoop() const { return loop_information_ != nullptr; }
832
David Brazdila4b8c212015-05-07 09:59:30 +0100833 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100834 bool Dominates(HBasicBlock* block) const;
835
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100836 size_t GetLifetimeStart() const { return lifetime_start_; }
837 size_t GetLifetimeEnd() const { return lifetime_end_; }
838
839 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
840 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
841
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100842 uint32_t GetDexPc() const { return dex_pc_; }
843
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000844 bool IsCatchBlock() const { return is_catch_block_; }
845 void SetIsCatchBlock() { is_catch_block_ = true; }
846
David Brazdil8d5b8b22015-03-24 10:51:52 +0000847 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +0000848 bool EndsWithIf() const;
849 bool HasSinglePhi() const;
850
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000851 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000852 HGraph* graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000853 GrowableArray<HBasicBlock*> predecessors_;
854 GrowableArray<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100855 HInstructionList instructions_;
856 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000857 HLoopInformation* loop_information_;
858 HBasicBlock* dominator_;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100859 GrowableArray<HBasicBlock*> dominated_blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000860 int block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100861 // The dex program counter of the first instruction of this block.
862 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100863 size_t lifetime_start_;
864 size_t lifetime_end_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000865 bool is_catch_block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000866
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000867 friend class HGraph;
868 friend class HInstruction;
869
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000870 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
871};
872
David Brazdilb2bd1c52015-03-25 11:17:37 +0000873// Iterates over the LoopInformation of all loops which contain 'block'
874// from the innermost to the outermost.
875class HLoopInformationOutwardIterator : public ValueObject {
876 public:
877 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
878 : current_(block.GetLoopInformation()) {}
879
880 bool Done() const { return current_ == nullptr; }
881
882 void Advance() {
883 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +0100884 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +0000885 }
886
887 HLoopInformation* Current() const {
888 DCHECK(!Done());
889 return current_;
890 }
891
892 private:
893 HLoopInformation* current_;
894
895 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
896};
897
Alexandre Ramesef20f712015-06-09 10:29:30 +0100898#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100899 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000900 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000901 M(ArrayGet, Instruction) \
902 M(ArrayLength, Instruction) \
903 M(ArraySet, Instruction) \
David Brazdil66d126e2015-04-03 16:02:44 +0100904 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000905 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +0000906 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000907 M(CheckCast, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100908 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000909 M(Compare, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100910 M(Condition, BinaryOperation) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100911 M(CurrentMethod, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700912 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000913 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +0000914 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000915 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100916 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000917 M(Exit, Instruction) \
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +0100918 M(FakeString, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000919 M(FloatConstant, Constant) \
920 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100921 M(GreaterThan, Condition) \
922 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100923 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000924 M(InstanceFieldGet, Instruction) \
925 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000926 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100927 M(IntConstant, Constant) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +0000928 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000929 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100930 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000931 M(LessThan, Condition) \
932 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000933 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000934 M(LoadException, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100935 M(LoadLocal, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000936 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100937 M(Local, Instruction) \
938 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +0100939 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +0000940 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000941 M(Mul, BinaryOperation) \
942 M(Neg, UnaryOperation) \
943 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100944 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +0100945 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000946 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000947 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000948 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000949 M(Or, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100950 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000951 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100952 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000953 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100954 M(Return, Instruction) \
955 M(ReturnVoid, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000956 M(Shl, BinaryOperation) \
957 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100958 M(StaticFieldGet, Instruction) \
959 M(StaticFieldSet, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100960 M(StoreLocal, Instruction) \
961 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100962 M(SuspendCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000963 M(Temporary, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000964 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +0000965 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +0000966 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000967 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000968 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000969
Alexandre Ramesef20f712015-06-09 10:29:30 +0100970#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
971
972#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
973
Alexandre Ramesf39e0642015-06-23 11:33:45 +0100974#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
975
Alexandre Ramesef20f712015-06-09 10:29:30 +0100976#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
977
978#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
979
980#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
981 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
982 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
983 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +0100984 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +0100985 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
986 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
987
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100988#define FOR_EACH_INSTRUCTION(M) \
989 FOR_EACH_CONCRETE_INSTRUCTION(M) \
990 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +0100991 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +0100992 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100993 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -0700994
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100995#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000996FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
997#undef FORWARD_DECLARATION
998
Roland Levillainccc07a92014-09-16 14:48:16 +0100999#define DECLARE_INSTRUCTION(type) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001000 InstructionKind GetKind() const OVERRIDE { return k##type; } \
1001 const char* DebugName() const OVERRIDE { return #type; } \
1002 const H##type* As##type() const OVERRIDE { return this; } \
1003 H##type* As##type() OVERRIDE { return this; } \
1004 bool InstructionTypeEquals(HInstruction* other) const OVERRIDE { \
Roland Levillainccc07a92014-09-16 14:48:16 +01001005 return other->Is##type(); \
1006 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001007 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001008
David Brazdiled596192015-01-23 10:39:45 +00001009template <typename T> class HUseList;
1010
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001011template <typename T>
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001012class HUseListNode : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001013 public:
David Brazdiled596192015-01-23 10:39:45 +00001014 HUseListNode* GetPrevious() const { return prev_; }
1015 HUseListNode* GetNext() const { return next_; }
1016 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001017 size_t GetIndex() const { return index_; }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001018 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001019
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001020 private:
David Brazdiled596192015-01-23 10:39:45 +00001021 HUseListNode(T user, size_t index)
1022 : user_(user), index_(index), prev_(nullptr), next_(nullptr) {}
1023
1024 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001025 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001026 HUseListNode<T>* prev_;
1027 HUseListNode<T>* next_;
1028
1029 friend class HUseList<T>;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001030
1031 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1032};
1033
David Brazdiled596192015-01-23 10:39:45 +00001034template <typename T>
1035class HUseList : public ValueObject {
1036 public:
1037 HUseList() : first_(nullptr) {}
1038
1039 void Clear() {
1040 first_ = nullptr;
1041 }
1042
1043 // Adds a new entry at the beginning of the use list and returns
1044 // the newly created node.
1045 HUseListNode<T>* AddUse(T user, size_t index, ArenaAllocator* arena) {
David Brazdilea55b932015-01-27 17:12:29 +00001046 HUseListNode<T>* new_node = new (arena) HUseListNode<T>(user, index);
David Brazdiled596192015-01-23 10:39:45 +00001047 if (IsEmpty()) {
1048 first_ = new_node;
1049 } else {
1050 first_->prev_ = new_node;
1051 new_node->next_ = first_;
1052 first_ = new_node;
1053 }
1054 return new_node;
1055 }
1056
1057 HUseListNode<T>* GetFirst() const {
1058 return first_;
1059 }
1060
1061 void Remove(HUseListNode<T>* node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001062 DCHECK(node != nullptr);
1063 DCHECK(Contains(node));
1064
David Brazdiled596192015-01-23 10:39:45 +00001065 if (node->prev_ != nullptr) {
1066 node->prev_->next_ = node->next_;
1067 }
1068 if (node->next_ != nullptr) {
1069 node->next_->prev_ = node->prev_;
1070 }
1071 if (node == first_) {
1072 first_ = node->next_;
1073 }
1074 }
1075
David Brazdil1abb4192015-02-17 18:33:36 +00001076 bool Contains(const HUseListNode<T>* node) const {
1077 if (node == nullptr) {
1078 return false;
1079 }
1080 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1081 if (current == node) {
1082 return true;
1083 }
1084 }
1085 return false;
1086 }
1087
David Brazdiled596192015-01-23 10:39:45 +00001088 bool IsEmpty() const {
1089 return first_ == nullptr;
1090 }
1091
1092 bool HasOnlyOneUse() const {
1093 return first_ != nullptr && first_->next_ == nullptr;
1094 }
1095
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001096 size_t SizeSlow() const {
1097 size_t count = 0;
1098 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1099 ++count;
1100 }
1101 return count;
1102 }
1103
David Brazdiled596192015-01-23 10:39:45 +00001104 private:
1105 HUseListNode<T>* first_;
1106};
1107
1108template<typename T>
1109class HUseIterator : public ValueObject {
1110 public:
1111 explicit HUseIterator(const HUseList<T>& uses) : current_(uses.GetFirst()) {}
1112
1113 bool Done() const { return current_ == nullptr; }
1114
1115 void Advance() {
1116 DCHECK(!Done());
1117 current_ = current_->GetNext();
1118 }
1119
1120 HUseListNode<T>* Current() const {
1121 DCHECK(!Done());
1122 return current_;
1123 }
1124
1125 private:
1126 HUseListNode<T>* current_;
1127
1128 friend class HValue;
1129};
1130
David Brazdil1abb4192015-02-17 18:33:36 +00001131// This class is used by HEnvironment and HInstruction classes to record the
1132// instructions they use and pointers to the corresponding HUseListNodes kept
1133// by the used instructions.
1134template <typename T>
1135class HUserRecord : public ValueObject {
1136 public:
1137 HUserRecord() : instruction_(nullptr), use_node_(nullptr) {}
1138 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), use_node_(nullptr) {}
1139
1140 HUserRecord(const HUserRecord<T>& old_record, HUseListNode<T>* use_node)
1141 : instruction_(old_record.instruction_), use_node_(use_node) {
1142 DCHECK(instruction_ != nullptr);
1143 DCHECK(use_node_ != nullptr);
1144 DCHECK(old_record.use_node_ == nullptr);
1145 }
1146
1147 HInstruction* GetInstruction() const { return instruction_; }
1148 HUseListNode<T>* GetUseNode() const { return use_node_; }
1149
1150 private:
1151 // Instruction used by the user.
1152 HInstruction* instruction_;
1153
1154 // Corresponding entry in the use list kept by 'instruction_'.
1155 HUseListNode<T>* use_node_;
1156};
1157
Calin Juravle27df7582015-04-17 19:12:31 +01001158// TODO: Add better documentation to this class and maybe refactor with more suggestive names.
1159// - Has(All)SideEffects suggests that all the side effects are present but only ChangesSomething
1160// flag is consider.
1161// - DependsOn suggests that there is a real dependency between side effects but it only
1162// checks DependendsOnSomething flag.
1163//
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001164// Represents the side effects an instruction may have.
1165class SideEffects : public ValueObject {
1166 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001167 SideEffects() : flags_(0) {}
1168
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001169 static SideEffects None() {
1170 return SideEffects(0);
1171 }
1172
1173 static SideEffects All() {
1174 return SideEffects(ChangesSomething().flags_ | DependsOnSomething().flags_);
1175 }
1176
1177 static SideEffects ChangesSomething() {
1178 return SideEffects((1 << kFlagChangesCount) - 1);
1179 }
1180
1181 static SideEffects DependsOnSomething() {
1182 int count = kFlagDependsOnCount - kFlagChangesCount;
1183 return SideEffects(((1 << count) - 1) << kFlagChangesCount);
1184 }
1185
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001186 SideEffects Union(SideEffects other) const {
1187 return SideEffects(flags_ | other.flags_);
1188 }
1189
Roland Levillain72bceff2014-09-15 18:29:00 +01001190 bool HasSideEffects() const {
1191 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
1192 return (flags_ & all_bits_set) != 0;
1193 }
1194
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001195 bool HasAllSideEffects() const {
1196 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
1197 return all_bits_set == (flags_ & all_bits_set);
1198 }
1199
1200 bool DependsOn(SideEffects other) const {
1201 size_t depends_flags = other.ComputeDependsFlags();
1202 return (flags_ & depends_flags) != 0;
1203 }
1204
1205 bool HasDependencies() const {
1206 int count = kFlagDependsOnCount - kFlagChangesCount;
1207 size_t all_bits_set = (1 << count) - 1;
1208 return ((flags_ >> kFlagChangesCount) & all_bits_set) != 0;
1209 }
1210
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001211 private:
1212 static constexpr int kFlagChangesSomething = 0;
1213 static constexpr int kFlagChangesCount = kFlagChangesSomething + 1;
1214
1215 static constexpr int kFlagDependsOnSomething = kFlagChangesCount;
1216 static constexpr int kFlagDependsOnCount = kFlagDependsOnSomething + 1;
1217
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001218 explicit SideEffects(size_t flags) : flags_(flags) {}
1219
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001220 size_t ComputeDependsFlags() const {
1221 return flags_ << kFlagChangesCount;
1222 }
1223
1224 size_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001225};
1226
David Brazdiled596192015-01-23 10:39:45 +00001227// A HEnvironment object contains the values of virtual registers at a given location.
1228class HEnvironment : public ArenaObject<kArenaAllocMisc> {
1229 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001230 HEnvironment(ArenaAllocator* arena,
1231 size_t number_of_vregs,
1232 const DexFile& dex_file,
1233 uint32_t method_idx,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001234 uint32_t dex_pc,
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001235 InvokeType invoke_type,
1236 HInstruction* holder)
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001237 : vregs_(arena, number_of_vregs),
1238 locations_(arena, number_of_vregs),
1239 parent_(nullptr),
1240 dex_file_(dex_file),
1241 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001242 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001243 invoke_type_(invoke_type),
1244 holder_(holder) {
David Brazdiled596192015-01-23 10:39:45 +00001245 vregs_.SetSize(number_of_vregs);
1246 for (size_t i = 0; i < number_of_vregs; i++) {
David Brazdil1abb4192015-02-17 18:33:36 +00001247 vregs_.Put(i, HUserRecord<HEnvironment*>());
David Brazdiled596192015-01-23 10:39:45 +00001248 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001249
1250 locations_.SetSize(number_of_vregs);
1251 for (size_t i = 0; i < number_of_vregs; ++i) {
1252 locations_.Put(i, Location());
1253 }
1254 }
1255
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001256 HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001257 : HEnvironment(arena,
1258 to_copy.Size(),
1259 to_copy.GetDexFile(),
1260 to_copy.GetMethodIdx(),
1261 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001262 to_copy.GetInvokeType(),
1263 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001264
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001265 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001266 if (parent_ != nullptr) {
1267 parent_->SetAndCopyParentChain(allocator, parent);
1268 } else {
1269 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1270 parent_->CopyFrom(parent);
1271 if (parent->GetParent() != nullptr) {
1272 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1273 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001274 }
David Brazdiled596192015-01-23 10:39:45 +00001275 }
1276
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001277 void CopyFrom(const GrowableArray<HInstruction*>& locals);
1278 void CopyFrom(HEnvironment* environment);
1279
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001280 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1281 // input to the loop phi instead. This is for inserting instructions that
1282 // require an environment (like HDeoptimization) in the loop pre-header.
1283 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001284
1285 void SetRawEnvAt(size_t index, HInstruction* instruction) {
David Brazdil1abb4192015-02-17 18:33:36 +00001286 vregs_.Put(index, HUserRecord<HEnvironment*>(instruction));
David Brazdiled596192015-01-23 10:39:45 +00001287 }
1288
1289 HInstruction* GetInstructionAt(size_t index) const {
David Brazdil1abb4192015-02-17 18:33:36 +00001290 return vregs_.Get(index).GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001291 }
1292
David Brazdil1abb4192015-02-17 18:33:36 +00001293 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001294
1295 size_t Size() const { return vregs_.Size(); }
1296
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001297 HEnvironment* GetParent() const { return parent_; }
1298
1299 void SetLocationAt(size_t index, Location location) {
1300 locations_.Put(index, location);
1301 }
1302
1303 Location GetLocationAt(size_t index) const {
1304 return locations_.Get(index);
1305 }
1306
1307 uint32_t GetDexPc() const {
1308 return dex_pc_;
1309 }
1310
1311 uint32_t GetMethodIdx() const {
1312 return method_idx_;
1313 }
1314
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001315 InvokeType GetInvokeType() const {
1316 return invoke_type_;
1317 }
1318
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001319 const DexFile& GetDexFile() const {
1320 return dex_file_;
1321 }
1322
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001323 HInstruction* GetHolder() const {
1324 return holder_;
1325 }
1326
David Brazdiled596192015-01-23 10:39:45 +00001327 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001328 // Record instructions' use entries of this environment for constant-time removal.
1329 // It should only be called by HInstruction when a new environment use is added.
1330 void RecordEnvUse(HUseListNode<HEnvironment*>* env_use) {
1331 DCHECK(env_use->GetUser() == this);
1332 size_t index = env_use->GetIndex();
1333 vregs_.Put(index, HUserRecord<HEnvironment*>(vregs_.Get(index), env_use));
1334 }
David Brazdiled596192015-01-23 10:39:45 +00001335
David Brazdil1abb4192015-02-17 18:33:36 +00001336 GrowableArray<HUserRecord<HEnvironment*> > vregs_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001337 GrowableArray<Location> locations_;
1338 HEnvironment* parent_;
1339 const DexFile& dex_file_;
1340 const uint32_t method_idx_;
1341 const uint32_t dex_pc_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001342 const InvokeType invoke_type_;
David Brazdiled596192015-01-23 10:39:45 +00001343
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001344 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001345 HInstruction* const holder_;
1346
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001347 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001348
1349 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1350};
1351
Calin Juravleacf735c2015-02-12 15:25:22 +00001352class ReferenceTypeInfo : ValueObject {
1353 public:
Calin Juravleb1498f62015-02-16 13:13:29 +00001354 typedef Handle<mirror::Class> TypeHandle;
1355
Calin Juravle7733bd62015-07-22 17:14:50 +00001356 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 }
Calin Juravleb1498f62015-02-16 13:13:29 +00001366 }
1367
Calin Juravle7733bd62015-07-22 17:14:50 +00001368 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_; }
Calin Juravle7733bd62015-07-22 17:14:50 +00001373 bool IsTop() const { return is_top_; }
Guillaume Sanchez222862c2015-06-09 18:33:02 +01001374 bool IsInterface() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Calin Juravle7733bd62015-07-22 17:14:50 +00001375 return !IsTop() && GetTypeHandle()->IsInterface();
Guillaume Sanchez222862c2015-06-09 18:33:02 +01001376 }
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 Juravle7733bd62015-07-22 17:14:50 +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 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001389 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
Calin Juravle7733bd62015-07-22 17:14:50 +00001394 // (e.g. tops are equal but they can be the result of a merge).
Calin Juravleacf735c2015-02-12 15:25:22 +00001395 bool IsEqual(ReferenceTypeInfo rti) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Calin Juravle7733bd62015-07-22 17:14:50 +00001396 if (IsExact() != rti.IsExact()) {
Calin Juravleacf735c2015-02-12 15:25:22 +00001397 return false;
1398 }
Calin Juravle7733bd62015-07-22 17:14:50 +00001399 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();
Calin Juravleacf735c2015-02-12 15:25:22 +00001412 }
1413
1414 private:
Calin Juravle7733bd62015-07-22 17:14:50 +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) {}
Calin Juravleb1498f62015-02-16 13:13:29 +00001418
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_;
Calin Juravle7733bd62015-07-22 17:14:50 +00001424 // 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_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001429};
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),
Calin Juravle7733bd62015-07-22 17:14:50 +00001446 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.
Calin Juravleb0d5fc02015-07-15 14:41:29 +01001491 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001492 virtual bool CanBeNull() const {
1493 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1494 return true;
1495 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001496
Calin Juravle641547a2015-04-21 22:08:51 +01001497 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj) const {
1498 UNUSED(obj);
1499 return false;
1500 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001501
Calin Juravle7733bd62015-07-22 17:14:50 +00001502 void SetReferenceTypeInfo(ReferenceTypeInfo reference_type_info) {
1503 DCHECK_EQ(GetType(), Primitive::kPrimNot);
1504 reference_type_info_ = reference_type_info;
1505 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001506
Calin Juravle61d544b2015-02-23 16:46:57 +00001507 ReferenceTypeInfo GetReferenceTypeInfo() const {
1508 DCHECK_EQ(GetType(), Primitive::kPrimNot);
1509 return reference_type_info_;
1510 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001511
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001512 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001513 DCHECK(user != nullptr);
1514 HUseListNode<HInstruction*>* use =
1515 uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1516 user->SetRawInputRecordAt(index, HUserRecord<HInstruction*>(user->InputRecordAt(index), use));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001517 }
1518
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001519 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001520 DCHECK(user != nullptr);
David Brazdiled596192015-01-23 10:39:45 +00001521 HUseListNode<HEnvironment*>* env_use =
1522 env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1523 user->RecordEnvUse(env_use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001524 }
1525
David Brazdil1abb4192015-02-17 18:33:36 +00001526 void RemoveAsUserOfInput(size_t input) {
1527 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
1528 input_use.GetInstruction()->uses_.Remove(input_use.GetUseNode());
1529 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001530
David Brazdil1abb4192015-02-17 18:33:36 +00001531 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1532 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001533
David Brazdiled596192015-01-23 10:39:45 +00001534 bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); }
1535 bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); }
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001536 bool HasNonEnvironmentUses() const { return !uses_.IsEmpty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001537 bool HasOnlyOneNonEnvironmentUse() const {
1538 return !HasEnvironmentUses() && GetUses().HasOnlyOneUse();
1539 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001540
Roland Levillain6c82d402014-10-13 16:10:27 +01001541 // Does this instruction strictly dominate `other_instruction`?
1542 // Returns false if this instruction and `other_instruction` are the same.
1543 // Aborts if this instruction and `other_instruction` are both phis.
1544 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001545
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001546 int GetId() const { return id_; }
1547 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001548
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001549 int GetSsaIndex() const { return ssa_index_; }
1550 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1551 bool HasSsaIndex() const { return ssa_index_ != -1; }
1552
1553 bool HasEnvironment() const { return environment_ != nullptr; }
1554 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001555 // Set the `environment_` field. Raw because this method does not
1556 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001557 void SetRawEnvironment(HEnvironment* environment) {
1558 DCHECK(environment_ == nullptr);
1559 DCHECK_EQ(environment->GetHolder(), this);
1560 environment_ = environment;
1561 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001562
1563 // Set the environment of this instruction, copying it from `environment`. While
1564 // copying, the uses lists are being updated.
1565 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001566 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001567 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001568 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001569 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001570 if (environment->GetParent() != nullptr) {
1571 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1572 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001573 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001574
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001575 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
1576 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001577 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001578 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001579 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001580 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001581 if (environment->GetParent() != nullptr) {
1582 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1583 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001584 }
1585
Nicolas Geoffray39468442014-09-02 15:17:15 +01001586 // Returns the number of entries in the environment. Typically, that is the
1587 // number of dex registers in a method. It could be more in case of inlining.
1588 size_t EnvironmentSize() const;
1589
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001590 LocationSummary* GetLocations() const { return locations_; }
1591 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001592
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001593 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001594 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001595
Alexandre Rames188d4312015-04-09 18:30:21 +01001596 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
1597 // uses of this instruction by `other` are *not* updated.
1598 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
1599 ReplaceWith(other);
1600 other->ReplaceInput(this, use_index);
1601 }
1602
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001603 // Move `this` instruction before `cursor`.
1604 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001605
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001606#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01001607 bool Is##type() const { return (As##type() != nullptr); } \
1608 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001609 virtual H##type* As##type() { return nullptr; }
1610
1611 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
1612#undef INSTRUCTION_TYPE_CHECK
1613
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001614 // Returns whether the instruction can be moved within the graph.
1615 virtual bool CanBeMoved() const { return false; }
1616
1617 // Returns whether the two instructions are of the same kind.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001618 virtual bool InstructionTypeEquals(HInstruction* other) const {
1619 UNUSED(other);
1620 return false;
1621 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001622
1623 // Returns whether any data encoded in the two instructions is equal.
1624 // This method does not look at the inputs. Both instructions must be
1625 // of the same type, otherwise the method has undefined behavior.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001626 virtual bool InstructionDataEquals(HInstruction* other) const {
1627 UNUSED(other);
1628 return false;
1629 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001630
1631 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00001632 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001633 // 2) Their inputs are identical.
1634 bool Equals(HInstruction* other) const;
1635
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001636 virtual InstructionKind GetKind() const = 0;
1637
1638 virtual size_t ComputeHashCode() const {
1639 size_t result = GetKind();
1640 for (size_t i = 0, e = InputCount(); i < e; ++i) {
1641 result = (result * 31) + InputAt(i)->GetId();
1642 }
1643 return result;
1644 }
1645
1646 SideEffects GetSideEffects() const { return side_effects_; }
1647
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001648 size_t GetLifetimePosition() const { return lifetime_position_; }
1649 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
1650 LiveInterval* GetLiveInterval() const { return live_interval_; }
1651 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
1652 bool HasLiveInterval() const { return live_interval_ != nullptr; }
1653
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001654 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
1655
1656 // Returns whether the code generation of the instruction will require to have access
1657 // to the current method. Such instructions are:
1658 // (1): Instructions that require an environment, as calling the runtime requires
1659 // to walk the stack and have the current method stored at a specific stack address.
1660 // (2): Object literals like classes and strings, that are loaded from the dex cache
1661 // fields of the current method.
1662 bool NeedsCurrentMethod() const {
1663 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
1664 }
1665
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001666 virtual bool NeedsDexCache() const { return false; }
1667
Mark Mendellc4701932015-04-10 13:18:51 -04001668 // Does this instruction have any use in an environment before
1669 // control flow hits 'other'?
1670 bool HasAnyEnvironmentUseBefore(HInstruction* other);
1671
1672 // Remove all references to environment uses of this instruction.
1673 // The caller must ensure that this is safe to do.
1674 void RemoveEnvironmentUsers();
1675
David Brazdil1abb4192015-02-17 18:33:36 +00001676 protected:
1677 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
1678 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
1679
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001680 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001681 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); }
1682
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001683 HInstruction* previous_;
1684 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001685 HBasicBlock* block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001686
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001687 // An instruction gets an id when it is added to the graph.
1688 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01001689 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001690 int id_;
1691
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001692 // When doing liveness analysis, instructions that have uses get an SSA index.
1693 int ssa_index_;
1694
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001695 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00001696 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001697
1698 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00001699 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001700
Nicolas Geoffray39468442014-09-02 15:17:15 +01001701 // The environment associated with this instruction. Not null if the instruction
1702 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001703 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001704
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001705 // Set by the code generator.
1706 LocationSummary* locations_;
1707
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001708 // Set by the liveness analysis.
1709 LiveInterval* live_interval_;
1710
1711 // Set by the liveness analysis, this is the position in a linear
1712 // order of blocks where this instruction's live interval start.
1713 size_t lifetime_position_;
1714
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001715 const SideEffects side_effects_;
1716
Calin Juravleacf735c2015-02-12 15:25:22 +00001717 // TODO: for primitive types this should be marked as invalid.
1718 ReferenceTypeInfo reference_type_info_;
1719
David Brazdil1abb4192015-02-17 18:33:36 +00001720 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001721 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00001722 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001723 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001724 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001725
1726 DISALLOW_COPY_AND_ASSIGN(HInstruction);
1727};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001728std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001729
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001730class HInputIterator : public ValueObject {
1731 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001732 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001733
1734 bool Done() const { return index_ == instruction_->InputCount(); }
1735 HInstruction* Current() const { return instruction_->InputAt(index_); }
1736 void Advance() { index_++; }
1737
1738 private:
1739 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001740 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001741
1742 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
1743};
1744
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001745class HInstructionIterator : public ValueObject {
1746 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001747 explicit HInstructionIterator(const HInstructionList& instructions)
1748 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001749 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001750 }
1751
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001752 bool Done() const { return instruction_ == nullptr; }
1753 HInstruction* Current() const { return instruction_; }
1754 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001755 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001756 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001757 }
1758
1759 private:
1760 HInstruction* instruction_;
1761 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001762
1763 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001764};
1765
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001766class HBackwardInstructionIterator : public ValueObject {
1767 public:
1768 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
1769 : instruction_(instructions.last_instruction_) {
1770 next_ = Done() ? nullptr : instruction_->GetPrevious();
1771 }
1772
1773 bool Done() const { return instruction_ == nullptr; }
1774 HInstruction* Current() const { return instruction_; }
1775 void Advance() {
1776 instruction_ = next_;
1777 next_ = Done() ? nullptr : instruction_->GetPrevious();
1778 }
1779
1780 private:
1781 HInstruction* instruction_;
1782 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001783
1784 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001785};
1786
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001787// An embedded container with N elements of type T. Used (with partial
1788// specialization for N=0) because embedded arrays cannot have size 0.
1789template<typename T, intptr_t N>
1790class EmbeddedArray {
1791 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001792 EmbeddedArray() : elements_() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001793
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001794 intptr_t GetLength() const { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001795
1796 const T& operator[](intptr_t i) const {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001797 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001798 return elements_[i];
1799 }
1800
1801 T& operator[](intptr_t i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001802 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001803 return elements_[i];
1804 }
1805
1806 const T& At(intptr_t i) const {
1807 return (*this)[i];
1808 }
1809
1810 void SetAt(intptr_t i, const T& val) {
1811 (*this)[i] = val;
1812 }
1813
1814 private:
1815 T elements_[N];
1816};
1817
1818template<typename T>
1819class EmbeddedArray<T, 0> {
1820 public:
1821 intptr_t length() const { return 0; }
1822 const T& operator[](intptr_t i) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001823 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001824 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001825 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001826 }
1827 T& operator[](intptr_t i) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001828 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001829 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001830 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001831 }
1832};
1833
1834template<intptr_t N>
1835class HTemplateInstruction: public HInstruction {
1836 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001837 HTemplateInstruction<N>(SideEffects side_effects)
1838 : HInstruction(side_effects), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07001839 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001840
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001841 size_t InputCount() const OVERRIDE { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001842
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001843 protected:
David Brazdil1abb4192015-02-17 18:33:36 +00001844 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_[i]; }
1845
1846 void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE {
1847 inputs_[i] = input;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001848 }
1849
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001850 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001851 EmbeddedArray<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001852
1853 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001854};
1855
Dave Allison20dfc792014-06-16 20:44:29 -07001856template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001857class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07001858 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001859 HExpression<N>(Primitive::Type type, SideEffects side_effects)
1860 : HTemplateInstruction<N>(side_effects), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07001861 virtual ~HExpression() {}
1862
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001863 Primitive::Type GetType() const OVERRIDE { return type_; }
Dave Allison20dfc792014-06-16 20:44:29 -07001864
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001865 protected:
1866 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07001867};
1868
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001869// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
1870// instruction that branches to the exit block.
1871class HReturnVoid : public HTemplateInstruction<0> {
1872 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001873 HReturnVoid() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001874
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001875 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001876
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001877 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001878
1879 private:
1880 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
1881};
1882
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001883// Represents dex's RETURN opcodes. A HReturn is a control flow
1884// instruction that branches to the exit block.
1885class HReturn : public HTemplateInstruction<1> {
1886 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001887 explicit HReturn(HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001888 SetRawInputAt(0, value);
1889 }
1890
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001891 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001892
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001893 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001894
1895 private:
1896 DISALLOW_COPY_AND_ASSIGN(HReturn);
1897};
1898
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001899// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001900// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001901// exit block.
1902class HExit : public HTemplateInstruction<0> {
1903 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001904 HExit() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001905
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001906 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001907
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001908 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001909
1910 private:
1911 DISALLOW_COPY_AND_ASSIGN(HExit);
1912};
1913
1914// Jumps from one block to another.
1915class HGoto : public HTemplateInstruction<0> {
1916 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001917 HGoto() : HTemplateInstruction(SideEffects::None()) {}
1918
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001919 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001920
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001921 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00001922 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001923 }
1924
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001925 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001926
1927 private:
1928 DISALLOW_COPY_AND_ASSIGN(HGoto);
1929};
1930
Dave Allison20dfc792014-06-16 20:44:29 -07001931
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001932// Conditional branch. A block ending with an HIf instruction must have
1933// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001934class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001935 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001936 explicit HIf(HInstruction* input) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001937 SetRawInputAt(0, input);
1938 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001939
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001940 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001941
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001942 HBasicBlock* IfTrueSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001943 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001944 }
1945
1946 HBasicBlock* IfFalseSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001947 return GetBlock()->GetSuccessors().Get(1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001948 }
1949
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001950 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001951
1952 private:
1953 DISALLOW_COPY_AND_ASSIGN(HIf);
1954};
1955
David Brazdilfc6a86a2015-06-26 10:33:45 +00001956
1957// Abstract instruction which marks the beginning and/or end of a try block and
1958// links it to the respective exception handlers. Behaves the same as a Goto in
1959// non-exceptional control flow.
1960// Normal-flow successor is stored at index zero, exception handlers under
1961// higher indices in no particular order.
1962class HTryBoundary : public HTemplateInstruction<0> {
1963 public:
David Brazdil56e1acc2015-06-30 15:41:36 +01001964 enum BoundaryKind {
1965 kEntry,
1966 kExit,
1967 };
1968
1969 explicit HTryBoundary(BoundaryKind kind)
1970 : HTemplateInstruction(SideEffects::None()), kind_(kind) {}
David Brazdilfc6a86a2015-06-26 10:33:45 +00001971
1972 bool IsControlFlow() const OVERRIDE { return true; }
1973
1974 // Returns the block's non-exceptional successor (index zero).
1975 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors().Get(0); }
1976
1977 // Returns whether `handler` is among its exception handlers (non-zero index
1978 // successors).
1979 bool HasExceptionHandler(HBasicBlock* handler) const {
1980 DCHECK(handler->IsCatchBlock());
1981 return GetBlock()->GetSuccessors().Contains(handler, /* start_from */ 1);
1982 }
1983
1984 // Returns whether successor at index `idx` is an exception handler.
1985 bool IsExceptionalSuccessor(size_t idx) const {
1986 DCHECK_LT(idx, GetBlock()->GetSuccessors().Size());
1987 bool is_handler = (idx != 0);
1988 DCHECK(!is_handler || GetBlock()->GetSuccessors().Get(idx)->IsCatchBlock());
1989 return is_handler;
1990 }
1991
1992 // If not present already, adds `handler` to its block's list of exception
1993 // handlers.
1994 void AddExceptionHandler(HBasicBlock* handler) {
1995 if (!HasExceptionHandler(handler)) {
1996 GetBlock()->AddSuccessor(handler);
1997 }
1998 }
1999
David Brazdil56e1acc2015-06-30 15:41:36 +01002000 bool IsEntry() const { return kind_ == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002001
2002 DECLARE_INSTRUCTION(TryBoundary);
2003
2004 private:
David Brazdil56e1acc2015-06-30 15:41:36 +01002005 const BoundaryKind kind_;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002006
2007 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2008};
2009
2010
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002011// Deoptimize to interpreter, upon checking a condition.
2012class HDeoptimize : public HTemplateInstruction<1> {
2013 public:
2014 HDeoptimize(HInstruction* cond, uint32_t dex_pc)
2015 : HTemplateInstruction(SideEffects::None()),
2016 dex_pc_(dex_pc) {
2017 SetRawInputAt(0, cond);
2018 }
2019
2020 bool NeedsEnvironment() const OVERRIDE { return true; }
2021 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002022 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002023
2024 DECLARE_INSTRUCTION(Deoptimize);
2025
2026 private:
2027 uint32_t dex_pc_;
2028
2029 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
2030};
2031
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002032// Represents the ArtMethod that was passed as a first argument to
2033// the method. It is used by instructions that depend on it, like
2034// instructions that work with the dex cache.
2035class HCurrentMethod : public HExpression<0> {
2036 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -07002037 explicit HCurrentMethod(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002038
2039 DECLARE_INSTRUCTION(CurrentMethod);
2040
2041 private:
2042 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
2043};
2044
Roland Levillain88cb1752014-10-20 16:36:47 +01002045class HUnaryOperation : public HExpression<1> {
2046 public:
2047 HUnaryOperation(Primitive::Type result_type, HInstruction* input)
2048 : HExpression(result_type, SideEffects::None()) {
2049 SetRawInputAt(0, input);
2050 }
2051
2052 HInstruction* GetInput() const { return InputAt(0); }
2053 Primitive::Type GetResultType() const { return GetType(); }
2054
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002055 bool CanBeMoved() const OVERRIDE { return true; }
2056 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002057 UNUSED(other);
2058 return true;
2059 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002060
Roland Levillain9240d6a2014-10-20 16:47:04 +01002061 // Try to statically evaluate `operation` and return a HConstant
2062 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002063 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002064 HConstant* TryStaticEvaluation() const;
2065
2066 // Apply this operation to `x`.
2067 virtual int32_t Evaluate(int32_t x) const = 0;
2068 virtual int64_t Evaluate(int64_t x) const = 0;
2069
Roland Levillain88cb1752014-10-20 16:36:47 +01002070 DECLARE_INSTRUCTION(UnaryOperation);
2071
2072 private:
2073 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
2074};
2075
Dave Allison20dfc792014-06-16 20:44:29 -07002076class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002077 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002078 HBinaryOperation(Primitive::Type result_type,
2079 HInstruction* left,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002080 HInstruction* right) : HExpression(result_type, SideEffects::None()) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002081 SetRawInputAt(0, left);
2082 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002083 }
2084
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002085 HInstruction* GetLeft() const { return InputAt(0); }
2086 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07002087 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002088
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002089 virtual bool IsCommutative() const { return false; }
2090
2091 // Put constant on the right.
2092 // Returns whether order is changed.
2093 bool OrderInputsWithConstantOnTheRight() {
2094 HInstruction* left = InputAt(0);
2095 HInstruction* right = InputAt(1);
2096 if (left->IsConstant() && !right->IsConstant()) {
2097 ReplaceInput(right, 0);
2098 ReplaceInput(left, 1);
2099 return true;
2100 }
2101 return false;
2102 }
2103
2104 // Order inputs by instruction id, but favor constant on the right side.
2105 // This helps GVN for commutative ops.
2106 void OrderInputs() {
2107 DCHECK(IsCommutative());
2108 HInstruction* left = InputAt(0);
2109 HInstruction* right = InputAt(1);
2110 if (left == right || (!left->IsConstant() && right->IsConstant())) {
2111 return;
2112 }
2113 if (OrderInputsWithConstantOnTheRight()) {
2114 return;
2115 }
2116 // Order according to instruction id.
2117 if (left->GetId() > right->GetId()) {
2118 ReplaceInput(right, 0);
2119 ReplaceInput(left, 1);
2120 }
2121 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002122
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002123 bool CanBeMoved() const OVERRIDE { return true; }
2124 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002125 UNUSED(other);
2126 return true;
2127 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002128
Roland Levillain9240d6a2014-10-20 16:47:04 +01002129 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01002130 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002131 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002132 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01002133
2134 // Apply this operation to `x` and `y`.
2135 virtual int32_t Evaluate(int32_t x, int32_t y) const = 0;
2136 virtual int64_t Evaluate(int64_t x, int64_t y) const = 0;
2137
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002138 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002139 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002140 HConstant* GetConstantRight() const;
2141
2142 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002143 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002144 HInstruction* GetLeastConstantLeft() const;
2145
Roland Levillainccc07a92014-09-16 14:48:16 +01002146 DECLARE_INSTRUCTION(BinaryOperation);
2147
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002148 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002149 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
2150};
2151
Mark Mendellc4701932015-04-10 13:18:51 -04002152// The comparison bias applies for floating point operations and indicates how NaN
2153// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01002154enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04002155 kNoBias, // bias is not applicable (i.e. for long operation)
2156 kGtBias, // return 1 for NaN comparisons
2157 kLtBias, // return -1 for NaN comparisons
2158};
2159
Dave Allison20dfc792014-06-16 20:44:29 -07002160class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002161 public:
Dave Allison20dfc792014-06-16 20:44:29 -07002162 HCondition(HInstruction* first, HInstruction* second)
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002163 : HBinaryOperation(Primitive::kPrimBoolean, first, second),
Mark Mendellc4701932015-04-10 13:18:51 -04002164 needs_materialization_(true),
Roland Levillain4fa13f62015-07-06 18:11:54 +01002165 bias_(ComparisonBias::kNoBias) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002166
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002167 bool NeedsMaterialization() const { return needs_materialization_; }
2168 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002169
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002170 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002171 // `instruction`, and disregard moves in between.
2172 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002173
Dave Allison20dfc792014-06-16 20:44:29 -07002174 DECLARE_INSTRUCTION(Condition);
2175
2176 virtual IfCondition GetCondition() const = 0;
2177
Mark Mendellc4701932015-04-10 13:18:51 -04002178 virtual IfCondition GetOppositeCondition() const = 0;
2179
Roland Levillain4fa13f62015-07-06 18:11:54 +01002180 bool IsGtBias() const { return bias_ == ComparisonBias::kGtBias; }
Mark Mendellc4701932015-04-10 13:18:51 -04002181
2182 void SetBias(ComparisonBias bias) { bias_ = bias; }
2183
2184 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2185 return bias_ == other->AsCondition()->bias_;
2186 }
2187
Roland Levillain4fa13f62015-07-06 18:11:54 +01002188 bool IsFPConditionTrueIfNaN() const {
2189 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2190 IfCondition if_cond = GetCondition();
2191 return IsGtBias() ? ((if_cond == kCondGT) || (if_cond == kCondGE)) : (if_cond == kCondNE);
2192 }
2193
2194 bool IsFPConditionFalseIfNaN() const {
2195 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2196 IfCondition if_cond = GetCondition();
2197 return IsGtBias() ? ((if_cond == kCondLT) || (if_cond == kCondLE)) : (if_cond == kCondEQ);
2198 }
2199
Dave Allison20dfc792014-06-16 20:44:29 -07002200 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002201 // For register allocation purposes, returns whether this instruction needs to be
2202 // materialized (that is, not just be in the processor flags).
2203 bool needs_materialization_;
2204
Mark Mendellc4701932015-04-10 13:18:51 -04002205 // Needed if we merge a HCompare into a HCondition.
2206 ComparisonBias bias_;
2207
Dave Allison20dfc792014-06-16 20:44:29 -07002208 DISALLOW_COPY_AND_ASSIGN(HCondition);
2209};
2210
2211// Instruction to check if two inputs are equal to each other.
2212class HEqual : public HCondition {
2213 public:
2214 HEqual(HInstruction* first, HInstruction* second)
2215 : HCondition(first, second) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002216
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002217 bool IsCommutative() const OVERRIDE { return true; }
2218
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002219 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002220 return x == y ? 1 : 0;
2221 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002222 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002223 return x == y ? 1 : 0;
2224 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002225
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002226 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002227
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002228 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002229 return kCondEQ;
2230 }
2231
Mark Mendellc4701932015-04-10 13:18:51 -04002232 IfCondition GetOppositeCondition() const OVERRIDE {
2233 return kCondNE;
2234 }
2235
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002236 private:
2237 DISALLOW_COPY_AND_ASSIGN(HEqual);
2238};
2239
Dave Allison20dfc792014-06-16 20:44:29 -07002240class HNotEqual : public HCondition {
2241 public:
2242 HNotEqual(HInstruction* first, HInstruction* second)
2243 : HCondition(first, second) {}
2244
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002245 bool IsCommutative() const OVERRIDE { return true; }
2246
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002247 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002248 return x != y ? 1 : 0;
2249 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002250 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002251 return x != y ? 1 : 0;
2252 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002253
Dave Allison20dfc792014-06-16 20:44:29 -07002254 DECLARE_INSTRUCTION(NotEqual);
2255
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002256 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002257 return kCondNE;
2258 }
2259
Mark Mendellc4701932015-04-10 13:18:51 -04002260 IfCondition GetOppositeCondition() const OVERRIDE {
2261 return kCondEQ;
2262 }
2263
Dave Allison20dfc792014-06-16 20:44:29 -07002264 private:
2265 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
2266};
2267
2268class HLessThan : public HCondition {
2269 public:
2270 HLessThan(HInstruction* first, HInstruction* second)
2271 : HCondition(first, second) {}
2272
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002273 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002274 return x < y ? 1 : 0;
2275 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002276 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002277 return x < y ? 1 : 0;
2278 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002279
Dave Allison20dfc792014-06-16 20:44:29 -07002280 DECLARE_INSTRUCTION(LessThan);
2281
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002282 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002283 return kCondLT;
2284 }
2285
Mark Mendellc4701932015-04-10 13:18:51 -04002286 IfCondition GetOppositeCondition() const OVERRIDE {
2287 return kCondGE;
2288 }
2289
Dave Allison20dfc792014-06-16 20:44:29 -07002290 private:
2291 DISALLOW_COPY_AND_ASSIGN(HLessThan);
2292};
2293
2294class HLessThanOrEqual : public HCondition {
2295 public:
2296 HLessThanOrEqual(HInstruction* first, HInstruction* second)
2297 : HCondition(first, second) {}
2298
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002299 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002300 return x <= y ? 1 : 0;
2301 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002302 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002303 return x <= y ? 1 : 0;
2304 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002305
Dave Allison20dfc792014-06-16 20:44:29 -07002306 DECLARE_INSTRUCTION(LessThanOrEqual);
2307
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002308 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002309 return kCondLE;
2310 }
2311
Mark Mendellc4701932015-04-10 13:18:51 -04002312 IfCondition GetOppositeCondition() const OVERRIDE {
2313 return kCondGT;
2314 }
2315
Dave Allison20dfc792014-06-16 20:44:29 -07002316 private:
2317 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
2318};
2319
2320class HGreaterThan : public HCondition {
2321 public:
2322 HGreaterThan(HInstruction* first, HInstruction* second)
2323 : HCondition(first, second) {}
2324
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002325 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002326 return x > y ? 1 : 0;
2327 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002328 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002329 return x > y ? 1 : 0;
2330 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002331
Dave Allison20dfc792014-06-16 20:44:29 -07002332 DECLARE_INSTRUCTION(GreaterThan);
2333
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002334 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002335 return kCondGT;
2336 }
2337
Mark Mendellc4701932015-04-10 13:18:51 -04002338 IfCondition GetOppositeCondition() const OVERRIDE {
2339 return kCondLE;
2340 }
2341
Dave Allison20dfc792014-06-16 20:44:29 -07002342 private:
2343 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
2344};
2345
2346class HGreaterThanOrEqual : public HCondition {
2347 public:
2348 HGreaterThanOrEqual(HInstruction* first, HInstruction* second)
2349 : HCondition(first, second) {}
2350
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002351 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002352 return x >= y ? 1 : 0;
2353 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002354 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002355 return x >= y ? 1 : 0;
2356 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002357
Dave Allison20dfc792014-06-16 20:44:29 -07002358 DECLARE_INSTRUCTION(GreaterThanOrEqual);
2359
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002360 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002361 return kCondGE;
2362 }
2363
Mark Mendellc4701932015-04-10 13:18:51 -04002364 IfCondition GetOppositeCondition() const OVERRIDE {
2365 return kCondLT;
2366 }
2367
Dave Allison20dfc792014-06-16 20:44:29 -07002368 private:
2369 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
2370};
2371
2372
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002373// Instruction to check how two inputs compare to each other.
2374// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
2375class HCompare : public HBinaryOperation {
2376 public:
Alexey Frunze4dda3372015-06-01 18:31:49 -07002377 HCompare(Primitive::Type type,
2378 HInstruction* first,
2379 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04002380 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07002381 uint32_t dex_pc)
2382 : HBinaryOperation(Primitive::kPrimInt, first, second), bias_(bias), dex_pc_(dex_pc) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002383 DCHECK_EQ(type, first->GetType());
2384 DCHECK_EQ(type, second->GetType());
2385 }
2386
Calin Juravleddb7df22014-11-25 20:56:51 +00002387 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01002388 return
2389 x == y ? 0 :
2390 x > y ? 1 :
2391 -1;
2392 }
Calin Juravleddb7df22014-11-25 20:56:51 +00002393
2394 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01002395 return
2396 x == y ? 0 :
2397 x > y ? 1 :
2398 -1;
2399 }
2400
Calin Juravleddb7df22014-11-25 20:56:51 +00002401 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2402 return bias_ == other->AsCompare()->bias_;
2403 }
2404
Mark Mendellc4701932015-04-10 13:18:51 -04002405 ComparisonBias GetBias() const { return bias_; }
2406
Roland Levillain4fa13f62015-07-06 18:11:54 +01002407 bool IsGtBias() { return bias_ == ComparisonBias::kGtBias; }
Calin Juravleddb7df22014-11-25 20:56:51 +00002408
Alexey Frunze4dda3372015-06-01 18:31:49 -07002409 uint32_t GetDexPc() const { return dex_pc_; }
2410
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002411 DECLARE_INSTRUCTION(Compare);
2412
2413 private:
Mark Mendellc4701932015-04-10 13:18:51 -04002414 const ComparisonBias bias_;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002415 const uint32_t dex_pc_;
Calin Juravleddb7df22014-11-25 20:56:51 +00002416
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002417 DISALLOW_COPY_AND_ASSIGN(HCompare);
2418};
2419
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002420// A local in the graph. Corresponds to a Dex register.
2421class HLocal : public HTemplateInstruction<0> {
2422 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002423 explicit HLocal(uint16_t reg_number)
2424 : HTemplateInstruction(SideEffects::None()), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002425
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002426 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002427
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002428 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002429
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002430 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002431 // The Dex register number.
2432 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002433
2434 DISALLOW_COPY_AND_ASSIGN(HLocal);
2435};
2436
2437// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07002438class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002439 public:
Roland Levillain5799fc02014-09-25 12:15:20 +01002440 HLoadLocal(HLocal* local, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002441 : HExpression(type, SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002442 SetRawInputAt(0, local);
2443 }
2444
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002445 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
2446
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002447 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002448
2449 private:
2450 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
2451};
2452
2453// Store a value in a given local. This instruction has two inputs: the value
2454// and the local.
2455class HStoreLocal : public HTemplateInstruction<2> {
2456 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002457 HStoreLocal(HLocal* local, HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002458 SetRawInputAt(0, local);
2459 SetRawInputAt(1, value);
2460 }
2461
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002462 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
2463
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002464 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002465
2466 private:
2467 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
2468};
2469
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002470class HConstant : public HExpression<0> {
2471 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002472 explicit HConstant(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
2473
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002474 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002475
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002476 virtual bool IsMinusOne() const { return false; }
2477 virtual bool IsZero() const { return false; }
2478 virtual bool IsOne() const { return false; }
2479
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002480 DECLARE_INSTRUCTION(Constant);
2481
2482 private:
2483 DISALLOW_COPY_AND_ASSIGN(HConstant);
2484};
2485
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002486class HFloatConstant : public HConstant {
2487 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002488 float GetValue() const { return value_; }
2489
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002490 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002491 return bit_cast<uint32_t, float>(other->AsFloatConstant()->value_) ==
2492 bit_cast<uint32_t, float>(value_);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002493 }
2494
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002495 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002496
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002497 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002498 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002499 }
2500 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002501 return value_ == 0.0f;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002502 }
2503 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002504 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
2505 }
2506 bool IsNaN() const {
2507 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002508 }
2509
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002510 DECLARE_INSTRUCTION(FloatConstant);
2511
2512 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002513 explicit HFloatConstant(float value) : HConstant(Primitive::kPrimFloat), value_(value) {}
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002514 explicit HFloatConstant(int32_t value)
2515 : HConstant(Primitive::kPrimFloat), value_(bit_cast<float, int32_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00002516
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002517 const float value_;
2518
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002519 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00002520 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002521 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002522 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2523};
2524
2525class HDoubleConstant : public HConstant {
2526 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002527 double GetValue() const { return value_; }
2528
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002529 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002530 return bit_cast<uint64_t, double>(other->AsDoubleConstant()->value_) ==
2531 bit_cast<uint64_t, double>(value_);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002532 }
2533
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002534 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002535
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002536 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002537 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002538 }
2539 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002540 return value_ == 0.0;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002541 }
2542 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002543 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
2544 }
2545 bool IsNaN() const {
2546 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002547 }
2548
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002549 DECLARE_INSTRUCTION(DoubleConstant);
2550
2551 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002552 explicit HDoubleConstant(double value) : HConstant(Primitive::kPrimDouble), value_(value) {}
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002553 explicit HDoubleConstant(int64_t value)
2554 : HConstant(Primitive::kPrimDouble), value_(bit_cast<double, int64_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00002555
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002556 const double value_;
2557
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002558 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00002559 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002560 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002561 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
2562};
2563
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002564class HNullConstant : public HConstant {
2565 public:
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002566 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2567 return true;
2568 }
2569
2570 size_t ComputeHashCode() const OVERRIDE { return 0; }
2571
2572 DECLARE_INSTRUCTION(NullConstant);
2573
2574 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002575 HNullConstant() : HConstant(Primitive::kPrimNot) {}
2576
2577 friend class HGraph;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002578 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2579};
2580
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002581// Constants of the type int. Those can be from Dex instructions, or
2582// synthesized (for example with the if-eqz instruction).
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002583class HIntConstant : public HConstant {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002584 public:
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002585 int32_t GetValue() const { return value_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002586
Calin Juravle61d544b2015-02-23 16:46:57 +00002587 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002588 return other->AsIntConstant()->value_ == value_;
2589 }
2590
Calin Juravle61d544b2015-02-23 16:46:57 +00002591 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2592
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002593 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2594 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2595 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2596
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002597 DECLARE_INSTRUCTION(IntConstant);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002598
2599 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002600 explicit HIntConstant(int32_t value) : HConstant(Primitive::kPrimInt), value_(value) {}
2601
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002602 const int32_t value_;
2603
David Brazdil8d5b8b22015-03-24 10:51:52 +00002604 friend class HGraph;
2605 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
Zheng Xuad4450e2015-04-17 18:48:56 +08002606 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002607 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2608};
2609
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002610class HLongConstant : public HConstant {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002611 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002612 int64_t GetValue() const { return value_; }
2613
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002614 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002615 return other->AsLongConstant()->value_ == value_;
2616 }
2617
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002618 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002619
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002620 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2621 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2622 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2623
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002624 DECLARE_INSTRUCTION(LongConstant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002625
2626 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002627 explicit HLongConstant(int64_t value) : HConstant(Primitive::kPrimLong), value_(value) {}
2628
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002629 const int64_t value_;
2630
David Brazdil8d5b8b22015-03-24 10:51:52 +00002631 friend class HGraph;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002632 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2633};
2634
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002635enum class Intrinsics {
2636#define OPTIMIZING_INTRINSICS(Name, IsStatic) k ## Name,
2637#include "intrinsics_list.h"
2638 kNone,
2639 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
2640#undef INTRINSICS_LIST
2641#undef OPTIMIZING_INTRINSICS
2642};
2643std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
2644
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002645class HInvoke : public HInstruction {
2646 public:
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002647 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002648
2649 // Runtime needs to walk the stack, so Dex -> Dex calls need to
2650 // know their environment.
Calin Juravle77520bc2015-01-12 18:45:46 +00002651 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002652
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01002653 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002654 SetRawInputAt(index, argument);
2655 }
2656
Roland Levillain3e3d7332015-04-28 11:00:54 +01002657 // Return the number of arguments. This number can be lower than
2658 // the number of inputs returned by InputCount(), as some invoke
2659 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
2660 // inputs at the end of their list of inputs.
2661 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
2662
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002663 Primitive::Type GetType() const OVERRIDE { return return_type_; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002664
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002665 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002666
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002667 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002668 const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002669
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002670 InvokeType GetOriginalInvokeType() const { return original_invoke_type_; }
2671
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01002672 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002673 return intrinsic_;
2674 }
2675
2676 void SetIntrinsic(Intrinsics intrinsic) {
2677 intrinsic_ = intrinsic;
2678 }
2679
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01002680 bool IsFromInlinedInvoke() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002681 return GetEnvironment()->GetParent() != nullptr;
2682 }
2683
2684 bool CanThrow() const OVERRIDE { return true; }
2685
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002686 DECLARE_INSTRUCTION(Invoke);
2687
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002688 protected:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002689 HInvoke(ArenaAllocator* arena,
2690 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01002691 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002692 Primitive::Type return_type,
2693 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002694 uint32_t dex_method_index,
2695 InvokeType original_invoke_type)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002696 : HInstruction(SideEffects::All()),
Roland Levillain3e3d7332015-04-28 11:00:54 +01002697 number_of_arguments_(number_of_arguments),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002698 inputs_(arena, number_of_arguments),
2699 return_type_(return_type),
2700 dex_pc_(dex_pc),
2701 dex_method_index_(dex_method_index),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002702 original_invoke_type_(original_invoke_type),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002703 intrinsic_(Intrinsics::kNone) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01002704 uint32_t number_of_inputs = number_of_arguments + number_of_other_inputs;
2705 inputs_.SetSize(number_of_inputs);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002706 }
2707
David Brazdil1abb4192015-02-17 18:33:36 +00002708 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
2709 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
2710 inputs_.Put(index, input);
2711 }
2712
Roland Levillain3e3d7332015-04-28 11:00:54 +01002713 uint32_t number_of_arguments_;
David Brazdil1abb4192015-02-17 18:33:36 +00002714 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002715 const Primitive::Type return_type_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002716 const uint32_t dex_pc_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002717 const uint32_t dex_method_index_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002718 const InvokeType original_invoke_type_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002719 Intrinsics intrinsic_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002720
2721 private:
2722 DISALLOW_COPY_AND_ASSIGN(HInvoke);
2723};
2724
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002725class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002726 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01002727 // Requirements of this method call regarding the class
2728 // initialization (clinit) check of its declaring class.
2729 enum class ClinitCheckRequirement {
2730 kNone, // Class already initialized.
2731 kExplicit, // Static call having explicit clinit check as last input.
2732 kImplicit, // Static call implicitly requiring a clinit check.
2733 };
2734
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002735 HInvokeStaticOrDirect(ArenaAllocator* arena,
2736 uint32_t number_of_arguments,
2737 Primitive::Type return_type,
2738 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002739 uint32_t dex_method_index,
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002740 bool is_recursive,
Jeff Hao848f70a2014-01-15 13:49:50 -08002741 int32_t string_init_offset,
Nicolas Geoffray79041292015-03-26 10:05:54 +00002742 InvokeType original_invoke_type,
Roland Levillain4c0eb422015-04-24 16:43:49 +01002743 InvokeType invoke_type,
2744 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01002745 : HInvoke(arena,
2746 number_of_arguments,
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01002747 // There is one extra argument for the HCurrentMethod node, and
2748 // potentially one other if the clinit check is explicit, and one other
2749 // if the method is a string factory.
2750 1u + (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u)
2751 + (string_init_offset ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01002752 return_type,
2753 dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002754 dex_method_index,
2755 original_invoke_type),
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002756 invoke_type_(invoke_type),
Roland Levillain4c0eb422015-04-24 16:43:49 +01002757 is_recursive_(is_recursive),
Jeff Hao848f70a2014-01-15 13:49:50 -08002758 clinit_check_requirement_(clinit_check_requirement),
2759 string_init_offset_(string_init_offset) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002760
Calin Juravle641547a2015-04-21 22:08:51 +01002761 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
2762 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00002763 // We access the method via the dex cache so we can't do an implicit null check.
2764 // TODO: for intrinsics we can generate implicit null checks.
2765 return false;
2766 }
2767
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002768 InvokeType GetInvokeType() const { return invoke_type_; }
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002769 bool IsRecursive() const { return is_recursive_; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002770 bool NeedsDexCache() const OVERRIDE { return !IsRecursive(); }
Jeff Hao848f70a2014-01-15 13:49:50 -08002771 bool IsStringInit() const { return string_init_offset_ != 0; }
2772 int32_t GetStringInitOffset() const { return string_init_offset_; }
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002773 uint32_t GetCurrentMethodInputIndex() const { return GetNumberOfArguments(); }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002774
Roland Levillain4c0eb422015-04-24 16:43:49 +01002775 // Is this instruction a call to a static method?
2776 bool IsStatic() const {
2777 return GetInvokeType() == kStatic;
2778 }
2779
Roland Levillain3e3d7332015-04-28 11:00:54 +01002780 // Remove the art::HLoadClass instruction set as last input by
2781 // art::PrepareForRegisterAllocation::VisitClinitCheck in lieu of
2782 // the initial art::HClinitCheck instruction (only relevant for
2783 // static calls with explicit clinit check).
2784 void RemoveLoadClassAsLastInput() {
Roland Levillain4c0eb422015-04-24 16:43:49 +01002785 DCHECK(IsStaticWithExplicitClinitCheck());
2786 size_t last_input_index = InputCount() - 1;
2787 HInstruction* last_input = InputAt(last_input_index);
2788 DCHECK(last_input != nullptr);
Roland Levillain3e3d7332015-04-28 11:00:54 +01002789 DCHECK(last_input->IsLoadClass()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01002790 RemoveAsUserOfInput(last_input_index);
2791 inputs_.DeleteAt(last_input_index);
2792 clinit_check_requirement_ = ClinitCheckRequirement::kImplicit;
2793 DCHECK(IsStaticWithImplicitClinitCheck());
2794 }
2795
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01002796 bool IsStringFactoryFor(HFakeString* str) const {
2797 if (!IsStringInit()) return false;
2798 // +1 for the current method.
2799 if (InputCount() == (number_of_arguments_ + 1)) return false;
2800 return InputAt(InputCount() - 1)->AsFakeString() == str;
2801 }
2802
2803 void RemoveFakeStringArgumentAsLastInput() {
2804 DCHECK(IsStringInit());
2805 size_t last_input_index = InputCount() - 1;
2806 HInstruction* last_input = InputAt(last_input_index);
2807 DCHECK(last_input != nullptr);
2808 DCHECK(last_input->IsFakeString()) << last_input->DebugName();
2809 RemoveAsUserOfInput(last_input_index);
2810 inputs_.DeleteAt(last_input_index);
2811 }
2812
Roland Levillain4c0eb422015-04-24 16:43:49 +01002813 // Is this a call to a static method whose declaring class has an
2814 // explicit intialization check in the graph?
2815 bool IsStaticWithExplicitClinitCheck() const {
2816 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kExplicit);
2817 }
2818
2819 // Is this a call to a static method whose declaring class has an
2820 // implicit intialization check requirement?
2821 bool IsStaticWithImplicitClinitCheck() const {
2822 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kImplicit);
2823 }
2824
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002825 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002826
Roland Levillain4c0eb422015-04-24 16:43:49 +01002827 protected:
2828 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
2829 const HUserRecord<HInstruction*> input_record = HInvoke::InputRecordAt(i);
2830 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck() && (i == InputCount() - 1)) {
2831 HInstruction* input = input_record.GetInstruction();
2832 // `input` is the last input of a static invoke marked as having
2833 // an explicit clinit check. It must either be:
2834 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
2835 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
2836 DCHECK(input != nullptr);
2837 DCHECK(input->IsClinitCheck() || input->IsLoadClass()) << input->DebugName();
2838 }
2839 return input_record;
2840 }
2841
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002842 private:
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002843 const InvokeType invoke_type_;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002844 const bool is_recursive_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01002845 ClinitCheckRequirement clinit_check_requirement_;
Jeff Hao848f70a2014-01-15 13:49:50 -08002846 // Thread entrypoint offset for string init method if this is a string init invoke.
2847 // Note that there are multiple string init methods, each having its own offset.
2848 int32_t string_init_offset_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002849
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002850 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002851};
2852
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002853class HInvokeVirtual : public HInvoke {
2854 public:
2855 HInvokeVirtual(ArenaAllocator* arena,
2856 uint32_t number_of_arguments,
2857 Primitive::Type return_type,
2858 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002859 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002860 uint32_t vtable_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002861 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002862 vtable_index_(vtable_index) {}
2863
Calin Juravle641547a2015-04-21 22:08:51 +01002864 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00002865 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01002866 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00002867 }
2868
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002869 uint32_t GetVTableIndex() const { return vtable_index_; }
2870
2871 DECLARE_INSTRUCTION(InvokeVirtual);
2872
2873 private:
2874 const uint32_t vtable_index_;
2875
2876 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
2877};
2878
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002879class HInvokeInterface : public HInvoke {
2880 public:
2881 HInvokeInterface(ArenaAllocator* arena,
2882 uint32_t number_of_arguments,
2883 Primitive::Type return_type,
2884 uint32_t dex_pc,
2885 uint32_t dex_method_index,
2886 uint32_t imt_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002887 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002888 imt_index_(imt_index) {}
2889
Calin Juravle641547a2015-04-21 22:08:51 +01002890 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00002891 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01002892 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00002893 }
2894
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002895 uint32_t GetImtIndex() const { return imt_index_; }
2896 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
2897
2898 DECLARE_INSTRUCTION(InvokeInterface);
2899
2900 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002901 const uint32_t imt_index_;
2902
2903 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
2904};
2905
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01002906class HNewInstance : public HExpression<1> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002907 public:
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01002908 HNewInstance(HCurrentMethod* current_method,
2909 uint32_t dex_pc,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002910 uint16_t type_index,
2911 const DexFile& dex_file,
2912 QuickEntrypointEnum entrypoint)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002913 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2914 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002915 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002916 dex_file_(dex_file),
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01002917 entrypoint_(entrypoint) {
2918 SetRawInputAt(0, current_method);
2919 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002920
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002921 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002922 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002923 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002924
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002925 // Calls runtime so needs an environment.
Calin Juravle92a6ed22014-12-02 18:58:03 +00002926 bool NeedsEnvironment() const OVERRIDE { return true; }
2927 // It may throw when called on:
2928 // - interfaces
2929 // - abstract/innaccessible/unknown classes
2930 // TODO: optimize when possible.
2931 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002932
Calin Juravle10e244f2015-01-26 18:54:32 +00002933 bool CanBeNull() const OVERRIDE { return false; }
2934
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002935 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
2936
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002937 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002938
2939 private:
2940 const uint32_t dex_pc_;
2941 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002942 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002943 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002944
2945 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
2946};
2947
Roland Levillain88cb1752014-10-20 16:36:47 +01002948class HNeg : public HUnaryOperation {
2949 public:
2950 explicit HNeg(Primitive::Type result_type, HInstruction* input)
2951 : HUnaryOperation(result_type, input) {}
2952
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002953 int32_t Evaluate(int32_t x) const OVERRIDE { return -x; }
2954 int64_t Evaluate(int64_t x) const OVERRIDE { return -x; }
Roland Levillain9240d6a2014-10-20 16:47:04 +01002955
Roland Levillain88cb1752014-10-20 16:36:47 +01002956 DECLARE_INSTRUCTION(Neg);
2957
2958 private:
2959 DISALLOW_COPY_AND_ASSIGN(HNeg);
2960};
2961
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01002962class HNewArray : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002963 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002964 HNewArray(HInstruction* length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01002965 HCurrentMethod* current_method,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002966 uint32_t dex_pc,
2967 uint16_t type_index,
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01002968 const DexFile& dex_file,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002969 QuickEntrypointEnum entrypoint)
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002970 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2971 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002972 type_index_(type_index),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01002973 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002974 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002975 SetRawInputAt(0, length);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01002976 SetRawInputAt(1, current_method);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002977 }
2978
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002979 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002980 uint16_t GetTypeIndex() const { return type_index_; }
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01002981 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002982
2983 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00002984 bool NeedsEnvironment() const OVERRIDE { return true; }
2985
Mingyao Yang0c365e62015-03-31 15:09:29 -07002986 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
2987 bool CanThrow() const OVERRIDE { return true; }
2988
Calin Juravle10e244f2015-01-26 18:54:32 +00002989 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002990
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002991 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
2992
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002993 DECLARE_INSTRUCTION(NewArray);
2994
2995 private:
2996 const uint32_t dex_pc_;
2997 const uint16_t type_index_;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01002998 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002999 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003000
3001 DISALLOW_COPY_AND_ASSIGN(HNewArray);
3002};
3003
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003004class HAdd : public HBinaryOperation {
3005 public:
3006 HAdd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3007 : HBinaryOperation(result_type, left, right) {}
3008
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003009 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003010
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003011 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01003012 return x + y;
3013 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003014 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01003015 return x + y;
3016 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003017
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003018 DECLARE_INSTRUCTION(Add);
3019
3020 private:
3021 DISALLOW_COPY_AND_ASSIGN(HAdd);
3022};
3023
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003024class HSub : public HBinaryOperation {
3025 public:
3026 HSub(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3027 : HBinaryOperation(result_type, left, right) {}
3028
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003029 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01003030 return x - y;
3031 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003032 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01003033 return x - y;
3034 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003035
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003036 DECLARE_INSTRUCTION(Sub);
3037
3038 private:
3039 DISALLOW_COPY_AND_ASSIGN(HSub);
3040};
3041
Calin Juravle34bacdf2014-10-07 20:23:36 +01003042class HMul : public HBinaryOperation {
3043 public:
3044 HMul(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3045 : HBinaryOperation(result_type, left, right) {}
3046
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003047 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003048
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003049 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x * y; }
3050 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x * y; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003051
3052 DECLARE_INSTRUCTION(Mul);
3053
3054 private:
3055 DISALLOW_COPY_AND_ASSIGN(HMul);
3056};
3057
Calin Juravle7c4954d2014-10-28 16:57:40 +00003058class HDiv : public HBinaryOperation {
3059 public:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003060 HDiv(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
3061 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00003062
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003063 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00003064 // Our graph structure ensures we never have 0 for `y` during constant folding.
3065 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00003066 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00003067 return (y == -1) ? -x : x / y;
3068 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003069
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003070 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00003071 DCHECK_NE(y, 0);
3072 // Special case -1 to avoid getting a SIGFPE on x86(_64).
3073 return (y == -1) ? -x : x / y;
3074 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003075
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003076 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003077
Calin Juravle7c4954d2014-10-28 16:57:40 +00003078 DECLARE_INSTRUCTION(Div);
3079
3080 private:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003081 const uint32_t dex_pc_;
3082
Calin Juravle7c4954d2014-10-28 16:57:40 +00003083 DISALLOW_COPY_AND_ASSIGN(HDiv);
3084};
3085
Calin Juravlebacfec32014-11-14 15:54:36 +00003086class HRem : public HBinaryOperation {
3087 public:
3088 HRem(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
3089 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
3090
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003091 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00003092 DCHECK_NE(y, 0);
3093 // Special case -1 to avoid getting a SIGFPE on x86(_64).
3094 return (y == -1) ? 0 : x % y;
3095 }
3096
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003097 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00003098 DCHECK_NE(y, 0);
3099 // Special case -1 to avoid getting a SIGFPE on x86(_64).
3100 return (y == -1) ? 0 : x % y;
3101 }
3102
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003103 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Calin Juravlebacfec32014-11-14 15:54:36 +00003104
3105 DECLARE_INSTRUCTION(Rem);
3106
3107 private:
3108 const uint32_t dex_pc_;
3109
3110 DISALLOW_COPY_AND_ASSIGN(HRem);
3111};
3112
Calin Juravled0d48522014-11-04 16:40:20 +00003113class HDivZeroCheck : public HExpression<1> {
3114 public:
3115 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
3116 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
3117 SetRawInputAt(0, value);
3118 }
3119
3120 bool CanBeMoved() const OVERRIDE { return true; }
3121
3122 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3123 UNUSED(other);
3124 return true;
3125 }
3126
3127 bool NeedsEnvironment() const OVERRIDE { return true; }
3128 bool CanThrow() const OVERRIDE { return true; }
3129
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003130 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Calin Juravled0d48522014-11-04 16:40:20 +00003131
3132 DECLARE_INSTRUCTION(DivZeroCheck);
3133
3134 private:
3135 const uint32_t dex_pc_;
3136
3137 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
3138};
3139
Calin Juravle9aec02f2014-11-18 23:06:35 +00003140class HShl : public HBinaryOperation {
3141 public:
3142 HShl(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3143 : HBinaryOperation(result_type, left, right) {}
3144
3145 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x << (y & kMaxIntShiftValue); }
3146 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x << (y & kMaxLongShiftValue); }
3147
3148 DECLARE_INSTRUCTION(Shl);
3149
3150 private:
3151 DISALLOW_COPY_AND_ASSIGN(HShl);
3152};
3153
3154class HShr : public HBinaryOperation {
3155 public:
3156 HShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3157 : HBinaryOperation(result_type, left, right) {}
3158
3159 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x >> (y & kMaxIntShiftValue); }
3160 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x >> (y & kMaxLongShiftValue); }
3161
3162 DECLARE_INSTRUCTION(Shr);
3163
3164 private:
3165 DISALLOW_COPY_AND_ASSIGN(HShr);
3166};
3167
3168class HUShr : public HBinaryOperation {
3169 public:
3170 HUShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3171 : HBinaryOperation(result_type, left, right) {}
3172
3173 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
3174 uint32_t ux = static_cast<uint32_t>(x);
3175 uint32_t uy = static_cast<uint32_t>(y) & kMaxIntShiftValue;
3176 return static_cast<int32_t>(ux >> uy);
3177 }
3178
3179 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
3180 uint64_t ux = static_cast<uint64_t>(x);
3181 uint64_t uy = static_cast<uint64_t>(y) & kMaxLongShiftValue;
3182 return static_cast<int64_t>(ux >> uy);
3183 }
3184
3185 DECLARE_INSTRUCTION(UShr);
3186
3187 private:
3188 DISALLOW_COPY_AND_ASSIGN(HUShr);
3189};
3190
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003191class HAnd : public HBinaryOperation {
3192 public:
3193 HAnd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3194 : HBinaryOperation(result_type, left, right) {}
3195
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003196 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003197
3198 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x & y; }
3199 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x & y; }
3200
3201 DECLARE_INSTRUCTION(And);
3202
3203 private:
3204 DISALLOW_COPY_AND_ASSIGN(HAnd);
3205};
3206
3207class HOr : public HBinaryOperation {
3208 public:
3209 HOr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3210 : HBinaryOperation(result_type, left, right) {}
3211
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003212 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003213
3214 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x | y; }
3215 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x | y; }
3216
3217 DECLARE_INSTRUCTION(Or);
3218
3219 private:
3220 DISALLOW_COPY_AND_ASSIGN(HOr);
3221};
3222
3223class HXor : public HBinaryOperation {
3224 public:
3225 HXor(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3226 : HBinaryOperation(result_type, left, right) {}
3227
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003228 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003229
3230 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x ^ y; }
3231 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x ^ y; }
3232
3233 DECLARE_INSTRUCTION(Xor);
3234
3235 private:
3236 DISALLOW_COPY_AND_ASSIGN(HXor);
3237};
3238
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003239// The value of a parameter in this method. Its location depends on
3240// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07003241class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003242 public:
Calin Juravle10e244f2015-01-26 18:54:32 +00003243 HParameterValue(uint8_t index, Primitive::Type parameter_type, bool is_this = false)
3244 : HExpression(parameter_type, SideEffects::None()), index_(index), is_this_(is_this) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003245
3246 uint8_t GetIndex() const { return index_; }
3247
Calin Juravle10e244f2015-01-26 18:54:32 +00003248 bool CanBeNull() const OVERRIDE { return !is_this_; }
3249
Calin Juravle3cd4fc82015-05-14 15:15:42 +01003250 bool IsThis() const { return is_this_; }
3251
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003252 DECLARE_INSTRUCTION(ParameterValue);
3253
3254 private:
3255 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00003256 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003257 const uint8_t index_;
3258
Calin Juravle10e244f2015-01-26 18:54:32 +00003259 // Whether or not the parameter value corresponds to 'this' argument.
3260 const bool is_this_;
3261
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003262 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
3263};
3264
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003265class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003266 public:
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003267 explicit HNot(Primitive::Type result_type, HInstruction* input)
3268 : HUnaryOperation(result_type, input) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003269
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003270 bool CanBeMoved() const OVERRIDE { return true; }
3271 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003272 UNUSED(other);
3273 return true;
3274 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003275
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003276 int32_t Evaluate(int32_t x) const OVERRIDE { return ~x; }
3277 int64_t Evaluate(int64_t x) const OVERRIDE { return ~x; }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003278
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003279 DECLARE_INSTRUCTION(Not);
3280
3281 private:
3282 DISALLOW_COPY_AND_ASSIGN(HNot);
3283};
3284
David Brazdil66d126e2015-04-03 16:02:44 +01003285class HBooleanNot : public HUnaryOperation {
3286 public:
3287 explicit HBooleanNot(HInstruction* input)
3288 : HUnaryOperation(Primitive::Type::kPrimBoolean, input) {}
3289
3290 bool CanBeMoved() const OVERRIDE { return true; }
3291 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3292 UNUSED(other);
3293 return true;
3294 }
3295
3296 int32_t Evaluate(int32_t x) const OVERRIDE {
3297 DCHECK(IsUint<1>(x));
3298 return !x;
3299 }
3300
3301 int64_t Evaluate(int64_t x ATTRIBUTE_UNUSED) const OVERRIDE {
3302 LOG(FATAL) << DebugName() << " cannot be used with 64-bit values";
3303 UNREACHABLE();
3304 }
3305
3306 DECLARE_INSTRUCTION(BooleanNot);
3307
3308 private:
3309 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
3310};
3311
Roland Levillaindff1f282014-11-05 14:15:05 +00003312class HTypeConversion : public HExpression<1> {
3313 public:
3314 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00003315 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
3316 : HExpression(result_type, SideEffects::None()), dex_pc_(dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00003317 SetRawInputAt(0, input);
3318 DCHECK_NE(input->GetType(), result_type);
3319 }
3320
3321 HInstruction* GetInput() const { return InputAt(0); }
3322 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
3323 Primitive::Type GetResultType() const { return GetType(); }
3324
Roland Levillain624279f2014-12-04 11:54:28 +00003325 // Required by the x86 and ARM code generators when producing calls
3326 // to the runtime.
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003327 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Roland Levillain624279f2014-12-04 11:54:28 +00003328
Roland Levillaindff1f282014-11-05 14:15:05 +00003329 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00003330 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00003331
Mark Mendelle82549b2015-05-06 10:55:34 -04003332 // Try to statically evaluate the conversion and return a HConstant
3333 // containing the result. If the input cannot be converted, return nullptr.
3334 HConstant* TryStaticEvaluation() const;
3335
Roland Levillaindff1f282014-11-05 14:15:05 +00003336 DECLARE_INSTRUCTION(TypeConversion);
3337
3338 private:
Roland Levillain624279f2014-12-04 11:54:28 +00003339 const uint32_t dex_pc_;
3340
Roland Levillaindff1f282014-11-05 14:15:05 +00003341 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
3342};
3343
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00003344static constexpr uint32_t kNoRegNumber = -1;
3345
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003346class HPhi : public HInstruction {
3347 public:
3348 HPhi(ArenaAllocator* arena, uint32_t reg_number, size_t number_of_inputs, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003349 : HInstruction(SideEffects::None()),
3350 inputs_(arena, number_of_inputs),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003351 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01003352 type_(type),
Calin Juravle10e244f2015-01-26 18:54:32 +00003353 is_live_(false),
3354 can_be_null_(true) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003355 inputs_.SetSize(number_of_inputs);
3356 }
3357
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00003358 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
3359 static Primitive::Type ToPhiType(Primitive::Type type) {
3360 switch (type) {
3361 case Primitive::kPrimBoolean:
3362 case Primitive::kPrimByte:
3363 case Primitive::kPrimShort:
3364 case Primitive::kPrimChar:
3365 return Primitive::kPrimInt;
3366 default:
3367 return type;
3368 }
3369 }
3370
Calin Juravle10e244f2015-01-26 18:54:32 +00003371 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003372
3373 void AddInput(HInstruction* input);
David Brazdil2d7352b2015-04-20 14:52:42 +01003374 void RemoveInputAt(size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003375
Calin Juravle10e244f2015-01-26 18:54:32 +00003376 Primitive::Type GetType() const OVERRIDE { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003377 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003378
Calin Juravle10e244f2015-01-26 18:54:32 +00003379 bool CanBeNull() const OVERRIDE { return can_be_null_; }
3380 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
3381
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003382 uint32_t GetRegNumber() const { return reg_number_; }
3383
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01003384 void SetDead() { is_live_ = false; }
3385 void SetLive() { is_live_ = true; }
3386 bool IsDead() const { return !is_live_; }
3387 bool IsLive() const { return is_live_; }
3388
Calin Juravlea4f88312015-04-16 12:57:19 +01003389 // Returns the next equivalent phi (starting from the current one) or null if there is none.
3390 // An equivalent phi is a phi having the same dex register and type.
3391 // It assumes that phis with the same dex register are adjacent.
3392 HPhi* GetNextEquivalentPhiWithSameType() {
3393 HInstruction* next = GetNext();
3394 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
3395 if (next->GetType() == GetType()) {
3396 return next->AsPhi();
3397 }
3398 next = next->GetNext();
3399 }
3400 return nullptr;
3401 }
3402
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003403 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003404
David Brazdil1abb4192015-02-17 18:33:36 +00003405 protected:
3406 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
3407
3408 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
3409 inputs_.Put(index, input);
3410 }
3411
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003412 private:
David Brazdil1abb4192015-02-17 18:33:36 +00003413 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003414 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003415 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01003416 bool is_live_;
Calin Juravle10e244f2015-01-26 18:54:32 +00003417 bool can_be_null_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003418
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003419 DISALLOW_COPY_AND_ASSIGN(HPhi);
3420};
3421
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003422class HNullCheck : public HExpression<1> {
3423 public:
3424 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003425 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003426 SetRawInputAt(0, value);
3427 }
3428
Calin Juravle10e244f2015-01-26 18:54:32 +00003429 bool CanBeMoved() const OVERRIDE { return true; }
3430 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003431 UNUSED(other);
3432 return true;
3433 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003434
Calin Juravle10e244f2015-01-26 18:54:32 +00003435 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003436
Calin Juravle10e244f2015-01-26 18:54:32 +00003437 bool CanThrow() const OVERRIDE { return true; }
3438
3439 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01003440
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003441 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003442
3443 DECLARE_INSTRUCTION(NullCheck);
3444
3445 private:
3446 const uint32_t dex_pc_;
3447
3448 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
3449};
3450
3451class FieldInfo : public ValueObject {
3452 public:
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003453 FieldInfo(MemberOffset field_offset,
3454 Primitive::Type field_type,
3455 bool is_volatile,
3456 uint32_t index,
3457 const DexFile& dex_file)
3458 : field_offset_(field_offset),
3459 field_type_(field_type),
3460 is_volatile_(is_volatile),
3461 index_(index),
3462 dex_file_(dex_file) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003463
3464 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003465 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003466 uint32_t GetFieldIndex() const { return index_; }
3467 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00003468 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003469
3470 private:
3471 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01003472 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00003473 const bool is_volatile_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003474 uint32_t index_;
3475 const DexFile& dex_file_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003476};
3477
3478class HInstanceFieldGet : public HExpression<1> {
3479 public:
3480 HInstanceFieldGet(HInstruction* value,
3481 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003482 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003483 bool is_volatile,
3484 uint32_t field_idx,
3485 const DexFile& dex_file)
Nicolas Geoffray2af23072015-04-30 11:15:40 +00003486 : HExpression(field_type, SideEffects::DependsOnSomething()),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003487 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003488 SetRawInputAt(0, value);
3489 }
3490
Calin Juravle10c9cbe2014-12-19 10:50:19 +00003491 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003492
3493 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3494 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
3495 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003496 }
3497
Calin Juravle641547a2015-04-21 22:08:51 +01003498 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3499 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00003500 }
3501
3502 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01003503 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
3504 }
3505
Calin Juravle52c48962014-12-16 17:02:57 +00003506 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003507 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003508 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003509 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003510
3511 DECLARE_INSTRUCTION(InstanceFieldGet);
3512
3513 private:
3514 const FieldInfo field_info_;
3515
3516 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
3517};
3518
3519class HInstanceFieldSet : public HTemplateInstruction<2> {
3520 public:
3521 HInstanceFieldSet(HInstruction* object,
3522 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01003523 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003524 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003525 bool is_volatile,
3526 uint32_t field_idx,
3527 const DexFile& dex_file)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003528 : HTemplateInstruction(SideEffects::ChangesSomething()),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003529 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003530 value_can_be_null_(true) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003531 SetRawInputAt(0, object);
3532 SetRawInputAt(1, value);
3533 }
3534
Calin Juravle641547a2015-04-21 22:08:51 +01003535 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3536 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00003537 }
3538
Calin Juravle52c48962014-12-16 17:02:57 +00003539 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003540 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003541 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003542 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003543 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003544 bool GetValueCanBeNull() const { return value_can_be_null_; }
3545 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003546
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003547 DECLARE_INSTRUCTION(InstanceFieldSet);
3548
3549 private:
3550 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003551 bool value_can_be_null_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003552
3553 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
3554};
3555
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003556class HArrayGet : public HExpression<2> {
3557 public:
3558 HArrayGet(HInstruction* array, HInstruction* index, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003559 : HExpression(type, SideEffects::DependsOnSomething()) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003560 SetRawInputAt(0, array);
3561 SetRawInputAt(1, index);
3562 }
3563
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003564 bool CanBeMoved() const OVERRIDE { return true; }
3565 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003566 UNUSED(other);
3567 return true;
3568 }
Calin Juravle641547a2015-04-21 22:08:51 +01003569 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3570 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00003571 // TODO: We can be smarter here.
3572 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
3573 // which generates the implicit null check. There are cases when these can be removed
3574 // to produce better code. If we ever add optimizations to do so we should allow an
3575 // implicit check here (as long as the address falls in the first page).
3576 return false;
3577 }
3578
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003579 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003580
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003581 HInstruction* GetArray() const { return InputAt(0); }
3582 HInstruction* GetIndex() const { return InputAt(1); }
3583
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003584 DECLARE_INSTRUCTION(ArrayGet);
3585
3586 private:
3587 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
3588};
3589
3590class HArraySet : public HTemplateInstruction<3> {
3591 public:
3592 HArraySet(HInstruction* array,
3593 HInstruction* index,
3594 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003595 Primitive::Type expected_component_type,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003596 uint32_t dex_pc)
3597 : HTemplateInstruction(SideEffects::ChangesSomething()),
3598 dex_pc_(dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003599 expected_component_type_(expected_component_type),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003600 needs_type_check_(value->GetType() == Primitive::kPrimNot),
3601 value_can_be_null_(true) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003602 SetRawInputAt(0, array);
3603 SetRawInputAt(1, index);
3604 SetRawInputAt(2, value);
3605 }
3606
Calin Juravle77520bc2015-01-12 18:45:46 +00003607 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003608 // We currently always call a runtime method to catch array store
3609 // exceptions.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003610 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003611 }
3612
Mingyao Yang81014cb2015-06-02 03:16:27 -07003613 // Can throw ArrayStoreException.
3614 bool CanThrow() const OVERRIDE { return needs_type_check_; }
3615
Calin Juravle641547a2015-04-21 22:08:51 +01003616 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3617 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00003618 // TODO: Same as for ArrayGet.
3619 return false;
3620 }
3621
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003622 void ClearNeedsTypeCheck() {
3623 needs_type_check_ = false;
3624 }
3625
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003626 void ClearValueCanBeNull() {
3627 value_can_be_null_ = false;
3628 }
3629
3630 bool GetValueCanBeNull() const { return value_can_be_null_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003631 bool NeedsTypeCheck() const { return needs_type_check_; }
3632
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003633 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003634
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003635 HInstruction* GetArray() const { return InputAt(0); }
3636 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003637 HInstruction* GetValue() const { return InputAt(2); }
3638
3639 Primitive::Type GetComponentType() const {
3640 // The Dex format does not type floating point index operations. Since the
3641 // `expected_component_type_` is set during building and can therefore not
3642 // be correct, we also check what is the value type. If it is a floating
3643 // point type, we must use that type.
3644 Primitive::Type value_type = GetValue()->GetType();
3645 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
3646 ? value_type
3647 : expected_component_type_;
3648 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003649
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003650 DECLARE_INSTRUCTION(ArraySet);
3651
3652 private:
3653 const uint32_t dex_pc_;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003654 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003655 bool needs_type_check_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003656 bool value_can_be_null_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003657
3658 DISALLOW_COPY_AND_ASSIGN(HArraySet);
3659};
3660
3661class HArrayLength : public HExpression<1> {
3662 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003663 explicit HArrayLength(HInstruction* array)
3664 : HExpression(Primitive::kPrimInt, SideEffects::None()) {
3665 // Note that arrays do not change length, so the instruction does not
3666 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003667 SetRawInputAt(0, array);
3668 }
3669
Calin Juravle77520bc2015-01-12 18:45:46 +00003670 bool CanBeMoved() const OVERRIDE { return true; }
3671 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003672 UNUSED(other);
3673 return true;
3674 }
Calin Juravle641547a2015-04-21 22:08:51 +01003675 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3676 return obj == InputAt(0);
3677 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003678
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003679 DECLARE_INSTRUCTION(ArrayLength);
3680
3681 private:
3682 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
3683};
3684
3685class HBoundsCheck : public HExpression<2> {
3686 public:
3687 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003688 : HExpression(index->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003689 DCHECK(index->GetType() == Primitive::kPrimInt);
3690 SetRawInputAt(0, index);
3691 SetRawInputAt(1, length);
3692 }
3693
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003694 bool CanBeMoved() const OVERRIDE { return true; }
3695 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003696 UNUSED(other);
3697 return true;
3698 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003699
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003700 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003701
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003702 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01003703
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003704 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003705
3706 DECLARE_INSTRUCTION(BoundsCheck);
3707
3708 private:
3709 const uint32_t dex_pc_;
3710
3711 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
3712};
3713
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003714/**
3715 * Some DEX instructions are folded into multiple HInstructions that need
3716 * to stay live until the last HInstruction. This class
3717 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00003718 * HInstruction stays live. `index` represents the stack location index of the
3719 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003720 */
3721class HTemporary : public HTemplateInstruction<0> {
3722 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003723 explicit HTemporary(size_t index) : HTemplateInstruction(SideEffects::None()), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003724
3725 size_t GetIndex() const { return index_; }
3726
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00003727 Primitive::Type GetType() const OVERRIDE {
3728 // The previous instruction is the one that will be stored in the temporary location.
3729 DCHECK(GetPrevious() != nullptr);
3730 return GetPrevious()->GetType();
3731 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00003732
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003733 DECLARE_INSTRUCTION(Temporary);
3734
3735 private:
3736 const size_t index_;
3737
3738 DISALLOW_COPY_AND_ASSIGN(HTemporary);
3739};
3740
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003741class HSuspendCheck : public HTemplateInstruction<0> {
3742 public:
3743 explicit HSuspendCheck(uint32_t dex_pc)
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01003744 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003745
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003746 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003747 return true;
3748 }
3749
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003750 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01003751 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
3752 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003753
3754 DECLARE_INSTRUCTION(SuspendCheck);
3755
3756 private:
3757 const uint32_t dex_pc_;
3758
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01003759 // Only used for code generation, in order to share the same slow path between back edges
3760 // of a same loop.
3761 SlowPathCode* slow_path_;
3762
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003763 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
3764};
3765
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003766/**
3767 * Instruction to load a Class object.
3768 */
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003769class HLoadClass : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003770 public:
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003771 HLoadClass(HCurrentMethod* current_method,
3772 uint16_t type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003773 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003774 bool is_referrers_class,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003775 uint32_t dex_pc)
3776 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3777 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003778 dex_file_(dex_file),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003779 is_referrers_class_(is_referrers_class),
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003780 dex_pc_(dex_pc),
Calin Juravleb1498f62015-02-16 13:13:29 +00003781 generate_clinit_check_(false),
Calin Juravle7733bd62015-07-22 17:14:50 +00003782 loaded_class_rti_(ReferenceTypeInfo::CreateTop(/* is_exact */ false)) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003783 SetRawInputAt(0, current_method);
3784 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003785
3786 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003787
3788 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3789 return other->AsLoadClass()->type_index_ == type_index_;
3790 }
3791
3792 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
3793
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003794 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003795 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003796 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01003797 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003798
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003799 bool NeedsEnvironment() const OVERRIDE {
3800 // Will call runtime and load the class if the class is not loaded yet.
3801 // TODO: finer grain decision.
3802 return !is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003803 }
3804
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003805 bool MustGenerateClinitCheck() const {
3806 return generate_clinit_check_;
3807 }
3808
Calin Juravle0ba218d2015-05-19 18:46:01 +01003809 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
3810 generate_clinit_check_ = generate_clinit_check;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003811 }
3812
3813 bool CanCallRuntime() const {
3814 return MustGenerateClinitCheck() || !is_referrers_class_;
3815 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003816
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003817 bool CanThrow() const OVERRIDE {
3818 // May call runtime and and therefore can throw.
3819 // TODO: finer grain decision.
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01003820 return CanCallRuntime();
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003821 }
3822
Calin Juravleacf735c2015-02-12 15:25:22 +00003823 ReferenceTypeInfo GetLoadedClassRTI() {
3824 return loaded_class_rti_;
3825 }
3826
3827 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
3828 // Make sure we only set exact types (the loaded class should never be merged).
3829 DCHECK(rti.IsExact());
3830 loaded_class_rti_ = rti;
3831 }
3832
Calin Juravle7733bd62015-07-22 17:14:50 +00003833 bool IsResolved() {
3834 return loaded_class_rti_.IsExact();
3835 }
3836
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003837 const DexFile& GetDexFile() { return dex_file_; }
3838
Nicolas Geoffray9437b782015-03-25 10:08:51 +00003839 bool NeedsDexCache() const OVERRIDE { return !is_referrers_class_; }
3840
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003841 DECLARE_INSTRUCTION(LoadClass);
3842
3843 private:
3844 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003845 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003846 const bool is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003847 const uint32_t dex_pc_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003848 // Whether this instruction must generate the initialization check.
3849 // Used for code generation.
3850 bool generate_clinit_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003851
Calin Juravleacf735c2015-02-12 15:25:22 +00003852 ReferenceTypeInfo loaded_class_rti_;
3853
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003854 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
3855};
3856
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01003857class HLoadString : public HExpression<1> {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003858 public:
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01003859 HLoadString(HCurrentMethod* current_method, uint32_t string_index, uint32_t dex_pc)
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003860 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3861 string_index_(string_index),
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01003862 dex_pc_(dex_pc) {
3863 SetRawInputAt(0, current_method);
3864 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003865
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003866 bool CanBeMoved() const OVERRIDE { return true; }
3867
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003868 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3869 return other->AsLoadString()->string_index_ == string_index_;
3870 }
3871
3872 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
3873
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003874 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003875 uint32_t GetStringIndex() const { return string_index_; }
3876
3877 // TODO: Can we deopt or debug when we resolve a string?
3878 bool NeedsEnvironment() const OVERRIDE { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00003879 bool NeedsDexCache() const OVERRIDE { return true; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003880
3881 DECLARE_INSTRUCTION(LoadString);
3882
3883 private:
3884 const uint32_t string_index_;
3885 const uint32_t dex_pc_;
3886
3887 DISALLOW_COPY_AND_ASSIGN(HLoadString);
3888};
3889
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003890/**
3891 * Performs an initialization check on its Class object input.
3892 */
3893class HClinitCheck : public HExpression<1> {
3894 public:
3895 explicit HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Nicolas Geoffraya0466e12015-03-27 15:00:40 +00003896 : HExpression(Primitive::kPrimNot, SideEffects::ChangesSomething()),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003897 dex_pc_(dex_pc) {
3898 SetRawInputAt(0, constant);
3899 }
3900
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003901 bool CanBeMoved() const OVERRIDE { return true; }
3902 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3903 UNUSED(other);
3904 return true;
3905 }
3906
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003907 bool NeedsEnvironment() const OVERRIDE {
3908 // May call runtime to initialize the class.
3909 return true;
3910 }
3911
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003912 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003913
3914 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
3915
3916 DECLARE_INSTRUCTION(ClinitCheck);
3917
3918 private:
3919 const uint32_t dex_pc_;
3920
3921 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
3922};
3923
3924class HStaticFieldGet : public HExpression<1> {
3925 public:
3926 HStaticFieldGet(HInstruction* cls,
3927 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003928 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003929 bool is_volatile,
3930 uint32_t field_idx,
3931 const DexFile& dex_file)
Nicolas Geoffray2af23072015-04-30 11:15:40 +00003932 : HExpression(field_type, SideEffects::DependsOnSomething()),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003933 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003934 SetRawInputAt(0, cls);
3935 }
3936
Calin Juravle52c48962014-12-16 17:02:57 +00003937
Calin Juravle10c9cbe2014-12-19 10:50:19 +00003938 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003939
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003940 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00003941 HStaticFieldGet* other_get = other->AsStaticFieldGet();
3942 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003943 }
3944
3945 size_t ComputeHashCode() const OVERRIDE {
3946 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
3947 }
3948
Calin Juravle52c48962014-12-16 17:02:57 +00003949 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003950 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
3951 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003952 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003953
3954 DECLARE_INSTRUCTION(StaticFieldGet);
3955
3956 private:
3957 const FieldInfo field_info_;
3958
3959 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
3960};
3961
3962class HStaticFieldSet : public HTemplateInstruction<2> {
3963 public:
3964 HStaticFieldSet(HInstruction* cls,
3965 HInstruction* value,
3966 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003967 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003968 bool is_volatile,
3969 uint32_t field_idx,
3970 const DexFile& dex_file)
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003971 : HTemplateInstruction(SideEffects::ChangesSomething()),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003972 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003973 value_can_be_null_(true) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003974 SetRawInputAt(0, cls);
3975 SetRawInputAt(1, value);
3976 }
3977
Calin Juravle52c48962014-12-16 17:02:57 +00003978 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003979 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
3980 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003981 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003982
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003983 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003984 bool GetValueCanBeNull() const { return value_can_be_null_; }
3985 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003986
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003987 DECLARE_INSTRUCTION(StaticFieldSet);
3988
3989 private:
3990 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003991 bool value_can_be_null_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003992
3993 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
3994};
3995
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003996// Implement the move-exception DEX instruction.
3997class HLoadException : public HExpression<0> {
3998 public:
3999 HLoadException() : HExpression(Primitive::kPrimNot, SideEffects::None()) {}
4000
4001 DECLARE_INSTRUCTION(LoadException);
4002
4003 private:
4004 DISALLOW_COPY_AND_ASSIGN(HLoadException);
4005};
4006
4007class HThrow : public HTemplateInstruction<1> {
4008 public:
4009 HThrow(HInstruction* exception, uint32_t dex_pc)
4010 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {
4011 SetRawInputAt(0, exception);
4012 }
4013
4014 bool IsControlFlow() const OVERRIDE { return true; }
4015
4016 bool NeedsEnvironment() const OVERRIDE { return true; }
4017
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004018 bool CanThrow() const OVERRIDE { return true; }
4019
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004020 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004021
4022 DECLARE_INSTRUCTION(Throw);
4023
4024 private:
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004025 const uint32_t dex_pc_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004026
4027 DISALLOW_COPY_AND_ASSIGN(HThrow);
4028};
4029
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004030class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004031 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004032 HInstanceOf(HInstruction* object,
4033 HLoadClass* constant,
4034 bool class_is_final,
4035 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004036 : HExpression(Primitive::kPrimBoolean, SideEffects::None()),
4037 class_is_final_(class_is_final),
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004038 must_do_null_check_(true),
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004039 dex_pc_(dex_pc) {
4040 SetRawInputAt(0, object);
4041 SetRawInputAt(1, constant);
4042 }
4043
4044 bool CanBeMoved() const OVERRIDE { return true; }
4045
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004046 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004047 return true;
4048 }
4049
4050 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004051 return false;
4052 }
4053
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004054 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004055
4056 bool IsClassFinal() const { return class_is_final_; }
4057
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004058 // Used only in code generation.
4059 bool MustDoNullCheck() const { return must_do_null_check_; }
4060 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
4061
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004062 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004063
4064 private:
4065 const bool class_is_final_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004066 bool must_do_null_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004067 const uint32_t dex_pc_;
4068
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004069 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
4070};
4071
Calin Juravleb1498f62015-02-16 13:13:29 +00004072class HBoundType : public HExpression<1> {
4073 public:
Calin Juravleb0d5fc02015-07-15 14:41:29 +01004074 HBoundType(HInstruction* input, ReferenceTypeInfo upper_bound, bool upper_can_be_null)
Calin Juravleb1498f62015-02-16 13:13:29 +00004075 : HExpression(Primitive::kPrimNot, SideEffects::None()),
Calin Juravleb0d5fc02015-07-15 14:41:29 +01004076 upper_bound_(upper_bound),
4077 upper_can_be_null_(upper_can_be_null) {
Calin Juravle61d544b2015-02-23 16:46:57 +00004078 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00004079 SetRawInputAt(0, input);
4080 }
4081
Calin Juravleb0d5fc02015-07-15 14:41:29 +01004082 // GetUpper* should only be used in reference type propagation.
4083 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
4084 bool GetUpperCanBeNull() const { return upper_can_be_null_; }
Calin Juravleb1498f62015-02-16 13:13:29 +00004085
Calin Juravleb0d5fc02015-07-15 14:41:29 +01004086 void SetCanBeNull(bool can_be_null) {
4087 DCHECK(upper_can_be_null_ || !can_be_null);
4088 can_be_null_ = can_be_null;
Calin Juravleb1498f62015-02-16 13:13:29 +00004089 }
4090
Calin Juravleb0d5fc02015-07-15 14:41:29 +01004091 bool CanBeNull() const OVERRIDE { return can_be_null_; }
4092
Calin Juravleb1498f62015-02-16 13:13:29 +00004093 DECLARE_INSTRUCTION(BoundType);
4094
4095 private:
4096 // Encodes the most upper class that this instruction can have. In other words
Calin Juravleb0d5fc02015-07-15 14:41:29 +01004097 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
4098 // It is used to bound the type in cases like:
4099 // if (x instanceof ClassX) {
4100 // // uper_bound_ will be ClassX
4101 // }
4102 const ReferenceTypeInfo upper_bound_;
4103 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
4104 // is false then can_be_null_ cannot be true).
4105 const bool upper_can_be_null_;
4106 bool can_be_null_;
Calin Juravleb1498f62015-02-16 13:13:29 +00004107
4108 DISALLOW_COPY_AND_ASSIGN(HBoundType);
4109};
4110
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004111class HCheckCast : public HTemplateInstruction<2> {
4112 public:
4113 HCheckCast(HInstruction* object,
4114 HLoadClass* constant,
4115 bool class_is_final,
4116 uint32_t dex_pc)
4117 : HTemplateInstruction(SideEffects::None()),
4118 class_is_final_(class_is_final),
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004119 must_do_null_check_(true),
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004120 dex_pc_(dex_pc) {
4121 SetRawInputAt(0, object);
4122 SetRawInputAt(1, constant);
4123 }
4124
4125 bool CanBeMoved() const OVERRIDE { return true; }
4126
4127 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
4128 return true;
4129 }
4130
4131 bool NeedsEnvironment() const OVERRIDE {
4132 // Instruction may throw a CheckCastError.
4133 return true;
4134 }
4135
4136 bool CanThrow() const OVERRIDE { return true; }
4137
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004138 bool MustDoNullCheck() const { return must_do_null_check_; }
4139 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
4140
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004141 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004142
4143 bool IsClassFinal() const { return class_is_final_; }
4144
4145 DECLARE_INSTRUCTION(CheckCast);
4146
4147 private:
4148 const bool class_is_final_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004149 bool must_do_null_check_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004150 const uint32_t dex_pc_;
4151
4152 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004153};
4154
Calin Juravle27df7582015-04-17 19:12:31 +01004155class HMemoryBarrier : public HTemplateInstruction<0> {
4156 public:
4157 explicit HMemoryBarrier(MemBarrierKind barrier_kind)
4158 : HTemplateInstruction(SideEffects::None()),
4159 barrier_kind_(barrier_kind) {}
4160
4161 MemBarrierKind GetBarrierKind() { return barrier_kind_; }
4162
4163 DECLARE_INSTRUCTION(MemoryBarrier);
4164
4165 private:
4166 const MemBarrierKind barrier_kind_;
4167
4168 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
4169};
4170
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004171class HMonitorOperation : public HTemplateInstruction<1> {
4172 public:
4173 enum OperationKind {
4174 kEnter,
4175 kExit,
4176 };
4177
4178 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
David Brazdilbff75032015-07-08 17:26:51 +00004179 : HTemplateInstruction(SideEffects::ChangesSomething()), kind_(kind), dex_pc_(dex_pc) {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004180 SetRawInputAt(0, object);
4181 }
4182
4183 // Instruction may throw a Java exception, so we need an environment.
David Brazdilbff75032015-07-08 17:26:51 +00004184 bool NeedsEnvironment() const OVERRIDE { return CanThrow(); }
4185
4186 bool CanThrow() const OVERRIDE {
4187 // Verifier guarantees that monitor-exit cannot throw.
4188 // This is important because it allows the HGraphBuilder to remove
4189 // a dead throw-catch loop generated for `synchronized` blocks/methods.
4190 return IsEnter();
4191 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004192
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004193 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004194
4195 bool IsEnter() const { return kind_ == kEnter; }
4196
4197 DECLARE_INSTRUCTION(MonitorOperation);
4198
Calin Juravle52c48962014-12-16 17:02:57 +00004199 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004200 const OperationKind kind_;
4201 const uint32_t dex_pc_;
4202
4203 private:
4204 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
4205};
4206
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01004207/**
4208 * A HInstruction used as a marker for the replacement of new + <init>
4209 * of a String to a call to a StringFactory. Only baseline will see
4210 * the node at code generation, where it will be be treated as null.
4211 * When compiling non-baseline, `HFakeString` instructions are being removed
4212 * in the instruction simplifier.
4213 */
4214class HFakeString : public HTemplateInstruction<0> {
4215 public:
4216 HFakeString() : HTemplateInstruction(SideEffects::None()) {}
4217
4218 Primitive::Type GetType() const OVERRIDE { return Primitive::kPrimNot; }
4219
4220 DECLARE_INSTRUCTION(FakeString);
4221
4222 private:
4223 DISALLOW_COPY_AND_ASSIGN(HFakeString);
4224};
4225
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004226class MoveOperands : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004227 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01004228 MoveOperands(Location source,
4229 Location destination,
4230 Primitive::Type type,
4231 HInstruction* instruction)
4232 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004233
4234 Location GetSource() const { return source_; }
4235 Location GetDestination() const { return destination_; }
4236
4237 void SetSource(Location value) { source_ = value; }
4238 void SetDestination(Location value) { destination_ = value; }
4239
4240 // The parallel move resolver marks moves as "in-progress" by clearing the
4241 // destination (but not the source).
4242 Location MarkPending() {
4243 DCHECK(!IsPending());
4244 Location dest = destination_;
4245 destination_ = Location::NoLocation();
4246 return dest;
4247 }
4248
4249 void ClearPending(Location dest) {
4250 DCHECK(IsPending());
4251 destination_ = dest;
4252 }
4253
4254 bool IsPending() const {
4255 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
4256 return destination_.IsInvalid() && !source_.IsInvalid();
4257 }
4258
4259 // True if this blocks a move from the given location.
4260 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08004261 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004262 }
4263
4264 // A move is redundant if it's been eliminated, if its source and
4265 // destination are the same, or if its destination is unneeded.
4266 bool IsRedundant() const {
4267 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
4268 }
4269
4270 // We clear both operands to indicate move that's been eliminated.
4271 void Eliminate() {
4272 source_ = destination_ = Location::NoLocation();
4273 }
4274
4275 bool IsEliminated() const {
4276 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
4277 return source_.IsInvalid();
4278 }
4279
Alexey Frunze4dda3372015-06-01 18:31:49 -07004280 Primitive::Type GetType() const { return type_; }
4281
Nicolas Geoffray90218252015-04-15 11:56:51 +01004282 bool Is64BitMove() const {
4283 return Primitive::Is64BitType(type_);
4284 }
4285
Nicolas Geoffray740475d2014-09-29 10:33:25 +01004286 HInstruction* GetInstruction() const { return instruction_; }
4287
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004288 private:
4289 Location source_;
4290 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01004291 // The type this move is for.
4292 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01004293 // The instruction this move is assocatied with. Null when this move is
4294 // for moving an input in the expected locations of user (including a phi user).
4295 // This is only used in debug mode, to ensure we do not connect interval siblings
4296 // in the same parallel move.
4297 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004298};
4299
4300static constexpr size_t kDefaultNumberOfMoves = 4;
4301
4302class HParallelMove : public HTemplateInstruction<0> {
4303 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004304 explicit HParallelMove(ArenaAllocator* arena)
4305 : HTemplateInstruction(SideEffects::None()), moves_(arena, kDefaultNumberOfMoves) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004306
Nicolas Geoffray90218252015-04-15 11:56:51 +01004307 void AddMove(Location source,
4308 Location destination,
4309 Primitive::Type type,
4310 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004311 DCHECK(source.IsValid());
4312 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004313 if (kIsDebugBuild) {
4314 if (instruction != nullptr) {
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00004315 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004316 if (moves_.Get(i).GetInstruction() == instruction) {
4317 // Special case the situation where the move is for the spill slot
4318 // of the instruction.
4319 if ((GetPrevious() == instruction)
4320 || ((GetPrevious() == nullptr)
4321 && instruction->IsPhi()
4322 && instruction->GetBlock() == GetBlock())) {
4323 DCHECK_NE(destination.GetKind(), moves_.Get(i).GetDestination().GetKind())
4324 << "Doing parallel moves for the same instruction.";
4325 } else {
4326 DCHECK(false) << "Doing parallel moves for the same instruction.";
4327 }
4328 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00004329 }
4330 }
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004331 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Zheng Xuad4450e2015-04-17 18:48:56 +08004332 DCHECK(!destination.OverlapsWith(moves_.Get(i).GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01004333 << "Overlapped destination for two moves in a parallel move: "
4334 << moves_.Get(i).GetSource() << " ==> " << moves_.Get(i).GetDestination() << " and "
4335 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004336 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01004337 }
Nicolas Geoffray90218252015-04-15 11:56:51 +01004338 moves_.Add(MoveOperands(source, destination, type, instruction));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004339 }
4340
4341 MoveOperands* MoveOperandsAt(size_t index) const {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004342 return moves_.GetRawStorage() + index;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004343 }
4344
4345 size_t NumMoves() const { return moves_.Size(); }
4346
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004347 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004348
4349 private:
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004350 GrowableArray<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004351
4352 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
4353};
4354
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004355class HGraphVisitor : public ValueObject {
4356 public:
Dave Allison20dfc792014-06-16 20:44:29 -07004357 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
4358 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004359
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004360 virtual void VisitInstruction(HInstruction* instruction) { UNUSED(instruction); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004361 virtual void VisitBasicBlock(HBasicBlock* block);
4362
Roland Levillain633021e2014-10-01 14:12:25 +01004363 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004364 void VisitInsertionOrder();
4365
Roland Levillain633021e2014-10-01 14:12:25 +01004366 // Visit the graph following dominator tree reverse post-order.
4367 void VisitReversePostOrder();
4368
Nicolas Geoffray787c3072014-03-17 10:20:19 +00004369 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00004370
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004371 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004372#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004373 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
4374
4375 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
4376
4377#undef DECLARE_VISIT_INSTRUCTION
4378
4379 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07004380 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004381
4382 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
4383};
4384
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004385class HGraphDelegateVisitor : public HGraphVisitor {
4386 public:
4387 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
4388 virtual ~HGraphDelegateVisitor() {}
4389
4390 // Visit functions that delegate to to super class.
4391#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004392 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004393
4394 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
4395
4396#undef DECLARE_VISIT_INSTRUCTION
4397
4398 private:
4399 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
4400};
4401
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004402class HInsertionOrderIterator : public ValueObject {
4403 public:
4404 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
4405
4406 bool Done() const { return index_ == graph_.GetBlocks().Size(); }
4407 HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); }
4408 void Advance() { ++index_; }
4409
4410 private:
4411 const HGraph& graph_;
4412 size_t index_;
4413
4414 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
4415};
4416
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004417class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004418 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00004419 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
4420 // Check that reverse post order of the graph has been built.
4421 DCHECK(!graph.GetReversePostOrder().IsEmpty());
4422 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004423
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004424 bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); }
4425 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004426 void Advance() { ++index_; }
4427
4428 private:
4429 const HGraph& graph_;
4430 size_t index_;
4431
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004432 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004433};
4434
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004435class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004436 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004437 explicit HPostOrderIterator(const HGraph& graph)
David Brazdil10f56cb2015-03-24 18:49:14 +00004438 : graph_(graph), index_(graph_.GetReversePostOrder().Size()) {
4439 // Check that reverse post order of the graph has been built.
4440 DCHECK(!graph.GetReversePostOrder().IsEmpty());
4441 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004442
4443 bool Done() const { return index_ == 0; }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004444 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004445 void Advance() { --index_; }
4446
4447 private:
4448 const HGraph& graph_;
4449 size_t index_;
4450
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004451 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004452};
4453
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01004454class HLinearPostOrderIterator : public ValueObject {
4455 public:
4456 explicit HLinearPostOrderIterator(const HGraph& graph)
4457 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().Size()) {}
4458
4459 bool Done() const { return index_ == 0; }
4460
4461 HBasicBlock* Current() const { return order_.Get(index_ -1); }
4462
4463 void Advance() {
4464 --index_;
4465 DCHECK_GE(index_, 0U);
4466 }
4467
4468 private:
4469 const GrowableArray<HBasicBlock*>& order_;
4470 size_t index_;
4471
4472 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
4473};
4474
4475class HLinearOrderIterator : public ValueObject {
4476 public:
4477 explicit HLinearOrderIterator(const HGraph& graph)
4478 : order_(graph.GetLinearOrder()), index_(0) {}
4479
4480 bool Done() const { return index_ == order_.Size(); }
4481 HBasicBlock* Current() const { return order_.Get(index_); }
4482 void Advance() { ++index_; }
4483
4484 private:
4485 const GrowableArray<HBasicBlock*>& order_;
4486 size_t index_;
4487
4488 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
4489};
4490
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004491// Iterator over the blocks that art part of the loop. Includes blocks part
4492// of an inner loop. The order in which the blocks are iterated is on their
4493// block id.
4494class HBlocksInLoopIterator : public ValueObject {
4495 public:
4496 explicit HBlocksInLoopIterator(const HLoopInformation& info)
4497 : blocks_in_loop_(info.GetBlocks()),
4498 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
4499 index_(0) {
4500 if (!blocks_in_loop_.IsBitSet(index_)) {
4501 Advance();
4502 }
4503 }
4504
4505 bool Done() const { return index_ == blocks_.Size(); }
4506 HBasicBlock* Current() const { return blocks_.Get(index_); }
4507 void Advance() {
4508 ++index_;
4509 for (size_t e = blocks_.Size(); index_ < e; ++index_) {
4510 if (blocks_in_loop_.IsBitSet(index_)) {
4511 break;
4512 }
4513 }
4514 }
4515
4516 private:
4517 const BitVector& blocks_in_loop_;
4518 const GrowableArray<HBasicBlock*>& blocks_;
4519 size_t index_;
4520
4521 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
4522};
4523
Mingyao Yang3584bce2015-05-19 16:01:59 -07004524// Iterator over the blocks that art part of the loop. Includes blocks part
4525// of an inner loop. The order in which the blocks are iterated is reverse
4526// post order.
4527class HBlocksInLoopReversePostOrderIterator : public ValueObject {
4528 public:
4529 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
4530 : blocks_in_loop_(info.GetBlocks()),
4531 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
4532 index_(0) {
4533 if (!blocks_in_loop_.IsBitSet(blocks_.Get(index_)->GetBlockId())) {
4534 Advance();
4535 }
4536 }
4537
4538 bool Done() const { return index_ == blocks_.Size(); }
4539 HBasicBlock* Current() const { return blocks_.Get(index_); }
4540 void Advance() {
4541 ++index_;
4542 for (size_t e = blocks_.Size(); index_ < e; ++index_) {
4543 if (blocks_in_loop_.IsBitSet(blocks_.Get(index_)->GetBlockId())) {
4544 break;
4545 }
4546 }
4547 }
4548
4549 private:
4550 const BitVector& blocks_in_loop_;
4551 const GrowableArray<HBasicBlock*>& blocks_;
4552 size_t index_;
4553
4554 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
4555};
4556
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00004557inline int64_t Int64FromConstant(HConstant* constant) {
4558 DCHECK(constant->IsIntConstant() || constant->IsLongConstant());
4559 return constant->IsIntConstant() ? constant->AsIntConstant()->GetValue()
4560 : constant->AsLongConstant()->GetValue();
4561}
4562
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004563} // namespace art
4564
4565#endif // ART_COMPILER_OPTIMIZING_NODES_H_