blob: 32459a9bbbc551764d1a3fb817a8d1b3c8ae761f [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
Aart Bik854a02b2015-07-14 16:07:00 -07001158/**
1159 * Side-effects representation for write/read dependences on fields/arrays.
1160 *
1161 * The dependence analysis uses type disambiguation (e.g. a float field write
1162 * cannot modify the value of an integer field read) and the access type (e.g.
1163 * a reference array write cannot modify the value of a reference field read
1164 * [although it may modify the reference fetch prior to reading the field,
1165 * which is represented by its own write/read dependence]). The analysis
1166 * makes conservative points-to assumptions on reference types (e.g. two same
1167 * typed arrays are assumed to be the same, and any reference read depends
1168 * on any reference read without further regard of its type).
1169 *
1170 * The internal representation uses the following 36-bit flags assignments:
1171 *
1172 * |ARRAY-R |FIELD-R |ARRAY-W |FIELD-W |
1173 * +---------+---------+---------+---------+
1174 * |543210987|654321098|765432109|876543210|
1175 * |DFJISCBZL|DFJISCBZL|DFJISCBZL|DFJISCBZL|
1176 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001177class SideEffects : public ValueObject {
1178 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001179 SideEffects() : flags_(0) {}
1180
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001181 static SideEffects None() {
1182 return SideEffects(0);
1183 }
1184
1185 static SideEffects All() {
Aart Bik854a02b2015-07-14 16:07:00 -07001186 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001187 }
1188
Aart Bik854a02b2015-07-14 16:07:00 -07001189 static SideEffects FieldWriteOfType(Primitive::Type type) {
1190 return SideEffects(TypeFlagWithAlias(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001191 }
1192
Aart Bik854a02b2015-07-14 16:07:00 -07001193 static SideEffects ArrayWriteOfType(Primitive::Type type) {
1194 return SideEffects(TypeFlagWithAlias(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001195 }
1196
Aart Bik854a02b2015-07-14 16:07:00 -07001197 static SideEffects FieldReadOfType(Primitive::Type type) {
1198 return SideEffects(TypeFlagWithAlias(type, kFieldReadOffset));
1199 }
1200
1201 static SideEffects ArrayReadOfType(Primitive::Type type) {
1202 return SideEffects(TypeFlagWithAlias(type, kArrayReadOffset));
1203 }
1204
1205 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001206 SideEffects Union(SideEffects other) const {
1207 return SideEffects(flags_ | other.flags_);
1208 }
1209
Aart Bik854a02b2015-07-14 16:07:00 -07001210 // Returns true if something is written.
1211 bool DoesAnyWrite() const {
1212 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001213 }
1214
Aart Bik854a02b2015-07-14 16:07:00 -07001215 // Returns true if something is read.
1216 bool DoesAnyRead() const {
1217 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001218 }
1219
Aart Bik854a02b2015-07-14 16:07:00 -07001220 // Returns true if nothing is written or read.
1221 bool DoesNothing() const {
1222 return flags_ == 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001223 }
1224
Aart Bik854a02b2015-07-14 16:07:00 -07001225 // Returns true if potentially everything is written and read
1226 // (every type and every kind of access).
1227 bool DoesAll() const {
1228 return flags_ == (kAllWrites | kAllReads);
1229 }
1230
1231 // Returns true if this may read something written by other.
1232 bool MayDependOn(SideEffects other) const {
1233 const uint64_t reads = (flags_ & kAllReads) >> kFieldReadOffset;
1234 return (other.flags_ & reads);
1235 }
1236
1237 // Returns string representation of flags (for debugging only).
1238 // Format: |DFJISCBZL|DFJISCBZL|DFJISCBZL|DFJISCBZL|
1239 std::string ToString() const {
1240 static const char *kDebug = "LZBCSIJFD";
1241 std::string flags = "|";
1242 for (int s = 35; s >= 0; s--) {
1243 const int t = s % kBits;
1244 if ((flags_ >> s) & 1)
1245 flags += kDebug[t];
1246 if (t == 0)
1247 flags += "|";
1248 }
1249 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001250 }
1251
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001252 private:
Aart Bik854a02b2015-07-14 16:07:00 -07001253 static constexpr int kBits = 9;
1254 static constexpr int kFieldWriteOffset = 0 * kBits;
1255 static constexpr int kArrayWriteOffset = 1 * kBits;
1256 static constexpr int kFieldReadOffset = 2 * kBits;
1257 static constexpr int kArrayReadOffset = 3 * kBits;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001258
Aart Bik854a02b2015-07-14 16:07:00 -07001259 static constexpr uint64_t kAllWrites = 0x0003ffff;
1260 static constexpr uint64_t kAllReads = kAllWrites << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001261
Aart Bik854a02b2015-07-14 16:07:00 -07001262 // Work around the fact that HIR aliases I/F and J/D.
1263 // TODO: remove this interceptor once HIR types are clean
1264 static uint64_t TypeFlagWithAlias(Primitive::Type type, int offset) {
1265 switch (type) {
1266 case Primitive::kPrimInt:
1267 case Primitive::kPrimFloat:
1268 return TypeFlag(Primitive::kPrimInt, offset) |
1269 TypeFlag(Primitive::kPrimFloat, offset);
1270 case Primitive::kPrimLong:
1271 case Primitive::kPrimDouble:
1272 return TypeFlag(Primitive::kPrimLong, offset) |
1273 TypeFlag(Primitive::kPrimDouble, offset);
1274 default:
1275 return TypeFlag(type, offset);
1276 }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001277 }
1278
Aart Bik854a02b2015-07-14 16:07:00 -07001279 // Translates type to bit flag.
1280 static uint64_t TypeFlag(Primitive::Type type, int offset) {
1281 CHECK_NE(type, Primitive::kPrimVoid);
1282 const uint64_t one = 1;
1283 const int shift = type; // 0-based consecutive enum
1284 DCHECK_LE(kFieldWriteOffset, shift);
1285 DCHECK_LT(shift, kArrayWriteOffset);
1286 return one << (type + offset);
1287 }
1288
1289 // Private constructor on direct flags value.
1290 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1291
1292 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001293};
1294
David Brazdiled596192015-01-23 10:39:45 +00001295// A HEnvironment object contains the values of virtual registers at a given location.
1296class HEnvironment : public ArenaObject<kArenaAllocMisc> {
1297 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001298 HEnvironment(ArenaAllocator* arena,
1299 size_t number_of_vregs,
1300 const DexFile& dex_file,
1301 uint32_t method_idx,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001302 uint32_t dex_pc,
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001303 InvokeType invoke_type,
1304 HInstruction* holder)
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001305 : vregs_(arena, number_of_vregs),
1306 locations_(arena, number_of_vregs),
1307 parent_(nullptr),
1308 dex_file_(dex_file),
1309 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001310 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001311 invoke_type_(invoke_type),
1312 holder_(holder) {
David Brazdiled596192015-01-23 10:39:45 +00001313 vregs_.SetSize(number_of_vregs);
1314 for (size_t i = 0; i < number_of_vregs; i++) {
David Brazdil1abb4192015-02-17 18:33:36 +00001315 vregs_.Put(i, HUserRecord<HEnvironment*>());
David Brazdiled596192015-01-23 10:39:45 +00001316 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001317
1318 locations_.SetSize(number_of_vregs);
1319 for (size_t i = 0; i < number_of_vregs; ++i) {
1320 locations_.Put(i, Location());
1321 }
1322 }
1323
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001324 HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001325 : HEnvironment(arena,
1326 to_copy.Size(),
1327 to_copy.GetDexFile(),
1328 to_copy.GetMethodIdx(),
1329 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001330 to_copy.GetInvokeType(),
1331 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001332
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001333 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001334 if (parent_ != nullptr) {
1335 parent_->SetAndCopyParentChain(allocator, parent);
1336 } else {
1337 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1338 parent_->CopyFrom(parent);
1339 if (parent->GetParent() != nullptr) {
1340 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1341 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001342 }
David Brazdiled596192015-01-23 10:39:45 +00001343 }
1344
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001345 void CopyFrom(const GrowableArray<HInstruction*>& locals);
1346 void CopyFrom(HEnvironment* environment);
1347
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001348 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1349 // input to the loop phi instead. This is for inserting instructions that
1350 // require an environment (like HDeoptimization) in the loop pre-header.
1351 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001352
1353 void SetRawEnvAt(size_t index, HInstruction* instruction) {
David Brazdil1abb4192015-02-17 18:33:36 +00001354 vregs_.Put(index, HUserRecord<HEnvironment*>(instruction));
David Brazdiled596192015-01-23 10:39:45 +00001355 }
1356
1357 HInstruction* GetInstructionAt(size_t index) const {
David Brazdil1abb4192015-02-17 18:33:36 +00001358 return vregs_.Get(index).GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001359 }
1360
David Brazdil1abb4192015-02-17 18:33:36 +00001361 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001362
1363 size_t Size() const { return vregs_.Size(); }
1364
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001365 HEnvironment* GetParent() const { return parent_; }
1366
1367 void SetLocationAt(size_t index, Location location) {
1368 locations_.Put(index, location);
1369 }
1370
1371 Location GetLocationAt(size_t index) const {
1372 return locations_.Get(index);
1373 }
1374
1375 uint32_t GetDexPc() const {
1376 return dex_pc_;
1377 }
1378
1379 uint32_t GetMethodIdx() const {
1380 return method_idx_;
1381 }
1382
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001383 InvokeType GetInvokeType() const {
1384 return invoke_type_;
1385 }
1386
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001387 const DexFile& GetDexFile() const {
1388 return dex_file_;
1389 }
1390
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001391 HInstruction* GetHolder() const {
1392 return holder_;
1393 }
1394
David Brazdiled596192015-01-23 10:39:45 +00001395 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001396 // Record instructions' use entries of this environment for constant-time removal.
1397 // It should only be called by HInstruction when a new environment use is added.
1398 void RecordEnvUse(HUseListNode<HEnvironment*>* env_use) {
1399 DCHECK(env_use->GetUser() == this);
1400 size_t index = env_use->GetIndex();
1401 vregs_.Put(index, HUserRecord<HEnvironment*>(vregs_.Get(index), env_use));
1402 }
David Brazdiled596192015-01-23 10:39:45 +00001403
David Brazdil1abb4192015-02-17 18:33:36 +00001404 GrowableArray<HUserRecord<HEnvironment*> > vregs_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001405 GrowableArray<Location> locations_;
1406 HEnvironment* parent_;
1407 const DexFile& dex_file_;
1408 const uint32_t method_idx_;
1409 const uint32_t dex_pc_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001410 const InvokeType invoke_type_;
David Brazdiled596192015-01-23 10:39:45 +00001411
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001412 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001413 HInstruction* const holder_;
1414
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001415 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001416
1417 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1418};
1419
Calin Juravleacf735c2015-02-12 15:25:22 +00001420class ReferenceTypeInfo : ValueObject {
1421 public:
Calin Juravleb1498f62015-02-16 13:13:29 +00001422 typedef Handle<mirror::Class> TypeHandle;
1423
1424 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact)
1425 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1426 if (type_handle->IsObjectClass()) {
1427 // Override the type handle to be consistent with the case when we get to
1428 // Top but don't have the Object class available. It avoids having to guess
1429 // what value the type_handle has when it's Top.
1430 return ReferenceTypeInfo(TypeHandle(), is_exact, true);
1431 } else {
1432 return ReferenceTypeInfo(type_handle, is_exact, false);
1433 }
1434 }
1435
1436 static ReferenceTypeInfo CreateTop(bool is_exact) {
1437 return ReferenceTypeInfo(TypeHandle(), is_exact, true);
Calin Juravleacf735c2015-02-12 15:25:22 +00001438 }
1439
1440 bool IsExact() const { return is_exact_; }
1441 bool IsTop() const { return is_top_; }
Guillaume Sanchez222862c2015-06-09 18:33:02 +01001442 bool IsInterface() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1443 return !IsTop() && GetTypeHandle()->IsInterface();
1444 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001445
1446 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
1447
Calin Juravleb1498f62015-02-16 13:13:29 +00001448 bool IsSupertypeOf(ReferenceTypeInfo rti) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Calin Juravleacf735c2015-02-12 15:25:22 +00001449 if (IsTop()) {
1450 // Top (equivalent for java.lang.Object) is supertype of anything.
1451 return true;
1452 }
1453 if (rti.IsTop()) {
1454 // If we get here `this` is not Top() so it can't be a supertype.
1455 return false;
1456 }
1457 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
1458 }
1459
1460 // Returns true if the type information provide the same amount of details.
1461 // Note that it does not mean that the instructions have the same actual type
1462 // (e.g. tops are equal but they can be the result of a merge).
1463 bool IsEqual(ReferenceTypeInfo rti) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1464 if (IsExact() != rti.IsExact()) {
1465 return false;
1466 }
1467 if (IsTop() && rti.IsTop()) {
1468 // `Top` means java.lang.Object, so the types are equivalent.
1469 return true;
1470 }
1471 if (IsTop() || rti.IsTop()) {
1472 // If only one is top or object than they are not equivalent.
1473 // NB: We need this extra check because the type_handle of `Top` is invalid
1474 // and we cannot inspect its reference.
1475 return false;
1476 }
1477
1478 // Finally check the types.
1479 return GetTypeHandle().Get() == rti.GetTypeHandle().Get();
1480 }
1481
1482 private:
Calin Juravleb1498f62015-02-16 13:13:29 +00001483 ReferenceTypeInfo() : ReferenceTypeInfo(TypeHandle(), false, true) {}
1484 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact, bool is_top)
1485 : type_handle_(type_handle), is_exact_(is_exact), is_top_(is_top) {}
1486
Calin Juravleacf735c2015-02-12 15:25:22 +00001487 // The class of the object.
Calin Juravleb1498f62015-02-16 13:13:29 +00001488 TypeHandle type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001489 // Whether or not the type is exact or a superclass of the actual type.
Calin Juravleb1498f62015-02-16 13:13:29 +00001490 // Whether or not we have any information about this type.
Calin Juravleacf735c2015-02-12 15:25:22 +00001491 bool is_exact_;
1492 // A true value here means that the object type should be java.lang.Object.
1493 // We don't have access to the corresponding mirror object every time so this
1494 // flag acts as a substitute. When true, the TypeHandle refers to a null
1495 // pointer and should not be used.
1496 bool is_top_;
1497};
1498
1499std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
1500
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001501class HInstruction : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001502 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001503 explicit HInstruction(SideEffects side_effects)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001504 : previous_(nullptr),
1505 next_(nullptr),
1506 block_(nullptr),
1507 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001508 ssa_index_(-1),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001509 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001510 locations_(nullptr),
1511 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001512 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001513 side_effects_(side_effects),
1514 reference_type_info_(ReferenceTypeInfo::CreateTop(/* is_exact */ false)) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001515
Dave Allison20dfc792014-06-16 20:44:29 -07001516 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001517
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001518#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001519 enum InstructionKind {
1520 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1521 };
1522#undef DECLARE_KIND
1523
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001524 HInstruction* GetNext() const { return next_; }
1525 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001526
Calin Juravle77520bc2015-01-12 18:45:46 +00001527 HInstruction* GetNextDisregardingMoves() const;
1528 HInstruction* GetPreviousDisregardingMoves() const;
1529
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001530 HBasicBlock* GetBlock() const { return block_; }
1531 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001532 bool IsInBlock() const { return block_ != nullptr; }
1533 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +01001534 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001535
Roland Levillain6b879dd2014-09-22 17:13:44 +01001536 virtual size_t InputCount() const = 0;
David Brazdil1abb4192015-02-17 18:33:36 +00001537 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001538
1539 virtual void Accept(HGraphVisitor* visitor) = 0;
1540 virtual const char* DebugName() const = 0;
1541
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001542 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
David Brazdil1abb4192015-02-17 18:33:36 +00001543 void SetRawInputAt(size_t index, HInstruction* input) {
1544 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1545 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001546
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001547 virtual bool NeedsEnvironment() const { return false; }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001548 virtual uint32_t GetDexPc() const {
1549 LOG(FATAL) << "GetDexPc() cannot be called on an instruction that"
1550 " does not need an environment";
1551 UNREACHABLE();
1552 }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001553 virtual bool IsControlFlow() const { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01001554 virtual bool CanThrow() const { return false; }
Aart Bik854a02b2015-07-14 16:07:00 -07001555
1556 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001557
Calin Juravle10e244f2015-01-26 18:54:32 +00001558 // Does not apply for all instructions, but having this at top level greatly
1559 // simplifies the null check elimination.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001560 virtual bool CanBeNull() const {
1561 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1562 return true;
1563 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001564
Calin Juravle641547a2015-04-21 22:08:51 +01001565 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj) const {
1566 UNUSED(obj);
1567 return false;
1568 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001569
Calin Juravleacf735c2015-02-12 15:25:22 +00001570 void SetReferenceTypeInfo(ReferenceTypeInfo reference_type_info) {
Calin Juravle61d544b2015-02-23 16:46:57 +00001571 DCHECK_EQ(GetType(), Primitive::kPrimNot);
Calin Juravleacf735c2015-02-12 15:25:22 +00001572 reference_type_info_ = reference_type_info;
1573 }
1574
Calin Juravle61d544b2015-02-23 16:46:57 +00001575 ReferenceTypeInfo GetReferenceTypeInfo() const {
1576 DCHECK_EQ(GetType(), Primitive::kPrimNot);
1577 return reference_type_info_;
1578 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001579
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001580 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001581 DCHECK(user != nullptr);
1582 HUseListNode<HInstruction*>* use =
1583 uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1584 user->SetRawInputRecordAt(index, HUserRecord<HInstruction*>(user->InputRecordAt(index), use));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001585 }
1586
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001587 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001588 DCHECK(user != nullptr);
David Brazdiled596192015-01-23 10:39:45 +00001589 HUseListNode<HEnvironment*>* env_use =
1590 env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1591 user->RecordEnvUse(env_use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001592 }
1593
David Brazdil1abb4192015-02-17 18:33:36 +00001594 void RemoveAsUserOfInput(size_t input) {
1595 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
1596 input_use.GetInstruction()->uses_.Remove(input_use.GetUseNode());
1597 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001598
David Brazdil1abb4192015-02-17 18:33:36 +00001599 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1600 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001601
David Brazdiled596192015-01-23 10:39:45 +00001602 bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); }
1603 bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); }
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001604 bool HasNonEnvironmentUses() const { return !uses_.IsEmpty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001605 bool HasOnlyOneNonEnvironmentUse() const {
1606 return !HasEnvironmentUses() && GetUses().HasOnlyOneUse();
1607 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001608
Roland Levillain6c82d402014-10-13 16:10:27 +01001609 // Does this instruction strictly dominate `other_instruction`?
1610 // Returns false if this instruction and `other_instruction` are the same.
1611 // Aborts if this instruction and `other_instruction` are both phis.
1612 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001613
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001614 int GetId() const { return id_; }
1615 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001616
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001617 int GetSsaIndex() const { return ssa_index_; }
1618 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1619 bool HasSsaIndex() const { return ssa_index_ != -1; }
1620
1621 bool HasEnvironment() const { return environment_ != nullptr; }
1622 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001623 // Set the `environment_` field. Raw because this method does not
1624 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001625 void SetRawEnvironment(HEnvironment* environment) {
1626 DCHECK(environment_ == nullptr);
1627 DCHECK_EQ(environment->GetHolder(), this);
1628 environment_ = environment;
1629 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001630
1631 // Set the environment of this instruction, copying it from `environment`. While
1632 // copying, the uses lists are being updated.
1633 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001634 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001635 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001636 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001637 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001638 if (environment->GetParent() != nullptr) {
1639 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1640 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001641 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001642
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001643 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
1644 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001645 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001646 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001647 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001648 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001649 if (environment->GetParent() != nullptr) {
1650 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1651 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001652 }
1653
Nicolas Geoffray39468442014-09-02 15:17:15 +01001654 // Returns the number of entries in the environment. Typically, that is the
1655 // number of dex registers in a method. It could be more in case of inlining.
1656 size_t EnvironmentSize() const;
1657
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001658 LocationSummary* GetLocations() const { return locations_; }
1659 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001660
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001661 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001662 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001663
Alexandre Rames188d4312015-04-09 18:30:21 +01001664 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
1665 // uses of this instruction by `other` are *not* updated.
1666 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
1667 ReplaceWith(other);
1668 other->ReplaceInput(this, use_index);
1669 }
1670
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001671 // Move `this` instruction before `cursor`.
1672 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001673
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001674#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01001675 bool Is##type() const { return (As##type() != nullptr); } \
1676 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001677 virtual H##type* As##type() { return nullptr; }
1678
1679 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
1680#undef INSTRUCTION_TYPE_CHECK
1681
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001682 // Returns whether the instruction can be moved within the graph.
1683 virtual bool CanBeMoved() const { return false; }
1684
1685 // Returns whether the two instructions are of the same kind.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001686 virtual bool InstructionTypeEquals(HInstruction* other) const {
1687 UNUSED(other);
1688 return false;
1689 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001690
1691 // Returns whether any data encoded in the two instructions is equal.
1692 // This method does not look at the inputs. Both instructions must be
1693 // of the same type, otherwise the method has undefined behavior.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001694 virtual bool InstructionDataEquals(HInstruction* other) const {
1695 UNUSED(other);
1696 return false;
1697 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001698
1699 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00001700 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001701 // 2) Their inputs are identical.
1702 bool Equals(HInstruction* other) const;
1703
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001704 virtual InstructionKind GetKind() const = 0;
1705
1706 virtual size_t ComputeHashCode() const {
1707 size_t result = GetKind();
1708 for (size_t i = 0, e = InputCount(); i < e; ++i) {
1709 result = (result * 31) + InputAt(i)->GetId();
1710 }
1711 return result;
1712 }
1713
1714 SideEffects GetSideEffects() const { return side_effects_; }
1715
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001716 size_t GetLifetimePosition() const { return lifetime_position_; }
1717 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
1718 LiveInterval* GetLiveInterval() const { return live_interval_; }
1719 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
1720 bool HasLiveInterval() const { return live_interval_ != nullptr; }
1721
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001722 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
1723
1724 // Returns whether the code generation of the instruction will require to have access
1725 // to the current method. Such instructions are:
1726 // (1): Instructions that require an environment, as calling the runtime requires
1727 // to walk the stack and have the current method stored at a specific stack address.
1728 // (2): Object literals like classes and strings, that are loaded from the dex cache
1729 // fields of the current method.
1730 bool NeedsCurrentMethod() const {
1731 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
1732 }
1733
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001734 virtual bool NeedsDexCache() const { return false; }
1735
Mark Mendellc4701932015-04-10 13:18:51 -04001736 // Does this instruction have any use in an environment before
1737 // control flow hits 'other'?
1738 bool HasAnyEnvironmentUseBefore(HInstruction* other);
1739
1740 // Remove all references to environment uses of this instruction.
1741 // The caller must ensure that this is safe to do.
1742 void RemoveEnvironmentUsers();
1743
David Brazdil1abb4192015-02-17 18:33:36 +00001744 protected:
1745 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
1746 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
1747
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001748 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001749 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); }
1750
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001751 HInstruction* previous_;
1752 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001753 HBasicBlock* block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001754
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001755 // An instruction gets an id when it is added to the graph.
1756 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01001757 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001758 int id_;
1759
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001760 // When doing liveness analysis, instructions that have uses get an SSA index.
1761 int ssa_index_;
1762
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001763 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00001764 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001765
1766 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00001767 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001768
Nicolas Geoffray39468442014-09-02 15:17:15 +01001769 // The environment associated with this instruction. Not null if the instruction
1770 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001771 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001772
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001773 // Set by the code generator.
1774 LocationSummary* locations_;
1775
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001776 // Set by the liveness analysis.
1777 LiveInterval* live_interval_;
1778
1779 // Set by the liveness analysis, this is the position in a linear
1780 // order of blocks where this instruction's live interval start.
1781 size_t lifetime_position_;
1782
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001783 const SideEffects side_effects_;
1784
Calin Juravleacf735c2015-02-12 15:25:22 +00001785 // TODO: for primitive types this should be marked as invalid.
1786 ReferenceTypeInfo reference_type_info_;
1787
David Brazdil1abb4192015-02-17 18:33:36 +00001788 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001789 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00001790 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001791 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001792 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001793
1794 DISALLOW_COPY_AND_ASSIGN(HInstruction);
1795};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001796std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001797
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001798class HInputIterator : public ValueObject {
1799 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001800 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001801
1802 bool Done() const { return index_ == instruction_->InputCount(); }
1803 HInstruction* Current() const { return instruction_->InputAt(index_); }
1804 void Advance() { index_++; }
1805
1806 private:
1807 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001808 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001809
1810 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
1811};
1812
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001813class HInstructionIterator : public ValueObject {
1814 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001815 explicit HInstructionIterator(const HInstructionList& instructions)
1816 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001817 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001818 }
1819
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001820 bool Done() const { return instruction_ == nullptr; }
1821 HInstruction* Current() const { return instruction_; }
1822 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001823 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001824 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001825 }
1826
1827 private:
1828 HInstruction* instruction_;
1829 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001830
1831 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001832};
1833
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001834class HBackwardInstructionIterator : public ValueObject {
1835 public:
1836 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
1837 : instruction_(instructions.last_instruction_) {
1838 next_ = Done() ? nullptr : instruction_->GetPrevious();
1839 }
1840
1841 bool Done() const { return instruction_ == nullptr; }
1842 HInstruction* Current() const { return instruction_; }
1843 void Advance() {
1844 instruction_ = next_;
1845 next_ = Done() ? nullptr : instruction_->GetPrevious();
1846 }
1847
1848 private:
1849 HInstruction* instruction_;
1850 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001851
1852 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001853};
1854
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001855// An embedded container with N elements of type T. Used (with partial
1856// specialization for N=0) because embedded arrays cannot have size 0.
1857template<typename T, intptr_t N>
1858class EmbeddedArray {
1859 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001860 EmbeddedArray() : elements_() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001861
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001862 intptr_t GetLength() const { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001863
1864 const T& operator[](intptr_t i) const {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001865 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001866 return elements_[i];
1867 }
1868
1869 T& operator[](intptr_t i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001870 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001871 return elements_[i];
1872 }
1873
1874 const T& At(intptr_t i) const {
1875 return (*this)[i];
1876 }
1877
1878 void SetAt(intptr_t i, const T& val) {
1879 (*this)[i] = val;
1880 }
1881
1882 private:
1883 T elements_[N];
1884};
1885
1886template<typename T>
1887class EmbeddedArray<T, 0> {
1888 public:
1889 intptr_t length() const { return 0; }
1890 const T& operator[](intptr_t i) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001891 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001892 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001893 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001894 }
1895 T& operator[](intptr_t i) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001896 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001897 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001898 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001899 }
1900};
1901
1902template<intptr_t N>
1903class HTemplateInstruction: public HInstruction {
1904 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001905 HTemplateInstruction<N>(SideEffects side_effects)
1906 : HInstruction(side_effects), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07001907 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001908
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001909 size_t InputCount() const OVERRIDE { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001910
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001911 protected:
David Brazdil1abb4192015-02-17 18:33:36 +00001912 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_[i]; }
1913
1914 void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE {
1915 inputs_[i] = input;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001916 }
1917
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001918 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001919 EmbeddedArray<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001920
1921 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001922};
1923
Dave Allison20dfc792014-06-16 20:44:29 -07001924template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001925class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07001926 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001927 HExpression<N>(Primitive::Type type, SideEffects side_effects)
1928 : HTemplateInstruction<N>(side_effects), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07001929 virtual ~HExpression() {}
1930
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001931 Primitive::Type GetType() const OVERRIDE { return type_; }
Dave Allison20dfc792014-06-16 20:44:29 -07001932
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001933 protected:
1934 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07001935};
1936
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001937// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
1938// instruction that branches to the exit block.
1939class HReturnVoid : public HTemplateInstruction<0> {
1940 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001941 HReturnVoid() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001942
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001943 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001944
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001945 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001946
1947 private:
1948 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
1949};
1950
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001951// Represents dex's RETURN opcodes. A HReturn is a control flow
1952// instruction that branches to the exit block.
1953class HReturn : public HTemplateInstruction<1> {
1954 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001955 explicit HReturn(HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001956 SetRawInputAt(0, value);
1957 }
1958
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001959 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001960
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001961 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001962
1963 private:
1964 DISALLOW_COPY_AND_ASSIGN(HReturn);
1965};
1966
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001967// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001968// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001969// exit block.
1970class HExit : public HTemplateInstruction<0> {
1971 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001972 HExit() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001973
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001974 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001975
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001976 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001977
1978 private:
1979 DISALLOW_COPY_AND_ASSIGN(HExit);
1980};
1981
1982// Jumps from one block to another.
1983class HGoto : public HTemplateInstruction<0> {
1984 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001985 HGoto() : HTemplateInstruction(SideEffects::None()) {}
1986
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001987 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001988
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001989 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00001990 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001991 }
1992
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001993 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001994
1995 private:
1996 DISALLOW_COPY_AND_ASSIGN(HGoto);
1997};
1998
Dave Allison20dfc792014-06-16 20:44:29 -07001999
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002000// Conditional branch. A block ending with an HIf instruction must have
2001// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002002class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002003 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002004 explicit HIf(HInstruction* input) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002005 SetRawInputAt(0, input);
2006 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002007
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002008 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002009
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002010 HBasicBlock* IfTrueSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002011 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002012 }
2013
2014 HBasicBlock* IfFalseSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002015 return GetBlock()->GetSuccessors().Get(1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002016 }
2017
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002018 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002019
2020 private:
2021 DISALLOW_COPY_AND_ASSIGN(HIf);
2022};
2023
David Brazdilfc6a86a2015-06-26 10:33:45 +00002024
2025// Abstract instruction which marks the beginning and/or end of a try block and
2026// links it to the respective exception handlers. Behaves the same as a Goto in
2027// non-exceptional control flow.
2028// Normal-flow successor is stored at index zero, exception handlers under
2029// higher indices in no particular order.
2030class HTryBoundary : public HTemplateInstruction<0> {
2031 public:
David Brazdil56e1acc2015-06-30 15:41:36 +01002032 enum BoundaryKind {
2033 kEntry,
2034 kExit,
2035 };
2036
2037 explicit HTryBoundary(BoundaryKind kind)
2038 : HTemplateInstruction(SideEffects::None()), kind_(kind) {}
David Brazdilfc6a86a2015-06-26 10:33:45 +00002039
2040 bool IsControlFlow() const OVERRIDE { return true; }
2041
2042 // Returns the block's non-exceptional successor (index zero).
2043 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors().Get(0); }
2044
2045 // Returns whether `handler` is among its exception handlers (non-zero index
2046 // successors).
2047 bool HasExceptionHandler(HBasicBlock* handler) const {
2048 DCHECK(handler->IsCatchBlock());
2049 return GetBlock()->GetSuccessors().Contains(handler, /* start_from */ 1);
2050 }
2051
2052 // Returns whether successor at index `idx` is an exception handler.
2053 bool IsExceptionalSuccessor(size_t idx) const {
2054 DCHECK_LT(idx, GetBlock()->GetSuccessors().Size());
2055 bool is_handler = (idx != 0);
2056 DCHECK(!is_handler || GetBlock()->GetSuccessors().Get(idx)->IsCatchBlock());
2057 return is_handler;
2058 }
2059
2060 // If not present already, adds `handler` to its block's list of exception
2061 // handlers.
2062 void AddExceptionHandler(HBasicBlock* handler) {
2063 if (!HasExceptionHandler(handler)) {
2064 GetBlock()->AddSuccessor(handler);
2065 }
2066 }
2067
David Brazdil56e1acc2015-06-30 15:41:36 +01002068 bool IsEntry() const { return kind_ == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002069
2070 DECLARE_INSTRUCTION(TryBoundary);
2071
2072 private:
David Brazdil56e1acc2015-06-30 15:41:36 +01002073 const BoundaryKind kind_;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002074
2075 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2076};
2077
2078
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002079// Deoptimize to interpreter, upon checking a condition.
2080class HDeoptimize : public HTemplateInstruction<1> {
2081 public:
2082 HDeoptimize(HInstruction* cond, uint32_t dex_pc)
2083 : HTemplateInstruction(SideEffects::None()),
2084 dex_pc_(dex_pc) {
2085 SetRawInputAt(0, cond);
2086 }
2087
2088 bool NeedsEnvironment() const OVERRIDE { return true; }
2089 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002090 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002091
2092 DECLARE_INSTRUCTION(Deoptimize);
2093
2094 private:
2095 uint32_t dex_pc_;
2096
2097 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
2098};
2099
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002100// Represents the ArtMethod that was passed as a first argument to
2101// the method. It is used by instructions that depend on it, like
2102// instructions that work with the dex cache.
2103class HCurrentMethod : public HExpression<0> {
2104 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -07002105 explicit HCurrentMethod(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002106
2107 DECLARE_INSTRUCTION(CurrentMethod);
2108
2109 private:
2110 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
2111};
2112
Roland Levillain88cb1752014-10-20 16:36:47 +01002113class HUnaryOperation : public HExpression<1> {
2114 public:
2115 HUnaryOperation(Primitive::Type result_type, HInstruction* input)
2116 : HExpression(result_type, SideEffects::None()) {
2117 SetRawInputAt(0, input);
2118 }
2119
2120 HInstruction* GetInput() const { return InputAt(0); }
2121 Primitive::Type GetResultType() const { return GetType(); }
2122
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 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002128
Roland Levillain9240d6a2014-10-20 16:47:04 +01002129 // Try to statically evaluate `operation` and return a HConstant
2130 // 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;
2133
2134 // Apply this operation to `x`.
2135 virtual int32_t Evaluate(int32_t x) const = 0;
2136 virtual int64_t Evaluate(int64_t x) const = 0;
2137
Roland Levillain88cb1752014-10-20 16:36:47 +01002138 DECLARE_INSTRUCTION(UnaryOperation);
2139
2140 private:
2141 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
2142};
2143
Dave Allison20dfc792014-06-16 20:44:29 -07002144class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002145 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002146 HBinaryOperation(Primitive::Type result_type,
2147 HInstruction* left,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002148 HInstruction* right) : HExpression(result_type, SideEffects::None()) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002149 SetRawInputAt(0, left);
2150 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002151 }
2152
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002153 HInstruction* GetLeft() const { return InputAt(0); }
2154 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07002155 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002156
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002157 virtual bool IsCommutative() const { return false; }
2158
2159 // Put constant on the right.
2160 // Returns whether order is changed.
2161 bool OrderInputsWithConstantOnTheRight() {
2162 HInstruction* left = InputAt(0);
2163 HInstruction* right = InputAt(1);
2164 if (left->IsConstant() && !right->IsConstant()) {
2165 ReplaceInput(right, 0);
2166 ReplaceInput(left, 1);
2167 return true;
2168 }
2169 return false;
2170 }
2171
2172 // Order inputs by instruction id, but favor constant on the right side.
2173 // This helps GVN for commutative ops.
2174 void OrderInputs() {
2175 DCHECK(IsCommutative());
2176 HInstruction* left = InputAt(0);
2177 HInstruction* right = InputAt(1);
2178 if (left == right || (!left->IsConstant() && right->IsConstant())) {
2179 return;
2180 }
2181 if (OrderInputsWithConstantOnTheRight()) {
2182 return;
2183 }
2184 // Order according to instruction id.
2185 if (left->GetId() > right->GetId()) {
2186 ReplaceInput(right, 0);
2187 ReplaceInput(left, 1);
2188 }
2189 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002190
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002191 bool CanBeMoved() const OVERRIDE { return true; }
2192 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002193 UNUSED(other);
2194 return true;
2195 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002196
Roland Levillain9240d6a2014-10-20 16:47:04 +01002197 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01002198 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002199 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002200 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01002201
2202 // Apply this operation to `x` and `y`.
2203 virtual int32_t Evaluate(int32_t x, int32_t y) const = 0;
2204 virtual int64_t Evaluate(int64_t x, int64_t y) const = 0;
2205
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002206 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002207 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002208 HConstant* GetConstantRight() const;
2209
2210 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002211 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002212 HInstruction* GetLeastConstantLeft() const;
2213
Roland Levillainccc07a92014-09-16 14:48:16 +01002214 DECLARE_INSTRUCTION(BinaryOperation);
2215
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002216 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002217 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
2218};
2219
Mark Mendellc4701932015-04-10 13:18:51 -04002220// The comparison bias applies for floating point operations and indicates how NaN
2221// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01002222enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04002223 kNoBias, // bias is not applicable (i.e. for long operation)
2224 kGtBias, // return 1 for NaN comparisons
2225 kLtBias, // return -1 for NaN comparisons
2226};
2227
Dave Allison20dfc792014-06-16 20:44:29 -07002228class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002229 public:
Dave Allison20dfc792014-06-16 20:44:29 -07002230 HCondition(HInstruction* first, HInstruction* second)
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002231 : HBinaryOperation(Primitive::kPrimBoolean, first, second),
Mark Mendellc4701932015-04-10 13:18:51 -04002232 needs_materialization_(true),
Roland Levillain4fa13f62015-07-06 18:11:54 +01002233 bias_(ComparisonBias::kNoBias) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002234
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002235 bool NeedsMaterialization() const { return needs_materialization_; }
2236 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002237
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002238 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002239 // `instruction`, and disregard moves in between.
2240 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002241
Dave Allison20dfc792014-06-16 20:44:29 -07002242 DECLARE_INSTRUCTION(Condition);
2243
2244 virtual IfCondition GetCondition() const = 0;
2245
Mark Mendellc4701932015-04-10 13:18:51 -04002246 virtual IfCondition GetOppositeCondition() const = 0;
2247
Roland Levillain4fa13f62015-07-06 18:11:54 +01002248 bool IsGtBias() const { return bias_ == ComparisonBias::kGtBias; }
Mark Mendellc4701932015-04-10 13:18:51 -04002249
2250 void SetBias(ComparisonBias bias) { bias_ = bias; }
2251
2252 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2253 return bias_ == other->AsCondition()->bias_;
2254 }
2255
Roland Levillain4fa13f62015-07-06 18:11:54 +01002256 bool IsFPConditionTrueIfNaN() const {
2257 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2258 IfCondition if_cond = GetCondition();
2259 return IsGtBias() ? ((if_cond == kCondGT) || (if_cond == kCondGE)) : (if_cond == kCondNE);
2260 }
2261
2262 bool IsFPConditionFalseIfNaN() const {
2263 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2264 IfCondition if_cond = GetCondition();
2265 return IsGtBias() ? ((if_cond == kCondLT) || (if_cond == kCondLE)) : (if_cond == kCondEQ);
2266 }
2267
Dave Allison20dfc792014-06-16 20:44:29 -07002268 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002269 // For register allocation purposes, returns whether this instruction needs to be
2270 // materialized (that is, not just be in the processor flags).
2271 bool needs_materialization_;
2272
Mark Mendellc4701932015-04-10 13:18:51 -04002273 // Needed if we merge a HCompare into a HCondition.
2274 ComparisonBias bias_;
2275
Dave Allison20dfc792014-06-16 20:44:29 -07002276 DISALLOW_COPY_AND_ASSIGN(HCondition);
2277};
2278
2279// Instruction to check if two inputs are equal to each other.
2280class HEqual : public HCondition {
2281 public:
2282 HEqual(HInstruction* first, HInstruction* second)
2283 : HCondition(first, second) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002284
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002285 bool IsCommutative() const OVERRIDE { return true; }
2286
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002287 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002288 return x == y ? 1 : 0;
2289 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002290 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002291 return x == y ? 1 : 0;
2292 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002293
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002294 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002295
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002296 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002297 return kCondEQ;
2298 }
2299
Mark Mendellc4701932015-04-10 13:18:51 -04002300 IfCondition GetOppositeCondition() const OVERRIDE {
2301 return kCondNE;
2302 }
2303
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002304 private:
2305 DISALLOW_COPY_AND_ASSIGN(HEqual);
2306};
2307
Dave Allison20dfc792014-06-16 20:44:29 -07002308class HNotEqual : public HCondition {
2309 public:
2310 HNotEqual(HInstruction* first, HInstruction* second)
2311 : HCondition(first, second) {}
2312
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002313 bool IsCommutative() const OVERRIDE { return true; }
2314
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002315 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002316 return x != y ? 1 : 0;
2317 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002318 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002319 return x != y ? 1 : 0;
2320 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002321
Dave Allison20dfc792014-06-16 20:44:29 -07002322 DECLARE_INSTRUCTION(NotEqual);
2323
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002324 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002325 return kCondNE;
2326 }
2327
Mark Mendellc4701932015-04-10 13:18:51 -04002328 IfCondition GetOppositeCondition() const OVERRIDE {
2329 return kCondEQ;
2330 }
2331
Dave Allison20dfc792014-06-16 20:44:29 -07002332 private:
2333 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
2334};
2335
2336class HLessThan : public HCondition {
2337 public:
2338 HLessThan(HInstruction* first, HInstruction* second)
2339 : HCondition(first, second) {}
2340
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002341 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002342 return x < y ? 1 : 0;
2343 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002344 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002345 return x < y ? 1 : 0;
2346 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002347
Dave Allison20dfc792014-06-16 20:44:29 -07002348 DECLARE_INSTRUCTION(LessThan);
2349
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002350 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002351 return kCondLT;
2352 }
2353
Mark Mendellc4701932015-04-10 13:18:51 -04002354 IfCondition GetOppositeCondition() const OVERRIDE {
2355 return kCondGE;
2356 }
2357
Dave Allison20dfc792014-06-16 20:44:29 -07002358 private:
2359 DISALLOW_COPY_AND_ASSIGN(HLessThan);
2360};
2361
2362class HLessThanOrEqual : public HCondition {
2363 public:
2364 HLessThanOrEqual(HInstruction* first, HInstruction* second)
2365 : HCondition(first, second) {}
2366
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002367 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002368 return x <= y ? 1 : 0;
2369 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002370 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002371 return x <= y ? 1 : 0;
2372 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002373
Dave Allison20dfc792014-06-16 20:44:29 -07002374 DECLARE_INSTRUCTION(LessThanOrEqual);
2375
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002376 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002377 return kCondLE;
2378 }
2379
Mark Mendellc4701932015-04-10 13:18:51 -04002380 IfCondition GetOppositeCondition() const OVERRIDE {
2381 return kCondGT;
2382 }
2383
Dave Allison20dfc792014-06-16 20:44:29 -07002384 private:
2385 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
2386};
2387
2388class HGreaterThan : public HCondition {
2389 public:
2390 HGreaterThan(HInstruction* first, HInstruction* second)
2391 : HCondition(first, second) {}
2392
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002393 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002394 return x > y ? 1 : 0;
2395 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002396 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002397 return x > y ? 1 : 0;
2398 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002399
Dave Allison20dfc792014-06-16 20:44:29 -07002400 DECLARE_INSTRUCTION(GreaterThan);
2401
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002402 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002403 return kCondGT;
2404 }
2405
Mark Mendellc4701932015-04-10 13:18:51 -04002406 IfCondition GetOppositeCondition() const OVERRIDE {
2407 return kCondLE;
2408 }
2409
Dave Allison20dfc792014-06-16 20:44:29 -07002410 private:
2411 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
2412};
2413
2414class HGreaterThanOrEqual : public HCondition {
2415 public:
2416 HGreaterThanOrEqual(HInstruction* first, HInstruction* second)
2417 : HCondition(first, second) {}
2418
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002419 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002420 return x >= y ? 1 : 0;
2421 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002422 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002423 return x >= y ? 1 : 0;
2424 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002425
Dave Allison20dfc792014-06-16 20:44:29 -07002426 DECLARE_INSTRUCTION(GreaterThanOrEqual);
2427
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002428 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002429 return kCondGE;
2430 }
2431
Mark Mendellc4701932015-04-10 13:18:51 -04002432 IfCondition GetOppositeCondition() const OVERRIDE {
2433 return kCondLT;
2434 }
2435
Dave Allison20dfc792014-06-16 20:44:29 -07002436 private:
2437 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
2438};
2439
2440
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002441// Instruction to check how two inputs compare to each other.
2442// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
2443class HCompare : public HBinaryOperation {
2444 public:
Alexey Frunze4dda3372015-06-01 18:31:49 -07002445 HCompare(Primitive::Type type,
2446 HInstruction* first,
2447 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04002448 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07002449 uint32_t dex_pc)
2450 : HBinaryOperation(Primitive::kPrimInt, first, second), bias_(bias), dex_pc_(dex_pc) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002451 DCHECK_EQ(type, first->GetType());
2452 DCHECK_EQ(type, second->GetType());
2453 }
2454
Calin Juravleddb7df22014-11-25 20:56:51 +00002455 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01002456 return
2457 x == y ? 0 :
2458 x > y ? 1 :
2459 -1;
2460 }
Calin Juravleddb7df22014-11-25 20:56:51 +00002461
2462 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01002463 return
2464 x == y ? 0 :
2465 x > y ? 1 :
2466 -1;
2467 }
2468
Calin Juravleddb7df22014-11-25 20:56:51 +00002469 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2470 return bias_ == other->AsCompare()->bias_;
2471 }
2472
Mark Mendellc4701932015-04-10 13:18:51 -04002473 ComparisonBias GetBias() const { return bias_; }
2474
Roland Levillain4fa13f62015-07-06 18:11:54 +01002475 bool IsGtBias() { return bias_ == ComparisonBias::kGtBias; }
Calin Juravleddb7df22014-11-25 20:56:51 +00002476
Alexey Frunze4dda3372015-06-01 18:31:49 -07002477 uint32_t GetDexPc() const { return dex_pc_; }
2478
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002479 DECLARE_INSTRUCTION(Compare);
2480
2481 private:
Mark Mendellc4701932015-04-10 13:18:51 -04002482 const ComparisonBias bias_;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002483 const uint32_t dex_pc_;
Calin Juravleddb7df22014-11-25 20:56:51 +00002484
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002485 DISALLOW_COPY_AND_ASSIGN(HCompare);
2486};
2487
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002488// A local in the graph. Corresponds to a Dex register.
2489class HLocal : public HTemplateInstruction<0> {
2490 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002491 explicit HLocal(uint16_t reg_number)
2492 : HTemplateInstruction(SideEffects::None()), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002493
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002494 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002495
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002496 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002497
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002498 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002499 // The Dex register number.
2500 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002501
2502 DISALLOW_COPY_AND_ASSIGN(HLocal);
2503};
2504
2505// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07002506class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002507 public:
Roland Levillain5799fc02014-09-25 12:15:20 +01002508 HLoadLocal(HLocal* local, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002509 : HExpression(type, SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002510 SetRawInputAt(0, local);
2511 }
2512
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002513 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
2514
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002515 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002516
2517 private:
2518 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
2519};
2520
2521// Store a value in a given local. This instruction has two inputs: the value
2522// and the local.
2523class HStoreLocal : public HTemplateInstruction<2> {
2524 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002525 HStoreLocal(HLocal* local, HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002526 SetRawInputAt(0, local);
2527 SetRawInputAt(1, value);
2528 }
2529
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002530 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
2531
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002532 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002533
2534 private:
2535 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
2536};
2537
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002538class HConstant : public HExpression<0> {
2539 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002540 explicit HConstant(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
2541
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002542 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002543
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002544 virtual bool IsMinusOne() const { return false; }
2545 virtual bool IsZero() const { return false; }
2546 virtual bool IsOne() const { return false; }
2547
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002548 DECLARE_INSTRUCTION(Constant);
2549
2550 private:
2551 DISALLOW_COPY_AND_ASSIGN(HConstant);
2552};
2553
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002554class HFloatConstant : public HConstant {
2555 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002556 float GetValue() const { return value_; }
2557
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002558 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002559 return bit_cast<uint32_t, float>(other->AsFloatConstant()->value_) ==
2560 bit_cast<uint32_t, float>(value_);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002561 }
2562
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002563 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002564
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002565 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002566 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002567 }
2568 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002569 return value_ == 0.0f;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002570 }
2571 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002572 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
2573 }
2574 bool IsNaN() const {
2575 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002576 }
2577
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002578 DECLARE_INSTRUCTION(FloatConstant);
2579
2580 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002581 explicit HFloatConstant(float value) : HConstant(Primitive::kPrimFloat), value_(value) {}
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002582 explicit HFloatConstant(int32_t value)
2583 : HConstant(Primitive::kPrimFloat), value_(bit_cast<float, int32_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00002584
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002585 const float value_;
2586
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002587 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00002588 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002589 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002590 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2591};
2592
2593class HDoubleConstant : public HConstant {
2594 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002595 double GetValue() const { return value_; }
2596
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002597 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002598 return bit_cast<uint64_t, double>(other->AsDoubleConstant()->value_) ==
2599 bit_cast<uint64_t, double>(value_);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002600 }
2601
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002602 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002603
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002604 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002605 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002606 }
2607 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002608 return value_ == 0.0;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002609 }
2610 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002611 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
2612 }
2613 bool IsNaN() const {
2614 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002615 }
2616
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002617 DECLARE_INSTRUCTION(DoubleConstant);
2618
2619 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002620 explicit HDoubleConstant(double value) : HConstant(Primitive::kPrimDouble), value_(value) {}
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002621 explicit HDoubleConstant(int64_t value)
2622 : HConstant(Primitive::kPrimDouble), value_(bit_cast<double, int64_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00002623
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002624 const double value_;
2625
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002626 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00002627 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002628 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002629 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
2630};
2631
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002632class HNullConstant : public HConstant {
2633 public:
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002634 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2635 return true;
2636 }
2637
2638 size_t ComputeHashCode() const OVERRIDE { return 0; }
2639
2640 DECLARE_INSTRUCTION(NullConstant);
2641
2642 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002643 HNullConstant() : HConstant(Primitive::kPrimNot) {}
2644
2645 friend class HGraph;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002646 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2647};
2648
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002649// Constants of the type int. Those can be from Dex instructions, or
2650// synthesized (for example with the if-eqz instruction).
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002651class HIntConstant : public HConstant {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002652 public:
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002653 int32_t GetValue() const { return value_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002654
Calin Juravle61d544b2015-02-23 16:46:57 +00002655 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002656 return other->AsIntConstant()->value_ == value_;
2657 }
2658
Calin Juravle61d544b2015-02-23 16:46:57 +00002659 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2660
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002661 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2662 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2663 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2664
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002665 DECLARE_INSTRUCTION(IntConstant);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002666
2667 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002668 explicit HIntConstant(int32_t value) : HConstant(Primitive::kPrimInt), value_(value) {}
2669
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002670 const int32_t value_;
2671
David Brazdil8d5b8b22015-03-24 10:51:52 +00002672 friend class HGraph;
2673 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
Zheng Xuad4450e2015-04-17 18:48:56 +08002674 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002675 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2676};
2677
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002678class HLongConstant : public HConstant {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002679 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002680 int64_t GetValue() const { return value_; }
2681
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002682 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002683 return other->AsLongConstant()->value_ == value_;
2684 }
2685
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002686 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002687
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002688 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2689 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2690 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2691
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002692 DECLARE_INSTRUCTION(LongConstant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002693
2694 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002695 explicit HLongConstant(int64_t value) : HConstant(Primitive::kPrimLong), value_(value) {}
2696
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002697 const int64_t value_;
2698
David Brazdil8d5b8b22015-03-24 10:51:52 +00002699 friend class HGraph;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002700 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2701};
2702
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002703enum class Intrinsics {
2704#define OPTIMIZING_INTRINSICS(Name, IsStatic) k ## Name,
2705#include "intrinsics_list.h"
2706 kNone,
2707 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
2708#undef INTRINSICS_LIST
2709#undef OPTIMIZING_INTRINSICS
2710};
2711std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
2712
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002713class HInvoke : public HInstruction {
2714 public:
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002715 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002716
2717 // Runtime needs to walk the stack, so Dex -> Dex calls need to
2718 // know their environment.
Calin Juravle77520bc2015-01-12 18:45:46 +00002719 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002720
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01002721 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002722 SetRawInputAt(index, argument);
2723 }
2724
Roland Levillain3e3d7332015-04-28 11:00:54 +01002725 // Return the number of arguments. This number can be lower than
2726 // the number of inputs returned by InputCount(), as some invoke
2727 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
2728 // inputs at the end of their list of inputs.
2729 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
2730
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002731 Primitive::Type GetType() const OVERRIDE { return return_type_; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002732
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002733 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002734
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002735 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002736 const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002737
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002738 InvokeType GetOriginalInvokeType() const { return original_invoke_type_; }
2739
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01002740 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002741 return intrinsic_;
2742 }
2743
2744 void SetIntrinsic(Intrinsics intrinsic) {
2745 intrinsic_ = intrinsic;
2746 }
2747
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01002748 bool IsFromInlinedInvoke() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002749 return GetEnvironment()->GetParent() != nullptr;
2750 }
2751
2752 bool CanThrow() const OVERRIDE { return true; }
2753
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002754 DECLARE_INSTRUCTION(Invoke);
2755
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002756 protected:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002757 HInvoke(ArenaAllocator* arena,
2758 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01002759 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002760 Primitive::Type return_type,
2761 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002762 uint32_t dex_method_index,
2763 InvokeType original_invoke_type)
Aart Bik854a02b2015-07-14 16:07:00 -07002764 : HInstruction(SideEffects::All()), // assume write/read on all fields/arrays
Roland Levillain3e3d7332015-04-28 11:00:54 +01002765 number_of_arguments_(number_of_arguments),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002766 inputs_(arena, number_of_arguments),
2767 return_type_(return_type),
2768 dex_pc_(dex_pc),
2769 dex_method_index_(dex_method_index),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002770 original_invoke_type_(original_invoke_type),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002771 intrinsic_(Intrinsics::kNone) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01002772 uint32_t number_of_inputs = number_of_arguments + number_of_other_inputs;
2773 inputs_.SetSize(number_of_inputs);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002774 }
2775
David Brazdil1abb4192015-02-17 18:33:36 +00002776 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
2777 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
2778 inputs_.Put(index, input);
2779 }
2780
Roland Levillain3e3d7332015-04-28 11:00:54 +01002781 uint32_t number_of_arguments_;
David Brazdil1abb4192015-02-17 18:33:36 +00002782 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002783 const Primitive::Type return_type_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002784 const uint32_t dex_pc_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002785 const uint32_t dex_method_index_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002786 const InvokeType original_invoke_type_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002787 Intrinsics intrinsic_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002788
2789 private:
2790 DISALLOW_COPY_AND_ASSIGN(HInvoke);
2791};
2792
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002793class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002794 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01002795 // Requirements of this method call regarding the class
2796 // initialization (clinit) check of its declaring class.
2797 enum class ClinitCheckRequirement {
2798 kNone, // Class already initialized.
2799 kExplicit, // Static call having explicit clinit check as last input.
2800 kImplicit, // Static call implicitly requiring a clinit check.
2801 };
2802
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002803 HInvokeStaticOrDirect(ArenaAllocator* arena,
2804 uint32_t number_of_arguments,
2805 Primitive::Type return_type,
2806 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002807 uint32_t dex_method_index,
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002808 bool is_recursive,
Jeff Hao848f70a2014-01-15 13:49:50 -08002809 int32_t string_init_offset,
Nicolas Geoffray79041292015-03-26 10:05:54 +00002810 InvokeType original_invoke_type,
Roland Levillain4c0eb422015-04-24 16:43:49 +01002811 InvokeType invoke_type,
2812 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01002813 : HInvoke(arena,
2814 number_of_arguments,
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01002815 // There is one extra argument for the HCurrentMethod node, and
2816 // potentially one other if the clinit check is explicit, and one other
2817 // if the method is a string factory.
2818 1u + (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u)
2819 + (string_init_offset ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01002820 return_type,
2821 dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002822 dex_method_index,
2823 original_invoke_type),
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002824 invoke_type_(invoke_type),
Roland Levillain4c0eb422015-04-24 16:43:49 +01002825 is_recursive_(is_recursive),
Jeff Hao848f70a2014-01-15 13:49:50 -08002826 clinit_check_requirement_(clinit_check_requirement),
2827 string_init_offset_(string_init_offset) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002828
Calin Juravle641547a2015-04-21 22:08:51 +01002829 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
2830 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00002831 // We access the method via the dex cache so we can't do an implicit null check.
2832 // TODO: for intrinsics we can generate implicit null checks.
2833 return false;
2834 }
2835
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002836 InvokeType GetInvokeType() const { return invoke_type_; }
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002837 bool IsRecursive() const { return is_recursive_; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002838 bool NeedsDexCache() const OVERRIDE { return !IsRecursive(); }
Jeff Hao848f70a2014-01-15 13:49:50 -08002839 bool IsStringInit() const { return string_init_offset_ != 0; }
2840 int32_t GetStringInitOffset() const { return string_init_offset_; }
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002841 uint32_t GetCurrentMethodInputIndex() const { return GetNumberOfArguments(); }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002842
Roland Levillain4c0eb422015-04-24 16:43:49 +01002843 // Is this instruction a call to a static method?
2844 bool IsStatic() const {
2845 return GetInvokeType() == kStatic;
2846 }
2847
Roland Levillain3e3d7332015-04-28 11:00:54 +01002848 // Remove the art::HLoadClass instruction set as last input by
2849 // art::PrepareForRegisterAllocation::VisitClinitCheck in lieu of
2850 // the initial art::HClinitCheck instruction (only relevant for
2851 // static calls with explicit clinit check).
2852 void RemoveLoadClassAsLastInput() {
Roland Levillain4c0eb422015-04-24 16:43:49 +01002853 DCHECK(IsStaticWithExplicitClinitCheck());
2854 size_t last_input_index = InputCount() - 1;
2855 HInstruction* last_input = InputAt(last_input_index);
2856 DCHECK(last_input != nullptr);
Roland Levillain3e3d7332015-04-28 11:00:54 +01002857 DCHECK(last_input->IsLoadClass()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01002858 RemoveAsUserOfInput(last_input_index);
2859 inputs_.DeleteAt(last_input_index);
2860 clinit_check_requirement_ = ClinitCheckRequirement::kImplicit;
2861 DCHECK(IsStaticWithImplicitClinitCheck());
2862 }
2863
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01002864 bool IsStringFactoryFor(HFakeString* str) const {
2865 if (!IsStringInit()) return false;
2866 // +1 for the current method.
2867 if (InputCount() == (number_of_arguments_ + 1)) return false;
2868 return InputAt(InputCount() - 1)->AsFakeString() == str;
2869 }
2870
2871 void RemoveFakeStringArgumentAsLastInput() {
2872 DCHECK(IsStringInit());
2873 size_t last_input_index = InputCount() - 1;
2874 HInstruction* last_input = InputAt(last_input_index);
2875 DCHECK(last_input != nullptr);
2876 DCHECK(last_input->IsFakeString()) << last_input->DebugName();
2877 RemoveAsUserOfInput(last_input_index);
2878 inputs_.DeleteAt(last_input_index);
2879 }
2880
Roland Levillain4c0eb422015-04-24 16:43:49 +01002881 // Is this a call to a static method whose declaring class has an
2882 // explicit intialization check in the graph?
2883 bool IsStaticWithExplicitClinitCheck() const {
2884 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kExplicit);
2885 }
2886
2887 // Is this a call to a static method whose declaring class has an
2888 // implicit intialization check requirement?
2889 bool IsStaticWithImplicitClinitCheck() const {
2890 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kImplicit);
2891 }
2892
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002893 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002894
Roland Levillain4c0eb422015-04-24 16:43:49 +01002895 protected:
2896 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
2897 const HUserRecord<HInstruction*> input_record = HInvoke::InputRecordAt(i);
2898 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck() && (i == InputCount() - 1)) {
2899 HInstruction* input = input_record.GetInstruction();
2900 // `input` is the last input of a static invoke marked as having
2901 // an explicit clinit check. It must either be:
2902 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
2903 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
2904 DCHECK(input != nullptr);
2905 DCHECK(input->IsClinitCheck() || input->IsLoadClass()) << input->DebugName();
2906 }
2907 return input_record;
2908 }
2909
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002910 private:
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002911 const InvokeType invoke_type_;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002912 const bool is_recursive_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01002913 ClinitCheckRequirement clinit_check_requirement_;
Jeff Hao848f70a2014-01-15 13:49:50 -08002914 // Thread entrypoint offset for string init method if this is a string init invoke.
2915 // Note that there are multiple string init methods, each having its own offset.
2916 int32_t string_init_offset_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002917
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002918 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002919};
2920
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002921class HInvokeVirtual : public HInvoke {
2922 public:
2923 HInvokeVirtual(ArenaAllocator* arena,
2924 uint32_t number_of_arguments,
2925 Primitive::Type return_type,
2926 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002927 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002928 uint32_t vtable_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002929 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002930 vtable_index_(vtable_index) {}
2931
Calin Juravle641547a2015-04-21 22:08:51 +01002932 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00002933 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01002934 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00002935 }
2936
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002937 uint32_t GetVTableIndex() const { return vtable_index_; }
2938
2939 DECLARE_INSTRUCTION(InvokeVirtual);
2940
2941 private:
2942 const uint32_t vtable_index_;
2943
2944 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
2945};
2946
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002947class HInvokeInterface : public HInvoke {
2948 public:
2949 HInvokeInterface(ArenaAllocator* arena,
2950 uint32_t number_of_arguments,
2951 Primitive::Type return_type,
2952 uint32_t dex_pc,
2953 uint32_t dex_method_index,
2954 uint32_t imt_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002955 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002956 imt_index_(imt_index) {}
2957
Calin Juravle641547a2015-04-21 22:08:51 +01002958 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00002959 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01002960 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00002961 }
2962
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002963 uint32_t GetImtIndex() const { return imt_index_; }
2964 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
2965
2966 DECLARE_INSTRUCTION(InvokeInterface);
2967
2968 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002969 const uint32_t imt_index_;
2970
2971 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
2972};
2973
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01002974class HNewInstance : public HExpression<1> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002975 public:
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01002976 HNewInstance(HCurrentMethod* current_method,
2977 uint32_t dex_pc,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002978 uint16_t type_index,
2979 const DexFile& dex_file,
2980 QuickEntrypointEnum entrypoint)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002981 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2982 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002983 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002984 dex_file_(dex_file),
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01002985 entrypoint_(entrypoint) {
2986 SetRawInputAt(0, current_method);
2987 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002988
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002989 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002990 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002991 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002992
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002993 // Calls runtime so needs an environment.
Calin Juravle92a6ed22014-12-02 18:58:03 +00002994 bool NeedsEnvironment() const OVERRIDE { return true; }
2995 // It may throw when called on:
2996 // - interfaces
2997 // - abstract/innaccessible/unknown classes
2998 // TODO: optimize when possible.
2999 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003000
Calin Juravle10e244f2015-01-26 18:54:32 +00003001 bool CanBeNull() const OVERRIDE { return false; }
3002
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003003 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3004
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003005 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003006
3007 private:
3008 const uint32_t dex_pc_;
3009 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003010 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003011 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003012
3013 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3014};
3015
Roland Levillain88cb1752014-10-20 16:36:47 +01003016class HNeg : public HUnaryOperation {
3017 public:
3018 explicit HNeg(Primitive::Type result_type, HInstruction* input)
3019 : HUnaryOperation(result_type, input) {}
3020
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003021 int32_t Evaluate(int32_t x) const OVERRIDE { return -x; }
3022 int64_t Evaluate(int64_t x) const OVERRIDE { return -x; }
Roland Levillain9240d6a2014-10-20 16:47:04 +01003023
Roland Levillain88cb1752014-10-20 16:36:47 +01003024 DECLARE_INSTRUCTION(Neg);
3025
3026 private:
3027 DISALLOW_COPY_AND_ASSIGN(HNeg);
3028};
3029
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003030class HNewArray : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003031 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003032 HNewArray(HInstruction* length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003033 HCurrentMethod* current_method,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003034 uint32_t dex_pc,
3035 uint16_t type_index,
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003036 const DexFile& dex_file,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003037 QuickEntrypointEnum entrypoint)
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003038 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3039 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003040 type_index_(type_index),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003041 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003042 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003043 SetRawInputAt(0, length);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003044 SetRawInputAt(1, current_method);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003045 }
3046
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003047 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003048 uint16_t GetTypeIndex() const { return type_index_; }
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003049 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003050
3051 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00003052 bool NeedsEnvironment() const OVERRIDE { return true; }
3053
Mingyao Yang0c365e62015-03-31 15:09:29 -07003054 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
3055 bool CanThrow() const OVERRIDE { return true; }
3056
Calin Juravle10e244f2015-01-26 18:54:32 +00003057 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003058
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003059 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3060
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003061 DECLARE_INSTRUCTION(NewArray);
3062
3063 private:
3064 const uint32_t dex_pc_;
3065 const uint16_t type_index_;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003066 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003067 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003068
3069 DISALLOW_COPY_AND_ASSIGN(HNewArray);
3070};
3071
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003072class HAdd : public HBinaryOperation {
3073 public:
3074 HAdd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3075 : HBinaryOperation(result_type, left, right) {}
3076
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003077 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003078
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003079 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01003080 return x + y;
3081 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003082 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01003083 return x + y;
3084 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003085
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003086 DECLARE_INSTRUCTION(Add);
3087
3088 private:
3089 DISALLOW_COPY_AND_ASSIGN(HAdd);
3090};
3091
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003092class HSub : public HBinaryOperation {
3093 public:
3094 HSub(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3095 : HBinaryOperation(result_type, left, right) {}
3096
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003097 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01003098 return x - y;
3099 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003100 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01003101 return x - y;
3102 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003103
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003104 DECLARE_INSTRUCTION(Sub);
3105
3106 private:
3107 DISALLOW_COPY_AND_ASSIGN(HSub);
3108};
3109
Calin Juravle34bacdf2014-10-07 20:23:36 +01003110class HMul : public HBinaryOperation {
3111 public:
3112 HMul(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3113 : HBinaryOperation(result_type, left, right) {}
3114
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003115 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003116
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003117 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x * y; }
3118 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x * y; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003119
3120 DECLARE_INSTRUCTION(Mul);
3121
3122 private:
3123 DISALLOW_COPY_AND_ASSIGN(HMul);
3124};
3125
Calin Juravle7c4954d2014-10-28 16:57:40 +00003126class HDiv : public HBinaryOperation {
3127 public:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003128 HDiv(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
3129 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00003130
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003131 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00003132 // Our graph structure ensures we never have 0 for `y` during constant folding.
3133 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00003134 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00003135 return (y == -1) ? -x : x / y;
3136 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003137
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003138 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00003139 DCHECK_NE(y, 0);
3140 // Special case -1 to avoid getting a SIGFPE on x86(_64).
3141 return (y == -1) ? -x : x / y;
3142 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003143
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003144 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003145
Calin Juravle7c4954d2014-10-28 16:57:40 +00003146 DECLARE_INSTRUCTION(Div);
3147
3148 private:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003149 const uint32_t dex_pc_;
3150
Calin Juravle7c4954d2014-10-28 16:57:40 +00003151 DISALLOW_COPY_AND_ASSIGN(HDiv);
3152};
3153
Calin Juravlebacfec32014-11-14 15:54:36 +00003154class HRem : public HBinaryOperation {
3155 public:
3156 HRem(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
3157 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
3158
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003159 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00003160 DCHECK_NE(y, 0);
3161 // Special case -1 to avoid getting a SIGFPE on x86(_64).
3162 return (y == -1) ? 0 : x % y;
3163 }
3164
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003165 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00003166 DCHECK_NE(y, 0);
3167 // Special case -1 to avoid getting a SIGFPE on x86(_64).
3168 return (y == -1) ? 0 : x % y;
3169 }
3170
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003171 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Calin Juravlebacfec32014-11-14 15:54:36 +00003172
3173 DECLARE_INSTRUCTION(Rem);
3174
3175 private:
3176 const uint32_t dex_pc_;
3177
3178 DISALLOW_COPY_AND_ASSIGN(HRem);
3179};
3180
Calin Juravled0d48522014-11-04 16:40:20 +00003181class HDivZeroCheck : public HExpression<1> {
3182 public:
3183 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
3184 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
3185 SetRawInputAt(0, value);
3186 }
3187
3188 bool CanBeMoved() const OVERRIDE { return true; }
3189
3190 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3191 UNUSED(other);
3192 return true;
3193 }
3194
3195 bool NeedsEnvironment() const OVERRIDE { return true; }
3196 bool CanThrow() const OVERRIDE { return true; }
3197
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003198 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Calin Juravled0d48522014-11-04 16:40:20 +00003199
3200 DECLARE_INSTRUCTION(DivZeroCheck);
3201
3202 private:
3203 const uint32_t dex_pc_;
3204
3205 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
3206};
3207
Calin Juravle9aec02f2014-11-18 23:06:35 +00003208class HShl : public HBinaryOperation {
3209 public:
3210 HShl(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3211 : HBinaryOperation(result_type, left, right) {}
3212
3213 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x << (y & kMaxIntShiftValue); }
3214 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x << (y & kMaxLongShiftValue); }
3215
3216 DECLARE_INSTRUCTION(Shl);
3217
3218 private:
3219 DISALLOW_COPY_AND_ASSIGN(HShl);
3220};
3221
3222class HShr : public HBinaryOperation {
3223 public:
3224 HShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3225 : HBinaryOperation(result_type, left, right) {}
3226
3227 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x >> (y & kMaxIntShiftValue); }
3228 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x >> (y & kMaxLongShiftValue); }
3229
3230 DECLARE_INSTRUCTION(Shr);
3231
3232 private:
3233 DISALLOW_COPY_AND_ASSIGN(HShr);
3234};
3235
3236class HUShr : public HBinaryOperation {
3237 public:
3238 HUShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3239 : HBinaryOperation(result_type, left, right) {}
3240
3241 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
3242 uint32_t ux = static_cast<uint32_t>(x);
3243 uint32_t uy = static_cast<uint32_t>(y) & kMaxIntShiftValue;
3244 return static_cast<int32_t>(ux >> uy);
3245 }
3246
3247 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
3248 uint64_t ux = static_cast<uint64_t>(x);
3249 uint64_t uy = static_cast<uint64_t>(y) & kMaxLongShiftValue;
3250 return static_cast<int64_t>(ux >> uy);
3251 }
3252
3253 DECLARE_INSTRUCTION(UShr);
3254
3255 private:
3256 DISALLOW_COPY_AND_ASSIGN(HUShr);
3257};
3258
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003259class HAnd : public HBinaryOperation {
3260 public:
3261 HAnd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3262 : HBinaryOperation(result_type, left, right) {}
3263
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003264 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003265
3266 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x & y; }
3267 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x & y; }
3268
3269 DECLARE_INSTRUCTION(And);
3270
3271 private:
3272 DISALLOW_COPY_AND_ASSIGN(HAnd);
3273};
3274
3275class HOr : public HBinaryOperation {
3276 public:
3277 HOr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3278 : HBinaryOperation(result_type, left, right) {}
3279
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003280 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003281
3282 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x | y; }
3283 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x | y; }
3284
3285 DECLARE_INSTRUCTION(Or);
3286
3287 private:
3288 DISALLOW_COPY_AND_ASSIGN(HOr);
3289};
3290
3291class HXor : public HBinaryOperation {
3292 public:
3293 HXor(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3294 : HBinaryOperation(result_type, left, right) {}
3295
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003296 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003297
3298 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x ^ y; }
3299 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x ^ y; }
3300
3301 DECLARE_INSTRUCTION(Xor);
3302
3303 private:
3304 DISALLOW_COPY_AND_ASSIGN(HXor);
3305};
3306
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003307// The value of a parameter in this method. Its location depends on
3308// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07003309class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003310 public:
Calin Juravle10e244f2015-01-26 18:54:32 +00003311 HParameterValue(uint8_t index, Primitive::Type parameter_type, bool is_this = false)
3312 : HExpression(parameter_type, SideEffects::None()), index_(index), is_this_(is_this) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003313
3314 uint8_t GetIndex() const { return index_; }
3315
Calin Juravle10e244f2015-01-26 18:54:32 +00003316 bool CanBeNull() const OVERRIDE { return !is_this_; }
3317
Calin Juravle3cd4fc82015-05-14 15:15:42 +01003318 bool IsThis() const { return is_this_; }
3319
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003320 DECLARE_INSTRUCTION(ParameterValue);
3321
3322 private:
3323 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00003324 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003325 const uint8_t index_;
3326
Calin Juravle10e244f2015-01-26 18:54:32 +00003327 // Whether or not the parameter value corresponds to 'this' argument.
3328 const bool is_this_;
3329
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003330 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
3331};
3332
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003333class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003334 public:
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003335 explicit HNot(Primitive::Type result_type, HInstruction* input)
3336 : HUnaryOperation(result_type, input) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003337
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003338 bool CanBeMoved() const OVERRIDE { return true; }
3339 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003340 UNUSED(other);
3341 return true;
3342 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003343
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003344 int32_t Evaluate(int32_t x) const OVERRIDE { return ~x; }
3345 int64_t Evaluate(int64_t x) const OVERRIDE { return ~x; }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003346
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003347 DECLARE_INSTRUCTION(Not);
3348
3349 private:
3350 DISALLOW_COPY_AND_ASSIGN(HNot);
3351};
3352
David Brazdil66d126e2015-04-03 16:02:44 +01003353class HBooleanNot : public HUnaryOperation {
3354 public:
3355 explicit HBooleanNot(HInstruction* input)
3356 : HUnaryOperation(Primitive::Type::kPrimBoolean, input) {}
3357
3358 bool CanBeMoved() const OVERRIDE { return true; }
3359 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3360 UNUSED(other);
3361 return true;
3362 }
3363
3364 int32_t Evaluate(int32_t x) const OVERRIDE {
3365 DCHECK(IsUint<1>(x));
3366 return !x;
3367 }
3368
3369 int64_t Evaluate(int64_t x ATTRIBUTE_UNUSED) const OVERRIDE {
3370 LOG(FATAL) << DebugName() << " cannot be used with 64-bit values";
3371 UNREACHABLE();
3372 }
3373
3374 DECLARE_INSTRUCTION(BooleanNot);
3375
3376 private:
3377 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
3378};
3379
Roland Levillaindff1f282014-11-05 14:15:05 +00003380class HTypeConversion : public HExpression<1> {
3381 public:
3382 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00003383 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
3384 : HExpression(result_type, SideEffects::None()), dex_pc_(dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00003385 SetRawInputAt(0, input);
3386 DCHECK_NE(input->GetType(), result_type);
3387 }
3388
3389 HInstruction* GetInput() const { return InputAt(0); }
3390 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
3391 Primitive::Type GetResultType() const { return GetType(); }
3392
Roland Levillain624279f2014-12-04 11:54:28 +00003393 // Required by the x86 and ARM code generators when producing calls
3394 // to the runtime.
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003395 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Roland Levillain624279f2014-12-04 11:54:28 +00003396
Roland Levillaindff1f282014-11-05 14:15:05 +00003397 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00003398 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00003399
Mark Mendelle82549b2015-05-06 10:55:34 -04003400 // Try to statically evaluate the conversion and return a HConstant
3401 // containing the result. If the input cannot be converted, return nullptr.
3402 HConstant* TryStaticEvaluation() const;
3403
Roland Levillaindff1f282014-11-05 14:15:05 +00003404 DECLARE_INSTRUCTION(TypeConversion);
3405
3406 private:
Roland Levillain624279f2014-12-04 11:54:28 +00003407 const uint32_t dex_pc_;
3408
Roland Levillaindff1f282014-11-05 14:15:05 +00003409 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
3410};
3411
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00003412static constexpr uint32_t kNoRegNumber = -1;
3413
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003414class HPhi : public HInstruction {
3415 public:
3416 HPhi(ArenaAllocator* arena, uint32_t reg_number, size_t number_of_inputs, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003417 : HInstruction(SideEffects::None()),
3418 inputs_(arena, number_of_inputs),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003419 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01003420 type_(type),
Calin Juravle10e244f2015-01-26 18:54:32 +00003421 is_live_(false),
3422 can_be_null_(true) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003423 inputs_.SetSize(number_of_inputs);
3424 }
3425
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00003426 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
3427 static Primitive::Type ToPhiType(Primitive::Type type) {
3428 switch (type) {
3429 case Primitive::kPrimBoolean:
3430 case Primitive::kPrimByte:
3431 case Primitive::kPrimShort:
3432 case Primitive::kPrimChar:
3433 return Primitive::kPrimInt;
3434 default:
3435 return type;
3436 }
3437 }
3438
Calin Juravle10e244f2015-01-26 18:54:32 +00003439 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003440
3441 void AddInput(HInstruction* input);
David Brazdil2d7352b2015-04-20 14:52:42 +01003442 void RemoveInputAt(size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003443
Calin Juravle10e244f2015-01-26 18:54:32 +00003444 Primitive::Type GetType() const OVERRIDE { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003445 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003446
Calin Juravle10e244f2015-01-26 18:54:32 +00003447 bool CanBeNull() const OVERRIDE { return can_be_null_; }
3448 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
3449
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003450 uint32_t GetRegNumber() const { return reg_number_; }
3451
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01003452 void SetDead() { is_live_ = false; }
3453 void SetLive() { is_live_ = true; }
3454 bool IsDead() const { return !is_live_; }
3455 bool IsLive() const { return is_live_; }
3456
Calin Juravlea4f88312015-04-16 12:57:19 +01003457 // Returns the next equivalent phi (starting from the current one) or null if there is none.
3458 // An equivalent phi is a phi having the same dex register and type.
3459 // It assumes that phis with the same dex register are adjacent.
3460 HPhi* GetNextEquivalentPhiWithSameType() {
3461 HInstruction* next = GetNext();
3462 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
3463 if (next->GetType() == GetType()) {
3464 return next->AsPhi();
3465 }
3466 next = next->GetNext();
3467 }
3468 return nullptr;
3469 }
3470
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003471 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003472
David Brazdil1abb4192015-02-17 18:33:36 +00003473 protected:
3474 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
3475
3476 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
3477 inputs_.Put(index, input);
3478 }
3479
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003480 private:
David Brazdil1abb4192015-02-17 18:33:36 +00003481 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003482 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003483 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01003484 bool is_live_;
Calin Juravle10e244f2015-01-26 18:54:32 +00003485 bool can_be_null_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003486
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003487 DISALLOW_COPY_AND_ASSIGN(HPhi);
3488};
3489
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003490class HNullCheck : public HExpression<1> {
3491 public:
3492 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003493 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003494 SetRawInputAt(0, value);
3495 }
3496
Calin Juravle10e244f2015-01-26 18:54:32 +00003497 bool CanBeMoved() const OVERRIDE { return true; }
3498 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003499 UNUSED(other);
3500 return true;
3501 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003502
Calin Juravle10e244f2015-01-26 18:54:32 +00003503 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003504
Calin Juravle10e244f2015-01-26 18:54:32 +00003505 bool CanThrow() const OVERRIDE { return true; }
3506
3507 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01003508
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003509 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003510
3511 DECLARE_INSTRUCTION(NullCheck);
3512
3513 private:
3514 const uint32_t dex_pc_;
3515
3516 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
3517};
3518
3519class FieldInfo : public ValueObject {
3520 public:
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003521 FieldInfo(MemberOffset field_offset,
3522 Primitive::Type field_type,
3523 bool is_volatile,
3524 uint32_t index,
3525 const DexFile& dex_file)
3526 : field_offset_(field_offset),
3527 field_type_(field_type),
3528 is_volatile_(is_volatile),
3529 index_(index),
3530 dex_file_(dex_file) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003531
3532 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003533 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003534 uint32_t GetFieldIndex() const { return index_; }
3535 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00003536 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003537
3538 private:
3539 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01003540 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00003541 const bool is_volatile_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003542 uint32_t index_;
3543 const DexFile& dex_file_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003544};
3545
3546class HInstanceFieldGet : public HExpression<1> {
3547 public:
3548 HInstanceFieldGet(HInstruction* value,
3549 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003550 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003551 bool is_volatile,
3552 uint32_t field_idx,
3553 const DexFile& dex_file)
Aart Bik854a02b2015-07-14 16:07:00 -07003554 : HExpression(field_type, SideEffects::SideEffects::FieldReadOfType(field_type)),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003555 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003556 SetRawInputAt(0, value);
3557 }
3558
Calin Juravle10c9cbe2014-12-19 10:50:19 +00003559 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003560
3561 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3562 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
3563 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003564 }
3565
Calin Juravle641547a2015-04-21 22:08:51 +01003566 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3567 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00003568 }
3569
3570 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01003571 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
3572 }
3573
Calin Juravle52c48962014-12-16 17:02:57 +00003574 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003575 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003576 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003577 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003578
3579 DECLARE_INSTRUCTION(InstanceFieldGet);
3580
3581 private:
3582 const FieldInfo field_info_;
3583
3584 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
3585};
3586
3587class HInstanceFieldSet : public HTemplateInstruction<2> {
3588 public:
3589 HInstanceFieldSet(HInstruction* object,
3590 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01003591 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003592 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003593 bool is_volatile,
3594 uint32_t field_idx,
3595 const DexFile& dex_file)
Aart Bik854a02b2015-07-14 16:07:00 -07003596 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type)),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003597 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003598 value_can_be_null_(true) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003599 SetRawInputAt(0, object);
3600 SetRawInputAt(1, value);
3601 }
3602
Calin Juravle641547a2015-04-21 22:08:51 +01003603 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3604 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00003605 }
3606
Calin Juravle52c48962014-12-16 17:02:57 +00003607 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003608 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003609 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003610 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003611 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003612 bool GetValueCanBeNull() const { return value_can_be_null_; }
3613 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003614
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003615 DECLARE_INSTRUCTION(InstanceFieldSet);
3616
3617 private:
3618 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003619 bool value_can_be_null_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003620
3621 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
3622};
3623
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003624class HArrayGet : public HExpression<2> {
3625 public:
3626 HArrayGet(HInstruction* array, HInstruction* index, Primitive::Type type)
Aart Bik854a02b2015-07-14 16:07:00 -07003627 : HExpression(type, SideEffects::ArrayReadOfType(type)) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003628 SetRawInputAt(0, array);
3629 SetRawInputAt(1, index);
3630 }
3631
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003632 bool CanBeMoved() const OVERRIDE { return true; }
3633 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003634 UNUSED(other);
3635 return true;
3636 }
Calin Juravle641547a2015-04-21 22:08:51 +01003637 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3638 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00003639 // TODO: We can be smarter here.
3640 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
3641 // which generates the implicit null check. There are cases when these can be removed
3642 // to produce better code. If we ever add optimizations to do so we should allow an
3643 // implicit check here (as long as the address falls in the first page).
3644 return false;
3645 }
3646
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003647 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003648
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003649 HInstruction* GetArray() const { return InputAt(0); }
3650 HInstruction* GetIndex() const { return InputAt(1); }
3651
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003652 DECLARE_INSTRUCTION(ArrayGet);
3653
3654 private:
3655 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
3656};
3657
3658class HArraySet : public HTemplateInstruction<3> {
3659 public:
3660 HArraySet(HInstruction* array,
3661 HInstruction* index,
3662 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003663 Primitive::Type expected_component_type,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003664 uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07003665 : HTemplateInstruction(SideEffects::ArrayWriteOfType(expected_component_type)),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003666 dex_pc_(dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003667 expected_component_type_(expected_component_type),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003668 needs_type_check_(value->GetType() == Primitive::kPrimNot),
3669 value_can_be_null_(true) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003670 SetRawInputAt(0, array);
3671 SetRawInputAt(1, index);
3672 SetRawInputAt(2, value);
3673 }
3674
Calin Juravle77520bc2015-01-12 18:45:46 +00003675 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003676 // We currently always call a runtime method to catch array store
3677 // exceptions.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003678 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003679 }
3680
Mingyao Yang81014cb2015-06-02 03:16:27 -07003681 // Can throw ArrayStoreException.
3682 bool CanThrow() const OVERRIDE { return needs_type_check_; }
3683
Calin Juravle641547a2015-04-21 22:08:51 +01003684 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3685 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00003686 // TODO: Same as for ArrayGet.
3687 return false;
3688 }
3689
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003690 void ClearNeedsTypeCheck() {
3691 needs_type_check_ = false;
3692 }
3693
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003694 void ClearValueCanBeNull() {
3695 value_can_be_null_ = false;
3696 }
3697
3698 bool GetValueCanBeNull() const { return value_can_be_null_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003699 bool NeedsTypeCheck() const { return needs_type_check_; }
3700
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003701 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003702
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003703 HInstruction* GetArray() const { return InputAt(0); }
3704 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003705 HInstruction* GetValue() const { return InputAt(2); }
3706
3707 Primitive::Type GetComponentType() const {
3708 // The Dex format does not type floating point index operations. Since the
3709 // `expected_component_type_` is set during building and can therefore not
3710 // be correct, we also check what is the value type. If it is a floating
3711 // point type, we must use that type.
3712 Primitive::Type value_type = GetValue()->GetType();
3713 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
3714 ? value_type
3715 : expected_component_type_;
3716 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003717
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003718 DECLARE_INSTRUCTION(ArraySet);
3719
3720 private:
3721 const uint32_t dex_pc_;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003722 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003723 bool needs_type_check_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003724 bool value_can_be_null_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003725
3726 DISALLOW_COPY_AND_ASSIGN(HArraySet);
3727};
3728
3729class HArrayLength : public HExpression<1> {
3730 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003731 explicit HArrayLength(HInstruction* array)
3732 : HExpression(Primitive::kPrimInt, SideEffects::None()) {
3733 // Note that arrays do not change length, so the instruction does not
3734 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003735 SetRawInputAt(0, array);
3736 }
3737
Calin Juravle77520bc2015-01-12 18:45:46 +00003738 bool CanBeMoved() const OVERRIDE { return true; }
3739 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003740 UNUSED(other);
3741 return true;
3742 }
Calin Juravle641547a2015-04-21 22:08:51 +01003743 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3744 return obj == InputAt(0);
3745 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003746
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003747 DECLARE_INSTRUCTION(ArrayLength);
3748
3749 private:
3750 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
3751};
3752
3753class HBoundsCheck : public HExpression<2> {
3754 public:
3755 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003756 : HExpression(index->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003757 DCHECK(index->GetType() == Primitive::kPrimInt);
3758 SetRawInputAt(0, index);
3759 SetRawInputAt(1, length);
3760 }
3761
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003762 bool CanBeMoved() const OVERRIDE { return true; }
3763 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003764 UNUSED(other);
3765 return true;
3766 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003767
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003768 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003769
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003770 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01003771
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003772 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003773
3774 DECLARE_INSTRUCTION(BoundsCheck);
3775
3776 private:
3777 const uint32_t dex_pc_;
3778
3779 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
3780};
3781
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003782/**
3783 * Some DEX instructions are folded into multiple HInstructions that need
3784 * to stay live until the last HInstruction. This class
3785 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00003786 * HInstruction stays live. `index` represents the stack location index of the
3787 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003788 */
3789class HTemporary : public HTemplateInstruction<0> {
3790 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003791 explicit HTemporary(size_t index) : HTemplateInstruction(SideEffects::None()), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003792
3793 size_t GetIndex() const { return index_; }
3794
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00003795 Primitive::Type GetType() const OVERRIDE {
3796 // The previous instruction is the one that will be stored in the temporary location.
3797 DCHECK(GetPrevious() != nullptr);
3798 return GetPrevious()->GetType();
3799 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00003800
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003801 DECLARE_INSTRUCTION(Temporary);
3802
3803 private:
3804 const size_t index_;
3805
3806 DISALLOW_COPY_AND_ASSIGN(HTemporary);
3807};
3808
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003809class HSuspendCheck : public HTemplateInstruction<0> {
3810 public:
3811 explicit HSuspendCheck(uint32_t dex_pc)
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01003812 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003813
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003814 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003815 return true;
3816 }
3817
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003818 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01003819 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
3820 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003821
3822 DECLARE_INSTRUCTION(SuspendCheck);
3823
3824 private:
3825 const uint32_t dex_pc_;
3826
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01003827 // Only used for code generation, in order to share the same slow path between back edges
3828 // of a same loop.
3829 SlowPathCode* slow_path_;
3830
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003831 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
3832};
3833
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003834/**
3835 * Instruction to load a Class object.
3836 */
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003837class HLoadClass : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003838 public:
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003839 HLoadClass(HCurrentMethod* current_method,
3840 uint16_t type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003841 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003842 bool is_referrers_class,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003843 uint32_t dex_pc)
3844 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3845 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003846 dex_file_(dex_file),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003847 is_referrers_class_(is_referrers_class),
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003848 dex_pc_(dex_pc),
Calin Juravleb1498f62015-02-16 13:13:29 +00003849 generate_clinit_check_(false),
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003850 loaded_class_rti_(ReferenceTypeInfo::CreateTop(/* is_exact */ false)) {
3851 SetRawInputAt(0, current_method);
3852 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003853
3854 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003855
3856 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3857 return other->AsLoadClass()->type_index_ == type_index_;
3858 }
3859
3860 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
3861
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003862 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003863 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003864 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01003865 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003866
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003867 bool NeedsEnvironment() const OVERRIDE {
3868 // Will call runtime and load the class if the class is not loaded yet.
3869 // TODO: finer grain decision.
3870 return !is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003871 }
3872
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003873 bool MustGenerateClinitCheck() const {
3874 return generate_clinit_check_;
3875 }
3876
Calin Juravle0ba218d2015-05-19 18:46:01 +01003877 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
3878 generate_clinit_check_ = generate_clinit_check;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003879 }
3880
3881 bool CanCallRuntime() const {
3882 return MustGenerateClinitCheck() || !is_referrers_class_;
3883 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003884
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003885 bool CanThrow() const OVERRIDE {
3886 // May call runtime and and therefore can throw.
3887 // TODO: finer grain decision.
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01003888 return CanCallRuntime();
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003889 }
3890
Calin Juravleacf735c2015-02-12 15:25:22 +00003891 ReferenceTypeInfo GetLoadedClassRTI() {
3892 return loaded_class_rti_;
3893 }
3894
3895 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
3896 // Make sure we only set exact types (the loaded class should never be merged).
3897 DCHECK(rti.IsExact());
3898 loaded_class_rti_ = rti;
3899 }
3900
3901 bool IsResolved() {
3902 return loaded_class_rti_.IsExact();
3903 }
3904
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003905 const DexFile& GetDexFile() { return dex_file_; }
3906
Nicolas Geoffray9437b782015-03-25 10:08:51 +00003907 bool NeedsDexCache() const OVERRIDE { return !is_referrers_class_; }
3908
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003909 DECLARE_INSTRUCTION(LoadClass);
3910
3911 private:
3912 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003913 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003914 const bool is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003915 const uint32_t dex_pc_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003916 // Whether this instruction must generate the initialization check.
3917 // Used for code generation.
3918 bool generate_clinit_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003919
Calin Juravleacf735c2015-02-12 15:25:22 +00003920 ReferenceTypeInfo loaded_class_rti_;
3921
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003922 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
3923};
3924
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01003925class HLoadString : public HExpression<1> {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003926 public:
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01003927 HLoadString(HCurrentMethod* current_method, uint32_t string_index, uint32_t dex_pc)
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003928 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3929 string_index_(string_index),
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01003930 dex_pc_(dex_pc) {
3931 SetRawInputAt(0, current_method);
3932 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003933
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003934 bool CanBeMoved() const OVERRIDE { return true; }
3935
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003936 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3937 return other->AsLoadString()->string_index_ == string_index_;
3938 }
3939
3940 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
3941
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003942 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003943 uint32_t GetStringIndex() const { return string_index_; }
3944
3945 // TODO: Can we deopt or debug when we resolve a string?
3946 bool NeedsEnvironment() const OVERRIDE { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00003947 bool NeedsDexCache() const OVERRIDE { return true; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003948
3949 DECLARE_INSTRUCTION(LoadString);
3950
3951 private:
3952 const uint32_t string_index_;
3953 const uint32_t dex_pc_;
3954
3955 DISALLOW_COPY_AND_ASSIGN(HLoadString);
3956};
3957
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003958/**
3959 * Performs an initialization check on its Class object input.
3960 */
3961class HClinitCheck : public HExpression<1> {
3962 public:
3963 explicit HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07003964 : HExpression(
3965 Primitive::kPrimNot,
3966 SideEffects::All()), // assume write/read on all fields/arrays
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003967 dex_pc_(dex_pc) {
3968 SetRawInputAt(0, constant);
3969 }
3970
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003971 bool CanBeMoved() const OVERRIDE { return true; }
3972 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3973 UNUSED(other);
3974 return true;
3975 }
3976
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003977 bool NeedsEnvironment() const OVERRIDE {
3978 // May call runtime to initialize the class.
3979 return true;
3980 }
3981
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003982 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003983
3984 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
3985
3986 DECLARE_INSTRUCTION(ClinitCheck);
3987
3988 private:
3989 const uint32_t dex_pc_;
3990
3991 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
3992};
3993
3994class HStaticFieldGet : public HExpression<1> {
3995 public:
3996 HStaticFieldGet(HInstruction* cls,
3997 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003998 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003999 bool is_volatile,
4000 uint32_t field_idx,
4001 const DexFile& dex_file)
Aart Bik854a02b2015-07-14 16:07:00 -07004002 : HExpression(field_type, SideEffects::SideEffects::FieldReadOfType(field_type)),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004003 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004004 SetRawInputAt(0, cls);
4005 }
4006
Calin Juravle52c48962014-12-16 17:02:57 +00004007
Calin Juravle10c9cbe2014-12-19 10:50:19 +00004008 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004009
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004010 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00004011 HStaticFieldGet* other_get = other->AsStaticFieldGet();
4012 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004013 }
4014
4015 size_t ComputeHashCode() const OVERRIDE {
4016 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
4017 }
4018
Calin Juravle52c48962014-12-16 17:02:57 +00004019 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004020 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
4021 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004022 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004023
4024 DECLARE_INSTRUCTION(StaticFieldGet);
4025
4026 private:
4027 const FieldInfo field_info_;
4028
4029 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
4030};
4031
4032class HStaticFieldSet : public HTemplateInstruction<2> {
4033 public:
4034 HStaticFieldSet(HInstruction* cls,
4035 HInstruction* value,
4036 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004037 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004038 bool is_volatile,
4039 uint32_t field_idx,
4040 const DexFile& dex_file)
Aart Bik854a02b2015-07-14 16:07:00 -07004041 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type)),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004042 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004043 value_can_be_null_(true) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004044 SetRawInputAt(0, cls);
4045 SetRawInputAt(1, value);
4046 }
4047
Calin Juravle52c48962014-12-16 17:02:57 +00004048 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004049 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
4050 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004051 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004052
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004053 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004054 bool GetValueCanBeNull() const { return value_can_be_null_; }
4055 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004056
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004057 DECLARE_INSTRUCTION(StaticFieldSet);
4058
4059 private:
4060 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004061 bool value_can_be_null_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004062
4063 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
4064};
4065
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004066// Implement the move-exception DEX instruction.
4067class HLoadException : public HExpression<0> {
4068 public:
4069 HLoadException() : HExpression(Primitive::kPrimNot, SideEffects::None()) {}
4070
4071 DECLARE_INSTRUCTION(LoadException);
4072
4073 private:
4074 DISALLOW_COPY_AND_ASSIGN(HLoadException);
4075};
4076
4077class HThrow : public HTemplateInstruction<1> {
4078 public:
4079 HThrow(HInstruction* exception, uint32_t dex_pc)
4080 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {
4081 SetRawInputAt(0, exception);
4082 }
4083
4084 bool IsControlFlow() const OVERRIDE { return true; }
4085
4086 bool NeedsEnvironment() const OVERRIDE { return true; }
4087
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004088 bool CanThrow() const OVERRIDE { return true; }
4089
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004090 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004091
4092 DECLARE_INSTRUCTION(Throw);
4093
4094 private:
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004095 const uint32_t dex_pc_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004096
4097 DISALLOW_COPY_AND_ASSIGN(HThrow);
4098};
4099
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004100class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004101 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004102 HInstanceOf(HInstruction* object,
4103 HLoadClass* constant,
4104 bool class_is_final,
4105 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004106 : HExpression(Primitive::kPrimBoolean, SideEffects::None()),
4107 class_is_final_(class_is_final),
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004108 must_do_null_check_(true),
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004109 dex_pc_(dex_pc) {
4110 SetRawInputAt(0, object);
4111 SetRawInputAt(1, constant);
4112 }
4113
4114 bool CanBeMoved() const OVERRIDE { return true; }
4115
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004116 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004117 return true;
4118 }
4119
4120 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004121 return false;
4122 }
4123
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004124 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004125
4126 bool IsClassFinal() const { return class_is_final_; }
4127
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004128 // Used only in code generation.
4129 bool MustDoNullCheck() const { return must_do_null_check_; }
4130 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
4131
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004132 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004133
4134 private:
4135 const bool class_is_final_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004136 bool must_do_null_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004137 const uint32_t dex_pc_;
4138
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004139 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
4140};
4141
Calin Juravleb1498f62015-02-16 13:13:29 +00004142class HBoundType : public HExpression<1> {
4143 public:
4144 HBoundType(HInstruction* input, ReferenceTypeInfo bound_type)
4145 : HExpression(Primitive::kPrimNot, SideEffects::None()),
4146 bound_type_(bound_type) {
Calin Juravle61d544b2015-02-23 16:46:57 +00004147 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00004148 SetRawInputAt(0, input);
4149 }
4150
4151 const ReferenceTypeInfo& GetBoundType() const { return bound_type_; }
4152
4153 bool CanBeNull() const OVERRIDE {
4154 // `null instanceof ClassX` always return false so we can't be null.
4155 return false;
4156 }
4157
4158 DECLARE_INSTRUCTION(BoundType);
4159
4160 private:
4161 // Encodes the most upper class that this instruction can have. In other words
4162 // it is always the case that GetBoundType().IsSupertypeOf(GetReferenceType()).
4163 // It is used to bound the type in cases like `if (x instanceof ClassX) {}`
4164 const ReferenceTypeInfo bound_type_;
4165
4166 DISALLOW_COPY_AND_ASSIGN(HBoundType);
4167};
4168
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004169class HCheckCast : public HTemplateInstruction<2> {
4170 public:
4171 HCheckCast(HInstruction* object,
4172 HLoadClass* constant,
4173 bool class_is_final,
4174 uint32_t dex_pc)
4175 : HTemplateInstruction(SideEffects::None()),
4176 class_is_final_(class_is_final),
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004177 must_do_null_check_(true),
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004178 dex_pc_(dex_pc) {
4179 SetRawInputAt(0, object);
4180 SetRawInputAt(1, constant);
4181 }
4182
4183 bool CanBeMoved() const OVERRIDE { return true; }
4184
4185 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
4186 return true;
4187 }
4188
4189 bool NeedsEnvironment() const OVERRIDE {
4190 // Instruction may throw a CheckCastError.
4191 return true;
4192 }
4193
4194 bool CanThrow() const OVERRIDE { return true; }
4195
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004196 bool MustDoNullCheck() const { return must_do_null_check_; }
4197 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
4198
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004199 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004200
4201 bool IsClassFinal() const { return class_is_final_; }
4202
4203 DECLARE_INSTRUCTION(CheckCast);
4204
4205 private:
4206 const bool class_is_final_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004207 bool must_do_null_check_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004208 const uint32_t dex_pc_;
4209
4210 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004211};
4212
Calin Juravle27df7582015-04-17 19:12:31 +01004213class HMemoryBarrier : public HTemplateInstruction<0> {
4214 public:
4215 explicit HMemoryBarrier(MemBarrierKind barrier_kind)
4216 : HTemplateInstruction(SideEffects::None()),
4217 barrier_kind_(barrier_kind) {}
4218
4219 MemBarrierKind GetBarrierKind() { return barrier_kind_; }
4220
4221 DECLARE_INSTRUCTION(MemoryBarrier);
4222
4223 private:
4224 const MemBarrierKind barrier_kind_;
4225
4226 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
4227};
4228
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004229class HMonitorOperation : public HTemplateInstruction<1> {
4230 public:
4231 enum OperationKind {
4232 kEnter,
4233 kExit,
4234 };
4235
4236 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07004237 : HTemplateInstruction(SideEffects::All()), // assume write/read on all fields/arrays
4238 kind_(kind), dex_pc_(dex_pc) {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004239 SetRawInputAt(0, object);
4240 }
4241
4242 // Instruction may throw a Java exception, so we need an environment.
David Brazdilbff75032015-07-08 17:26:51 +00004243 bool NeedsEnvironment() const OVERRIDE { return CanThrow(); }
4244
4245 bool CanThrow() const OVERRIDE {
4246 // Verifier guarantees that monitor-exit cannot throw.
4247 // This is important because it allows the HGraphBuilder to remove
4248 // a dead throw-catch loop generated for `synchronized` blocks/methods.
4249 return IsEnter();
4250 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004251
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004252 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004253
4254 bool IsEnter() const { return kind_ == kEnter; }
4255
4256 DECLARE_INSTRUCTION(MonitorOperation);
4257
Calin Juravle52c48962014-12-16 17:02:57 +00004258 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004259 const OperationKind kind_;
4260 const uint32_t dex_pc_;
4261
4262 private:
4263 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
4264};
4265
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01004266/**
4267 * A HInstruction used as a marker for the replacement of new + <init>
4268 * of a String to a call to a StringFactory. Only baseline will see
4269 * the node at code generation, where it will be be treated as null.
4270 * When compiling non-baseline, `HFakeString` instructions are being removed
4271 * in the instruction simplifier.
4272 */
4273class HFakeString : public HTemplateInstruction<0> {
4274 public:
4275 HFakeString() : HTemplateInstruction(SideEffects::None()) {}
4276
4277 Primitive::Type GetType() const OVERRIDE { return Primitive::kPrimNot; }
4278
4279 DECLARE_INSTRUCTION(FakeString);
4280
4281 private:
4282 DISALLOW_COPY_AND_ASSIGN(HFakeString);
4283};
4284
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004285class MoveOperands : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004286 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01004287 MoveOperands(Location source,
4288 Location destination,
4289 Primitive::Type type,
4290 HInstruction* instruction)
4291 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004292
4293 Location GetSource() const { return source_; }
4294 Location GetDestination() const { return destination_; }
4295
4296 void SetSource(Location value) { source_ = value; }
4297 void SetDestination(Location value) { destination_ = value; }
4298
4299 // The parallel move resolver marks moves as "in-progress" by clearing the
4300 // destination (but not the source).
4301 Location MarkPending() {
4302 DCHECK(!IsPending());
4303 Location dest = destination_;
4304 destination_ = Location::NoLocation();
4305 return dest;
4306 }
4307
4308 void ClearPending(Location dest) {
4309 DCHECK(IsPending());
4310 destination_ = dest;
4311 }
4312
4313 bool IsPending() const {
4314 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
4315 return destination_.IsInvalid() && !source_.IsInvalid();
4316 }
4317
4318 // True if this blocks a move from the given location.
4319 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08004320 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004321 }
4322
4323 // A move is redundant if it's been eliminated, if its source and
4324 // destination are the same, or if its destination is unneeded.
4325 bool IsRedundant() const {
4326 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
4327 }
4328
4329 // We clear both operands to indicate move that's been eliminated.
4330 void Eliminate() {
4331 source_ = destination_ = Location::NoLocation();
4332 }
4333
4334 bool IsEliminated() const {
4335 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
4336 return source_.IsInvalid();
4337 }
4338
Alexey Frunze4dda3372015-06-01 18:31:49 -07004339 Primitive::Type GetType() const { return type_; }
4340
Nicolas Geoffray90218252015-04-15 11:56:51 +01004341 bool Is64BitMove() const {
4342 return Primitive::Is64BitType(type_);
4343 }
4344
Nicolas Geoffray740475d2014-09-29 10:33:25 +01004345 HInstruction* GetInstruction() const { return instruction_; }
4346
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004347 private:
4348 Location source_;
4349 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01004350 // The type this move is for.
4351 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01004352 // The instruction this move is assocatied with. Null when this move is
4353 // for moving an input in the expected locations of user (including a phi user).
4354 // This is only used in debug mode, to ensure we do not connect interval siblings
4355 // in the same parallel move.
4356 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004357};
4358
4359static constexpr size_t kDefaultNumberOfMoves = 4;
4360
4361class HParallelMove : public HTemplateInstruction<0> {
4362 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004363 explicit HParallelMove(ArenaAllocator* arena)
4364 : HTemplateInstruction(SideEffects::None()), moves_(arena, kDefaultNumberOfMoves) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004365
Nicolas Geoffray90218252015-04-15 11:56:51 +01004366 void AddMove(Location source,
4367 Location destination,
4368 Primitive::Type type,
4369 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004370 DCHECK(source.IsValid());
4371 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004372 if (kIsDebugBuild) {
4373 if (instruction != nullptr) {
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00004374 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004375 if (moves_.Get(i).GetInstruction() == instruction) {
4376 // Special case the situation where the move is for the spill slot
4377 // of the instruction.
4378 if ((GetPrevious() == instruction)
4379 || ((GetPrevious() == nullptr)
4380 && instruction->IsPhi()
4381 && instruction->GetBlock() == GetBlock())) {
4382 DCHECK_NE(destination.GetKind(), moves_.Get(i).GetDestination().GetKind())
4383 << "Doing parallel moves for the same instruction.";
4384 } else {
4385 DCHECK(false) << "Doing parallel moves for the same instruction.";
4386 }
4387 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00004388 }
4389 }
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004390 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Zheng Xuad4450e2015-04-17 18:48:56 +08004391 DCHECK(!destination.OverlapsWith(moves_.Get(i).GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01004392 << "Overlapped destination for two moves in a parallel move: "
4393 << moves_.Get(i).GetSource() << " ==> " << moves_.Get(i).GetDestination() << " and "
4394 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004395 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01004396 }
Nicolas Geoffray90218252015-04-15 11:56:51 +01004397 moves_.Add(MoveOperands(source, destination, type, instruction));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004398 }
4399
4400 MoveOperands* MoveOperandsAt(size_t index) const {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004401 return moves_.GetRawStorage() + index;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004402 }
4403
4404 size_t NumMoves() const { return moves_.Size(); }
4405
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004406 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004407
4408 private:
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004409 GrowableArray<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004410
4411 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
4412};
4413
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004414class HGraphVisitor : public ValueObject {
4415 public:
Dave Allison20dfc792014-06-16 20:44:29 -07004416 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
4417 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004418
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004419 virtual void VisitInstruction(HInstruction* instruction) { UNUSED(instruction); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004420 virtual void VisitBasicBlock(HBasicBlock* block);
4421
Roland Levillain633021e2014-10-01 14:12:25 +01004422 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004423 void VisitInsertionOrder();
4424
Roland Levillain633021e2014-10-01 14:12:25 +01004425 // Visit the graph following dominator tree reverse post-order.
4426 void VisitReversePostOrder();
4427
Nicolas Geoffray787c3072014-03-17 10:20:19 +00004428 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00004429
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004430 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004431#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004432 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
4433
4434 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
4435
4436#undef DECLARE_VISIT_INSTRUCTION
4437
4438 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07004439 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004440
4441 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
4442};
4443
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004444class HGraphDelegateVisitor : public HGraphVisitor {
4445 public:
4446 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
4447 virtual ~HGraphDelegateVisitor() {}
4448
4449 // Visit functions that delegate to to super class.
4450#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004451 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004452
4453 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
4454
4455#undef DECLARE_VISIT_INSTRUCTION
4456
4457 private:
4458 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
4459};
4460
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004461class HInsertionOrderIterator : public ValueObject {
4462 public:
4463 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
4464
4465 bool Done() const { return index_ == graph_.GetBlocks().Size(); }
4466 HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); }
4467 void Advance() { ++index_; }
4468
4469 private:
4470 const HGraph& graph_;
4471 size_t index_;
4472
4473 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
4474};
4475
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004476class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004477 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00004478 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
4479 // Check that reverse post order of the graph has been built.
4480 DCHECK(!graph.GetReversePostOrder().IsEmpty());
4481 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004482
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004483 bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); }
4484 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004485 void Advance() { ++index_; }
4486
4487 private:
4488 const HGraph& graph_;
4489 size_t index_;
4490
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004491 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004492};
4493
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004494class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004495 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004496 explicit HPostOrderIterator(const HGraph& graph)
David Brazdil10f56cb2015-03-24 18:49:14 +00004497 : graph_(graph), index_(graph_.GetReversePostOrder().Size()) {
4498 // Check that reverse post order of the graph has been built.
4499 DCHECK(!graph.GetReversePostOrder().IsEmpty());
4500 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004501
4502 bool Done() const { return index_ == 0; }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004503 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004504 void Advance() { --index_; }
4505
4506 private:
4507 const HGraph& graph_;
4508 size_t index_;
4509
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004510 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004511};
4512
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01004513class HLinearPostOrderIterator : public ValueObject {
4514 public:
4515 explicit HLinearPostOrderIterator(const HGraph& graph)
4516 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().Size()) {}
4517
4518 bool Done() const { return index_ == 0; }
4519
4520 HBasicBlock* Current() const { return order_.Get(index_ -1); }
4521
4522 void Advance() {
4523 --index_;
4524 DCHECK_GE(index_, 0U);
4525 }
4526
4527 private:
4528 const GrowableArray<HBasicBlock*>& order_;
4529 size_t index_;
4530
4531 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
4532};
4533
4534class HLinearOrderIterator : public ValueObject {
4535 public:
4536 explicit HLinearOrderIterator(const HGraph& graph)
4537 : order_(graph.GetLinearOrder()), index_(0) {}
4538
4539 bool Done() const { return index_ == order_.Size(); }
4540 HBasicBlock* Current() const { return order_.Get(index_); }
4541 void Advance() { ++index_; }
4542
4543 private:
4544 const GrowableArray<HBasicBlock*>& order_;
4545 size_t index_;
4546
4547 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
4548};
4549
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004550// Iterator over the blocks that art part of the loop. Includes blocks part
4551// of an inner loop. The order in which the blocks are iterated is on their
4552// block id.
4553class HBlocksInLoopIterator : public ValueObject {
4554 public:
4555 explicit HBlocksInLoopIterator(const HLoopInformation& info)
4556 : blocks_in_loop_(info.GetBlocks()),
4557 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
4558 index_(0) {
4559 if (!blocks_in_loop_.IsBitSet(index_)) {
4560 Advance();
4561 }
4562 }
4563
4564 bool Done() const { return index_ == blocks_.Size(); }
4565 HBasicBlock* Current() const { return blocks_.Get(index_); }
4566 void Advance() {
4567 ++index_;
4568 for (size_t e = blocks_.Size(); index_ < e; ++index_) {
4569 if (blocks_in_loop_.IsBitSet(index_)) {
4570 break;
4571 }
4572 }
4573 }
4574
4575 private:
4576 const BitVector& blocks_in_loop_;
4577 const GrowableArray<HBasicBlock*>& blocks_;
4578 size_t index_;
4579
4580 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
4581};
4582
Mingyao Yang3584bce2015-05-19 16:01:59 -07004583// Iterator over the blocks that art part of the loop. Includes blocks part
4584// of an inner loop. The order in which the blocks are iterated is reverse
4585// post order.
4586class HBlocksInLoopReversePostOrderIterator : public ValueObject {
4587 public:
4588 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
4589 : blocks_in_loop_(info.GetBlocks()),
4590 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
4591 index_(0) {
4592 if (!blocks_in_loop_.IsBitSet(blocks_.Get(index_)->GetBlockId())) {
4593 Advance();
4594 }
4595 }
4596
4597 bool Done() const { return index_ == blocks_.Size(); }
4598 HBasicBlock* Current() const { return blocks_.Get(index_); }
4599 void Advance() {
4600 ++index_;
4601 for (size_t e = blocks_.Size(); index_ < e; ++index_) {
4602 if (blocks_in_loop_.IsBitSet(blocks_.Get(index_)->GetBlockId())) {
4603 break;
4604 }
4605 }
4606 }
4607
4608 private:
4609 const BitVector& blocks_in_loop_;
4610 const GrowableArray<HBasicBlock*>& blocks_;
4611 size_t index_;
4612
4613 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
4614};
4615
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00004616inline int64_t Int64FromConstant(HConstant* constant) {
4617 DCHECK(constant->IsIntConstant() || constant->IsLongConstant());
4618 return constant->IsIntConstant() ? constant->AsIntConstant()->GetValue()
4619 : constant->AsLongConstant()->GetValue();
4620}
4621
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004622} // namespace art
4623
4624#endif // ART_COMPILER_OPTIMIZING_NODES_H_