blob: 5c102453f554a1735510f4a3cd78e7810b8df874 [file] [log] [blame]
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ART_COMPILER_OPTIMIZING_NODES_H_
18#define ART_COMPILER_OPTIMIZING_NODES_H_
19
David Brazdil8d5b8b22015-03-24 10:51:52 +000020#include "base/arena_containers.h"
Mathieu Chartierb666f482015-02-18 14:33:14 -080021#include "base/arena_object.h"
Calin Juravle27df7582015-04-17 19:12:31 +010022#include "dex/compiler_enums.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000023#include "entrypoints/quick/quick_entrypoints_enum.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000024#include "handle.h"
25#include "handle_scope.h"
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000026#include "invoke_type.h"
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +010027#include "locations.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000028#include "mirror/class.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010029#include "offsets.h"
Ian Rogerse63db272014-07-15 15:36:11 -070030#include "primitive.h"
Nicolas Geoffray0e336432014-02-26 18:24:38 +000031#include "utils/arena_bit_vector.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000032#include "utils/growable_array.h"
33
34namespace art {
35
David Brazdil1abb4192015-02-17 18:33:36 +000036class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000037class HBasicBlock;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010038class HCurrentMethod;
David Brazdil8d5b8b22015-03-24 10:51:52 +000039class HDoubleConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010040class HEnvironment;
David Brazdil8d5b8b22015-03-24 10:51:52 +000041class HFloatConstant;
David Brazdilfc6a86a2015-06-26 10:33:45 +000042class HGraphBuilder;
David Brazdil8d5b8b22015-03-24 10:51:52 +000043class HGraphVisitor;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000044class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000045class HIntConstant;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000046class HInvoke;
David Brazdil8d5b8b22015-03-24 10:51:52 +000047class HLongConstant;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +000048class HNullConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010049class HPhi;
Nicolas Geoffray3c049742014-09-24 18:10:46 +010050class HSuspendCheck;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010051class LiveInterval;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000052class LocationSummary;
Nicolas Geoffraydb216f42015-05-05 17:02:20 +010053class SlowPathCode;
David Brazdil8d5b8b22015-03-24 10:51:52 +000054class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000055
56static const int kDefaultNumberOfBlocks = 8;
57static const int kDefaultNumberOfSuccessors = 2;
58static const int kDefaultNumberOfPredecessors = 2;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010059static const int kDefaultNumberOfDominatedBlocks = 1;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000060static const int kDefaultNumberOfBackEdges = 1;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000061
Calin Juravle9aec02f2014-11-18 23:06:35 +000062static constexpr uint32_t kMaxIntShiftValue = 0x1f;
63static constexpr uint64_t kMaxLongShiftValue = 0x3f;
64
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010065static constexpr uint32_t kUnknownFieldIndex = static_cast<uint32_t>(-1);
66
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +010067static constexpr InvokeType kInvalidInvokeType = static_cast<InvokeType>(-1);
68
Dave Allison20dfc792014-06-16 20:44:29 -070069enum IfCondition {
70 kCondEQ,
71 kCondNE,
72 kCondLT,
73 kCondLE,
74 kCondGT,
75 kCondGE,
76};
77
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010078class HInstructionList {
79 public:
80 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
81
82 void AddInstruction(HInstruction* instruction);
83 void RemoveInstruction(HInstruction* instruction);
84
David Brazdilc3d743f2015-04-22 13:40:50 +010085 // Insert `instruction` before/after an existing instruction `cursor`.
86 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
87 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
88
Roland Levillain6b469232014-09-25 10:10:38 +010089 // Return true if this list contains `instruction`.
90 bool Contains(HInstruction* instruction) const;
91
Roland Levillainccc07a92014-09-16 14:48:16 +010092 // Return true if `instruction1` is found before `instruction2` in
93 // this instruction list and false otherwise. Abort if none
94 // of these instructions is found.
95 bool FoundBefore(const HInstruction* instruction1,
96 const HInstruction* instruction2) const;
97
Nicolas Geoffray276d9da2015-02-02 18:24:11 +000098 bool IsEmpty() const { return first_instruction_ == nullptr; }
99 void Clear() { first_instruction_ = last_instruction_ = nullptr; }
100
101 // Update the block of all instructions to be `block`.
102 void SetBlockOfInstructions(HBasicBlock* block) const;
103
104 void AddAfter(HInstruction* cursor, const HInstructionList& instruction_list);
105 void Add(const HInstructionList& instruction_list);
106
David Brazdil2d7352b2015-04-20 14:52:42 +0100107 // Return the number of instructions in the list. This is an expensive operation.
108 size_t CountSize() const;
109
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100110 private:
111 HInstruction* first_instruction_;
112 HInstruction* last_instruction_;
113
114 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000115 friend class HGraph;
116 friend class HInstruction;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100117 friend class HInstructionIterator;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100118 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100119
120 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
121};
122
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000123// Control-flow graph of a method. Contains a list of basic blocks.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700124class HGraph : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000125 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100126 HGraph(ArenaAllocator* arena,
127 const DexFile& dex_file,
128 uint32_t method_idx,
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100129 bool should_generate_constructor_barrier,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700130 InstructionSet instruction_set,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100131 InvokeType invoke_type = kInvalidInvokeType,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100132 bool debuggable = false,
133 int start_instruction_id = 0)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000134 : arena_(arena),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000135 blocks_(arena, kDefaultNumberOfBlocks),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100136 reverse_post_order_(arena, kDefaultNumberOfBlocks),
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100137 linear_order_(arena, kDefaultNumberOfBlocks),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700138 entry_block_(nullptr),
139 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100140 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100141 number_of_vregs_(0),
142 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000143 temporaries_vreg_slots_(0),
Mark Mendell1152c922015-04-24 17:06:35 -0400144 has_bounds_checks_(false),
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000145 debuggable_(debuggable),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000146 current_instruction_id_(start_instruction_id),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100147 dex_file_(dex_file),
148 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100149 invoke_type_(invoke_type),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100150 in_ssa_form_(false),
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100151 should_generate_constructor_barrier_(should_generate_constructor_barrier),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700152 instruction_set_(instruction_set),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000153 cached_null_constant_(nullptr),
154 cached_int_constants_(std::less<int32_t>(), arena->Adapter()),
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000155 cached_float_constants_(std::less<int32_t>(), arena->Adapter()),
156 cached_long_constants_(std::less<int64_t>(), arena->Adapter()),
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100157 cached_double_constants_(std::less<int64_t>(), arena->Adapter()),
158 cached_current_method_(nullptr) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000159
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000160 ArenaAllocator* GetArena() const { return arena_; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100161 const GrowableArray<HBasicBlock*>& GetBlocks() const { return blocks_; }
Roland Levillain93445682014-10-06 19:24:02 +0100162 HBasicBlock* GetBlock(size_t id) const { return blocks_.Get(id); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000163
David Brazdil69ba7b72015-06-23 18:27:30 +0100164 bool IsInSsaForm() const { return in_ssa_form_; }
165
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000166 HBasicBlock* GetEntryBlock() const { return entry_block_; }
167 HBasicBlock* GetExitBlock() const { return exit_block_; }
David Brazdilc7af85d2015-05-26 12:05:55 +0100168 bool HasExitBlock() const { return exit_block_ != nullptr; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000169
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000170 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
171 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000172
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000173 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100174
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000175 // Try building the SSA form of this graph, with dominance computation and loop
176 // recognition. Returns whether it was successful in doing all these steps.
177 bool TryBuildingSsa() {
178 BuildDominatorTree();
Nicolas Geoffrayd3350832015-03-12 11:16:23 +0000179 // The SSA builder requires loops to all be natural. Specifically, the dead phi
180 // elimination phase checks the consistency of the graph when doing a post-order
181 // visit for eliminating dead phis: a dead phi can only have loop header phi
182 // users remaining when being visited.
183 if (!AnalyzeNaturalLoops()) return false;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000184 TransformToSsa();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100185 in_ssa_form_ = true;
Nicolas Geoffrayd3350832015-03-12 11:16:23 +0000186 return true;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000187 }
188
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100189 void ComputeDominanceInformation();
190 void ClearDominanceInformation();
191
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000192 void BuildDominatorTree();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000193 void TransformToSsa();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100194 void SimplifyCFG();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000195
Nicolas Geoffrayf5370122014-12-02 11:51:19 +0000196 // Analyze all natural loops in this graph. Returns false if one
197 // loop is not natural, that is the header does not dominate the
198 // back edge.
199 bool AnalyzeNaturalLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100200
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000201 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
202 void InlineInto(HGraph* outer_graph, HInvoke* invoke);
203
Mingyao Yang3584bce2015-05-19 16:01:59 -0700204 // Need to add a couple of blocks to test if the loop body is entered and
205 // put deoptimization instructions, etc.
206 void TransformLoopHeaderForBCE(HBasicBlock* header);
207
David Brazdil2d7352b2015-04-20 14:52:42 +0100208 // Removes `block` from the graph.
209 void DeleteDeadBlock(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000210
David Brazdilfc6a86a2015-06-26 10:33:45 +0000211 // Splits the edge between `block` and `successor` while preserving the
212 // indices in the predecessor/successor lists. If there are multiple edges
213 // between the blocks, the lowest indices are used.
214 // Returns the new block which is empty and has the same dex pc as `successor`.
215 HBasicBlock* SplitEdge(HBasicBlock* block, HBasicBlock* successor);
216
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100217 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
218 void SimplifyLoop(HBasicBlock* header);
219
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000220 int32_t GetNextInstructionId() {
221 DCHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000222 return current_instruction_id_++;
223 }
224
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000225 int32_t GetCurrentInstructionId() const {
226 return current_instruction_id_;
227 }
228
229 void SetCurrentInstructionId(int32_t id) {
230 current_instruction_id_ = id;
231 }
232
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100233 uint16_t GetMaximumNumberOfOutVRegs() const {
234 return maximum_number_of_out_vregs_;
235 }
236
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000237 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
238 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100239 }
240
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100241 void UpdateMaximumNumberOfOutVRegs(uint16_t other_value) {
242 maximum_number_of_out_vregs_ = std::max(maximum_number_of_out_vregs_, other_value);
243 }
244
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000245 void UpdateTemporariesVRegSlots(size_t slots) {
246 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100247 }
248
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000249 size_t GetTemporariesVRegSlots() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100250 DCHECK(!in_ssa_form_);
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000251 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100252 }
253
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100254 void SetNumberOfVRegs(uint16_t number_of_vregs) {
255 number_of_vregs_ = number_of_vregs;
256 }
257
258 uint16_t GetNumberOfVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100259 DCHECK(!in_ssa_form_);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100260 return number_of_vregs_;
261 }
262
263 void SetNumberOfInVRegs(uint16_t value) {
264 number_of_in_vregs_ = value;
265 }
266
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100267 uint16_t GetNumberOfLocalVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100268 DCHECK(!in_ssa_form_);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100269 return number_of_vregs_ - number_of_in_vregs_;
270 }
271
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100272 const GrowableArray<HBasicBlock*>& GetReversePostOrder() const {
273 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100274 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100275
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100276 const GrowableArray<HBasicBlock*>& GetLinearOrder() const {
277 return linear_order_;
278 }
279
Mark Mendell1152c922015-04-24 17:06:35 -0400280 bool HasBoundsChecks() const {
281 return has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800282 }
283
Mark Mendell1152c922015-04-24 17:06:35 -0400284 void SetHasBoundsChecks(bool value) {
285 has_bounds_checks_ = value;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800286 }
287
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100288 bool ShouldGenerateConstructorBarrier() const {
289 return should_generate_constructor_barrier_;
290 }
291
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000292 bool IsDebuggable() const { return debuggable_; }
293
David Brazdil8d5b8b22015-03-24 10:51:52 +0000294 // Returns a constant of the given type and value. If it does not exist
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000295 // already, it is created and inserted into the graph. This method is only for
296 // integral types.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000297 HConstant* GetConstant(Primitive::Type type, int64_t value);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000298 HNullConstant* GetNullConstant();
David Brazdil8d5b8b22015-03-24 10:51:52 +0000299 HIntConstant* GetIntConstant(int32_t value) {
300 return CreateConstant(value, &cached_int_constants_);
301 }
302 HLongConstant* GetLongConstant(int64_t value) {
303 return CreateConstant(value, &cached_long_constants_);
304 }
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000305 HFloatConstant* GetFloatConstant(float value) {
306 return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_);
307 }
308 HDoubleConstant* GetDoubleConstant(double value) {
309 return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_);
310 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000311
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100312 HCurrentMethod* GetCurrentMethod();
313
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000314 HBasicBlock* FindCommonDominator(HBasicBlock* first, HBasicBlock* second) const;
David Brazdil2d7352b2015-04-20 14:52:42 +0100315
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100316 const DexFile& GetDexFile() const {
317 return dex_file_;
318 }
319
320 uint32_t GetMethodIdx() const {
321 return method_idx_;
322 }
323
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100324 InvokeType GetInvokeType() const {
325 return invoke_type_;
326 }
327
David Brazdil2d7352b2015-04-20 14:52:42 +0100328 private:
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000329 void VisitBlockForDominatorTree(HBasicBlock* block,
330 HBasicBlock* predecessor,
331 GrowableArray<size_t>* visits);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100332 void FindBackEdges(ArenaBitVector* visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000333 void VisitBlockForBackEdges(HBasicBlock* block,
334 ArenaBitVector* visited,
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100335 ArenaBitVector* visiting);
Roland Levillainfc600dc2014-12-02 17:16:31 +0000336 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100337 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000338
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000339 template <class InstructionType, typename ValueType>
340 InstructionType* CreateConstant(ValueType value,
341 ArenaSafeMap<ValueType, InstructionType*>* cache) {
342 // Try to find an existing constant of the given value.
343 InstructionType* constant = nullptr;
344 auto cached_constant = cache->find(value);
345 if (cached_constant != cache->end()) {
346 constant = cached_constant->second;
347 }
348
349 // If not found or previously deleted, create and cache a new instruction.
Nicolas Geoffrayf78848f2015-06-17 11:57:56 +0100350 // Don't bother reviving a previously deleted instruction, for simplicity.
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000351 if (constant == nullptr || constant->GetBlock() == nullptr) {
352 constant = new (arena_) InstructionType(value);
353 cache->Overwrite(value, constant);
354 InsertConstant(constant);
355 }
356 return constant;
357 }
358
David Brazdil8d5b8b22015-03-24 10:51:52 +0000359 void InsertConstant(HConstant* instruction);
360
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000361 // Cache a float constant into the graph. This method should only be
362 // called by the SsaBuilder when creating "equivalent" instructions.
363 void CacheFloatConstant(HFloatConstant* constant);
364
365 // See CacheFloatConstant comment.
366 void CacheDoubleConstant(HDoubleConstant* constant);
367
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000368 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000369
370 // List of blocks in insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000371 GrowableArray<HBasicBlock*> blocks_;
372
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100373 // List of blocks to perform a reverse post order tree traversal.
374 GrowableArray<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000375
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100376 // List of blocks to perform a linear order tree traversal.
377 GrowableArray<HBasicBlock*> linear_order_;
378
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000379 HBasicBlock* entry_block_;
380 HBasicBlock* exit_block_;
381
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100382 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100383 uint16_t maximum_number_of_out_vregs_;
384
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100385 // The number of virtual registers in this method. Contains the parameters.
386 uint16_t number_of_vregs_;
387
388 // The number of virtual registers used by parameters of this method.
389 uint16_t number_of_in_vregs_;
390
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000391 // Number of vreg size slots that the temporaries use (used in baseline compiler).
392 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100393
Mark Mendell1152c922015-04-24 17:06:35 -0400394 // Has bounds checks. We can totally skip BCE if it's false.
395 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800396
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000397 // Indicates whether the graph should be compiled in a way that
398 // ensures full debuggability. If false, we can apply more
399 // aggressive optimizations that may limit the level of debugging.
400 const bool debuggable_;
401
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000402 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000403 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000404
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100405 // The dex file from which the method is from.
406 const DexFile& dex_file_;
407
408 // The method index in the dex file.
409 const uint32_t method_idx_;
410
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100411 // If inlined, this encodes how the callee is being invoked.
412 const InvokeType invoke_type_;
413
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100414 // Whether the graph has been transformed to SSA form. Only used
415 // in debug mode to ensure we are not using properties only valid
416 // for non-SSA form (like the number of temporaries).
417 bool in_ssa_form_;
418
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100419 const bool should_generate_constructor_barrier_;
420
Mathieu Chartiere401d142015-04-22 13:56:20 -0700421 const InstructionSet instruction_set_;
422
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000423 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000424 HNullConstant* cached_null_constant_;
425 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000426 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000427 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000428 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000429
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100430 HCurrentMethod* cached_current_method_;
431
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000432 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100433 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000434 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000435 DISALLOW_COPY_AND_ASSIGN(HGraph);
436};
437
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700438class HLoopInformation : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000439 public:
440 HLoopInformation(HBasicBlock* header, HGraph* graph)
441 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100442 suspend_check_(nullptr),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100443 back_edges_(graph->GetArena(), kDefaultNumberOfBackEdges),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100444 // Make bit vector growable, as the number of blocks may change.
445 blocks_(graph->GetArena(), graph->GetBlocks().Size(), true) {}
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100446
447 HBasicBlock* GetHeader() const {
448 return header_;
449 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000450
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000451 void SetHeader(HBasicBlock* block) {
452 header_ = block;
453 }
454
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100455 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
456 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
457 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
458
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000459 void AddBackEdge(HBasicBlock* back_edge) {
460 back_edges_.Add(back_edge);
461 }
462
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100463 void RemoveBackEdge(HBasicBlock* back_edge) {
464 back_edges_.Delete(back_edge);
465 }
466
David Brazdil46e2a392015-03-16 17:31:52 +0000467 bool IsBackEdge(const HBasicBlock& block) const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100468 for (size_t i = 0, e = back_edges_.Size(); i < e; ++i) {
David Brazdil46e2a392015-03-16 17:31:52 +0000469 if (back_edges_.Get(i) == &block) return true;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100470 }
471 return false;
472 }
473
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000474 size_t NumberOfBackEdges() const {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000475 return back_edges_.Size();
476 }
477
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100478 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100479
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100480 const GrowableArray<HBasicBlock*>& GetBackEdges() const {
481 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100482 }
483
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100484 // Returns the lifetime position of the back edge that has the
485 // greatest lifetime position.
486 size_t GetLifetimeEnd() const;
487
488 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
489 for (size_t i = 0, e = back_edges_.Size(); i < e; ++i) {
490 if (back_edges_.Get(i) == existing) {
491 back_edges_.Put(i, new_back_edge);
492 return;
493 }
494 }
495 UNREACHABLE();
Nicolas Geoffray57902602015-04-21 14:28:41 +0100496 }
497
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100498 // Finds blocks that are part of this loop. Returns whether the loop is a natural loop,
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100499 // that is the header dominates the back edge.
500 bool Populate();
501
David Brazdila4b8c212015-05-07 09:59:30 +0100502 // Reanalyzes the loop by removing loop info from its blocks and re-running
503 // Populate(). If there are no back edges left, the loop info is completely
504 // removed as well as its SuspendCheck instruction. It must be run on nested
505 // inner loops first.
506 void Update();
507
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100508 // Returns whether this loop information contains `block`.
509 // Note that this loop information *must* be populated before entering this function.
510 bool Contains(const HBasicBlock& block) const;
511
512 // Returns whether this loop information is an inner loop of `other`.
513 // Note that `other` *must* be populated before entering this function.
514 bool IsIn(const HLoopInformation& other) const;
515
516 const ArenaBitVector& GetBlocks() const { return blocks_; }
517
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000518 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000519 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000520
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000521 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100522 // Internal recursive implementation of `Populate`.
523 void PopulateRecursive(HBasicBlock* block);
524
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000525 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100526 HSuspendCheck* suspend_check_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000527 GrowableArray<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100528 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000529
530 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
531};
532
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100533static constexpr size_t kNoLifetime = -1;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100534static constexpr uint32_t kNoDexPc = -1;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100535
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000536// A block in a method. Contains the list of instructions represented
537// as a double linked list. Each block knows its predecessors and
538// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100539
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700540class HBasicBlock : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000541 public:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100542 explicit HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000543 : graph_(graph),
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000544 predecessors_(graph->GetArena(), kDefaultNumberOfPredecessors),
545 successors_(graph->GetArena(), kDefaultNumberOfSuccessors),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000546 loop_information_(nullptr),
547 dominator_(nullptr),
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100548 dominated_blocks_(graph->GetArena(), kDefaultNumberOfDominatedBlocks),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100549 block_id_(-1),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100550 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100551 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000552 lifetime_end_(kNoLifetime),
553 is_catch_block_(false) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000554
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100555 const GrowableArray<HBasicBlock*>& GetPredecessors() const {
556 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000557 }
558
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100559 const GrowableArray<HBasicBlock*>& GetSuccessors() const {
560 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000561 }
562
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100563 const GrowableArray<HBasicBlock*>& GetDominatedBlocks() const {
564 return dominated_blocks_;
565 }
566
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100567 bool IsEntryBlock() const {
568 return graph_->GetEntryBlock() == this;
569 }
570
571 bool IsExitBlock() const {
572 return graph_->GetExitBlock() == this;
573 }
574
David Brazdil46e2a392015-03-16 17:31:52 +0000575 bool IsSingleGoto() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000576 bool IsSingleTryBoundary() const;
577
578 // Returns true if this block emits nothing but a jump.
579 bool IsSingleJump() const {
580 HLoopInformation* loop_info = GetLoopInformation();
581 return (IsSingleGoto() || IsSingleTryBoundary())
582 // Back edges generate a suspend check.
583 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
584 }
David Brazdil46e2a392015-03-16 17:31:52 +0000585
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000586 void AddBackEdge(HBasicBlock* back_edge) {
587 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000588 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000589 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100590 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000591 loop_information_->AddBackEdge(back_edge);
592 }
593
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000594 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000595 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000596
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000597 int GetBlockId() const { return block_id_; }
598 void SetBlockId(int id) { block_id_ = id; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000599
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000600 HBasicBlock* GetDominator() const { return dominator_; }
601 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100602 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.Add(block); }
David Brazdil2d7352b2015-04-20 14:52:42 +0100603 void RemoveDominatedBlock(HBasicBlock* block) { dominated_blocks_.Delete(block); }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000604 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
605 for (size_t i = 0, e = dominated_blocks_.Size(); i < e; ++i) {
606 if (dominated_blocks_.Get(i) == existing) {
607 dominated_blocks_.Put(i, new_block);
608 return;
609 }
610 }
611 LOG(FATAL) << "Unreachable";
612 UNREACHABLE();
613 }
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100614 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000615
616 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100617 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000618 }
619
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100620 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
621 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100622 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100623 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +0100624 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
625 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000626
627 void AddSuccessor(HBasicBlock* block) {
628 successors_.Add(block);
629 block->predecessors_.Add(this);
630 }
631
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100632 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
633 size_t successor_index = GetSuccessorIndexOf(existing);
634 DCHECK_NE(successor_index, static_cast<size_t>(-1));
635 existing->RemovePredecessor(this);
636 new_block->predecessors_.Add(this);
637 successors_.Put(successor_index, new_block);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000638 }
639
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000640 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
641 size_t predecessor_index = GetPredecessorIndexOf(existing);
642 DCHECK_NE(predecessor_index, static_cast<size_t>(-1));
643 existing->RemoveSuccessor(this);
644 new_block->successors_.Add(this);
645 predecessors_.Put(predecessor_index, new_block);
646 }
647
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100648 // Insert `this` between `predecessor` and `successor. This method
649 // preserves the indicies, and will update the first edge found between
650 // `predecessor` and `successor`.
651 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
652 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
653 DCHECK_NE(predecessor_index, static_cast<size_t>(-1));
654 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
655 DCHECK_NE(successor_index, static_cast<size_t>(-1));
656 successor->predecessors_.Put(predecessor_index, this);
657 predecessor->successors_.Put(successor_index, this);
658 successors_.Add(successor);
659 predecessors_.Add(predecessor);
660 }
661
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100662 void RemovePredecessor(HBasicBlock* block) {
663 predecessors_.Delete(block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100664 }
665
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000666 void RemoveSuccessor(HBasicBlock* block) {
667 successors_.Delete(block);
668 }
669
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100670 void ClearAllPredecessors() {
671 predecessors_.Reset();
672 }
673
674 void AddPredecessor(HBasicBlock* block) {
675 predecessors_.Add(block);
676 block->successors_.Add(this);
677 }
678
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100679 void SwapPredecessors() {
Nicolas Geoffrayc83d4412014-09-18 16:46:20 +0100680 DCHECK_EQ(predecessors_.Size(), 2u);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100681 HBasicBlock* temp = predecessors_.Get(0);
682 predecessors_.Put(0, predecessors_.Get(1));
683 predecessors_.Put(1, temp);
684 }
685
David Brazdil769c9e52015-04-27 13:54:09 +0100686 void SwapSuccessors() {
687 DCHECK_EQ(successors_.Size(), 2u);
688 HBasicBlock* temp = successors_.Get(0);
689 successors_.Put(0, successors_.Get(1));
690 successors_.Put(1, temp);
691 }
692
David Brazdilfc6a86a2015-06-26 10:33:45 +0000693 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100694 for (size_t i = 0, e = predecessors_.Size(); i < e; ++i) {
695 if (predecessors_.Get(i) == predecessor) {
696 return i;
697 }
698 }
699 return -1;
700 }
701
David Brazdilfc6a86a2015-06-26 10:33:45 +0000702 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100703 for (size_t i = 0, e = successors_.Size(); i < e; ++i) {
704 if (successors_.Get(i) == successor) {
705 return i;
706 }
707 }
708 return -1;
709 }
710
David Brazdilfc6a86a2015-06-26 10:33:45 +0000711 HBasicBlock* GetSinglePredecessor() const {
712 DCHECK_EQ(GetPredecessors().Size(), 1u);
713 return GetPredecessors().Get(0);
714 }
715
716 HBasicBlock* GetSingleSuccessor() const {
717 DCHECK_EQ(GetSuccessors().Size(), 1u);
718 return GetSuccessors().Get(0);
719 }
720
721 // Returns whether the first occurrence of `predecessor` in the list of
722 // predecessors is at index `idx`.
723 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
724 DCHECK_EQ(GetPredecessors().Get(idx), predecessor);
725 return GetPredecessorIndexOf(predecessor) == idx;
726 }
727
728 // Returns whether successor at index `idx` is an exception handler.
729 bool IsExceptionalSuccessor(size_t idx) const;
730
731 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +0100732 // created, latter block. Note that this method will add the block to the
733 // graph, create a Goto at the end of the former block and will create an edge
734 // between the blocks. It will not, however, update the reverse post order or
735 // loop information.
David Brazdilfc6a86a2015-06-26 10:33:45 +0000736 HBasicBlock* SplitBefore(HInstruction* cursor);
737
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000738 // Split the block into two blocks just after `cursor`. Returns the newly
739 // created block. Note that this method just updates raw block information,
740 // like predecessors, successors, dominators, and instruction list. It does not
741 // update the graph, reverse post order, loop information, nor make sure the
742 // blocks are consistent (for example ending with a control flow instruction).
743 HBasicBlock* SplitAfter(HInstruction* cursor);
744
745 // Merge `other` at the end of `this`. Successors and dominated blocks of
746 // `other` are changed to be successors and dominated blocks of `this`. Note
747 // that this method does not update the graph, reverse post order, loop
748 // information, nor make sure the blocks are consistent (for example ending
749 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +0100750 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000751
752 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
753 // of `this` are moved to `other`.
754 // Note that this method does not update the graph, reverse post order, loop
755 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +0000756 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000757 void ReplaceWith(HBasicBlock* other);
758
David Brazdil2d7352b2015-04-20 14:52:42 +0100759 // Merge `other` at the end of `this`. This method updates loops, reverse post
760 // order, links to predecessors, successors, dominators and deletes the block
761 // from the graph. The two blocks must be successive, i.e. `this` the only
762 // predecessor of `other` and vice versa.
763 void MergeWith(HBasicBlock* other);
764
765 // Disconnects `this` from all its predecessors, successors and dominator,
766 // removes it from all loops it is included in and eventually from the graph.
767 // The block must not dominate any other block. Predecessors and successors
768 // are safely updated.
769 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +0000770
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000771 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +0100772 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100773 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +0100774 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +0100775 // Replace instruction `initial` with `replacement` within this block.
776 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
777 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100778 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100779 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +0000780 // RemoveInstruction and RemovePhi delete a given instruction from the respective
781 // instruction list. With 'ensure_safety' set to true, it verifies that the
782 // instruction is not in use and removes it from the use lists of its inputs.
783 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
784 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +0100785 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100786
787 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +0100788 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100789 }
790
Roland Levillain6b879dd2014-09-22 17:13:44 +0100791 bool IsLoopPreHeaderFirstPredecessor() const {
792 DCHECK(IsLoopHeader());
793 DCHECK(!GetPredecessors().IsEmpty());
794 return GetPredecessors().Get(0) == GetLoopInformation()->GetPreHeader();
795 }
796
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100797 HLoopInformation* GetLoopInformation() const {
798 return loop_information_;
799 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000800
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000801 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100802 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000803 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100804 void SetInLoop(HLoopInformation* info) {
805 if (IsLoopHeader()) {
806 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +0100807 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100808 loop_information_ = info;
809 } else if (loop_information_->Contains(*info->GetHeader())) {
810 // Block is currently part of an outer loop. Make it part of this inner loop.
811 // Note that a non loop header having a loop information means this loop information
812 // has already been populated
813 loop_information_ = info;
814 } else {
815 // Block is part of an inner loop. Do not update the loop information.
816 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
817 // at this point, because this method is being called while populating `info`.
818 }
819 }
820
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000821 // Raw update of the loop information.
822 void SetLoopInformation(HLoopInformation* info) {
823 loop_information_ = info;
824 }
825
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100826 bool IsInLoop() const { return loop_information_ != nullptr; }
827
David Brazdila4b8c212015-05-07 09:59:30 +0100828 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100829 bool Dominates(HBasicBlock* block) const;
830
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100831 size_t GetLifetimeStart() const { return lifetime_start_; }
832 size_t GetLifetimeEnd() const { return lifetime_end_; }
833
834 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
835 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
836
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100837 uint32_t GetDexPc() const { return dex_pc_; }
838
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000839 bool IsCatchBlock() const { return is_catch_block_; }
840 void SetIsCatchBlock() { is_catch_block_ = true; }
841
David Brazdil8d5b8b22015-03-24 10:51:52 +0000842 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +0000843 bool EndsWithIf() const;
844 bool HasSinglePhi() const;
845
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000846 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000847 HGraph* graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000848 GrowableArray<HBasicBlock*> predecessors_;
849 GrowableArray<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100850 HInstructionList instructions_;
851 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000852 HLoopInformation* loop_information_;
853 HBasicBlock* dominator_;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100854 GrowableArray<HBasicBlock*> dominated_blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000855 int block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100856 // The dex program counter of the first instruction of this block.
857 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100858 size_t lifetime_start_;
859 size_t lifetime_end_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000860 bool is_catch_block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000861
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000862 friend class HGraph;
863 friend class HInstruction;
864
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000865 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
866};
867
David Brazdilb2bd1c52015-03-25 11:17:37 +0000868// Iterates over the LoopInformation of all loops which contain 'block'
869// from the innermost to the outermost.
870class HLoopInformationOutwardIterator : public ValueObject {
871 public:
872 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
873 : current_(block.GetLoopInformation()) {}
874
875 bool Done() const { return current_ == nullptr; }
876
877 void Advance() {
878 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +0100879 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +0000880 }
881
882 HLoopInformation* Current() const {
883 DCHECK(!Done());
884 return current_;
885 }
886
887 private:
888 HLoopInformation* current_;
889
890 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
891};
892
Alexandre Ramesef20f712015-06-09 10:29:30 +0100893#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100894 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000895 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000896 M(ArrayGet, Instruction) \
897 M(ArrayLength, Instruction) \
898 M(ArraySet, Instruction) \
David Brazdil66d126e2015-04-03 16:02:44 +0100899 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000900 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +0000901 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000902 M(CheckCast, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100903 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000904 M(Compare, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100905 M(Condition, BinaryOperation) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100906 M(CurrentMethod, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700907 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000908 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +0000909 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000910 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100911 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000912 M(Exit, Instruction) \
913 M(FloatConstant, Constant) \
914 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100915 M(GreaterThan, Condition) \
916 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100917 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000918 M(InstanceFieldGet, Instruction) \
919 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000920 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100921 M(IntConstant, Constant) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +0000922 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000923 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100924 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000925 M(LessThan, Condition) \
926 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000927 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000928 M(LoadException, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100929 M(LoadLocal, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000930 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100931 M(Local, Instruction) \
932 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +0100933 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +0000934 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000935 M(Mul, BinaryOperation) \
936 M(Neg, UnaryOperation) \
937 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100938 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +0100939 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000940 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000941 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000942 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000943 M(Or, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100944 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000945 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100946 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000947 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100948 M(Return, Instruction) \
949 M(ReturnVoid, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000950 M(Shl, BinaryOperation) \
951 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100952 M(StaticFieldGet, Instruction) \
953 M(StaticFieldSet, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100954 M(StoreLocal, Instruction) \
955 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100956 M(SuspendCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000957 M(Temporary, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000958 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +0000959 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +0000960 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000961 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000962 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000963
Alexandre Ramesef20f712015-06-09 10:29:30 +0100964#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
965
966#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
967
Alexandre Ramesf39e0642015-06-23 11:33:45 +0100968#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
969
Alexandre Ramesef20f712015-06-09 10:29:30 +0100970#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
971
972#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
973
974#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
975 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
976 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
977 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +0100978 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +0100979 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
980 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
981
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100982#define FOR_EACH_INSTRUCTION(M) \
983 FOR_EACH_CONCRETE_INSTRUCTION(M) \
984 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +0100985 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +0100986 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100987 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -0700988
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100989#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000990FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
991#undef FORWARD_DECLARATION
992
Roland Levillainccc07a92014-09-16 14:48:16 +0100993#define DECLARE_INSTRUCTION(type) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000994 InstructionKind GetKind() const OVERRIDE { return k##type; } \
995 const char* DebugName() const OVERRIDE { return #type; } \
996 const H##type* As##type() const OVERRIDE { return this; } \
997 H##type* As##type() OVERRIDE { return this; } \
998 bool InstructionTypeEquals(HInstruction* other) const OVERRIDE { \
Roland Levillainccc07a92014-09-16 14:48:16 +0100999 return other->Is##type(); \
1000 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001001 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001002
David Brazdiled596192015-01-23 10:39:45 +00001003template <typename T> class HUseList;
1004
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001005template <typename T>
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001006class HUseListNode : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001007 public:
David Brazdiled596192015-01-23 10:39:45 +00001008 HUseListNode* GetPrevious() const { return prev_; }
1009 HUseListNode* GetNext() const { return next_; }
1010 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001011 size_t GetIndex() const { return index_; }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001012 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001013
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001014 private:
David Brazdiled596192015-01-23 10:39:45 +00001015 HUseListNode(T user, size_t index)
1016 : user_(user), index_(index), prev_(nullptr), next_(nullptr) {}
1017
1018 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001019 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001020 HUseListNode<T>* prev_;
1021 HUseListNode<T>* next_;
1022
1023 friend class HUseList<T>;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001024
1025 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1026};
1027
David Brazdiled596192015-01-23 10:39:45 +00001028template <typename T>
1029class HUseList : public ValueObject {
1030 public:
1031 HUseList() : first_(nullptr) {}
1032
1033 void Clear() {
1034 first_ = nullptr;
1035 }
1036
1037 // Adds a new entry at the beginning of the use list and returns
1038 // the newly created node.
1039 HUseListNode<T>* AddUse(T user, size_t index, ArenaAllocator* arena) {
David Brazdilea55b932015-01-27 17:12:29 +00001040 HUseListNode<T>* new_node = new (arena) HUseListNode<T>(user, index);
David Brazdiled596192015-01-23 10:39:45 +00001041 if (IsEmpty()) {
1042 first_ = new_node;
1043 } else {
1044 first_->prev_ = new_node;
1045 new_node->next_ = first_;
1046 first_ = new_node;
1047 }
1048 return new_node;
1049 }
1050
1051 HUseListNode<T>* GetFirst() const {
1052 return first_;
1053 }
1054
1055 void Remove(HUseListNode<T>* node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001056 DCHECK(node != nullptr);
1057 DCHECK(Contains(node));
1058
David Brazdiled596192015-01-23 10:39:45 +00001059 if (node->prev_ != nullptr) {
1060 node->prev_->next_ = node->next_;
1061 }
1062 if (node->next_ != nullptr) {
1063 node->next_->prev_ = node->prev_;
1064 }
1065 if (node == first_) {
1066 first_ = node->next_;
1067 }
1068 }
1069
David Brazdil1abb4192015-02-17 18:33:36 +00001070 bool Contains(const HUseListNode<T>* node) const {
1071 if (node == nullptr) {
1072 return false;
1073 }
1074 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1075 if (current == node) {
1076 return true;
1077 }
1078 }
1079 return false;
1080 }
1081
David Brazdiled596192015-01-23 10:39:45 +00001082 bool IsEmpty() const {
1083 return first_ == nullptr;
1084 }
1085
1086 bool HasOnlyOneUse() const {
1087 return first_ != nullptr && first_->next_ == nullptr;
1088 }
1089
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001090 size_t SizeSlow() const {
1091 size_t count = 0;
1092 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1093 ++count;
1094 }
1095 return count;
1096 }
1097
David Brazdiled596192015-01-23 10:39:45 +00001098 private:
1099 HUseListNode<T>* first_;
1100};
1101
1102template<typename T>
1103class HUseIterator : public ValueObject {
1104 public:
1105 explicit HUseIterator(const HUseList<T>& uses) : current_(uses.GetFirst()) {}
1106
1107 bool Done() const { return current_ == nullptr; }
1108
1109 void Advance() {
1110 DCHECK(!Done());
1111 current_ = current_->GetNext();
1112 }
1113
1114 HUseListNode<T>* Current() const {
1115 DCHECK(!Done());
1116 return current_;
1117 }
1118
1119 private:
1120 HUseListNode<T>* current_;
1121
1122 friend class HValue;
1123};
1124
David Brazdil1abb4192015-02-17 18:33:36 +00001125// This class is used by HEnvironment and HInstruction classes to record the
1126// instructions they use and pointers to the corresponding HUseListNodes kept
1127// by the used instructions.
1128template <typename T>
1129class HUserRecord : public ValueObject {
1130 public:
1131 HUserRecord() : instruction_(nullptr), use_node_(nullptr) {}
1132 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), use_node_(nullptr) {}
1133
1134 HUserRecord(const HUserRecord<T>& old_record, HUseListNode<T>* use_node)
1135 : instruction_(old_record.instruction_), use_node_(use_node) {
1136 DCHECK(instruction_ != nullptr);
1137 DCHECK(use_node_ != nullptr);
1138 DCHECK(old_record.use_node_ == nullptr);
1139 }
1140
1141 HInstruction* GetInstruction() const { return instruction_; }
1142 HUseListNode<T>* GetUseNode() const { return use_node_; }
1143
1144 private:
1145 // Instruction used by the user.
1146 HInstruction* instruction_;
1147
1148 // Corresponding entry in the use list kept by 'instruction_'.
1149 HUseListNode<T>* use_node_;
1150};
1151
Calin Juravle27df7582015-04-17 19:12:31 +01001152// TODO: Add better documentation to this class and maybe refactor with more suggestive names.
1153// - Has(All)SideEffects suggests that all the side effects are present but only ChangesSomething
1154// flag is consider.
1155// - DependsOn suggests that there is a real dependency between side effects but it only
1156// checks DependendsOnSomething flag.
1157//
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001158// Represents the side effects an instruction may have.
1159class SideEffects : public ValueObject {
1160 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001161 SideEffects() : flags_(0) {}
1162
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001163 static SideEffects None() {
1164 return SideEffects(0);
1165 }
1166
1167 static SideEffects All() {
1168 return SideEffects(ChangesSomething().flags_ | DependsOnSomething().flags_);
1169 }
1170
1171 static SideEffects ChangesSomething() {
1172 return SideEffects((1 << kFlagChangesCount) - 1);
1173 }
1174
1175 static SideEffects DependsOnSomething() {
1176 int count = kFlagDependsOnCount - kFlagChangesCount;
1177 return SideEffects(((1 << count) - 1) << kFlagChangesCount);
1178 }
1179
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001180 SideEffects Union(SideEffects other) const {
1181 return SideEffects(flags_ | other.flags_);
1182 }
1183
Roland Levillain72bceff2014-09-15 18:29:00 +01001184 bool HasSideEffects() const {
1185 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
1186 return (flags_ & all_bits_set) != 0;
1187 }
1188
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001189 bool HasAllSideEffects() const {
1190 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
1191 return all_bits_set == (flags_ & all_bits_set);
1192 }
1193
1194 bool DependsOn(SideEffects other) const {
1195 size_t depends_flags = other.ComputeDependsFlags();
1196 return (flags_ & depends_flags) != 0;
1197 }
1198
1199 bool HasDependencies() const {
1200 int count = kFlagDependsOnCount - kFlagChangesCount;
1201 size_t all_bits_set = (1 << count) - 1;
1202 return ((flags_ >> kFlagChangesCount) & all_bits_set) != 0;
1203 }
1204
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001205 private:
1206 static constexpr int kFlagChangesSomething = 0;
1207 static constexpr int kFlagChangesCount = kFlagChangesSomething + 1;
1208
1209 static constexpr int kFlagDependsOnSomething = kFlagChangesCount;
1210 static constexpr int kFlagDependsOnCount = kFlagDependsOnSomething + 1;
1211
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001212 explicit SideEffects(size_t flags) : flags_(flags) {}
1213
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001214 size_t ComputeDependsFlags() const {
1215 return flags_ << kFlagChangesCount;
1216 }
1217
1218 size_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001219};
1220
David Brazdiled596192015-01-23 10:39:45 +00001221// A HEnvironment object contains the values of virtual registers at a given location.
1222class HEnvironment : public ArenaObject<kArenaAllocMisc> {
1223 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001224 HEnvironment(ArenaAllocator* arena,
1225 size_t number_of_vregs,
1226 const DexFile& dex_file,
1227 uint32_t method_idx,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001228 uint32_t dex_pc,
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001229 InvokeType invoke_type,
1230 HInstruction* holder)
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001231 : vregs_(arena, number_of_vregs),
1232 locations_(arena, number_of_vregs),
1233 parent_(nullptr),
1234 dex_file_(dex_file),
1235 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001236 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001237 invoke_type_(invoke_type),
1238 holder_(holder) {
David Brazdiled596192015-01-23 10:39:45 +00001239 vregs_.SetSize(number_of_vregs);
1240 for (size_t i = 0; i < number_of_vregs; i++) {
David Brazdil1abb4192015-02-17 18:33:36 +00001241 vregs_.Put(i, HUserRecord<HEnvironment*>());
David Brazdiled596192015-01-23 10:39:45 +00001242 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001243
1244 locations_.SetSize(number_of_vregs);
1245 for (size_t i = 0; i < number_of_vregs; ++i) {
1246 locations_.Put(i, Location());
1247 }
1248 }
1249
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001250 HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001251 : HEnvironment(arena,
1252 to_copy.Size(),
1253 to_copy.GetDexFile(),
1254 to_copy.GetMethodIdx(),
1255 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001256 to_copy.GetInvokeType(),
1257 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001258
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001259 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001260 if (parent_ != nullptr) {
1261 parent_->SetAndCopyParentChain(allocator, parent);
1262 } else {
1263 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1264 parent_->CopyFrom(parent);
1265 if (parent->GetParent() != nullptr) {
1266 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1267 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001268 }
David Brazdiled596192015-01-23 10:39:45 +00001269 }
1270
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001271 void CopyFrom(const GrowableArray<HInstruction*>& locals);
1272 void CopyFrom(HEnvironment* environment);
1273
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001274 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1275 // input to the loop phi instead. This is for inserting instructions that
1276 // require an environment (like HDeoptimization) in the loop pre-header.
1277 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001278
1279 void SetRawEnvAt(size_t index, HInstruction* instruction) {
David Brazdil1abb4192015-02-17 18:33:36 +00001280 vregs_.Put(index, HUserRecord<HEnvironment*>(instruction));
David Brazdiled596192015-01-23 10:39:45 +00001281 }
1282
1283 HInstruction* GetInstructionAt(size_t index) const {
David Brazdil1abb4192015-02-17 18:33:36 +00001284 return vregs_.Get(index).GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001285 }
1286
David Brazdil1abb4192015-02-17 18:33:36 +00001287 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001288
1289 size_t Size() const { return vregs_.Size(); }
1290
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001291 HEnvironment* GetParent() const { return parent_; }
1292
1293 void SetLocationAt(size_t index, Location location) {
1294 locations_.Put(index, location);
1295 }
1296
1297 Location GetLocationAt(size_t index) const {
1298 return locations_.Get(index);
1299 }
1300
1301 uint32_t GetDexPc() const {
1302 return dex_pc_;
1303 }
1304
1305 uint32_t GetMethodIdx() const {
1306 return method_idx_;
1307 }
1308
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001309 InvokeType GetInvokeType() const {
1310 return invoke_type_;
1311 }
1312
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001313 const DexFile& GetDexFile() const {
1314 return dex_file_;
1315 }
1316
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001317 HInstruction* GetHolder() const {
1318 return holder_;
1319 }
1320
David Brazdiled596192015-01-23 10:39:45 +00001321 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001322 // Record instructions' use entries of this environment for constant-time removal.
1323 // It should only be called by HInstruction when a new environment use is added.
1324 void RecordEnvUse(HUseListNode<HEnvironment*>* env_use) {
1325 DCHECK(env_use->GetUser() == this);
1326 size_t index = env_use->GetIndex();
1327 vregs_.Put(index, HUserRecord<HEnvironment*>(vregs_.Get(index), env_use));
1328 }
David Brazdiled596192015-01-23 10:39:45 +00001329
David Brazdil1abb4192015-02-17 18:33:36 +00001330 GrowableArray<HUserRecord<HEnvironment*> > vregs_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001331 GrowableArray<Location> locations_;
1332 HEnvironment* parent_;
1333 const DexFile& dex_file_;
1334 const uint32_t method_idx_;
1335 const uint32_t dex_pc_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001336 const InvokeType invoke_type_;
David Brazdiled596192015-01-23 10:39:45 +00001337
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001338 // The instruction that holds this environment. Only used in debug mode
1339 // to ensure the graph is consistent.
1340 HInstruction* const holder_;
1341
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001342 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001343
1344 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1345};
1346
Calin Juravleacf735c2015-02-12 15:25:22 +00001347class ReferenceTypeInfo : ValueObject {
1348 public:
Calin Juravleb1498f62015-02-16 13:13:29 +00001349 typedef Handle<mirror::Class> TypeHandle;
1350
1351 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact)
1352 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1353 if (type_handle->IsObjectClass()) {
1354 // Override the type handle to be consistent with the case when we get to
1355 // Top but don't have the Object class available. It avoids having to guess
1356 // what value the type_handle has when it's Top.
1357 return ReferenceTypeInfo(TypeHandle(), is_exact, true);
1358 } else {
1359 return ReferenceTypeInfo(type_handle, is_exact, false);
1360 }
1361 }
1362
1363 static ReferenceTypeInfo CreateTop(bool is_exact) {
1364 return ReferenceTypeInfo(TypeHandle(), is_exact, true);
Calin Juravleacf735c2015-02-12 15:25:22 +00001365 }
1366
1367 bool IsExact() const { return is_exact_; }
1368 bool IsTop() const { return is_top_; }
Guillaume Sanchez222862c2015-06-09 18:33:02 +01001369 bool IsInterface() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1370 return !IsTop() && GetTypeHandle()->IsInterface();
1371 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001372
1373 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
1374
Calin Juravleb1498f62015-02-16 13:13:29 +00001375 bool IsSupertypeOf(ReferenceTypeInfo rti) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Calin Juravleacf735c2015-02-12 15:25:22 +00001376 if (IsTop()) {
1377 // Top (equivalent for java.lang.Object) is supertype of anything.
1378 return true;
1379 }
1380 if (rti.IsTop()) {
1381 // If we get here `this` is not Top() so it can't be a supertype.
1382 return false;
1383 }
1384 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
1385 }
1386
1387 // Returns true if the type information provide the same amount of details.
1388 // Note that it does not mean that the instructions have the same actual type
1389 // (e.g. tops are equal but they can be the result of a merge).
1390 bool IsEqual(ReferenceTypeInfo rti) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1391 if (IsExact() != rti.IsExact()) {
1392 return false;
1393 }
1394 if (IsTop() && rti.IsTop()) {
1395 // `Top` means java.lang.Object, so the types are equivalent.
1396 return true;
1397 }
1398 if (IsTop() || rti.IsTop()) {
1399 // If only one is top or object than they are not equivalent.
1400 // NB: We need this extra check because the type_handle of `Top` is invalid
1401 // and we cannot inspect its reference.
1402 return false;
1403 }
1404
1405 // Finally check the types.
1406 return GetTypeHandle().Get() == rti.GetTypeHandle().Get();
1407 }
1408
1409 private:
Calin Juravleb1498f62015-02-16 13:13:29 +00001410 ReferenceTypeInfo() : ReferenceTypeInfo(TypeHandle(), false, true) {}
1411 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact, bool is_top)
1412 : type_handle_(type_handle), is_exact_(is_exact), is_top_(is_top) {}
1413
Calin Juravleacf735c2015-02-12 15:25:22 +00001414 // The class of the object.
Calin Juravleb1498f62015-02-16 13:13:29 +00001415 TypeHandle type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001416 // Whether or not the type is exact or a superclass of the actual type.
Calin Juravleb1498f62015-02-16 13:13:29 +00001417 // Whether or not we have any information about this type.
Calin Juravleacf735c2015-02-12 15:25:22 +00001418 bool is_exact_;
1419 // A true value here means that the object type should be java.lang.Object.
1420 // We don't have access to the corresponding mirror object every time so this
1421 // flag acts as a substitute. When true, the TypeHandle refers to a null
1422 // pointer and should not be used.
1423 bool is_top_;
1424};
1425
1426std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
1427
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001428class HInstruction : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001429 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001430 explicit HInstruction(SideEffects side_effects)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001431 : previous_(nullptr),
1432 next_(nullptr),
1433 block_(nullptr),
1434 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001435 ssa_index_(-1),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001436 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001437 locations_(nullptr),
1438 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001439 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001440 side_effects_(side_effects),
1441 reference_type_info_(ReferenceTypeInfo::CreateTop(/* is_exact */ false)) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001442
Dave Allison20dfc792014-06-16 20:44:29 -07001443 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001444
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001445#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001446 enum InstructionKind {
1447 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1448 };
1449#undef DECLARE_KIND
1450
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001451 HInstruction* GetNext() const { return next_; }
1452 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001453
Calin Juravle77520bc2015-01-12 18:45:46 +00001454 HInstruction* GetNextDisregardingMoves() const;
1455 HInstruction* GetPreviousDisregardingMoves() const;
1456
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001457 HBasicBlock* GetBlock() const { return block_; }
1458 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001459 bool IsInBlock() const { return block_ != nullptr; }
1460 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +01001461 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001462
Roland Levillain6b879dd2014-09-22 17:13:44 +01001463 virtual size_t InputCount() const = 0;
David Brazdil1abb4192015-02-17 18:33:36 +00001464 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001465
1466 virtual void Accept(HGraphVisitor* visitor) = 0;
1467 virtual const char* DebugName() const = 0;
1468
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001469 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
David Brazdil1abb4192015-02-17 18:33:36 +00001470 void SetRawInputAt(size_t index, HInstruction* input) {
1471 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1472 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001473
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001474 virtual bool NeedsEnvironment() const { return false; }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001475 virtual uint32_t GetDexPc() const {
1476 LOG(FATAL) << "GetDexPc() cannot be called on an instruction that"
1477 " does not need an environment";
1478 UNREACHABLE();
1479 }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001480 virtual bool IsControlFlow() const { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01001481 virtual bool CanThrow() const { return false; }
Roland Levillain72bceff2014-09-15 18:29:00 +01001482 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001483
Calin Juravle10e244f2015-01-26 18:54:32 +00001484 // Does not apply for all instructions, but having this at top level greatly
1485 // simplifies the null check elimination.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001486 virtual bool CanBeNull() const {
1487 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1488 return true;
1489 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001490
Calin Juravle641547a2015-04-21 22:08:51 +01001491 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj) const {
1492 UNUSED(obj);
1493 return false;
1494 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001495
Calin Juravleacf735c2015-02-12 15:25:22 +00001496 void SetReferenceTypeInfo(ReferenceTypeInfo reference_type_info) {
Calin Juravle61d544b2015-02-23 16:46:57 +00001497 DCHECK_EQ(GetType(), Primitive::kPrimNot);
Calin Juravleacf735c2015-02-12 15:25:22 +00001498 reference_type_info_ = reference_type_info;
1499 }
1500
Calin Juravle61d544b2015-02-23 16:46:57 +00001501 ReferenceTypeInfo GetReferenceTypeInfo() const {
1502 DCHECK_EQ(GetType(), Primitive::kPrimNot);
1503 return reference_type_info_;
1504 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001505
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001506 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001507 DCHECK(user != nullptr);
1508 HUseListNode<HInstruction*>* use =
1509 uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1510 user->SetRawInputRecordAt(index, HUserRecord<HInstruction*>(user->InputRecordAt(index), use));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001511 }
1512
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001513 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001514 DCHECK(user != nullptr);
David Brazdiled596192015-01-23 10:39:45 +00001515 HUseListNode<HEnvironment*>* env_use =
1516 env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1517 user->RecordEnvUse(env_use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001518 }
1519
David Brazdil1abb4192015-02-17 18:33:36 +00001520 void RemoveAsUserOfInput(size_t input) {
1521 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
1522 input_use.GetInstruction()->uses_.Remove(input_use.GetUseNode());
1523 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001524
David Brazdil1abb4192015-02-17 18:33:36 +00001525 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1526 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001527
David Brazdiled596192015-01-23 10:39:45 +00001528 bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); }
1529 bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); }
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001530 bool HasNonEnvironmentUses() const { return !uses_.IsEmpty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001531 bool HasOnlyOneNonEnvironmentUse() const {
1532 return !HasEnvironmentUses() && GetUses().HasOnlyOneUse();
1533 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001534
Roland Levillain6c82d402014-10-13 16:10:27 +01001535 // Does this instruction strictly dominate `other_instruction`?
1536 // Returns false if this instruction and `other_instruction` are the same.
1537 // Aborts if this instruction and `other_instruction` are both phis.
1538 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001539
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001540 int GetId() const { return id_; }
1541 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001542
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001543 int GetSsaIndex() const { return ssa_index_; }
1544 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1545 bool HasSsaIndex() const { return ssa_index_ != -1; }
1546
1547 bool HasEnvironment() const { return environment_ != nullptr; }
1548 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001549 // Set the `environment_` field. Raw because this method does not
1550 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001551 void SetRawEnvironment(HEnvironment* environment) {
1552 DCHECK(environment_ == nullptr);
1553 DCHECK_EQ(environment->GetHolder(), this);
1554 environment_ = environment;
1555 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001556
1557 // Set the environment of this instruction, copying it from `environment`. While
1558 // copying, the uses lists are being updated.
1559 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001560 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001561 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001562 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001563 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001564 if (environment->GetParent() != nullptr) {
1565 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1566 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001567 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001568
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001569 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
1570 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001571 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001572 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001573 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001574 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001575 if (environment->GetParent() != nullptr) {
1576 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1577 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001578 }
1579
Nicolas Geoffray39468442014-09-02 15:17:15 +01001580 // Returns the number of entries in the environment. Typically, that is the
1581 // number of dex registers in a method. It could be more in case of inlining.
1582 size_t EnvironmentSize() const;
1583
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001584 LocationSummary* GetLocations() const { return locations_; }
1585 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001586
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001587 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001588 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001589
Alexandre Rames188d4312015-04-09 18:30:21 +01001590 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
1591 // uses of this instruction by `other` are *not* updated.
1592 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
1593 ReplaceWith(other);
1594 other->ReplaceInput(this, use_index);
1595 }
1596
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001597 // Move `this` instruction before `cursor`.
1598 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001599
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001600#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01001601 bool Is##type() const { return (As##type() != nullptr); } \
1602 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001603 virtual H##type* As##type() { return nullptr; }
1604
1605 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
1606#undef INSTRUCTION_TYPE_CHECK
1607
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001608 // Returns whether the instruction can be moved within the graph.
1609 virtual bool CanBeMoved() const { return false; }
1610
1611 // Returns whether the two instructions are of the same kind.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001612 virtual bool InstructionTypeEquals(HInstruction* other) const {
1613 UNUSED(other);
1614 return false;
1615 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001616
1617 // Returns whether any data encoded in the two instructions is equal.
1618 // This method does not look at the inputs. Both instructions must be
1619 // of the same type, otherwise the method has undefined behavior.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001620 virtual bool InstructionDataEquals(HInstruction* other) const {
1621 UNUSED(other);
1622 return false;
1623 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001624
1625 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00001626 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001627 // 2) Their inputs are identical.
1628 bool Equals(HInstruction* other) const;
1629
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001630 virtual InstructionKind GetKind() const = 0;
1631
1632 virtual size_t ComputeHashCode() const {
1633 size_t result = GetKind();
1634 for (size_t i = 0, e = InputCount(); i < e; ++i) {
1635 result = (result * 31) + InputAt(i)->GetId();
1636 }
1637 return result;
1638 }
1639
1640 SideEffects GetSideEffects() const { return side_effects_; }
1641
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001642 size_t GetLifetimePosition() const { return lifetime_position_; }
1643 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
1644 LiveInterval* GetLiveInterval() const { return live_interval_; }
1645 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
1646 bool HasLiveInterval() const { return live_interval_ != nullptr; }
1647
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001648 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
1649
1650 // Returns whether the code generation of the instruction will require to have access
1651 // to the current method. Such instructions are:
1652 // (1): Instructions that require an environment, as calling the runtime requires
1653 // to walk the stack and have the current method stored at a specific stack address.
1654 // (2): Object literals like classes and strings, that are loaded from the dex cache
1655 // fields of the current method.
1656 bool NeedsCurrentMethod() const {
1657 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
1658 }
1659
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001660 virtual bool NeedsDexCache() const { return false; }
1661
David Brazdil1abb4192015-02-17 18:33:36 +00001662 protected:
1663 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
1664 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
1665
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001666 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001667 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); }
1668
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001669 HInstruction* previous_;
1670 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001671 HBasicBlock* block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001672
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001673 // An instruction gets an id when it is added to the graph.
1674 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01001675 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001676 int id_;
1677
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001678 // When doing liveness analysis, instructions that have uses get an SSA index.
1679 int ssa_index_;
1680
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001681 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00001682 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001683
1684 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00001685 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001686
Nicolas Geoffray39468442014-09-02 15:17:15 +01001687 // The environment associated with this instruction. Not null if the instruction
1688 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001689 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001690
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001691 // Set by the code generator.
1692 LocationSummary* locations_;
1693
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001694 // Set by the liveness analysis.
1695 LiveInterval* live_interval_;
1696
1697 // Set by the liveness analysis, this is the position in a linear
1698 // order of blocks where this instruction's live interval start.
1699 size_t lifetime_position_;
1700
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001701 const SideEffects side_effects_;
1702
Calin Juravleacf735c2015-02-12 15:25:22 +00001703 // TODO: for primitive types this should be marked as invalid.
1704 ReferenceTypeInfo reference_type_info_;
1705
David Brazdil1abb4192015-02-17 18:33:36 +00001706 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001707 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00001708 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001709 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001710 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001711
1712 DISALLOW_COPY_AND_ASSIGN(HInstruction);
1713};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001714std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001715
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001716class HInputIterator : public ValueObject {
1717 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001718 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001719
1720 bool Done() const { return index_ == instruction_->InputCount(); }
1721 HInstruction* Current() const { return instruction_->InputAt(index_); }
1722 void Advance() { index_++; }
1723
1724 private:
1725 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001726 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001727
1728 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
1729};
1730
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001731class HInstructionIterator : public ValueObject {
1732 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001733 explicit HInstructionIterator(const HInstructionList& instructions)
1734 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001735 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001736 }
1737
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001738 bool Done() const { return instruction_ == nullptr; }
1739 HInstruction* Current() const { return instruction_; }
1740 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001741 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001742 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001743 }
1744
1745 private:
1746 HInstruction* instruction_;
1747 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001748
1749 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001750};
1751
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001752class HBackwardInstructionIterator : public ValueObject {
1753 public:
1754 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
1755 : instruction_(instructions.last_instruction_) {
1756 next_ = Done() ? nullptr : instruction_->GetPrevious();
1757 }
1758
1759 bool Done() const { return instruction_ == nullptr; }
1760 HInstruction* Current() const { return instruction_; }
1761 void Advance() {
1762 instruction_ = next_;
1763 next_ = Done() ? nullptr : instruction_->GetPrevious();
1764 }
1765
1766 private:
1767 HInstruction* instruction_;
1768 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001769
1770 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001771};
1772
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001773// An embedded container with N elements of type T. Used (with partial
1774// specialization for N=0) because embedded arrays cannot have size 0.
1775template<typename T, intptr_t N>
1776class EmbeddedArray {
1777 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001778 EmbeddedArray() : elements_() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001779
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001780 intptr_t GetLength() const { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001781
1782 const T& operator[](intptr_t i) const {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001783 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001784 return elements_[i];
1785 }
1786
1787 T& operator[](intptr_t i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001788 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001789 return elements_[i];
1790 }
1791
1792 const T& At(intptr_t i) const {
1793 return (*this)[i];
1794 }
1795
1796 void SetAt(intptr_t i, const T& val) {
1797 (*this)[i] = val;
1798 }
1799
1800 private:
1801 T elements_[N];
1802};
1803
1804template<typename T>
1805class EmbeddedArray<T, 0> {
1806 public:
1807 intptr_t length() const { return 0; }
1808 const T& operator[](intptr_t i) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001809 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001810 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001811 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001812 }
1813 T& operator[](intptr_t i) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001814 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001815 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001816 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001817 }
1818};
1819
1820template<intptr_t N>
1821class HTemplateInstruction: public HInstruction {
1822 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001823 HTemplateInstruction<N>(SideEffects side_effects)
1824 : HInstruction(side_effects), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07001825 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001826
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001827 size_t InputCount() const OVERRIDE { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001828
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001829 protected:
David Brazdil1abb4192015-02-17 18:33:36 +00001830 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_[i]; }
1831
1832 void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE {
1833 inputs_[i] = input;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001834 }
1835
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001836 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001837 EmbeddedArray<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001838
1839 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001840};
1841
Dave Allison20dfc792014-06-16 20:44:29 -07001842template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001843class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07001844 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001845 HExpression<N>(Primitive::Type type, SideEffects side_effects)
1846 : HTemplateInstruction<N>(side_effects), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07001847 virtual ~HExpression() {}
1848
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001849 Primitive::Type GetType() const OVERRIDE { return type_; }
Dave Allison20dfc792014-06-16 20:44:29 -07001850
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001851 protected:
1852 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07001853};
1854
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001855// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
1856// instruction that branches to the exit block.
1857class HReturnVoid : public HTemplateInstruction<0> {
1858 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001859 HReturnVoid() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001860
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001861 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001862
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001863 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001864
1865 private:
1866 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
1867};
1868
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001869// Represents dex's RETURN opcodes. A HReturn is a control flow
1870// instruction that branches to the exit block.
1871class HReturn : public HTemplateInstruction<1> {
1872 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001873 explicit HReturn(HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001874 SetRawInputAt(0, value);
1875 }
1876
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001877 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001878
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001879 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001880
1881 private:
1882 DISALLOW_COPY_AND_ASSIGN(HReturn);
1883};
1884
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001885// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001886// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001887// exit block.
1888class HExit : public HTemplateInstruction<0> {
1889 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001890 HExit() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001891
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001892 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001893
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001894 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001895
1896 private:
1897 DISALLOW_COPY_AND_ASSIGN(HExit);
1898};
1899
1900// Jumps from one block to another.
1901class HGoto : public HTemplateInstruction<0> {
1902 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001903 HGoto() : HTemplateInstruction(SideEffects::None()) {}
1904
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001905 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001906
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001907 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00001908 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001909 }
1910
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001911 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001912
1913 private:
1914 DISALLOW_COPY_AND_ASSIGN(HGoto);
1915};
1916
Dave Allison20dfc792014-06-16 20:44:29 -07001917
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001918// Conditional branch. A block ending with an HIf instruction must have
1919// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001920class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001921 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001922 explicit HIf(HInstruction* input) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001923 SetRawInputAt(0, input);
1924 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001925
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001926 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001927
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001928 HBasicBlock* IfTrueSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001929 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001930 }
1931
1932 HBasicBlock* IfFalseSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001933 return GetBlock()->GetSuccessors().Get(1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001934 }
1935
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001936 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001937
1938 private:
1939 DISALLOW_COPY_AND_ASSIGN(HIf);
1940};
1941
David Brazdilfc6a86a2015-06-26 10:33:45 +00001942
1943// Abstract instruction which marks the beginning and/or end of a try block and
1944// links it to the respective exception handlers. Behaves the same as a Goto in
1945// non-exceptional control flow.
1946// Normal-flow successor is stored at index zero, exception handlers under
1947// higher indices in no particular order.
1948class HTryBoundary : public HTemplateInstruction<0> {
1949 public:
David Brazdil56e1acc2015-06-30 15:41:36 +01001950 enum BoundaryKind {
1951 kEntry,
1952 kExit,
1953 };
1954
1955 explicit HTryBoundary(BoundaryKind kind)
1956 : HTemplateInstruction(SideEffects::None()), kind_(kind) {}
David Brazdilfc6a86a2015-06-26 10:33:45 +00001957
1958 bool IsControlFlow() const OVERRIDE { return true; }
1959
David Brazdil56e1acc2015-06-30 15:41:36 +01001960 bool CanThrow() const OVERRIDE { return true; }
1961
David Brazdilfc6a86a2015-06-26 10:33:45 +00001962 // Returns the block's non-exceptional successor (index zero).
1963 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors().Get(0); }
1964
1965 // Returns whether `handler` is among its exception handlers (non-zero index
1966 // successors).
1967 bool HasExceptionHandler(HBasicBlock* handler) const {
1968 DCHECK(handler->IsCatchBlock());
1969 return GetBlock()->GetSuccessors().Contains(handler, /* start_from */ 1);
1970 }
1971
1972 // Returns whether successor at index `idx` is an exception handler.
1973 bool IsExceptionalSuccessor(size_t idx) const {
1974 DCHECK_LT(idx, GetBlock()->GetSuccessors().Size());
1975 bool is_handler = (idx != 0);
1976 DCHECK(!is_handler || GetBlock()->GetSuccessors().Get(idx)->IsCatchBlock());
1977 return is_handler;
1978 }
1979
1980 // If not present already, adds `handler` to its block's list of exception
1981 // handlers.
1982 void AddExceptionHandler(HBasicBlock* handler) {
1983 if (!HasExceptionHandler(handler)) {
1984 GetBlock()->AddSuccessor(handler);
1985 }
1986 }
1987
David Brazdil56e1acc2015-06-30 15:41:36 +01001988 bool IsEntry() const { return kind_ == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00001989
1990 DECLARE_INSTRUCTION(TryBoundary);
1991
1992 private:
David Brazdil56e1acc2015-06-30 15:41:36 +01001993 const BoundaryKind kind_;
David Brazdilfc6a86a2015-06-26 10:33:45 +00001994
1995 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
1996};
1997
1998
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001999// Deoptimize to interpreter, upon checking a condition.
2000class HDeoptimize : public HTemplateInstruction<1> {
2001 public:
2002 HDeoptimize(HInstruction* cond, uint32_t dex_pc)
2003 : HTemplateInstruction(SideEffects::None()),
2004 dex_pc_(dex_pc) {
2005 SetRawInputAt(0, cond);
2006 }
2007
2008 bool NeedsEnvironment() const OVERRIDE { return true; }
2009 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002010 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002011
2012 DECLARE_INSTRUCTION(Deoptimize);
2013
2014 private:
2015 uint32_t dex_pc_;
2016
2017 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
2018};
2019
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002020// Represents the ArtMethod that was passed as a first argument to
2021// the method. It is used by instructions that depend on it, like
2022// instructions that work with the dex cache.
2023class HCurrentMethod : public HExpression<0> {
2024 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -07002025 explicit HCurrentMethod(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002026
2027 DECLARE_INSTRUCTION(CurrentMethod);
2028
2029 private:
2030 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
2031};
2032
Roland Levillain88cb1752014-10-20 16:36:47 +01002033class HUnaryOperation : public HExpression<1> {
2034 public:
2035 HUnaryOperation(Primitive::Type result_type, HInstruction* input)
2036 : HExpression(result_type, SideEffects::None()) {
2037 SetRawInputAt(0, input);
2038 }
2039
2040 HInstruction* GetInput() const { return InputAt(0); }
2041 Primitive::Type GetResultType() const { return GetType(); }
2042
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002043 bool CanBeMoved() const OVERRIDE { return true; }
2044 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002045 UNUSED(other);
2046 return true;
2047 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002048
Roland Levillain9240d6a2014-10-20 16:47:04 +01002049 // Try to statically evaluate `operation` and return a HConstant
2050 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002051 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002052 HConstant* TryStaticEvaluation() const;
2053
2054 // Apply this operation to `x`.
2055 virtual int32_t Evaluate(int32_t x) const = 0;
2056 virtual int64_t Evaluate(int64_t x) const = 0;
2057
Roland Levillain88cb1752014-10-20 16:36:47 +01002058 DECLARE_INSTRUCTION(UnaryOperation);
2059
2060 private:
2061 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
2062};
2063
Dave Allison20dfc792014-06-16 20:44:29 -07002064class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002065 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002066 HBinaryOperation(Primitive::Type result_type,
2067 HInstruction* left,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002068 HInstruction* right) : HExpression(result_type, SideEffects::None()) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002069 SetRawInputAt(0, left);
2070 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002071 }
2072
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002073 HInstruction* GetLeft() const { return InputAt(0); }
2074 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07002075 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002076
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002077 virtual bool IsCommutative() const { return false; }
2078
2079 // Put constant on the right.
2080 // Returns whether order is changed.
2081 bool OrderInputsWithConstantOnTheRight() {
2082 HInstruction* left = InputAt(0);
2083 HInstruction* right = InputAt(1);
2084 if (left->IsConstant() && !right->IsConstant()) {
2085 ReplaceInput(right, 0);
2086 ReplaceInput(left, 1);
2087 return true;
2088 }
2089 return false;
2090 }
2091
2092 // Order inputs by instruction id, but favor constant on the right side.
2093 // This helps GVN for commutative ops.
2094 void OrderInputs() {
2095 DCHECK(IsCommutative());
2096 HInstruction* left = InputAt(0);
2097 HInstruction* right = InputAt(1);
2098 if (left == right || (!left->IsConstant() && right->IsConstant())) {
2099 return;
2100 }
2101 if (OrderInputsWithConstantOnTheRight()) {
2102 return;
2103 }
2104 // Order according to instruction id.
2105 if (left->GetId() > right->GetId()) {
2106 ReplaceInput(right, 0);
2107 ReplaceInput(left, 1);
2108 }
2109 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002110
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002111 bool CanBeMoved() const OVERRIDE { return true; }
2112 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002113 UNUSED(other);
2114 return true;
2115 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002116
Roland Levillain9240d6a2014-10-20 16:47:04 +01002117 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01002118 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002119 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002120 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01002121
2122 // Apply this operation to `x` and `y`.
2123 virtual int32_t Evaluate(int32_t x, int32_t y) const = 0;
2124 virtual int64_t Evaluate(int64_t x, int64_t y) const = 0;
2125
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002126 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002127 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002128 HConstant* GetConstantRight() const;
2129
2130 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002131 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002132 HInstruction* GetLeastConstantLeft() const;
2133
Roland Levillainccc07a92014-09-16 14:48:16 +01002134 DECLARE_INSTRUCTION(BinaryOperation);
2135
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002136 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002137 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
2138};
2139
Dave Allison20dfc792014-06-16 20:44:29 -07002140class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002141 public:
Dave Allison20dfc792014-06-16 20:44:29 -07002142 HCondition(HInstruction* first, HInstruction* second)
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002143 : HBinaryOperation(Primitive::kPrimBoolean, first, second),
2144 needs_materialization_(true) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002145
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002146 bool NeedsMaterialization() const { return needs_materialization_; }
2147 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002148
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002149 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002150 // `instruction`, and disregard moves in between.
2151 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002152
Dave Allison20dfc792014-06-16 20:44:29 -07002153 DECLARE_INSTRUCTION(Condition);
2154
2155 virtual IfCondition GetCondition() const = 0;
2156
2157 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002158 // For register allocation purposes, returns whether this instruction needs to be
2159 // materialized (that is, not just be in the processor flags).
2160 bool needs_materialization_;
2161
Dave Allison20dfc792014-06-16 20:44:29 -07002162 DISALLOW_COPY_AND_ASSIGN(HCondition);
2163};
2164
2165// Instruction to check if two inputs are equal to each other.
2166class HEqual : public HCondition {
2167 public:
2168 HEqual(HInstruction* first, HInstruction* second)
2169 : HCondition(first, second) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002170
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002171 bool IsCommutative() const OVERRIDE { return true; }
2172
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002173 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002174 return x == y ? 1 : 0;
2175 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002176 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002177 return x == y ? 1 : 0;
2178 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002179
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002180 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002181
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002182 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002183 return kCondEQ;
2184 }
2185
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002186 private:
2187 DISALLOW_COPY_AND_ASSIGN(HEqual);
2188};
2189
Dave Allison20dfc792014-06-16 20:44:29 -07002190class HNotEqual : public HCondition {
2191 public:
2192 HNotEqual(HInstruction* first, HInstruction* second)
2193 : HCondition(first, second) {}
2194
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002195 bool IsCommutative() const OVERRIDE { return true; }
2196
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002197 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002198 return x != y ? 1 : 0;
2199 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002200 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002201 return x != y ? 1 : 0;
2202 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002203
Dave Allison20dfc792014-06-16 20:44:29 -07002204 DECLARE_INSTRUCTION(NotEqual);
2205
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002206 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002207 return kCondNE;
2208 }
2209
2210 private:
2211 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
2212};
2213
2214class HLessThan : public HCondition {
2215 public:
2216 HLessThan(HInstruction* first, HInstruction* second)
2217 : HCondition(first, second) {}
2218
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002219 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002220 return x < y ? 1 : 0;
2221 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002222 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002223 return x < y ? 1 : 0;
2224 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002225
Dave Allison20dfc792014-06-16 20:44:29 -07002226 DECLARE_INSTRUCTION(LessThan);
2227
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002228 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002229 return kCondLT;
2230 }
2231
2232 private:
2233 DISALLOW_COPY_AND_ASSIGN(HLessThan);
2234};
2235
2236class HLessThanOrEqual : public HCondition {
2237 public:
2238 HLessThanOrEqual(HInstruction* first, HInstruction* second)
2239 : HCondition(first, second) {}
2240
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002241 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002242 return x <= y ? 1 : 0;
2243 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002244 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002245 return x <= y ? 1 : 0;
2246 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002247
Dave Allison20dfc792014-06-16 20:44:29 -07002248 DECLARE_INSTRUCTION(LessThanOrEqual);
2249
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002250 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002251 return kCondLE;
2252 }
2253
2254 private:
2255 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
2256};
2257
2258class HGreaterThan : public HCondition {
2259 public:
2260 HGreaterThan(HInstruction* first, HInstruction* second)
2261 : HCondition(first, second) {}
2262
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002263 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002264 return x > y ? 1 : 0;
2265 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002266 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002267 return x > y ? 1 : 0;
2268 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002269
Dave Allison20dfc792014-06-16 20:44:29 -07002270 DECLARE_INSTRUCTION(GreaterThan);
2271
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002272 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002273 return kCondGT;
2274 }
2275
2276 private:
2277 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
2278};
2279
2280class HGreaterThanOrEqual : public HCondition {
2281 public:
2282 HGreaterThanOrEqual(HInstruction* first, HInstruction* second)
2283 : HCondition(first, second) {}
2284
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002285 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002286 return x >= y ? 1 : 0;
2287 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002288 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002289 return x >= y ? 1 : 0;
2290 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002291
Dave Allison20dfc792014-06-16 20:44:29 -07002292 DECLARE_INSTRUCTION(GreaterThanOrEqual);
2293
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002294 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002295 return kCondGE;
2296 }
2297
2298 private:
2299 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
2300};
2301
2302
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002303// Instruction to check how two inputs compare to each other.
2304// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
2305class HCompare : public HBinaryOperation {
2306 public:
Calin Juravleddb7df22014-11-25 20:56:51 +00002307 // The bias applies for floating point operations and indicates how NaN
2308 // comparisons are treated:
2309 enum Bias {
2310 kNoBias, // bias is not applicable (i.e. for long operation)
2311 kGtBias, // return 1 for NaN comparisons
2312 kLtBias, // return -1 for NaN comparisons
2313 };
2314
Alexey Frunze4dda3372015-06-01 18:31:49 -07002315 HCompare(Primitive::Type type,
2316 HInstruction* first,
2317 HInstruction* second,
2318 Bias bias,
2319 uint32_t dex_pc)
2320 : HBinaryOperation(Primitive::kPrimInt, first, second), bias_(bias), dex_pc_(dex_pc) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002321 DCHECK_EQ(type, first->GetType());
2322 DCHECK_EQ(type, second->GetType());
2323 }
2324
Calin Juravleddb7df22014-11-25 20:56:51 +00002325 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01002326 return
2327 x == y ? 0 :
2328 x > y ? 1 :
2329 -1;
2330 }
Calin Juravleddb7df22014-11-25 20:56:51 +00002331
2332 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01002333 return
2334 x == y ? 0 :
2335 x > y ? 1 :
2336 -1;
2337 }
2338
Calin Juravleddb7df22014-11-25 20:56:51 +00002339 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2340 return bias_ == other->AsCompare()->bias_;
2341 }
2342
2343 bool IsGtBias() { return bias_ == kGtBias; }
2344
Alexey Frunze4dda3372015-06-01 18:31:49 -07002345 uint32_t GetDexPc() const { return dex_pc_; }
2346
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002347 DECLARE_INSTRUCTION(Compare);
2348
2349 private:
Calin Juravleddb7df22014-11-25 20:56:51 +00002350 const Bias bias_;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002351 const uint32_t dex_pc_;
Calin Juravleddb7df22014-11-25 20:56:51 +00002352
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002353 DISALLOW_COPY_AND_ASSIGN(HCompare);
2354};
2355
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002356// A local in the graph. Corresponds to a Dex register.
2357class HLocal : public HTemplateInstruction<0> {
2358 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002359 explicit HLocal(uint16_t reg_number)
2360 : HTemplateInstruction(SideEffects::None()), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002361
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002362 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002363
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002364 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002365
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002366 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002367 // The Dex register number.
2368 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002369
2370 DISALLOW_COPY_AND_ASSIGN(HLocal);
2371};
2372
2373// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07002374class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002375 public:
Roland Levillain5799fc02014-09-25 12:15:20 +01002376 HLoadLocal(HLocal* local, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002377 : HExpression(type, SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002378 SetRawInputAt(0, local);
2379 }
2380
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002381 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
2382
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002383 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002384
2385 private:
2386 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
2387};
2388
2389// Store a value in a given local. This instruction has two inputs: the value
2390// and the local.
2391class HStoreLocal : public HTemplateInstruction<2> {
2392 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002393 HStoreLocal(HLocal* local, HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002394 SetRawInputAt(0, local);
2395 SetRawInputAt(1, value);
2396 }
2397
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002398 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
2399
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002400 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002401
2402 private:
2403 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
2404};
2405
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002406class HConstant : public HExpression<0> {
2407 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002408 explicit HConstant(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
2409
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002410 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002411
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002412 virtual bool IsMinusOne() const { return false; }
2413 virtual bool IsZero() const { return false; }
2414 virtual bool IsOne() const { return false; }
2415
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002416 DECLARE_INSTRUCTION(Constant);
2417
2418 private:
2419 DISALLOW_COPY_AND_ASSIGN(HConstant);
2420};
2421
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002422class HFloatConstant : public HConstant {
2423 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002424 float GetValue() const { return value_; }
2425
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002426 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002427 return bit_cast<uint32_t, float>(other->AsFloatConstant()->value_) ==
2428 bit_cast<uint32_t, float>(value_);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002429 }
2430
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002431 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002432
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002433 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002434 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002435 }
2436 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002437 return value_ == 0.0f;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002438 }
2439 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002440 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
2441 }
2442 bool IsNaN() const {
2443 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002444 }
2445
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002446 DECLARE_INSTRUCTION(FloatConstant);
2447
2448 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002449 explicit HFloatConstant(float value) : HConstant(Primitive::kPrimFloat), value_(value) {}
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002450 explicit HFloatConstant(int32_t value)
2451 : HConstant(Primitive::kPrimFloat), value_(bit_cast<float, int32_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00002452
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002453 const float value_;
2454
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002455 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00002456 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002457 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002458 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2459};
2460
2461class HDoubleConstant : public HConstant {
2462 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002463 double GetValue() const { return value_; }
2464
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002465 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002466 return bit_cast<uint64_t, double>(other->AsDoubleConstant()->value_) ==
2467 bit_cast<uint64_t, double>(value_);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002468 }
2469
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002470 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002471
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002472 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002473 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002474 }
2475 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002476 return value_ == 0.0;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002477 }
2478 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002479 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
2480 }
2481 bool IsNaN() const {
2482 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002483 }
2484
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002485 DECLARE_INSTRUCTION(DoubleConstant);
2486
2487 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002488 explicit HDoubleConstant(double value) : HConstant(Primitive::kPrimDouble), value_(value) {}
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002489 explicit HDoubleConstant(int64_t value)
2490 : HConstant(Primitive::kPrimDouble), value_(bit_cast<double, int64_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00002491
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002492 const double value_;
2493
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002494 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00002495 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002496 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002497 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
2498};
2499
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002500class HNullConstant : public HConstant {
2501 public:
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002502 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2503 return true;
2504 }
2505
2506 size_t ComputeHashCode() const OVERRIDE { return 0; }
2507
2508 DECLARE_INSTRUCTION(NullConstant);
2509
2510 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002511 HNullConstant() : HConstant(Primitive::kPrimNot) {}
2512
2513 friend class HGraph;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002514 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2515};
2516
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002517// Constants of the type int. Those can be from Dex instructions, or
2518// synthesized (for example with the if-eqz instruction).
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002519class HIntConstant : public HConstant {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002520 public:
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002521 int32_t GetValue() const { return value_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002522
Calin Juravle61d544b2015-02-23 16:46:57 +00002523 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002524 return other->AsIntConstant()->value_ == value_;
2525 }
2526
Calin Juravle61d544b2015-02-23 16:46:57 +00002527 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2528
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002529 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2530 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2531 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2532
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002533 DECLARE_INSTRUCTION(IntConstant);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002534
2535 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002536 explicit HIntConstant(int32_t value) : HConstant(Primitive::kPrimInt), value_(value) {}
2537
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002538 const int32_t value_;
2539
David Brazdil8d5b8b22015-03-24 10:51:52 +00002540 friend class HGraph;
2541 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
Zheng Xuad4450e2015-04-17 18:48:56 +08002542 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002543 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2544};
2545
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002546class HLongConstant : public HConstant {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002547 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002548 int64_t GetValue() const { return value_; }
2549
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002550 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002551 return other->AsLongConstant()->value_ == value_;
2552 }
2553
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002554 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002555
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002556 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2557 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2558 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2559
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002560 DECLARE_INSTRUCTION(LongConstant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002561
2562 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002563 explicit HLongConstant(int64_t value) : HConstant(Primitive::kPrimLong), value_(value) {}
2564
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002565 const int64_t value_;
2566
David Brazdil8d5b8b22015-03-24 10:51:52 +00002567 friend class HGraph;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002568 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2569};
2570
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002571enum class Intrinsics {
2572#define OPTIMIZING_INTRINSICS(Name, IsStatic) k ## Name,
2573#include "intrinsics_list.h"
2574 kNone,
2575 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
2576#undef INTRINSICS_LIST
2577#undef OPTIMIZING_INTRINSICS
2578};
2579std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
2580
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002581class HInvoke : public HInstruction {
2582 public:
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002583 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002584
2585 // Runtime needs to walk the stack, so Dex -> Dex calls need to
2586 // know their environment.
Calin Juravle77520bc2015-01-12 18:45:46 +00002587 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002588
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01002589 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002590 SetRawInputAt(index, argument);
2591 }
2592
Roland Levillain3e3d7332015-04-28 11:00:54 +01002593 // Return the number of arguments. This number can be lower than
2594 // the number of inputs returned by InputCount(), as some invoke
2595 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
2596 // inputs at the end of their list of inputs.
2597 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
2598
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002599 Primitive::Type GetType() const OVERRIDE { return return_type_; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002600
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002601 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002602
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002603 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002604 const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002605
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002606 InvokeType GetOriginalInvokeType() const { return original_invoke_type_; }
2607
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01002608 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002609 return intrinsic_;
2610 }
2611
2612 void SetIntrinsic(Intrinsics intrinsic) {
2613 intrinsic_ = intrinsic;
2614 }
2615
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01002616 bool IsFromInlinedInvoke() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002617 return GetEnvironment()->GetParent() != nullptr;
2618 }
2619
2620 bool CanThrow() const OVERRIDE { return true; }
2621
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002622 DECLARE_INSTRUCTION(Invoke);
2623
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002624 protected:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002625 HInvoke(ArenaAllocator* arena,
2626 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01002627 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002628 Primitive::Type return_type,
2629 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002630 uint32_t dex_method_index,
2631 InvokeType original_invoke_type)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002632 : HInstruction(SideEffects::All()),
Roland Levillain3e3d7332015-04-28 11:00:54 +01002633 number_of_arguments_(number_of_arguments),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002634 inputs_(arena, number_of_arguments),
2635 return_type_(return_type),
2636 dex_pc_(dex_pc),
2637 dex_method_index_(dex_method_index),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002638 original_invoke_type_(original_invoke_type),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002639 intrinsic_(Intrinsics::kNone) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01002640 uint32_t number_of_inputs = number_of_arguments + number_of_other_inputs;
2641 inputs_.SetSize(number_of_inputs);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002642 }
2643
David Brazdil1abb4192015-02-17 18:33:36 +00002644 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
2645 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
2646 inputs_.Put(index, input);
2647 }
2648
Roland Levillain3e3d7332015-04-28 11:00:54 +01002649 uint32_t number_of_arguments_;
David Brazdil1abb4192015-02-17 18:33:36 +00002650 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002651 const Primitive::Type return_type_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002652 const uint32_t dex_pc_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002653 const uint32_t dex_method_index_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002654 const InvokeType original_invoke_type_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002655 Intrinsics intrinsic_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002656
2657 private:
2658 DISALLOW_COPY_AND_ASSIGN(HInvoke);
2659};
2660
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002661class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002662 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01002663 // Requirements of this method call regarding the class
2664 // initialization (clinit) check of its declaring class.
2665 enum class ClinitCheckRequirement {
2666 kNone, // Class already initialized.
2667 kExplicit, // Static call having explicit clinit check as last input.
2668 kImplicit, // Static call implicitly requiring a clinit check.
2669 };
2670
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002671 HInvokeStaticOrDirect(ArenaAllocator* arena,
2672 uint32_t number_of_arguments,
2673 Primitive::Type return_type,
2674 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002675 uint32_t dex_method_index,
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002676 bool is_recursive,
Jeff Hao848f70a2014-01-15 13:49:50 -08002677 int32_t string_init_offset,
Nicolas Geoffray79041292015-03-26 10:05:54 +00002678 InvokeType original_invoke_type,
Roland Levillain4c0eb422015-04-24 16:43:49 +01002679 InvokeType invoke_type,
2680 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01002681 : HInvoke(arena,
2682 number_of_arguments,
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002683 // There is one extra argument for the HCurrentMethod node, and
2684 // potentially one other if the clinit check is explicit.
2685 clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 2u : 1u,
Roland Levillain3e3d7332015-04-28 11:00:54 +01002686 return_type,
2687 dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002688 dex_method_index,
2689 original_invoke_type),
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002690 invoke_type_(invoke_type),
Roland Levillain4c0eb422015-04-24 16:43:49 +01002691 is_recursive_(is_recursive),
Jeff Hao848f70a2014-01-15 13:49:50 -08002692 clinit_check_requirement_(clinit_check_requirement),
2693 string_init_offset_(string_init_offset) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002694
Calin Juravle641547a2015-04-21 22:08:51 +01002695 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
2696 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00002697 // We access the method via the dex cache so we can't do an implicit null check.
2698 // TODO: for intrinsics we can generate implicit null checks.
2699 return false;
2700 }
2701
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002702 InvokeType GetInvokeType() const { return invoke_type_; }
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002703 bool IsRecursive() const { return is_recursive_; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002704 bool NeedsDexCache() const OVERRIDE { return !IsRecursive(); }
Jeff Hao848f70a2014-01-15 13:49:50 -08002705 bool IsStringInit() const { return string_init_offset_ != 0; }
2706 int32_t GetStringInitOffset() const { return string_init_offset_; }
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002707 uint32_t GetCurrentMethodInputIndex() const { return GetNumberOfArguments(); }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002708
Roland Levillain4c0eb422015-04-24 16:43:49 +01002709 // Is this instruction a call to a static method?
2710 bool IsStatic() const {
2711 return GetInvokeType() == kStatic;
2712 }
2713
Roland Levillain3e3d7332015-04-28 11:00:54 +01002714 // Remove the art::HLoadClass instruction set as last input by
2715 // art::PrepareForRegisterAllocation::VisitClinitCheck in lieu of
2716 // the initial art::HClinitCheck instruction (only relevant for
2717 // static calls with explicit clinit check).
2718 void RemoveLoadClassAsLastInput() {
Roland Levillain4c0eb422015-04-24 16:43:49 +01002719 DCHECK(IsStaticWithExplicitClinitCheck());
2720 size_t last_input_index = InputCount() - 1;
2721 HInstruction* last_input = InputAt(last_input_index);
2722 DCHECK(last_input != nullptr);
Roland Levillain3e3d7332015-04-28 11:00:54 +01002723 DCHECK(last_input->IsLoadClass()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01002724 RemoveAsUserOfInput(last_input_index);
2725 inputs_.DeleteAt(last_input_index);
2726 clinit_check_requirement_ = ClinitCheckRequirement::kImplicit;
2727 DCHECK(IsStaticWithImplicitClinitCheck());
2728 }
2729
2730 // Is this a call to a static method whose declaring class has an
2731 // explicit intialization check in the graph?
2732 bool IsStaticWithExplicitClinitCheck() const {
2733 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kExplicit);
2734 }
2735
2736 // Is this a call to a static method whose declaring class has an
2737 // implicit intialization check requirement?
2738 bool IsStaticWithImplicitClinitCheck() const {
2739 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kImplicit);
2740 }
2741
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002742 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002743
Roland Levillain4c0eb422015-04-24 16:43:49 +01002744 protected:
2745 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
2746 const HUserRecord<HInstruction*> input_record = HInvoke::InputRecordAt(i);
2747 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck() && (i == InputCount() - 1)) {
2748 HInstruction* input = input_record.GetInstruction();
2749 // `input` is the last input of a static invoke marked as having
2750 // an explicit clinit check. It must either be:
2751 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
2752 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
2753 DCHECK(input != nullptr);
2754 DCHECK(input->IsClinitCheck() || input->IsLoadClass()) << input->DebugName();
2755 }
2756 return input_record;
2757 }
2758
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002759 private:
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002760 const InvokeType invoke_type_;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002761 const bool is_recursive_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01002762 ClinitCheckRequirement clinit_check_requirement_;
Jeff Hao848f70a2014-01-15 13:49:50 -08002763 // Thread entrypoint offset for string init method if this is a string init invoke.
2764 // Note that there are multiple string init methods, each having its own offset.
2765 int32_t string_init_offset_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002766
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002767 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002768};
2769
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002770class HInvokeVirtual : public HInvoke {
2771 public:
2772 HInvokeVirtual(ArenaAllocator* arena,
2773 uint32_t number_of_arguments,
2774 Primitive::Type return_type,
2775 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002776 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002777 uint32_t vtable_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002778 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002779 vtable_index_(vtable_index) {}
2780
Calin Juravle641547a2015-04-21 22:08:51 +01002781 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00002782 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01002783 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00002784 }
2785
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002786 uint32_t GetVTableIndex() const { return vtable_index_; }
2787
2788 DECLARE_INSTRUCTION(InvokeVirtual);
2789
2790 private:
2791 const uint32_t vtable_index_;
2792
2793 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
2794};
2795
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002796class HInvokeInterface : public HInvoke {
2797 public:
2798 HInvokeInterface(ArenaAllocator* arena,
2799 uint32_t number_of_arguments,
2800 Primitive::Type return_type,
2801 uint32_t dex_pc,
2802 uint32_t dex_method_index,
2803 uint32_t imt_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002804 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002805 imt_index_(imt_index) {}
2806
Calin Juravle641547a2015-04-21 22:08:51 +01002807 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00002808 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01002809 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00002810 }
2811
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002812 uint32_t GetImtIndex() const { return imt_index_; }
2813 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
2814
2815 DECLARE_INSTRUCTION(InvokeInterface);
2816
2817 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002818 const uint32_t imt_index_;
2819
2820 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
2821};
2822
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01002823class HNewInstance : public HExpression<1> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002824 public:
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01002825 HNewInstance(HCurrentMethod* current_method,
2826 uint32_t dex_pc,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002827 uint16_t type_index,
2828 const DexFile& dex_file,
2829 QuickEntrypointEnum entrypoint)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002830 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2831 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002832 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002833 dex_file_(dex_file),
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01002834 entrypoint_(entrypoint) {
2835 SetRawInputAt(0, current_method);
2836 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002837
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002838 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002839 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002840 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002841
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002842 // Calls runtime so needs an environment.
Calin Juravle92a6ed22014-12-02 18:58:03 +00002843 bool NeedsEnvironment() const OVERRIDE { return true; }
2844 // It may throw when called on:
2845 // - interfaces
2846 // - abstract/innaccessible/unknown classes
2847 // TODO: optimize when possible.
2848 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002849
Calin Juravle10e244f2015-01-26 18:54:32 +00002850 bool CanBeNull() const OVERRIDE { return false; }
2851
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002852 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
2853
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002854 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002855
2856 private:
2857 const uint32_t dex_pc_;
2858 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002859 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002860 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002861
2862 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
2863};
2864
Roland Levillain88cb1752014-10-20 16:36:47 +01002865class HNeg : public HUnaryOperation {
2866 public:
2867 explicit HNeg(Primitive::Type result_type, HInstruction* input)
2868 : HUnaryOperation(result_type, input) {}
2869
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002870 int32_t Evaluate(int32_t x) const OVERRIDE { return -x; }
2871 int64_t Evaluate(int64_t x) const OVERRIDE { return -x; }
Roland Levillain9240d6a2014-10-20 16:47:04 +01002872
Roland Levillain88cb1752014-10-20 16:36:47 +01002873 DECLARE_INSTRUCTION(Neg);
2874
2875 private:
2876 DISALLOW_COPY_AND_ASSIGN(HNeg);
2877};
2878
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01002879class HNewArray : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002880 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002881 HNewArray(HInstruction* length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01002882 HCurrentMethod* current_method,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002883 uint32_t dex_pc,
2884 uint16_t type_index,
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01002885 const DexFile& dex_file,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002886 QuickEntrypointEnum entrypoint)
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002887 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2888 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002889 type_index_(type_index),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01002890 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002891 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002892 SetRawInputAt(0, length);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01002893 SetRawInputAt(1, current_method);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002894 }
2895
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002896 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002897 uint16_t GetTypeIndex() const { return type_index_; }
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01002898 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002899
2900 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00002901 bool NeedsEnvironment() const OVERRIDE { return true; }
2902
Mingyao Yang0c365e62015-03-31 15:09:29 -07002903 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
2904 bool CanThrow() const OVERRIDE { return true; }
2905
Calin Juravle10e244f2015-01-26 18:54:32 +00002906 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002907
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002908 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
2909
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002910 DECLARE_INSTRUCTION(NewArray);
2911
2912 private:
2913 const uint32_t dex_pc_;
2914 const uint16_t type_index_;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01002915 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002916 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002917
2918 DISALLOW_COPY_AND_ASSIGN(HNewArray);
2919};
2920
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002921class HAdd : public HBinaryOperation {
2922 public:
2923 HAdd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2924 : HBinaryOperation(result_type, left, right) {}
2925
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002926 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002927
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002928 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002929 return x + y;
2930 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002931 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002932 return x + y;
2933 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002934
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002935 DECLARE_INSTRUCTION(Add);
2936
2937 private:
2938 DISALLOW_COPY_AND_ASSIGN(HAdd);
2939};
2940
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002941class HSub : public HBinaryOperation {
2942 public:
2943 HSub(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2944 : HBinaryOperation(result_type, left, right) {}
2945
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002946 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002947 return x - y;
2948 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002949 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002950 return x - y;
2951 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002952
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002953 DECLARE_INSTRUCTION(Sub);
2954
2955 private:
2956 DISALLOW_COPY_AND_ASSIGN(HSub);
2957};
2958
Calin Juravle34bacdf2014-10-07 20:23:36 +01002959class HMul : public HBinaryOperation {
2960 public:
2961 HMul(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2962 : HBinaryOperation(result_type, left, right) {}
2963
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002964 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002965
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002966 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x * y; }
2967 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x * y; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002968
2969 DECLARE_INSTRUCTION(Mul);
2970
2971 private:
2972 DISALLOW_COPY_AND_ASSIGN(HMul);
2973};
2974
Calin Juravle7c4954d2014-10-28 16:57:40 +00002975class HDiv : public HBinaryOperation {
2976 public:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002977 HDiv(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
2978 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00002979
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002980 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00002981 // Our graph structure ensures we never have 0 for `y` during constant folding.
2982 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00002983 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00002984 return (y == -1) ? -x : x / y;
2985 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002986
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002987 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002988 DCHECK_NE(y, 0);
2989 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2990 return (y == -1) ? -x : x / y;
2991 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002992
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002993 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002994
Calin Juravle7c4954d2014-10-28 16:57:40 +00002995 DECLARE_INSTRUCTION(Div);
2996
2997 private:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002998 const uint32_t dex_pc_;
2999
Calin Juravle7c4954d2014-10-28 16:57:40 +00003000 DISALLOW_COPY_AND_ASSIGN(HDiv);
3001};
3002
Calin Juravlebacfec32014-11-14 15:54:36 +00003003class HRem : public HBinaryOperation {
3004 public:
3005 HRem(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
3006 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
3007
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003008 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00003009 DCHECK_NE(y, 0);
3010 // Special case -1 to avoid getting a SIGFPE on x86(_64).
3011 return (y == -1) ? 0 : x % y;
3012 }
3013
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003014 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00003015 DCHECK_NE(y, 0);
3016 // Special case -1 to avoid getting a SIGFPE on x86(_64).
3017 return (y == -1) ? 0 : x % y;
3018 }
3019
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003020 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Calin Juravlebacfec32014-11-14 15:54:36 +00003021
3022 DECLARE_INSTRUCTION(Rem);
3023
3024 private:
3025 const uint32_t dex_pc_;
3026
3027 DISALLOW_COPY_AND_ASSIGN(HRem);
3028};
3029
Calin Juravled0d48522014-11-04 16:40:20 +00003030class HDivZeroCheck : public HExpression<1> {
3031 public:
3032 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
3033 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
3034 SetRawInputAt(0, value);
3035 }
3036
3037 bool CanBeMoved() const OVERRIDE { return true; }
3038
3039 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3040 UNUSED(other);
3041 return true;
3042 }
3043
3044 bool NeedsEnvironment() const OVERRIDE { return true; }
3045 bool CanThrow() const OVERRIDE { return true; }
3046
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003047 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Calin Juravled0d48522014-11-04 16:40:20 +00003048
3049 DECLARE_INSTRUCTION(DivZeroCheck);
3050
3051 private:
3052 const uint32_t dex_pc_;
3053
3054 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
3055};
3056
Calin Juravle9aec02f2014-11-18 23:06:35 +00003057class HShl : public HBinaryOperation {
3058 public:
3059 HShl(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3060 : HBinaryOperation(result_type, left, right) {}
3061
3062 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x << (y & kMaxIntShiftValue); }
3063 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x << (y & kMaxLongShiftValue); }
3064
3065 DECLARE_INSTRUCTION(Shl);
3066
3067 private:
3068 DISALLOW_COPY_AND_ASSIGN(HShl);
3069};
3070
3071class HShr : public HBinaryOperation {
3072 public:
3073 HShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3074 : HBinaryOperation(result_type, left, right) {}
3075
3076 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x >> (y & kMaxIntShiftValue); }
3077 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x >> (y & kMaxLongShiftValue); }
3078
3079 DECLARE_INSTRUCTION(Shr);
3080
3081 private:
3082 DISALLOW_COPY_AND_ASSIGN(HShr);
3083};
3084
3085class HUShr : public HBinaryOperation {
3086 public:
3087 HUShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3088 : HBinaryOperation(result_type, left, right) {}
3089
3090 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
3091 uint32_t ux = static_cast<uint32_t>(x);
3092 uint32_t uy = static_cast<uint32_t>(y) & kMaxIntShiftValue;
3093 return static_cast<int32_t>(ux >> uy);
3094 }
3095
3096 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
3097 uint64_t ux = static_cast<uint64_t>(x);
3098 uint64_t uy = static_cast<uint64_t>(y) & kMaxLongShiftValue;
3099 return static_cast<int64_t>(ux >> uy);
3100 }
3101
3102 DECLARE_INSTRUCTION(UShr);
3103
3104 private:
3105 DISALLOW_COPY_AND_ASSIGN(HUShr);
3106};
3107
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003108class HAnd : public HBinaryOperation {
3109 public:
3110 HAnd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3111 : HBinaryOperation(result_type, left, right) {}
3112
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003113 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003114
3115 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x & y; }
3116 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x & y; }
3117
3118 DECLARE_INSTRUCTION(And);
3119
3120 private:
3121 DISALLOW_COPY_AND_ASSIGN(HAnd);
3122};
3123
3124class HOr : public HBinaryOperation {
3125 public:
3126 HOr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3127 : HBinaryOperation(result_type, left, right) {}
3128
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003129 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003130
3131 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x | y; }
3132 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x | y; }
3133
3134 DECLARE_INSTRUCTION(Or);
3135
3136 private:
3137 DISALLOW_COPY_AND_ASSIGN(HOr);
3138};
3139
3140class HXor : public HBinaryOperation {
3141 public:
3142 HXor(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3143 : HBinaryOperation(result_type, left, right) {}
3144
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003145 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003146
3147 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x ^ y; }
3148 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x ^ y; }
3149
3150 DECLARE_INSTRUCTION(Xor);
3151
3152 private:
3153 DISALLOW_COPY_AND_ASSIGN(HXor);
3154};
3155
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003156// The value of a parameter in this method. Its location depends on
3157// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07003158class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003159 public:
Calin Juravle10e244f2015-01-26 18:54:32 +00003160 HParameterValue(uint8_t index, Primitive::Type parameter_type, bool is_this = false)
3161 : HExpression(parameter_type, SideEffects::None()), index_(index), is_this_(is_this) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003162
3163 uint8_t GetIndex() const { return index_; }
3164
Calin Juravle10e244f2015-01-26 18:54:32 +00003165 bool CanBeNull() const OVERRIDE { return !is_this_; }
3166
Calin Juravle3cd4fc82015-05-14 15:15:42 +01003167 bool IsThis() const { return is_this_; }
3168
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003169 DECLARE_INSTRUCTION(ParameterValue);
3170
3171 private:
3172 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00003173 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003174 const uint8_t index_;
3175
Calin Juravle10e244f2015-01-26 18:54:32 +00003176 // Whether or not the parameter value corresponds to 'this' argument.
3177 const bool is_this_;
3178
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003179 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
3180};
3181
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003182class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003183 public:
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003184 explicit HNot(Primitive::Type result_type, HInstruction* input)
3185 : HUnaryOperation(result_type, input) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003186
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003187 bool CanBeMoved() const OVERRIDE { return true; }
3188 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003189 UNUSED(other);
3190 return true;
3191 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003192
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003193 int32_t Evaluate(int32_t x) const OVERRIDE { return ~x; }
3194 int64_t Evaluate(int64_t x) const OVERRIDE { return ~x; }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003195
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003196 DECLARE_INSTRUCTION(Not);
3197
3198 private:
3199 DISALLOW_COPY_AND_ASSIGN(HNot);
3200};
3201
David Brazdil66d126e2015-04-03 16:02:44 +01003202class HBooleanNot : public HUnaryOperation {
3203 public:
3204 explicit HBooleanNot(HInstruction* input)
3205 : HUnaryOperation(Primitive::Type::kPrimBoolean, input) {}
3206
3207 bool CanBeMoved() const OVERRIDE { return true; }
3208 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3209 UNUSED(other);
3210 return true;
3211 }
3212
3213 int32_t Evaluate(int32_t x) const OVERRIDE {
3214 DCHECK(IsUint<1>(x));
3215 return !x;
3216 }
3217
3218 int64_t Evaluate(int64_t x ATTRIBUTE_UNUSED) const OVERRIDE {
3219 LOG(FATAL) << DebugName() << " cannot be used with 64-bit values";
3220 UNREACHABLE();
3221 }
3222
3223 DECLARE_INSTRUCTION(BooleanNot);
3224
3225 private:
3226 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
3227};
3228
Roland Levillaindff1f282014-11-05 14:15:05 +00003229class HTypeConversion : public HExpression<1> {
3230 public:
3231 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00003232 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
3233 : HExpression(result_type, SideEffects::None()), dex_pc_(dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00003234 SetRawInputAt(0, input);
3235 DCHECK_NE(input->GetType(), result_type);
3236 }
3237
3238 HInstruction* GetInput() const { return InputAt(0); }
3239 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
3240 Primitive::Type GetResultType() const { return GetType(); }
3241
Roland Levillain624279f2014-12-04 11:54:28 +00003242 // Required by the x86 and ARM code generators when producing calls
3243 // to the runtime.
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003244 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Roland Levillain624279f2014-12-04 11:54:28 +00003245
Roland Levillaindff1f282014-11-05 14:15:05 +00003246 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00003247 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00003248
Mark Mendelle82549b2015-05-06 10:55:34 -04003249 // Try to statically evaluate the conversion and return a HConstant
3250 // containing the result. If the input cannot be converted, return nullptr.
3251 HConstant* TryStaticEvaluation() const;
3252
Roland Levillaindff1f282014-11-05 14:15:05 +00003253 DECLARE_INSTRUCTION(TypeConversion);
3254
3255 private:
Roland Levillain624279f2014-12-04 11:54:28 +00003256 const uint32_t dex_pc_;
3257
Roland Levillaindff1f282014-11-05 14:15:05 +00003258 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
3259};
3260
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00003261static constexpr uint32_t kNoRegNumber = -1;
3262
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003263class HPhi : public HInstruction {
3264 public:
3265 HPhi(ArenaAllocator* arena, uint32_t reg_number, size_t number_of_inputs, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003266 : HInstruction(SideEffects::None()),
3267 inputs_(arena, number_of_inputs),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003268 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01003269 type_(type),
Calin Juravle10e244f2015-01-26 18:54:32 +00003270 is_live_(false),
3271 can_be_null_(true) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003272 inputs_.SetSize(number_of_inputs);
3273 }
3274
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00003275 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
3276 static Primitive::Type ToPhiType(Primitive::Type type) {
3277 switch (type) {
3278 case Primitive::kPrimBoolean:
3279 case Primitive::kPrimByte:
3280 case Primitive::kPrimShort:
3281 case Primitive::kPrimChar:
3282 return Primitive::kPrimInt;
3283 default:
3284 return type;
3285 }
3286 }
3287
Calin Juravle10e244f2015-01-26 18:54:32 +00003288 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003289
3290 void AddInput(HInstruction* input);
David Brazdil2d7352b2015-04-20 14:52:42 +01003291 void RemoveInputAt(size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003292
Calin Juravle10e244f2015-01-26 18:54:32 +00003293 Primitive::Type GetType() const OVERRIDE { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003294 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003295
Calin Juravle10e244f2015-01-26 18:54:32 +00003296 bool CanBeNull() const OVERRIDE { return can_be_null_; }
3297 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
3298
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003299 uint32_t GetRegNumber() const { return reg_number_; }
3300
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01003301 void SetDead() { is_live_ = false; }
3302 void SetLive() { is_live_ = true; }
3303 bool IsDead() const { return !is_live_; }
3304 bool IsLive() const { return is_live_; }
3305
Calin Juravlea4f88312015-04-16 12:57:19 +01003306 // Returns the next equivalent phi (starting from the current one) or null if there is none.
3307 // An equivalent phi is a phi having the same dex register and type.
3308 // It assumes that phis with the same dex register are adjacent.
3309 HPhi* GetNextEquivalentPhiWithSameType() {
3310 HInstruction* next = GetNext();
3311 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
3312 if (next->GetType() == GetType()) {
3313 return next->AsPhi();
3314 }
3315 next = next->GetNext();
3316 }
3317 return nullptr;
3318 }
3319
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003320 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003321
David Brazdil1abb4192015-02-17 18:33:36 +00003322 protected:
3323 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
3324
3325 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
3326 inputs_.Put(index, input);
3327 }
3328
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003329 private:
David Brazdil1abb4192015-02-17 18:33:36 +00003330 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003331 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003332 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01003333 bool is_live_;
Calin Juravle10e244f2015-01-26 18:54:32 +00003334 bool can_be_null_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003335
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003336 DISALLOW_COPY_AND_ASSIGN(HPhi);
3337};
3338
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003339class HNullCheck : public HExpression<1> {
3340 public:
3341 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003342 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003343 SetRawInputAt(0, value);
3344 }
3345
Calin Juravle10e244f2015-01-26 18:54:32 +00003346 bool CanBeMoved() const OVERRIDE { return true; }
3347 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003348 UNUSED(other);
3349 return true;
3350 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003351
Calin Juravle10e244f2015-01-26 18:54:32 +00003352 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003353
Calin Juravle10e244f2015-01-26 18:54:32 +00003354 bool CanThrow() const OVERRIDE { return true; }
3355
3356 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01003357
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003358 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003359
3360 DECLARE_INSTRUCTION(NullCheck);
3361
3362 private:
3363 const uint32_t dex_pc_;
3364
3365 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
3366};
3367
3368class FieldInfo : public ValueObject {
3369 public:
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003370 FieldInfo(MemberOffset field_offset,
3371 Primitive::Type field_type,
3372 bool is_volatile,
3373 uint32_t index,
3374 const DexFile& dex_file)
3375 : field_offset_(field_offset),
3376 field_type_(field_type),
3377 is_volatile_(is_volatile),
3378 index_(index),
3379 dex_file_(dex_file) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003380
3381 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003382 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003383 uint32_t GetFieldIndex() const { return index_; }
3384 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00003385 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003386
3387 private:
3388 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01003389 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00003390 const bool is_volatile_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003391 uint32_t index_;
3392 const DexFile& dex_file_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003393};
3394
3395class HInstanceFieldGet : public HExpression<1> {
3396 public:
3397 HInstanceFieldGet(HInstruction* value,
3398 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003399 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003400 bool is_volatile,
3401 uint32_t field_idx,
3402 const DexFile& dex_file)
Nicolas Geoffray2af23072015-04-30 11:15:40 +00003403 : HExpression(field_type, SideEffects::DependsOnSomething()),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003404 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003405 SetRawInputAt(0, value);
3406 }
3407
Calin Juravle10c9cbe2014-12-19 10:50:19 +00003408 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003409
3410 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3411 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
3412 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003413 }
3414
Calin Juravle641547a2015-04-21 22:08:51 +01003415 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3416 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00003417 }
3418
3419 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01003420 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
3421 }
3422
Calin Juravle52c48962014-12-16 17:02:57 +00003423 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003424 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003425 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003426 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003427
3428 DECLARE_INSTRUCTION(InstanceFieldGet);
3429
3430 private:
3431 const FieldInfo field_info_;
3432
3433 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
3434};
3435
3436class HInstanceFieldSet : public HTemplateInstruction<2> {
3437 public:
3438 HInstanceFieldSet(HInstruction* object,
3439 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01003440 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003441 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003442 bool is_volatile,
3443 uint32_t field_idx,
3444 const DexFile& dex_file)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003445 : HTemplateInstruction(SideEffects::ChangesSomething()),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003446 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003447 value_can_be_null_(true) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003448 SetRawInputAt(0, object);
3449 SetRawInputAt(1, value);
3450 }
3451
Calin Juravle641547a2015-04-21 22:08:51 +01003452 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3453 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00003454 }
3455
Calin Juravle52c48962014-12-16 17:02:57 +00003456 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003457 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003458 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003459 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003460 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003461 bool GetValueCanBeNull() const { return value_can_be_null_; }
3462 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003463
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003464 DECLARE_INSTRUCTION(InstanceFieldSet);
3465
3466 private:
3467 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003468 bool value_can_be_null_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003469
3470 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
3471};
3472
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003473class HArrayGet : public HExpression<2> {
3474 public:
3475 HArrayGet(HInstruction* array, HInstruction* index, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003476 : HExpression(type, SideEffects::DependsOnSomething()) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003477 SetRawInputAt(0, array);
3478 SetRawInputAt(1, index);
3479 }
3480
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003481 bool CanBeMoved() const OVERRIDE { return true; }
3482 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003483 UNUSED(other);
3484 return true;
3485 }
Calin Juravle641547a2015-04-21 22:08:51 +01003486 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3487 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00003488 // TODO: We can be smarter here.
3489 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
3490 // which generates the implicit null check. There are cases when these can be removed
3491 // to produce better code. If we ever add optimizations to do so we should allow an
3492 // implicit check here (as long as the address falls in the first page).
3493 return false;
3494 }
3495
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003496 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003497
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003498 HInstruction* GetArray() const { return InputAt(0); }
3499 HInstruction* GetIndex() const { return InputAt(1); }
3500
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003501 DECLARE_INSTRUCTION(ArrayGet);
3502
3503 private:
3504 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
3505};
3506
3507class HArraySet : public HTemplateInstruction<3> {
3508 public:
3509 HArraySet(HInstruction* array,
3510 HInstruction* index,
3511 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003512 Primitive::Type expected_component_type,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003513 uint32_t dex_pc)
3514 : HTemplateInstruction(SideEffects::ChangesSomething()),
3515 dex_pc_(dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003516 expected_component_type_(expected_component_type),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003517 needs_type_check_(value->GetType() == Primitive::kPrimNot),
3518 value_can_be_null_(true) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003519 SetRawInputAt(0, array);
3520 SetRawInputAt(1, index);
3521 SetRawInputAt(2, value);
3522 }
3523
Calin Juravle77520bc2015-01-12 18:45:46 +00003524 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003525 // We currently always call a runtime method to catch array store
3526 // exceptions.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003527 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003528 }
3529
Mingyao Yang81014cb2015-06-02 03:16:27 -07003530 // Can throw ArrayStoreException.
3531 bool CanThrow() const OVERRIDE { return needs_type_check_; }
3532
Calin Juravle641547a2015-04-21 22:08:51 +01003533 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3534 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00003535 // TODO: Same as for ArrayGet.
3536 return false;
3537 }
3538
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003539 void ClearNeedsTypeCheck() {
3540 needs_type_check_ = false;
3541 }
3542
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003543 void ClearValueCanBeNull() {
3544 value_can_be_null_ = false;
3545 }
3546
3547 bool GetValueCanBeNull() const { return value_can_be_null_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003548 bool NeedsTypeCheck() const { return needs_type_check_; }
3549
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003550 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003551
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003552 HInstruction* GetArray() const { return InputAt(0); }
3553 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003554 HInstruction* GetValue() const { return InputAt(2); }
3555
3556 Primitive::Type GetComponentType() const {
3557 // The Dex format does not type floating point index operations. Since the
3558 // `expected_component_type_` is set during building and can therefore not
3559 // be correct, we also check what is the value type. If it is a floating
3560 // point type, we must use that type.
3561 Primitive::Type value_type = GetValue()->GetType();
3562 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
3563 ? value_type
3564 : expected_component_type_;
3565 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003566
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003567 DECLARE_INSTRUCTION(ArraySet);
3568
3569 private:
3570 const uint32_t dex_pc_;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003571 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003572 bool needs_type_check_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003573 bool value_can_be_null_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003574
3575 DISALLOW_COPY_AND_ASSIGN(HArraySet);
3576};
3577
3578class HArrayLength : public HExpression<1> {
3579 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003580 explicit HArrayLength(HInstruction* array)
3581 : HExpression(Primitive::kPrimInt, SideEffects::None()) {
3582 // Note that arrays do not change length, so the instruction does not
3583 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003584 SetRawInputAt(0, array);
3585 }
3586
Calin Juravle77520bc2015-01-12 18:45:46 +00003587 bool CanBeMoved() const OVERRIDE { return true; }
3588 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003589 UNUSED(other);
3590 return true;
3591 }
Calin Juravle641547a2015-04-21 22:08:51 +01003592 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3593 return obj == InputAt(0);
3594 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003595
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003596 DECLARE_INSTRUCTION(ArrayLength);
3597
3598 private:
3599 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
3600};
3601
3602class HBoundsCheck : public HExpression<2> {
3603 public:
3604 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003605 : HExpression(index->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003606 DCHECK(index->GetType() == Primitive::kPrimInt);
3607 SetRawInputAt(0, index);
3608 SetRawInputAt(1, length);
3609 }
3610
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003611 bool CanBeMoved() const OVERRIDE { return true; }
3612 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003613 UNUSED(other);
3614 return true;
3615 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003616
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003617 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003618
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003619 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01003620
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003621 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003622
3623 DECLARE_INSTRUCTION(BoundsCheck);
3624
3625 private:
3626 const uint32_t dex_pc_;
3627
3628 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
3629};
3630
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003631/**
3632 * Some DEX instructions are folded into multiple HInstructions that need
3633 * to stay live until the last HInstruction. This class
3634 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00003635 * HInstruction stays live. `index` represents the stack location index of the
3636 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003637 */
3638class HTemporary : public HTemplateInstruction<0> {
3639 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003640 explicit HTemporary(size_t index) : HTemplateInstruction(SideEffects::None()), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003641
3642 size_t GetIndex() const { return index_; }
3643
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00003644 Primitive::Type GetType() const OVERRIDE {
3645 // The previous instruction is the one that will be stored in the temporary location.
3646 DCHECK(GetPrevious() != nullptr);
3647 return GetPrevious()->GetType();
3648 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00003649
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003650 DECLARE_INSTRUCTION(Temporary);
3651
3652 private:
3653 const size_t index_;
3654
3655 DISALLOW_COPY_AND_ASSIGN(HTemporary);
3656};
3657
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003658class HSuspendCheck : public HTemplateInstruction<0> {
3659 public:
3660 explicit HSuspendCheck(uint32_t dex_pc)
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01003661 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003662
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003663 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003664 return true;
3665 }
3666
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003667 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01003668 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
3669 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003670
3671 DECLARE_INSTRUCTION(SuspendCheck);
3672
3673 private:
3674 const uint32_t dex_pc_;
3675
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01003676 // Only used for code generation, in order to share the same slow path between back edges
3677 // of a same loop.
3678 SlowPathCode* slow_path_;
3679
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003680 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
3681};
3682
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003683/**
3684 * Instruction to load a Class object.
3685 */
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003686class HLoadClass : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003687 public:
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003688 HLoadClass(HCurrentMethod* current_method,
3689 uint16_t type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003690 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003691 bool is_referrers_class,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003692 uint32_t dex_pc)
3693 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3694 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003695 dex_file_(dex_file),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003696 is_referrers_class_(is_referrers_class),
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003697 dex_pc_(dex_pc),
Calin Juravleb1498f62015-02-16 13:13:29 +00003698 generate_clinit_check_(false),
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003699 loaded_class_rti_(ReferenceTypeInfo::CreateTop(/* is_exact */ false)) {
3700 SetRawInputAt(0, current_method);
3701 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003702
3703 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003704
3705 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3706 return other->AsLoadClass()->type_index_ == type_index_;
3707 }
3708
3709 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
3710
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003711 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003712 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003713 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003714
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003715 bool NeedsEnvironment() const OVERRIDE {
3716 // Will call runtime and load the class if the class is not loaded yet.
3717 // TODO: finer grain decision.
3718 return !is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003719 }
3720
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003721 bool MustGenerateClinitCheck() const {
3722 return generate_clinit_check_;
3723 }
3724
Calin Juravle0ba218d2015-05-19 18:46:01 +01003725 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
3726 generate_clinit_check_ = generate_clinit_check;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003727 }
3728
3729 bool CanCallRuntime() const {
3730 return MustGenerateClinitCheck() || !is_referrers_class_;
3731 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003732
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003733 bool CanThrow() const OVERRIDE {
3734 // May call runtime and and therefore can throw.
3735 // TODO: finer grain decision.
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01003736 return CanCallRuntime();
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003737 }
3738
Calin Juravleacf735c2015-02-12 15:25:22 +00003739 ReferenceTypeInfo GetLoadedClassRTI() {
3740 return loaded_class_rti_;
3741 }
3742
3743 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
3744 // Make sure we only set exact types (the loaded class should never be merged).
3745 DCHECK(rti.IsExact());
3746 loaded_class_rti_ = rti;
3747 }
3748
3749 bool IsResolved() {
3750 return loaded_class_rti_.IsExact();
3751 }
3752
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003753 const DexFile& GetDexFile() { return dex_file_; }
3754
Nicolas Geoffray9437b782015-03-25 10:08:51 +00003755 bool NeedsDexCache() const OVERRIDE { return !is_referrers_class_; }
3756
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003757 DECLARE_INSTRUCTION(LoadClass);
3758
3759 private:
3760 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003761 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003762 const bool is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003763 const uint32_t dex_pc_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003764 // Whether this instruction must generate the initialization check.
3765 // Used for code generation.
3766 bool generate_clinit_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003767
Calin Juravleacf735c2015-02-12 15:25:22 +00003768 ReferenceTypeInfo loaded_class_rti_;
3769
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003770 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
3771};
3772
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01003773class HLoadString : public HExpression<1> {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003774 public:
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01003775 HLoadString(HCurrentMethod* current_method, uint32_t string_index, uint32_t dex_pc)
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003776 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3777 string_index_(string_index),
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01003778 dex_pc_(dex_pc) {
3779 SetRawInputAt(0, current_method);
3780 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003781
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003782 bool CanBeMoved() const OVERRIDE { return true; }
3783
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003784 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3785 return other->AsLoadString()->string_index_ == string_index_;
3786 }
3787
3788 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
3789
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003790 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003791 uint32_t GetStringIndex() const { return string_index_; }
3792
3793 // TODO: Can we deopt or debug when we resolve a string?
3794 bool NeedsEnvironment() const OVERRIDE { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00003795 bool NeedsDexCache() const OVERRIDE { return true; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003796
3797 DECLARE_INSTRUCTION(LoadString);
3798
3799 private:
3800 const uint32_t string_index_;
3801 const uint32_t dex_pc_;
3802
3803 DISALLOW_COPY_AND_ASSIGN(HLoadString);
3804};
3805
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003806/**
3807 * Performs an initialization check on its Class object input.
3808 */
3809class HClinitCheck : public HExpression<1> {
3810 public:
3811 explicit HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Nicolas Geoffraya0466e12015-03-27 15:00:40 +00003812 : HExpression(Primitive::kPrimNot, SideEffects::ChangesSomething()),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003813 dex_pc_(dex_pc) {
3814 SetRawInputAt(0, constant);
3815 }
3816
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003817 bool CanBeMoved() const OVERRIDE { return true; }
3818 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3819 UNUSED(other);
3820 return true;
3821 }
3822
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003823 bool NeedsEnvironment() const OVERRIDE {
3824 // May call runtime to initialize the class.
3825 return true;
3826 }
3827
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003828 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003829
3830 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
3831
3832 DECLARE_INSTRUCTION(ClinitCheck);
3833
3834 private:
3835 const uint32_t dex_pc_;
3836
3837 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
3838};
3839
3840class HStaticFieldGet : public HExpression<1> {
3841 public:
3842 HStaticFieldGet(HInstruction* cls,
3843 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003844 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003845 bool is_volatile,
3846 uint32_t field_idx,
3847 const DexFile& dex_file)
Nicolas Geoffray2af23072015-04-30 11:15:40 +00003848 : HExpression(field_type, SideEffects::DependsOnSomething()),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003849 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003850 SetRawInputAt(0, cls);
3851 }
3852
Calin Juravle52c48962014-12-16 17:02:57 +00003853
Calin Juravle10c9cbe2014-12-19 10:50:19 +00003854 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003855
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003856 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00003857 HStaticFieldGet* other_get = other->AsStaticFieldGet();
3858 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003859 }
3860
3861 size_t ComputeHashCode() const OVERRIDE {
3862 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
3863 }
3864
Calin Juravle52c48962014-12-16 17:02:57 +00003865 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003866 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
3867 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003868 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003869
3870 DECLARE_INSTRUCTION(StaticFieldGet);
3871
3872 private:
3873 const FieldInfo field_info_;
3874
3875 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
3876};
3877
3878class HStaticFieldSet : public HTemplateInstruction<2> {
3879 public:
3880 HStaticFieldSet(HInstruction* cls,
3881 HInstruction* value,
3882 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003883 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003884 bool is_volatile,
3885 uint32_t field_idx,
3886 const DexFile& dex_file)
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003887 : HTemplateInstruction(SideEffects::ChangesSomething()),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003888 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003889 value_can_be_null_(true) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003890 SetRawInputAt(0, cls);
3891 SetRawInputAt(1, value);
3892 }
3893
Calin Juravle52c48962014-12-16 17:02:57 +00003894 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003895 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
3896 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003897 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003898
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003899 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003900 bool GetValueCanBeNull() const { return value_can_be_null_; }
3901 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003902
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003903 DECLARE_INSTRUCTION(StaticFieldSet);
3904
3905 private:
3906 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003907 bool value_can_be_null_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003908
3909 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
3910};
3911
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003912// Implement the move-exception DEX instruction.
3913class HLoadException : public HExpression<0> {
3914 public:
3915 HLoadException() : HExpression(Primitive::kPrimNot, SideEffects::None()) {}
3916
3917 DECLARE_INSTRUCTION(LoadException);
3918
3919 private:
3920 DISALLOW_COPY_AND_ASSIGN(HLoadException);
3921};
3922
3923class HThrow : public HTemplateInstruction<1> {
3924 public:
3925 HThrow(HInstruction* exception, uint32_t dex_pc)
3926 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {
3927 SetRawInputAt(0, exception);
3928 }
3929
3930 bool IsControlFlow() const OVERRIDE { return true; }
3931
3932 bool NeedsEnvironment() const OVERRIDE { return true; }
3933
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003934 bool CanThrow() const OVERRIDE { return true; }
3935
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003936 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003937
3938 DECLARE_INSTRUCTION(Throw);
3939
3940 private:
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003941 const uint32_t dex_pc_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003942
3943 DISALLOW_COPY_AND_ASSIGN(HThrow);
3944};
3945
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003946class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003947 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003948 HInstanceOf(HInstruction* object,
3949 HLoadClass* constant,
3950 bool class_is_final,
3951 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003952 : HExpression(Primitive::kPrimBoolean, SideEffects::None()),
3953 class_is_final_(class_is_final),
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003954 must_do_null_check_(true),
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003955 dex_pc_(dex_pc) {
3956 SetRawInputAt(0, object);
3957 SetRawInputAt(1, constant);
3958 }
3959
3960 bool CanBeMoved() const OVERRIDE { return true; }
3961
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003962 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003963 return true;
3964 }
3965
3966 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003967 return false;
3968 }
3969
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003970 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003971
3972 bool IsClassFinal() const { return class_is_final_; }
3973
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003974 // Used only in code generation.
3975 bool MustDoNullCheck() const { return must_do_null_check_; }
3976 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
3977
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003978 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003979
3980 private:
3981 const bool class_is_final_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003982 bool must_do_null_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003983 const uint32_t dex_pc_;
3984
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003985 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
3986};
3987
Calin Juravleb1498f62015-02-16 13:13:29 +00003988class HBoundType : public HExpression<1> {
3989 public:
3990 HBoundType(HInstruction* input, ReferenceTypeInfo bound_type)
3991 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3992 bound_type_(bound_type) {
Calin Juravle61d544b2015-02-23 16:46:57 +00003993 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00003994 SetRawInputAt(0, input);
3995 }
3996
3997 const ReferenceTypeInfo& GetBoundType() const { return bound_type_; }
3998
3999 bool CanBeNull() const OVERRIDE {
4000 // `null instanceof ClassX` always return false so we can't be null.
4001 return false;
4002 }
4003
4004 DECLARE_INSTRUCTION(BoundType);
4005
4006 private:
4007 // Encodes the most upper class that this instruction can have. In other words
4008 // it is always the case that GetBoundType().IsSupertypeOf(GetReferenceType()).
4009 // It is used to bound the type in cases like `if (x instanceof ClassX) {}`
4010 const ReferenceTypeInfo bound_type_;
4011
4012 DISALLOW_COPY_AND_ASSIGN(HBoundType);
4013};
4014
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004015class HCheckCast : public HTemplateInstruction<2> {
4016 public:
4017 HCheckCast(HInstruction* object,
4018 HLoadClass* constant,
4019 bool class_is_final,
4020 uint32_t dex_pc)
4021 : HTemplateInstruction(SideEffects::None()),
4022 class_is_final_(class_is_final),
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004023 must_do_null_check_(true),
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004024 dex_pc_(dex_pc) {
4025 SetRawInputAt(0, object);
4026 SetRawInputAt(1, constant);
4027 }
4028
4029 bool CanBeMoved() const OVERRIDE { return true; }
4030
4031 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
4032 return true;
4033 }
4034
4035 bool NeedsEnvironment() const OVERRIDE {
4036 // Instruction may throw a CheckCastError.
4037 return true;
4038 }
4039
4040 bool CanThrow() const OVERRIDE { return true; }
4041
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004042 bool MustDoNullCheck() const { return must_do_null_check_; }
4043 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
4044
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004045 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004046
4047 bool IsClassFinal() const { return class_is_final_; }
4048
4049 DECLARE_INSTRUCTION(CheckCast);
4050
4051 private:
4052 const bool class_is_final_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004053 bool must_do_null_check_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004054 const uint32_t dex_pc_;
4055
4056 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004057};
4058
Calin Juravle27df7582015-04-17 19:12:31 +01004059class HMemoryBarrier : public HTemplateInstruction<0> {
4060 public:
4061 explicit HMemoryBarrier(MemBarrierKind barrier_kind)
4062 : HTemplateInstruction(SideEffects::None()),
4063 barrier_kind_(barrier_kind) {}
4064
4065 MemBarrierKind GetBarrierKind() { return barrier_kind_; }
4066
4067 DECLARE_INSTRUCTION(MemoryBarrier);
4068
4069 private:
4070 const MemBarrierKind barrier_kind_;
4071
4072 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
4073};
4074
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004075class HMonitorOperation : public HTemplateInstruction<1> {
4076 public:
4077 enum OperationKind {
4078 kEnter,
4079 kExit,
4080 };
4081
4082 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
4083 : HTemplateInstruction(SideEffects::None()), kind_(kind), dex_pc_(dex_pc) {
4084 SetRawInputAt(0, object);
4085 }
4086
4087 // Instruction may throw a Java exception, so we need an environment.
4088 bool NeedsEnvironment() const OVERRIDE { return true; }
4089 bool CanThrow() const OVERRIDE { return true; }
4090
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004091 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004092
4093 bool IsEnter() const { return kind_ == kEnter; }
4094
4095 DECLARE_INSTRUCTION(MonitorOperation);
4096
Calin Juravle52c48962014-12-16 17:02:57 +00004097 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004098 const OperationKind kind_;
4099 const uint32_t dex_pc_;
4100
4101 private:
4102 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
4103};
4104
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004105class MoveOperands : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004106 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01004107 MoveOperands(Location source,
4108 Location destination,
4109 Primitive::Type type,
4110 HInstruction* instruction)
4111 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004112
4113 Location GetSource() const { return source_; }
4114 Location GetDestination() const { return destination_; }
4115
4116 void SetSource(Location value) { source_ = value; }
4117 void SetDestination(Location value) { destination_ = value; }
4118
4119 // The parallel move resolver marks moves as "in-progress" by clearing the
4120 // destination (but not the source).
4121 Location MarkPending() {
4122 DCHECK(!IsPending());
4123 Location dest = destination_;
4124 destination_ = Location::NoLocation();
4125 return dest;
4126 }
4127
4128 void ClearPending(Location dest) {
4129 DCHECK(IsPending());
4130 destination_ = dest;
4131 }
4132
4133 bool IsPending() const {
4134 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
4135 return destination_.IsInvalid() && !source_.IsInvalid();
4136 }
4137
4138 // True if this blocks a move from the given location.
4139 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08004140 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004141 }
4142
4143 // A move is redundant if it's been eliminated, if its source and
4144 // destination are the same, or if its destination is unneeded.
4145 bool IsRedundant() const {
4146 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
4147 }
4148
4149 // We clear both operands to indicate move that's been eliminated.
4150 void Eliminate() {
4151 source_ = destination_ = Location::NoLocation();
4152 }
4153
4154 bool IsEliminated() const {
4155 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
4156 return source_.IsInvalid();
4157 }
4158
Alexey Frunze4dda3372015-06-01 18:31:49 -07004159 Primitive::Type GetType() const { return type_; }
4160
Nicolas Geoffray90218252015-04-15 11:56:51 +01004161 bool Is64BitMove() const {
4162 return Primitive::Is64BitType(type_);
4163 }
4164
Nicolas Geoffray740475d2014-09-29 10:33:25 +01004165 HInstruction* GetInstruction() const { return instruction_; }
4166
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004167 private:
4168 Location source_;
4169 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01004170 // The type this move is for.
4171 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01004172 // The instruction this move is assocatied with. Null when this move is
4173 // for moving an input in the expected locations of user (including a phi user).
4174 // This is only used in debug mode, to ensure we do not connect interval siblings
4175 // in the same parallel move.
4176 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004177};
4178
4179static constexpr size_t kDefaultNumberOfMoves = 4;
4180
4181class HParallelMove : public HTemplateInstruction<0> {
4182 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004183 explicit HParallelMove(ArenaAllocator* arena)
4184 : HTemplateInstruction(SideEffects::None()), moves_(arena, kDefaultNumberOfMoves) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004185
Nicolas Geoffray90218252015-04-15 11:56:51 +01004186 void AddMove(Location source,
4187 Location destination,
4188 Primitive::Type type,
4189 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004190 DCHECK(source.IsValid());
4191 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004192 if (kIsDebugBuild) {
4193 if (instruction != nullptr) {
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00004194 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004195 if (moves_.Get(i).GetInstruction() == instruction) {
4196 // Special case the situation where the move is for the spill slot
4197 // of the instruction.
4198 if ((GetPrevious() == instruction)
4199 || ((GetPrevious() == nullptr)
4200 && instruction->IsPhi()
4201 && instruction->GetBlock() == GetBlock())) {
4202 DCHECK_NE(destination.GetKind(), moves_.Get(i).GetDestination().GetKind())
4203 << "Doing parallel moves for the same instruction.";
4204 } else {
4205 DCHECK(false) << "Doing parallel moves for the same instruction.";
4206 }
4207 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00004208 }
4209 }
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004210 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Zheng Xuad4450e2015-04-17 18:48:56 +08004211 DCHECK(!destination.OverlapsWith(moves_.Get(i).GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01004212 << "Overlapped destination for two moves in a parallel move: "
4213 << moves_.Get(i).GetSource() << " ==> " << moves_.Get(i).GetDestination() << " and "
4214 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004215 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01004216 }
Nicolas Geoffray90218252015-04-15 11:56:51 +01004217 moves_.Add(MoveOperands(source, destination, type, instruction));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004218 }
4219
4220 MoveOperands* MoveOperandsAt(size_t index) const {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004221 return moves_.GetRawStorage() + index;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004222 }
4223
4224 size_t NumMoves() const { return moves_.Size(); }
4225
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004226 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004227
4228 private:
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004229 GrowableArray<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004230
4231 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
4232};
4233
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004234class HGraphVisitor : public ValueObject {
4235 public:
Dave Allison20dfc792014-06-16 20:44:29 -07004236 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
4237 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004238
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004239 virtual void VisitInstruction(HInstruction* instruction) { UNUSED(instruction); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004240 virtual void VisitBasicBlock(HBasicBlock* block);
4241
Roland Levillain633021e2014-10-01 14:12:25 +01004242 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004243 void VisitInsertionOrder();
4244
Roland Levillain633021e2014-10-01 14:12:25 +01004245 // Visit the graph following dominator tree reverse post-order.
4246 void VisitReversePostOrder();
4247
Nicolas Geoffray787c3072014-03-17 10:20:19 +00004248 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00004249
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004250 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004251#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004252 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
4253
4254 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
4255
4256#undef DECLARE_VISIT_INSTRUCTION
4257
4258 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07004259 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004260
4261 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
4262};
4263
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004264class HGraphDelegateVisitor : public HGraphVisitor {
4265 public:
4266 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
4267 virtual ~HGraphDelegateVisitor() {}
4268
4269 // Visit functions that delegate to to super class.
4270#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004271 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004272
4273 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
4274
4275#undef DECLARE_VISIT_INSTRUCTION
4276
4277 private:
4278 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
4279};
4280
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004281class HInsertionOrderIterator : public ValueObject {
4282 public:
4283 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
4284
4285 bool Done() const { return index_ == graph_.GetBlocks().Size(); }
4286 HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); }
4287 void Advance() { ++index_; }
4288
4289 private:
4290 const HGraph& graph_;
4291 size_t index_;
4292
4293 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
4294};
4295
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004296class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004297 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00004298 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
4299 // Check that reverse post order of the graph has been built.
4300 DCHECK(!graph.GetReversePostOrder().IsEmpty());
4301 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004302
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004303 bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); }
4304 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004305 void Advance() { ++index_; }
4306
4307 private:
4308 const HGraph& graph_;
4309 size_t index_;
4310
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004311 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004312};
4313
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004314class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004315 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004316 explicit HPostOrderIterator(const HGraph& graph)
David Brazdil10f56cb2015-03-24 18:49:14 +00004317 : graph_(graph), index_(graph_.GetReversePostOrder().Size()) {
4318 // Check that reverse post order of the graph has been built.
4319 DCHECK(!graph.GetReversePostOrder().IsEmpty());
4320 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004321
4322 bool Done() const { return index_ == 0; }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004323 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004324 void Advance() { --index_; }
4325
4326 private:
4327 const HGraph& graph_;
4328 size_t index_;
4329
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004330 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004331};
4332
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01004333class HLinearPostOrderIterator : public ValueObject {
4334 public:
4335 explicit HLinearPostOrderIterator(const HGraph& graph)
4336 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().Size()) {}
4337
4338 bool Done() const { return index_ == 0; }
4339
4340 HBasicBlock* Current() const { return order_.Get(index_ -1); }
4341
4342 void Advance() {
4343 --index_;
4344 DCHECK_GE(index_, 0U);
4345 }
4346
4347 private:
4348 const GrowableArray<HBasicBlock*>& order_;
4349 size_t index_;
4350
4351 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
4352};
4353
4354class HLinearOrderIterator : public ValueObject {
4355 public:
4356 explicit HLinearOrderIterator(const HGraph& graph)
4357 : order_(graph.GetLinearOrder()), index_(0) {}
4358
4359 bool Done() const { return index_ == order_.Size(); }
4360 HBasicBlock* Current() const { return order_.Get(index_); }
4361 void Advance() { ++index_; }
4362
4363 private:
4364 const GrowableArray<HBasicBlock*>& order_;
4365 size_t index_;
4366
4367 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
4368};
4369
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004370// Iterator over the blocks that art part of the loop. Includes blocks part
4371// of an inner loop. The order in which the blocks are iterated is on their
4372// block id.
4373class HBlocksInLoopIterator : public ValueObject {
4374 public:
4375 explicit HBlocksInLoopIterator(const HLoopInformation& info)
4376 : blocks_in_loop_(info.GetBlocks()),
4377 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
4378 index_(0) {
4379 if (!blocks_in_loop_.IsBitSet(index_)) {
4380 Advance();
4381 }
4382 }
4383
4384 bool Done() const { return index_ == blocks_.Size(); }
4385 HBasicBlock* Current() const { return blocks_.Get(index_); }
4386 void Advance() {
4387 ++index_;
4388 for (size_t e = blocks_.Size(); index_ < e; ++index_) {
4389 if (blocks_in_loop_.IsBitSet(index_)) {
4390 break;
4391 }
4392 }
4393 }
4394
4395 private:
4396 const BitVector& blocks_in_loop_;
4397 const GrowableArray<HBasicBlock*>& blocks_;
4398 size_t index_;
4399
4400 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
4401};
4402
Mingyao Yang3584bce2015-05-19 16:01:59 -07004403// Iterator over the blocks that art part of the loop. Includes blocks part
4404// of an inner loop. The order in which the blocks are iterated is reverse
4405// post order.
4406class HBlocksInLoopReversePostOrderIterator : public ValueObject {
4407 public:
4408 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
4409 : blocks_in_loop_(info.GetBlocks()),
4410 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
4411 index_(0) {
4412 if (!blocks_in_loop_.IsBitSet(blocks_.Get(index_)->GetBlockId())) {
4413 Advance();
4414 }
4415 }
4416
4417 bool Done() const { return index_ == blocks_.Size(); }
4418 HBasicBlock* Current() const { return blocks_.Get(index_); }
4419 void Advance() {
4420 ++index_;
4421 for (size_t e = blocks_.Size(); index_ < e; ++index_) {
4422 if (blocks_in_loop_.IsBitSet(blocks_.Get(index_)->GetBlockId())) {
4423 break;
4424 }
4425 }
4426 }
4427
4428 private:
4429 const BitVector& blocks_in_loop_;
4430 const GrowableArray<HBasicBlock*>& blocks_;
4431 size_t index_;
4432
4433 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
4434};
4435
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00004436inline int64_t Int64FromConstant(HConstant* constant) {
4437 DCHECK(constant->IsIntConstant() || constant->IsLongConstant());
4438 return constant->IsIntConstant() ? constant->AsIntConstant()->GetValue()
4439 : constant->AsLongConstant()->GetValue();
4440}
4441
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004442} // namespace art
4443
4444#endif // ART_COMPILER_OPTIMIZING_NODES_H_