blob: 581645f21aa20f52462d40410602d7fb5f52b4cc [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;
42class HGraphVisitor;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000043class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000044class HIntConstant;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000045class HInvoke;
David Brazdil8d5b8b22015-03-24 10:51:52 +000046class HLongConstant;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +000047class HNullConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010048class HPhi;
Nicolas Geoffray3c049742014-09-24 18:10:46 +010049class HSuspendCheck;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010050class LiveInterval;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000051class LocationSummary;
Nicolas Geoffraydb216f42015-05-05 17:02:20 +010052class SlowPathCode;
David Brazdil8d5b8b22015-03-24 10:51:52 +000053class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000054
55static const int kDefaultNumberOfBlocks = 8;
56static const int kDefaultNumberOfSuccessors = 2;
57static const int kDefaultNumberOfPredecessors = 2;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010058static const int kDefaultNumberOfDominatedBlocks = 1;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000059static const int kDefaultNumberOfBackEdges = 1;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000060
Calin Juravle9aec02f2014-11-18 23:06:35 +000061static constexpr uint32_t kMaxIntShiftValue = 0x1f;
62static constexpr uint64_t kMaxLongShiftValue = 0x3f;
63
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010064static constexpr uint32_t kUnknownFieldIndex = static_cast<uint32_t>(-1);
65
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +010066static constexpr InvokeType kInvalidInvokeType = static_cast<InvokeType>(-1);
67
Dave Allison20dfc792014-06-16 20:44:29 -070068enum IfCondition {
69 kCondEQ,
70 kCondNE,
71 kCondLT,
72 kCondLE,
73 kCondGT,
74 kCondGE,
75};
76
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010077class HInstructionList {
78 public:
79 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
80
81 void AddInstruction(HInstruction* instruction);
82 void RemoveInstruction(HInstruction* instruction);
83
David Brazdilc3d743f2015-04-22 13:40:50 +010084 // Insert `instruction` before/after an existing instruction `cursor`.
85 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
86 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
87
Roland Levillain6b469232014-09-25 10:10:38 +010088 // Return true if this list contains `instruction`.
89 bool Contains(HInstruction* instruction) const;
90
Roland Levillainccc07a92014-09-16 14:48:16 +010091 // Return true if `instruction1` is found before `instruction2` in
92 // this instruction list and false otherwise. Abort if none
93 // of these instructions is found.
94 bool FoundBefore(const HInstruction* instruction1,
95 const HInstruction* instruction2) const;
96
Nicolas Geoffray276d9da2015-02-02 18:24:11 +000097 bool IsEmpty() const { return first_instruction_ == nullptr; }
98 void Clear() { first_instruction_ = last_instruction_ = nullptr; }
99
100 // Update the block of all instructions to be `block`.
101 void SetBlockOfInstructions(HBasicBlock* block) const;
102
103 void AddAfter(HInstruction* cursor, const HInstructionList& instruction_list);
104 void Add(const HInstructionList& instruction_list);
105
David Brazdil2d7352b2015-04-20 14:52:42 +0100106 // Return the number of instructions in the list. This is an expensive operation.
107 size_t CountSize() const;
108
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100109 private:
110 HInstruction* first_instruction_;
111 HInstruction* last_instruction_;
112
113 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000114 friend class HGraph;
115 friend class HInstruction;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100116 friend class HInstructionIterator;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100117 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100118
119 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
120};
121
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000122// Control-flow graph of a method. Contains a list of basic blocks.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700123class HGraph : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000124 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100125 HGraph(ArenaAllocator* arena,
126 const DexFile& dex_file,
127 uint32_t method_idx,
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100128 bool should_generate_constructor_barrier,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700129 InstructionSet instruction_set,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100130 InvokeType invoke_type = kInvalidInvokeType,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100131 bool debuggable = false,
132 int start_instruction_id = 0)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000133 : arena_(arena),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000134 blocks_(arena, kDefaultNumberOfBlocks),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100135 reverse_post_order_(arena, kDefaultNumberOfBlocks),
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100136 linear_order_(arena, kDefaultNumberOfBlocks),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700137 entry_block_(nullptr),
138 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100139 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100140 number_of_vregs_(0),
141 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000142 temporaries_vreg_slots_(0),
Mark Mendell1152c922015-04-24 17:06:35 -0400143 has_bounds_checks_(false),
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000144 debuggable_(debuggable),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000145 current_instruction_id_(start_instruction_id),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100146 dex_file_(dex_file),
147 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100148 invoke_type_(invoke_type),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100149 in_ssa_form_(false),
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100150 should_generate_constructor_barrier_(should_generate_constructor_barrier),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700151 instruction_set_(instruction_set),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000152 cached_null_constant_(nullptr),
153 cached_int_constants_(std::less<int32_t>(), arena->Adapter()),
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000154 cached_float_constants_(std::less<int32_t>(), arena->Adapter()),
155 cached_long_constants_(std::less<int64_t>(), arena->Adapter()),
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100156 cached_double_constants_(std::less<int64_t>(), arena->Adapter()),
157 cached_current_method_(nullptr) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000158
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000159 ArenaAllocator* GetArena() const { return arena_; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100160 const GrowableArray<HBasicBlock*>& GetBlocks() const { return blocks_; }
Roland Levillain93445682014-10-06 19:24:02 +0100161 HBasicBlock* GetBlock(size_t id) const { return blocks_.Get(id); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000162
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000163 HBasicBlock* GetEntryBlock() const { return entry_block_; }
164 HBasicBlock* GetExitBlock() const { return exit_block_; }
David Brazdilc7af85d2015-05-26 12:05:55 +0100165 bool HasExitBlock() const { return exit_block_ != nullptr; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000166
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000167 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
168 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000169
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000170 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100171
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000172 // Try building the SSA form of this graph, with dominance computation and loop
173 // recognition. Returns whether it was successful in doing all these steps.
174 bool TryBuildingSsa() {
175 BuildDominatorTree();
Nicolas Geoffrayd3350832015-03-12 11:16:23 +0000176 // The SSA builder requires loops to all be natural. Specifically, the dead phi
177 // elimination phase checks the consistency of the graph when doing a post-order
178 // visit for eliminating dead phis: a dead phi can only have loop header phi
179 // users remaining when being visited.
180 if (!AnalyzeNaturalLoops()) return false;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000181 TransformToSsa();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100182 in_ssa_form_ = true;
Nicolas Geoffrayd3350832015-03-12 11:16:23 +0000183 return true;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000184 }
185
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100186 void ComputeDominanceInformation();
187 void ClearDominanceInformation();
188
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000189 void BuildDominatorTree();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000190 void TransformToSsa();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100191 void SimplifyCFG();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000192
Nicolas Geoffrayf5370122014-12-02 11:51:19 +0000193 // Analyze all natural loops in this graph. Returns false if one
194 // loop is not natural, that is the header does not dominate the
195 // back edge.
196 bool AnalyzeNaturalLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100197
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000198 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
199 void InlineInto(HGraph* outer_graph, HInvoke* invoke);
200
Mingyao Yang3584bce2015-05-19 16:01:59 -0700201 // Need to add a couple of blocks to test if the loop body is entered and
202 // put deoptimization instructions, etc.
203 void TransformLoopHeaderForBCE(HBasicBlock* header);
204
David Brazdil2d7352b2015-04-20 14:52:42 +0100205 // Removes `block` from the graph.
206 void DeleteDeadBlock(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000207
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100208 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
209 void SimplifyLoop(HBasicBlock* header);
210
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000211 int32_t GetNextInstructionId() {
212 DCHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000213 return current_instruction_id_++;
214 }
215
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000216 int32_t GetCurrentInstructionId() const {
217 return current_instruction_id_;
218 }
219
220 void SetCurrentInstructionId(int32_t id) {
221 current_instruction_id_ = id;
222 }
223
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100224 uint16_t GetMaximumNumberOfOutVRegs() const {
225 return maximum_number_of_out_vregs_;
226 }
227
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000228 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
229 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100230 }
231
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100232 void UpdateMaximumNumberOfOutVRegs(uint16_t other_value) {
233 maximum_number_of_out_vregs_ = std::max(maximum_number_of_out_vregs_, other_value);
234 }
235
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000236 void UpdateTemporariesVRegSlots(size_t slots) {
237 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100238 }
239
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000240 size_t GetTemporariesVRegSlots() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100241 DCHECK(!in_ssa_form_);
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000242 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100243 }
244
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100245 void SetNumberOfVRegs(uint16_t number_of_vregs) {
246 number_of_vregs_ = number_of_vregs;
247 }
248
249 uint16_t GetNumberOfVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100250 DCHECK(!in_ssa_form_);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100251 return number_of_vregs_;
252 }
253
254 void SetNumberOfInVRegs(uint16_t value) {
255 number_of_in_vregs_ = value;
256 }
257
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100258 uint16_t GetNumberOfLocalVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100259 DCHECK(!in_ssa_form_);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100260 return number_of_vregs_ - number_of_in_vregs_;
261 }
262
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100263 const GrowableArray<HBasicBlock*>& GetReversePostOrder() const {
264 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100265 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100266
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100267 const GrowableArray<HBasicBlock*>& GetLinearOrder() const {
268 return linear_order_;
269 }
270
Mark Mendell1152c922015-04-24 17:06:35 -0400271 bool HasBoundsChecks() const {
272 return has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800273 }
274
Mark Mendell1152c922015-04-24 17:06:35 -0400275 void SetHasBoundsChecks(bool value) {
276 has_bounds_checks_ = value;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800277 }
278
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100279 bool ShouldGenerateConstructorBarrier() const {
280 return should_generate_constructor_barrier_;
281 }
282
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000283 bool IsDebuggable() const { return debuggable_; }
284
David Brazdil8d5b8b22015-03-24 10:51:52 +0000285 // Returns a constant of the given type and value. If it does not exist
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000286 // already, it is created and inserted into the graph. This method is only for
287 // integral types.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000288 HConstant* GetConstant(Primitive::Type type, int64_t value);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000289 HNullConstant* GetNullConstant();
David Brazdil8d5b8b22015-03-24 10:51:52 +0000290 HIntConstant* GetIntConstant(int32_t value) {
291 return CreateConstant(value, &cached_int_constants_);
292 }
293 HLongConstant* GetLongConstant(int64_t value) {
294 return CreateConstant(value, &cached_long_constants_);
295 }
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000296 HFloatConstant* GetFloatConstant(float value) {
297 return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_);
298 }
299 HDoubleConstant* GetDoubleConstant(double value) {
300 return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_);
301 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000302
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100303 HCurrentMethod* GetCurrentMethod();
304
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000305 HBasicBlock* FindCommonDominator(HBasicBlock* first, HBasicBlock* second) const;
David Brazdil2d7352b2015-04-20 14:52:42 +0100306
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100307 const DexFile& GetDexFile() const {
308 return dex_file_;
309 }
310
311 uint32_t GetMethodIdx() const {
312 return method_idx_;
313 }
314
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100315 InvokeType GetInvokeType() const {
316 return invoke_type_;
317 }
318
David Brazdil2d7352b2015-04-20 14:52:42 +0100319 private:
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000320 void VisitBlockForDominatorTree(HBasicBlock* block,
321 HBasicBlock* predecessor,
322 GrowableArray<size_t>* visits);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100323 void FindBackEdges(ArenaBitVector* visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000324 void VisitBlockForBackEdges(HBasicBlock* block,
325 ArenaBitVector* visited,
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100326 ArenaBitVector* visiting);
Roland Levillainfc600dc2014-12-02 17:16:31 +0000327 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100328 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000329
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000330 template <class InstructionType, typename ValueType>
331 InstructionType* CreateConstant(ValueType value,
332 ArenaSafeMap<ValueType, InstructionType*>* cache) {
333 // Try to find an existing constant of the given value.
334 InstructionType* constant = nullptr;
335 auto cached_constant = cache->find(value);
336 if (cached_constant != cache->end()) {
337 constant = cached_constant->second;
338 }
339
340 // If not found or previously deleted, create and cache a new instruction.
Nicolas Geoffrayf78848f2015-06-17 11:57:56 +0100341 // Don't bother reviving a previously deleted instruction, for simplicity.
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000342 if (constant == nullptr || constant->GetBlock() == nullptr) {
343 constant = new (arena_) InstructionType(value);
344 cache->Overwrite(value, constant);
345 InsertConstant(constant);
346 }
347 return constant;
348 }
349
David Brazdil8d5b8b22015-03-24 10:51:52 +0000350 void InsertConstant(HConstant* instruction);
351
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000352 // Cache a float constant into the graph. This method should only be
353 // called by the SsaBuilder when creating "equivalent" instructions.
354 void CacheFloatConstant(HFloatConstant* constant);
355
356 // See CacheFloatConstant comment.
357 void CacheDoubleConstant(HDoubleConstant* constant);
358
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000359 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000360
361 // List of blocks in insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000362 GrowableArray<HBasicBlock*> blocks_;
363
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100364 // List of blocks to perform a reverse post order tree traversal.
365 GrowableArray<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000366
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100367 // List of blocks to perform a linear order tree traversal.
368 GrowableArray<HBasicBlock*> linear_order_;
369
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000370 HBasicBlock* entry_block_;
371 HBasicBlock* exit_block_;
372
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100373 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100374 uint16_t maximum_number_of_out_vregs_;
375
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100376 // The number of virtual registers in this method. Contains the parameters.
377 uint16_t number_of_vregs_;
378
379 // The number of virtual registers used by parameters of this method.
380 uint16_t number_of_in_vregs_;
381
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000382 // Number of vreg size slots that the temporaries use (used in baseline compiler).
383 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100384
Mark Mendell1152c922015-04-24 17:06:35 -0400385 // Has bounds checks. We can totally skip BCE if it's false.
386 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800387
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000388 // Indicates whether the graph should be compiled in a way that
389 // ensures full debuggability. If false, we can apply more
390 // aggressive optimizations that may limit the level of debugging.
391 const bool debuggable_;
392
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000393 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000394 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000395
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100396 // The dex file from which the method is from.
397 const DexFile& dex_file_;
398
399 // The method index in the dex file.
400 const uint32_t method_idx_;
401
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100402 // If inlined, this encodes how the callee is being invoked.
403 const InvokeType invoke_type_;
404
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100405 // Whether the graph has been transformed to SSA form. Only used
406 // in debug mode to ensure we are not using properties only valid
407 // for non-SSA form (like the number of temporaries).
408 bool in_ssa_form_;
409
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100410 const bool should_generate_constructor_barrier_;
411
Mathieu Chartiere401d142015-04-22 13:56:20 -0700412 const InstructionSet instruction_set_;
413
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000414 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000415 HNullConstant* cached_null_constant_;
416 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000417 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000418 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000419 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000420
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100421 HCurrentMethod* cached_current_method_;
422
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000423 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100424 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000425 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000426 DISALLOW_COPY_AND_ASSIGN(HGraph);
427};
428
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700429class HLoopInformation : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000430 public:
431 HLoopInformation(HBasicBlock* header, HGraph* graph)
432 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100433 suspend_check_(nullptr),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100434 back_edges_(graph->GetArena(), kDefaultNumberOfBackEdges),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100435 // Make bit vector growable, as the number of blocks may change.
436 blocks_(graph->GetArena(), graph->GetBlocks().Size(), true) {}
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100437
438 HBasicBlock* GetHeader() const {
439 return header_;
440 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000441
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000442 void SetHeader(HBasicBlock* block) {
443 header_ = block;
444 }
445
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100446 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
447 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
448 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
449
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000450 void AddBackEdge(HBasicBlock* back_edge) {
451 back_edges_.Add(back_edge);
452 }
453
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100454 void RemoveBackEdge(HBasicBlock* back_edge) {
455 back_edges_.Delete(back_edge);
456 }
457
David Brazdil46e2a392015-03-16 17:31:52 +0000458 bool IsBackEdge(const HBasicBlock& block) const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100459 for (size_t i = 0, e = back_edges_.Size(); i < e; ++i) {
David Brazdil46e2a392015-03-16 17:31:52 +0000460 if (back_edges_.Get(i) == &block) return true;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100461 }
462 return false;
463 }
464
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000465 size_t NumberOfBackEdges() const {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000466 return back_edges_.Size();
467 }
468
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100469 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100470
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100471 const GrowableArray<HBasicBlock*>& GetBackEdges() const {
472 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100473 }
474
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100475 // Returns the lifetime position of the back edge that has the
476 // greatest lifetime position.
477 size_t GetLifetimeEnd() const;
478
479 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
480 for (size_t i = 0, e = back_edges_.Size(); i < e; ++i) {
481 if (back_edges_.Get(i) == existing) {
482 back_edges_.Put(i, new_back_edge);
483 return;
484 }
485 }
486 UNREACHABLE();
Nicolas Geoffray57902602015-04-21 14:28:41 +0100487 }
488
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100489 // Finds blocks that are part of this loop. Returns whether the loop is a natural loop,
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100490 // that is the header dominates the back edge.
491 bool Populate();
492
David Brazdila4b8c212015-05-07 09:59:30 +0100493 // Reanalyzes the loop by removing loop info from its blocks and re-running
494 // Populate(). If there are no back edges left, the loop info is completely
495 // removed as well as its SuspendCheck instruction. It must be run on nested
496 // inner loops first.
497 void Update();
498
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100499 // Returns whether this loop information contains `block`.
500 // Note that this loop information *must* be populated before entering this function.
501 bool Contains(const HBasicBlock& block) const;
502
503 // Returns whether this loop information is an inner loop of `other`.
504 // Note that `other` *must* be populated before entering this function.
505 bool IsIn(const HLoopInformation& other) const;
506
507 const ArenaBitVector& GetBlocks() const { return blocks_; }
508
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000509 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000510 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000511
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000512 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100513 // Internal recursive implementation of `Populate`.
514 void PopulateRecursive(HBasicBlock* block);
515
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000516 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100517 HSuspendCheck* suspend_check_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000518 GrowableArray<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100519 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000520
521 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
522};
523
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100524static constexpr size_t kNoLifetime = -1;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100525static constexpr uint32_t kNoDexPc = -1;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100526
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000527// A block in a method. Contains the list of instructions represented
528// as a double linked list. Each block knows its predecessors and
529// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100530
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700531class HBasicBlock : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000532 public:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100533 explicit HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000534 : graph_(graph),
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000535 predecessors_(graph->GetArena(), kDefaultNumberOfPredecessors),
536 successors_(graph->GetArena(), kDefaultNumberOfSuccessors),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000537 loop_information_(nullptr),
538 dominator_(nullptr),
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100539 dominated_blocks_(graph->GetArena(), kDefaultNumberOfDominatedBlocks),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100540 block_id_(-1),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100541 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100542 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000543 lifetime_end_(kNoLifetime),
544 is_catch_block_(false) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000545
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100546 const GrowableArray<HBasicBlock*>& GetPredecessors() const {
547 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000548 }
549
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100550 const GrowableArray<HBasicBlock*>& GetSuccessors() const {
551 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000552 }
553
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100554 const GrowableArray<HBasicBlock*>& GetDominatedBlocks() const {
555 return dominated_blocks_;
556 }
557
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100558 bool IsEntryBlock() const {
559 return graph_->GetEntryBlock() == this;
560 }
561
562 bool IsExitBlock() const {
563 return graph_->GetExitBlock() == this;
564 }
565
David Brazdil46e2a392015-03-16 17:31:52 +0000566 bool IsSingleGoto() const;
567
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000568 void AddBackEdge(HBasicBlock* back_edge) {
569 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000570 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000571 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100572 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000573 loop_information_->AddBackEdge(back_edge);
574 }
575
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000576 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000577 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000578
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000579 int GetBlockId() const { return block_id_; }
580 void SetBlockId(int id) { block_id_ = id; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000581
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000582 HBasicBlock* GetDominator() const { return dominator_; }
583 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100584 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.Add(block); }
David Brazdil2d7352b2015-04-20 14:52:42 +0100585 void RemoveDominatedBlock(HBasicBlock* block) { dominated_blocks_.Delete(block); }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000586 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
587 for (size_t i = 0, e = dominated_blocks_.Size(); i < e; ++i) {
588 if (dominated_blocks_.Get(i) == existing) {
589 dominated_blocks_.Put(i, new_block);
590 return;
591 }
592 }
593 LOG(FATAL) << "Unreachable";
594 UNREACHABLE();
595 }
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100596 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000597
598 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100599 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000600 }
601
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100602 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
603 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100604 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100605 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +0100606 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
607 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000608
609 void AddSuccessor(HBasicBlock* block) {
610 successors_.Add(block);
611 block->predecessors_.Add(this);
612 }
613
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100614 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
615 size_t successor_index = GetSuccessorIndexOf(existing);
616 DCHECK_NE(successor_index, static_cast<size_t>(-1));
617 existing->RemovePredecessor(this);
618 new_block->predecessors_.Add(this);
619 successors_.Put(successor_index, new_block);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000620 }
621
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000622 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
623 size_t predecessor_index = GetPredecessorIndexOf(existing);
624 DCHECK_NE(predecessor_index, static_cast<size_t>(-1));
625 existing->RemoveSuccessor(this);
626 new_block->successors_.Add(this);
627 predecessors_.Put(predecessor_index, new_block);
628 }
629
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100630 // Insert `this` between `predecessor` and `successor. This method
631 // preserves the indicies, and will update the first edge found between
632 // `predecessor` and `successor`.
633 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
634 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
635 DCHECK_NE(predecessor_index, static_cast<size_t>(-1));
636 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
637 DCHECK_NE(successor_index, static_cast<size_t>(-1));
638 successor->predecessors_.Put(predecessor_index, this);
639 predecessor->successors_.Put(successor_index, this);
640 successors_.Add(successor);
641 predecessors_.Add(predecessor);
642 }
643
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100644 void RemovePredecessor(HBasicBlock* block) {
645 predecessors_.Delete(block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100646 }
647
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000648 void RemoveSuccessor(HBasicBlock* block) {
649 successors_.Delete(block);
650 }
651
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100652 void ClearAllPredecessors() {
653 predecessors_.Reset();
654 }
655
656 void AddPredecessor(HBasicBlock* block) {
657 predecessors_.Add(block);
658 block->successors_.Add(this);
659 }
660
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100661 void SwapPredecessors() {
Nicolas Geoffrayc83d4412014-09-18 16:46:20 +0100662 DCHECK_EQ(predecessors_.Size(), 2u);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100663 HBasicBlock* temp = predecessors_.Get(0);
664 predecessors_.Put(0, predecessors_.Get(1));
665 predecessors_.Put(1, temp);
666 }
667
David Brazdil769c9e52015-04-27 13:54:09 +0100668 void SwapSuccessors() {
669 DCHECK_EQ(successors_.Size(), 2u);
670 HBasicBlock* temp = successors_.Get(0);
671 successors_.Put(0, successors_.Get(1));
672 successors_.Put(1, temp);
673 }
674
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100675 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) {
676 for (size_t i = 0, e = predecessors_.Size(); i < e; ++i) {
677 if (predecessors_.Get(i) == predecessor) {
678 return i;
679 }
680 }
681 return -1;
682 }
683
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100684 size_t GetSuccessorIndexOf(HBasicBlock* successor) {
685 for (size_t i = 0, e = successors_.Size(); i < e; ++i) {
686 if (successors_.Get(i) == successor) {
687 return i;
688 }
689 }
690 return -1;
691 }
692
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000693 // Split the block into two blocks just after `cursor`. Returns the newly
694 // created block. Note that this method just updates raw block information,
695 // like predecessors, successors, dominators, and instruction list. It does not
696 // update the graph, reverse post order, loop information, nor make sure the
697 // blocks are consistent (for example ending with a control flow instruction).
698 HBasicBlock* SplitAfter(HInstruction* cursor);
699
700 // Merge `other` at the end of `this`. Successors and dominated blocks of
701 // `other` are changed to be successors and dominated blocks of `this`. Note
702 // that this method does not update the graph, reverse post order, loop
703 // information, nor make sure the blocks are consistent (for example ending
704 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +0100705 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000706
707 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
708 // of `this` are moved to `other`.
709 // Note that this method does not update the graph, reverse post order, loop
710 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +0000711 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000712 void ReplaceWith(HBasicBlock* other);
713
David Brazdil2d7352b2015-04-20 14:52:42 +0100714 // Merge `other` at the end of `this`. This method updates loops, reverse post
715 // order, links to predecessors, successors, dominators and deletes the block
716 // from the graph. The two blocks must be successive, i.e. `this` the only
717 // predecessor of `other` and vice versa.
718 void MergeWith(HBasicBlock* other);
719
720 // Disconnects `this` from all its predecessors, successors and dominator,
721 // removes it from all loops it is included in and eventually from the graph.
722 // The block must not dominate any other block. Predecessors and successors
723 // are safely updated.
724 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +0000725
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000726 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +0100727 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100728 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +0100729 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +0100730 // Replace instruction `initial` with `replacement` within this block.
731 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
732 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100733 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100734 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +0000735 // RemoveInstruction and RemovePhi delete a given instruction from the respective
736 // instruction list. With 'ensure_safety' set to true, it verifies that the
737 // instruction is not in use and removes it from the use lists of its inputs.
738 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
739 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +0100740 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100741
742 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +0100743 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100744 }
745
Roland Levillain6b879dd2014-09-22 17:13:44 +0100746 bool IsLoopPreHeaderFirstPredecessor() const {
747 DCHECK(IsLoopHeader());
748 DCHECK(!GetPredecessors().IsEmpty());
749 return GetPredecessors().Get(0) == GetLoopInformation()->GetPreHeader();
750 }
751
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100752 HLoopInformation* GetLoopInformation() const {
753 return loop_information_;
754 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000755
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000756 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100757 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000758 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100759 void SetInLoop(HLoopInformation* info) {
760 if (IsLoopHeader()) {
761 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +0100762 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100763 loop_information_ = info;
764 } else if (loop_information_->Contains(*info->GetHeader())) {
765 // Block is currently part of an outer loop. Make it part of this inner loop.
766 // Note that a non loop header having a loop information means this loop information
767 // has already been populated
768 loop_information_ = info;
769 } else {
770 // Block is part of an inner loop. Do not update the loop information.
771 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
772 // at this point, because this method is being called while populating `info`.
773 }
774 }
775
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000776 // Raw update of the loop information.
777 void SetLoopInformation(HLoopInformation* info) {
778 loop_information_ = info;
779 }
780
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100781 bool IsInLoop() const { return loop_information_ != nullptr; }
782
David Brazdila4b8c212015-05-07 09:59:30 +0100783 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100784 bool Dominates(HBasicBlock* block) const;
785
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100786 size_t GetLifetimeStart() const { return lifetime_start_; }
787 size_t GetLifetimeEnd() const { return lifetime_end_; }
788
789 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
790 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
791
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100792 uint32_t GetDexPc() const { return dex_pc_; }
793
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000794 bool IsCatchBlock() const { return is_catch_block_; }
795 void SetIsCatchBlock() { is_catch_block_ = true; }
796
David Brazdil8d5b8b22015-03-24 10:51:52 +0000797 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +0000798 bool EndsWithIf() const;
799 bool HasSinglePhi() const;
800
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000801 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000802 HGraph* graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000803 GrowableArray<HBasicBlock*> predecessors_;
804 GrowableArray<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100805 HInstructionList instructions_;
806 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000807 HLoopInformation* loop_information_;
808 HBasicBlock* dominator_;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100809 GrowableArray<HBasicBlock*> dominated_blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000810 int block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100811 // The dex program counter of the first instruction of this block.
812 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100813 size_t lifetime_start_;
814 size_t lifetime_end_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000815 bool is_catch_block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000816
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000817 friend class HGraph;
818 friend class HInstruction;
819
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000820 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
821};
822
David Brazdilb2bd1c52015-03-25 11:17:37 +0000823// Iterates over the LoopInformation of all loops which contain 'block'
824// from the innermost to the outermost.
825class HLoopInformationOutwardIterator : public ValueObject {
826 public:
827 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
828 : current_(block.GetLoopInformation()) {}
829
830 bool Done() const { return current_ == nullptr; }
831
832 void Advance() {
833 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +0100834 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +0000835 }
836
837 HLoopInformation* Current() const {
838 DCHECK(!Done());
839 return current_;
840 }
841
842 private:
843 HLoopInformation* current_;
844
845 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
846};
847
Alexandre Ramesef20f712015-06-09 10:29:30 +0100848#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100849 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000850 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000851 M(ArrayGet, Instruction) \
852 M(ArrayLength, Instruction) \
853 M(ArraySet, Instruction) \
David Brazdil66d126e2015-04-03 16:02:44 +0100854 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000855 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +0000856 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000857 M(CheckCast, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100858 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000859 M(Compare, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100860 M(Condition, BinaryOperation) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100861 M(CurrentMethod, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700862 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000863 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +0000864 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000865 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100866 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000867 M(Exit, Instruction) \
868 M(FloatConstant, Constant) \
869 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100870 M(GreaterThan, Condition) \
871 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100872 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000873 M(InstanceFieldGet, Instruction) \
874 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000875 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100876 M(IntConstant, Constant) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +0000877 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000878 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100879 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000880 M(LessThan, Condition) \
881 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000882 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000883 M(LoadException, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100884 M(LoadLocal, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000885 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100886 M(Local, Instruction) \
887 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +0100888 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +0000889 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000890 M(Mul, BinaryOperation) \
891 M(Neg, UnaryOperation) \
892 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100893 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +0100894 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000895 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000896 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000897 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000898 M(Or, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100899 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000900 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100901 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000902 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100903 M(Return, Instruction) \
904 M(ReturnVoid, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000905 M(Shl, BinaryOperation) \
906 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100907 M(StaticFieldGet, Instruction) \
908 M(StaticFieldSet, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100909 M(StoreLocal, Instruction) \
910 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100911 M(SuspendCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000912 M(Temporary, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000913 M(Throw, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +0000914 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000915 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000916 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000917
Alexandre Ramesef20f712015-06-09 10:29:30 +0100918#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
919
920#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
921
922#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
923
924#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
925
926#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
927 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
928 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
929 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
930 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
931 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
932
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100933#define FOR_EACH_INSTRUCTION(M) \
934 FOR_EACH_CONCRETE_INSTRUCTION(M) \
935 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +0100936 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +0100937 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100938 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -0700939
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100940#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000941FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
942#undef FORWARD_DECLARATION
943
Roland Levillainccc07a92014-09-16 14:48:16 +0100944#define DECLARE_INSTRUCTION(type) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000945 InstructionKind GetKind() const OVERRIDE { return k##type; } \
946 const char* DebugName() const OVERRIDE { return #type; } \
947 const H##type* As##type() const OVERRIDE { return this; } \
948 H##type* As##type() OVERRIDE { return this; } \
949 bool InstructionTypeEquals(HInstruction* other) const OVERRIDE { \
Roland Levillainccc07a92014-09-16 14:48:16 +0100950 return other->Is##type(); \
951 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000952 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000953
David Brazdiled596192015-01-23 10:39:45 +0000954template <typename T> class HUseList;
955
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100956template <typename T>
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700957class HUseListNode : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000958 public:
David Brazdiled596192015-01-23 10:39:45 +0000959 HUseListNode* GetPrevious() const { return prev_; }
960 HUseListNode* GetNext() const { return next_; }
961 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100962 size_t GetIndex() const { return index_; }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +0100963 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100964
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000965 private:
David Brazdiled596192015-01-23 10:39:45 +0000966 HUseListNode(T user, size_t index)
967 : user_(user), index_(index), prev_(nullptr), next_(nullptr) {}
968
969 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +0100970 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +0000971 HUseListNode<T>* prev_;
972 HUseListNode<T>* next_;
973
974 friend class HUseList<T>;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000975
976 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
977};
978
David Brazdiled596192015-01-23 10:39:45 +0000979template <typename T>
980class HUseList : public ValueObject {
981 public:
982 HUseList() : first_(nullptr) {}
983
984 void Clear() {
985 first_ = nullptr;
986 }
987
988 // Adds a new entry at the beginning of the use list and returns
989 // the newly created node.
990 HUseListNode<T>* AddUse(T user, size_t index, ArenaAllocator* arena) {
David Brazdilea55b932015-01-27 17:12:29 +0000991 HUseListNode<T>* new_node = new (arena) HUseListNode<T>(user, index);
David Brazdiled596192015-01-23 10:39:45 +0000992 if (IsEmpty()) {
993 first_ = new_node;
994 } else {
995 first_->prev_ = new_node;
996 new_node->next_ = first_;
997 first_ = new_node;
998 }
999 return new_node;
1000 }
1001
1002 HUseListNode<T>* GetFirst() const {
1003 return first_;
1004 }
1005
1006 void Remove(HUseListNode<T>* node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001007 DCHECK(node != nullptr);
1008 DCHECK(Contains(node));
1009
David Brazdiled596192015-01-23 10:39:45 +00001010 if (node->prev_ != nullptr) {
1011 node->prev_->next_ = node->next_;
1012 }
1013 if (node->next_ != nullptr) {
1014 node->next_->prev_ = node->prev_;
1015 }
1016 if (node == first_) {
1017 first_ = node->next_;
1018 }
1019 }
1020
David Brazdil1abb4192015-02-17 18:33:36 +00001021 bool Contains(const HUseListNode<T>* node) const {
1022 if (node == nullptr) {
1023 return false;
1024 }
1025 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1026 if (current == node) {
1027 return true;
1028 }
1029 }
1030 return false;
1031 }
1032
David Brazdiled596192015-01-23 10:39:45 +00001033 bool IsEmpty() const {
1034 return first_ == nullptr;
1035 }
1036
1037 bool HasOnlyOneUse() const {
1038 return first_ != nullptr && first_->next_ == nullptr;
1039 }
1040
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001041 size_t SizeSlow() const {
1042 size_t count = 0;
1043 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1044 ++count;
1045 }
1046 return count;
1047 }
1048
David Brazdiled596192015-01-23 10:39:45 +00001049 private:
1050 HUseListNode<T>* first_;
1051};
1052
1053template<typename T>
1054class HUseIterator : public ValueObject {
1055 public:
1056 explicit HUseIterator(const HUseList<T>& uses) : current_(uses.GetFirst()) {}
1057
1058 bool Done() const { return current_ == nullptr; }
1059
1060 void Advance() {
1061 DCHECK(!Done());
1062 current_ = current_->GetNext();
1063 }
1064
1065 HUseListNode<T>* Current() const {
1066 DCHECK(!Done());
1067 return current_;
1068 }
1069
1070 private:
1071 HUseListNode<T>* current_;
1072
1073 friend class HValue;
1074};
1075
David Brazdil1abb4192015-02-17 18:33:36 +00001076// This class is used by HEnvironment and HInstruction classes to record the
1077// instructions they use and pointers to the corresponding HUseListNodes kept
1078// by the used instructions.
1079template <typename T>
1080class HUserRecord : public ValueObject {
1081 public:
1082 HUserRecord() : instruction_(nullptr), use_node_(nullptr) {}
1083 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), use_node_(nullptr) {}
1084
1085 HUserRecord(const HUserRecord<T>& old_record, HUseListNode<T>* use_node)
1086 : instruction_(old_record.instruction_), use_node_(use_node) {
1087 DCHECK(instruction_ != nullptr);
1088 DCHECK(use_node_ != nullptr);
1089 DCHECK(old_record.use_node_ == nullptr);
1090 }
1091
1092 HInstruction* GetInstruction() const { return instruction_; }
1093 HUseListNode<T>* GetUseNode() const { return use_node_; }
1094
1095 private:
1096 // Instruction used by the user.
1097 HInstruction* instruction_;
1098
1099 // Corresponding entry in the use list kept by 'instruction_'.
1100 HUseListNode<T>* use_node_;
1101};
1102
Calin Juravle27df7582015-04-17 19:12:31 +01001103// TODO: Add better documentation to this class and maybe refactor with more suggestive names.
1104// - Has(All)SideEffects suggests that all the side effects are present but only ChangesSomething
1105// flag is consider.
1106// - DependsOn suggests that there is a real dependency between side effects but it only
1107// checks DependendsOnSomething flag.
1108//
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001109// Represents the side effects an instruction may have.
1110class SideEffects : public ValueObject {
1111 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001112 SideEffects() : flags_(0) {}
1113
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001114 static SideEffects None() {
1115 return SideEffects(0);
1116 }
1117
1118 static SideEffects All() {
1119 return SideEffects(ChangesSomething().flags_ | DependsOnSomething().flags_);
1120 }
1121
1122 static SideEffects ChangesSomething() {
1123 return SideEffects((1 << kFlagChangesCount) - 1);
1124 }
1125
1126 static SideEffects DependsOnSomething() {
1127 int count = kFlagDependsOnCount - kFlagChangesCount;
1128 return SideEffects(((1 << count) - 1) << kFlagChangesCount);
1129 }
1130
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001131 SideEffects Union(SideEffects other) const {
1132 return SideEffects(flags_ | other.flags_);
1133 }
1134
Roland Levillain72bceff2014-09-15 18:29:00 +01001135 bool HasSideEffects() const {
1136 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
1137 return (flags_ & all_bits_set) != 0;
1138 }
1139
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001140 bool HasAllSideEffects() const {
1141 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
1142 return all_bits_set == (flags_ & all_bits_set);
1143 }
1144
1145 bool DependsOn(SideEffects other) const {
1146 size_t depends_flags = other.ComputeDependsFlags();
1147 return (flags_ & depends_flags) != 0;
1148 }
1149
1150 bool HasDependencies() const {
1151 int count = kFlagDependsOnCount - kFlagChangesCount;
1152 size_t all_bits_set = (1 << count) - 1;
1153 return ((flags_ >> kFlagChangesCount) & all_bits_set) != 0;
1154 }
1155
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001156 private:
1157 static constexpr int kFlagChangesSomething = 0;
1158 static constexpr int kFlagChangesCount = kFlagChangesSomething + 1;
1159
1160 static constexpr int kFlagDependsOnSomething = kFlagChangesCount;
1161 static constexpr int kFlagDependsOnCount = kFlagDependsOnSomething + 1;
1162
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001163 explicit SideEffects(size_t flags) : flags_(flags) {}
1164
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001165 size_t ComputeDependsFlags() const {
1166 return flags_ << kFlagChangesCount;
1167 }
1168
1169 size_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001170};
1171
David Brazdiled596192015-01-23 10:39:45 +00001172// A HEnvironment object contains the values of virtual registers at a given location.
1173class HEnvironment : public ArenaObject<kArenaAllocMisc> {
1174 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001175 HEnvironment(ArenaAllocator* arena,
1176 size_t number_of_vregs,
1177 const DexFile& dex_file,
1178 uint32_t method_idx,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001179 uint32_t dex_pc,
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001180 InvokeType invoke_type,
1181 HInstruction* holder)
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001182 : vregs_(arena, number_of_vregs),
1183 locations_(arena, number_of_vregs),
1184 parent_(nullptr),
1185 dex_file_(dex_file),
1186 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001187 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001188 invoke_type_(invoke_type),
1189 holder_(holder) {
David Brazdiled596192015-01-23 10:39:45 +00001190 vregs_.SetSize(number_of_vregs);
1191 for (size_t i = 0; i < number_of_vregs; i++) {
David Brazdil1abb4192015-02-17 18:33:36 +00001192 vregs_.Put(i, HUserRecord<HEnvironment*>());
David Brazdiled596192015-01-23 10:39:45 +00001193 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001194
1195 locations_.SetSize(number_of_vregs);
1196 for (size_t i = 0; i < number_of_vregs; ++i) {
1197 locations_.Put(i, Location());
1198 }
1199 }
1200
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001201 HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001202 : HEnvironment(arena,
1203 to_copy.Size(),
1204 to_copy.GetDexFile(),
1205 to_copy.GetMethodIdx(),
1206 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001207 to_copy.GetInvokeType(),
1208 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001209
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001210 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001211 if (parent_ != nullptr) {
1212 parent_->SetAndCopyParentChain(allocator, parent);
1213 } else {
1214 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1215 parent_->CopyFrom(parent);
1216 if (parent->GetParent() != nullptr) {
1217 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1218 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001219 }
David Brazdiled596192015-01-23 10:39:45 +00001220 }
1221
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001222 void CopyFrom(const GrowableArray<HInstruction*>& locals);
1223 void CopyFrom(HEnvironment* environment);
1224
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001225 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1226 // input to the loop phi instead. This is for inserting instructions that
1227 // require an environment (like HDeoptimization) in the loop pre-header.
1228 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001229
1230 void SetRawEnvAt(size_t index, HInstruction* instruction) {
David Brazdil1abb4192015-02-17 18:33:36 +00001231 vregs_.Put(index, HUserRecord<HEnvironment*>(instruction));
David Brazdiled596192015-01-23 10:39:45 +00001232 }
1233
1234 HInstruction* GetInstructionAt(size_t index) const {
David Brazdil1abb4192015-02-17 18:33:36 +00001235 return vregs_.Get(index).GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001236 }
1237
David Brazdil1abb4192015-02-17 18:33:36 +00001238 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001239
1240 size_t Size() const { return vregs_.Size(); }
1241
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001242 HEnvironment* GetParent() const { return parent_; }
1243
1244 void SetLocationAt(size_t index, Location location) {
1245 locations_.Put(index, location);
1246 }
1247
1248 Location GetLocationAt(size_t index) const {
1249 return locations_.Get(index);
1250 }
1251
1252 uint32_t GetDexPc() const {
1253 return dex_pc_;
1254 }
1255
1256 uint32_t GetMethodIdx() const {
1257 return method_idx_;
1258 }
1259
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001260 InvokeType GetInvokeType() const {
1261 return invoke_type_;
1262 }
1263
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001264 const DexFile& GetDexFile() const {
1265 return dex_file_;
1266 }
1267
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001268 HInstruction* GetHolder() const {
1269 return holder_;
1270 }
1271
David Brazdiled596192015-01-23 10:39:45 +00001272 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001273 // Record instructions' use entries of this environment for constant-time removal.
1274 // It should only be called by HInstruction when a new environment use is added.
1275 void RecordEnvUse(HUseListNode<HEnvironment*>* env_use) {
1276 DCHECK(env_use->GetUser() == this);
1277 size_t index = env_use->GetIndex();
1278 vregs_.Put(index, HUserRecord<HEnvironment*>(vregs_.Get(index), env_use));
1279 }
David Brazdiled596192015-01-23 10:39:45 +00001280
David Brazdil1abb4192015-02-17 18:33:36 +00001281 GrowableArray<HUserRecord<HEnvironment*> > vregs_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001282 GrowableArray<Location> locations_;
1283 HEnvironment* parent_;
1284 const DexFile& dex_file_;
1285 const uint32_t method_idx_;
1286 const uint32_t dex_pc_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001287 const InvokeType invoke_type_;
David Brazdiled596192015-01-23 10:39:45 +00001288
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001289 // The instruction that holds this environment. Only used in debug mode
1290 // to ensure the graph is consistent.
1291 HInstruction* const holder_;
1292
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001293 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001294
1295 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1296};
1297
Calin Juravleacf735c2015-02-12 15:25:22 +00001298class ReferenceTypeInfo : ValueObject {
1299 public:
Calin Juravleb1498f62015-02-16 13:13:29 +00001300 typedef Handle<mirror::Class> TypeHandle;
1301
1302 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact)
1303 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1304 if (type_handle->IsObjectClass()) {
1305 // Override the type handle to be consistent with the case when we get to
1306 // Top but don't have the Object class available. It avoids having to guess
1307 // what value the type_handle has when it's Top.
1308 return ReferenceTypeInfo(TypeHandle(), is_exact, true);
1309 } else {
1310 return ReferenceTypeInfo(type_handle, is_exact, false);
1311 }
1312 }
1313
1314 static ReferenceTypeInfo CreateTop(bool is_exact) {
1315 return ReferenceTypeInfo(TypeHandle(), is_exact, true);
Calin Juravleacf735c2015-02-12 15:25:22 +00001316 }
1317
1318 bool IsExact() const { return is_exact_; }
1319 bool IsTop() const { return is_top_; }
Guillaume Sanchez222862c2015-06-09 18:33:02 +01001320 bool IsInterface() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1321 return !IsTop() && GetTypeHandle()->IsInterface();
1322 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001323
1324 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
1325
Calin Juravleb1498f62015-02-16 13:13:29 +00001326 bool IsSupertypeOf(ReferenceTypeInfo rti) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Calin Juravleacf735c2015-02-12 15:25:22 +00001327 if (IsTop()) {
1328 // Top (equivalent for java.lang.Object) is supertype of anything.
1329 return true;
1330 }
1331 if (rti.IsTop()) {
1332 // If we get here `this` is not Top() so it can't be a supertype.
1333 return false;
1334 }
1335 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
1336 }
1337
1338 // Returns true if the type information provide the same amount of details.
1339 // Note that it does not mean that the instructions have the same actual type
1340 // (e.g. tops are equal but they can be the result of a merge).
1341 bool IsEqual(ReferenceTypeInfo rti) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1342 if (IsExact() != rti.IsExact()) {
1343 return false;
1344 }
1345 if (IsTop() && rti.IsTop()) {
1346 // `Top` means java.lang.Object, so the types are equivalent.
1347 return true;
1348 }
1349 if (IsTop() || rti.IsTop()) {
1350 // If only one is top or object than they are not equivalent.
1351 // NB: We need this extra check because the type_handle of `Top` is invalid
1352 // and we cannot inspect its reference.
1353 return false;
1354 }
1355
1356 // Finally check the types.
1357 return GetTypeHandle().Get() == rti.GetTypeHandle().Get();
1358 }
1359
1360 private:
Calin Juravleb1498f62015-02-16 13:13:29 +00001361 ReferenceTypeInfo() : ReferenceTypeInfo(TypeHandle(), false, true) {}
1362 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact, bool is_top)
1363 : type_handle_(type_handle), is_exact_(is_exact), is_top_(is_top) {}
1364
Calin Juravleacf735c2015-02-12 15:25:22 +00001365 // The class of the object.
Calin Juravleb1498f62015-02-16 13:13:29 +00001366 TypeHandle type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001367 // Whether or not the type is exact or a superclass of the actual type.
Calin Juravleb1498f62015-02-16 13:13:29 +00001368 // Whether or not we have any information about this type.
Calin Juravleacf735c2015-02-12 15:25:22 +00001369 bool is_exact_;
1370 // A true value here means that the object type should be java.lang.Object.
1371 // We don't have access to the corresponding mirror object every time so this
1372 // flag acts as a substitute. When true, the TypeHandle refers to a null
1373 // pointer and should not be used.
1374 bool is_top_;
1375};
1376
1377std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
1378
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001379class HInstruction : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001380 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001381 explicit HInstruction(SideEffects side_effects)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001382 : previous_(nullptr),
1383 next_(nullptr),
1384 block_(nullptr),
1385 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001386 ssa_index_(-1),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001387 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001388 locations_(nullptr),
1389 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001390 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001391 side_effects_(side_effects),
1392 reference_type_info_(ReferenceTypeInfo::CreateTop(/* is_exact */ false)) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001393
Dave Allison20dfc792014-06-16 20:44:29 -07001394 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001395
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001396#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001397 enum InstructionKind {
1398 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1399 };
1400#undef DECLARE_KIND
1401
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001402 HInstruction* GetNext() const { return next_; }
1403 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001404
Calin Juravle77520bc2015-01-12 18:45:46 +00001405 HInstruction* GetNextDisregardingMoves() const;
1406 HInstruction* GetPreviousDisregardingMoves() const;
1407
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001408 HBasicBlock* GetBlock() const { return block_; }
1409 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001410 bool IsInBlock() const { return block_ != nullptr; }
1411 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +01001412 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001413
Roland Levillain6b879dd2014-09-22 17:13:44 +01001414 virtual size_t InputCount() const = 0;
David Brazdil1abb4192015-02-17 18:33:36 +00001415 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001416
1417 virtual void Accept(HGraphVisitor* visitor) = 0;
1418 virtual const char* DebugName() const = 0;
1419
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001420 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
David Brazdil1abb4192015-02-17 18:33:36 +00001421 void SetRawInputAt(size_t index, HInstruction* input) {
1422 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1423 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001424
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001425 virtual bool NeedsEnvironment() const { return false; }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001426 virtual uint32_t GetDexPc() const {
1427 LOG(FATAL) << "GetDexPc() cannot be called on an instruction that"
1428 " does not need an environment";
1429 UNREACHABLE();
1430 }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001431 virtual bool IsControlFlow() const { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01001432 virtual bool CanThrow() const { return false; }
Roland Levillain72bceff2014-09-15 18:29:00 +01001433 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001434
Calin Juravle10e244f2015-01-26 18:54:32 +00001435 // Does not apply for all instructions, but having this at top level greatly
1436 // simplifies the null check elimination.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001437 virtual bool CanBeNull() const {
1438 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1439 return true;
1440 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001441
Calin Juravle641547a2015-04-21 22:08:51 +01001442 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj) const {
1443 UNUSED(obj);
1444 return false;
1445 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001446
Calin Juravleacf735c2015-02-12 15:25:22 +00001447 void SetReferenceTypeInfo(ReferenceTypeInfo reference_type_info) {
Calin Juravle61d544b2015-02-23 16:46:57 +00001448 DCHECK_EQ(GetType(), Primitive::kPrimNot);
Calin Juravleacf735c2015-02-12 15:25:22 +00001449 reference_type_info_ = reference_type_info;
1450 }
1451
Calin Juravle61d544b2015-02-23 16:46:57 +00001452 ReferenceTypeInfo GetReferenceTypeInfo() const {
1453 DCHECK_EQ(GetType(), Primitive::kPrimNot);
1454 return reference_type_info_;
1455 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001456
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001457 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001458 DCHECK(user != nullptr);
1459 HUseListNode<HInstruction*>* use =
1460 uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1461 user->SetRawInputRecordAt(index, HUserRecord<HInstruction*>(user->InputRecordAt(index), use));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001462 }
1463
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001464 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001465 DCHECK(user != nullptr);
David Brazdiled596192015-01-23 10:39:45 +00001466 HUseListNode<HEnvironment*>* env_use =
1467 env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1468 user->RecordEnvUse(env_use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001469 }
1470
David Brazdil1abb4192015-02-17 18:33:36 +00001471 void RemoveAsUserOfInput(size_t input) {
1472 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
1473 input_use.GetInstruction()->uses_.Remove(input_use.GetUseNode());
1474 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001475
David Brazdil1abb4192015-02-17 18:33:36 +00001476 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1477 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001478
David Brazdiled596192015-01-23 10:39:45 +00001479 bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); }
1480 bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); }
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001481 bool HasNonEnvironmentUses() const { return !uses_.IsEmpty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001482 bool HasOnlyOneNonEnvironmentUse() const {
1483 return !HasEnvironmentUses() && GetUses().HasOnlyOneUse();
1484 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001485
Roland Levillain6c82d402014-10-13 16:10:27 +01001486 // Does this instruction strictly dominate `other_instruction`?
1487 // Returns false if this instruction and `other_instruction` are the same.
1488 // Aborts if this instruction and `other_instruction` are both phis.
1489 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001490
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001491 int GetId() const { return id_; }
1492 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001493
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001494 int GetSsaIndex() const { return ssa_index_; }
1495 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1496 bool HasSsaIndex() const { return ssa_index_ != -1; }
1497
1498 bool HasEnvironment() const { return environment_ != nullptr; }
1499 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001500 // Set the `environment_` field. Raw because this method does not
1501 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001502 void SetRawEnvironment(HEnvironment* environment) {
1503 DCHECK(environment_ == nullptr);
1504 DCHECK_EQ(environment->GetHolder(), this);
1505 environment_ = environment;
1506 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001507
1508 // Set the environment of this instruction, copying it from `environment`. While
1509 // copying, the uses lists are being updated.
1510 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001511 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001512 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001513 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001514 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001515 if (environment->GetParent() != nullptr) {
1516 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1517 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001518 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001519
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001520 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
1521 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001522 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001523 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001524 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001525 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001526 if (environment->GetParent() != nullptr) {
1527 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1528 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001529 }
1530
Nicolas Geoffray39468442014-09-02 15:17:15 +01001531 // Returns the number of entries in the environment. Typically, that is the
1532 // number of dex registers in a method. It could be more in case of inlining.
1533 size_t EnvironmentSize() const;
1534
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001535 LocationSummary* GetLocations() const { return locations_; }
1536 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001537
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001538 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001539 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001540
Alexandre Rames188d4312015-04-09 18:30:21 +01001541 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
1542 // uses of this instruction by `other` are *not* updated.
1543 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
1544 ReplaceWith(other);
1545 other->ReplaceInput(this, use_index);
1546 }
1547
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001548 // Move `this` instruction before `cursor`.
1549 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001550
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001551#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01001552 bool Is##type() const { return (As##type() != nullptr); } \
1553 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001554 virtual H##type* As##type() { return nullptr; }
1555
1556 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
1557#undef INSTRUCTION_TYPE_CHECK
1558
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001559 // Returns whether the instruction can be moved within the graph.
1560 virtual bool CanBeMoved() const { return false; }
1561
1562 // Returns whether the two instructions are of the same kind.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001563 virtual bool InstructionTypeEquals(HInstruction* other) const {
1564 UNUSED(other);
1565 return false;
1566 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001567
1568 // Returns whether any data encoded in the two instructions is equal.
1569 // This method does not look at the inputs. Both instructions must be
1570 // of the same type, otherwise the method has undefined behavior.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001571 virtual bool InstructionDataEquals(HInstruction* other) const {
1572 UNUSED(other);
1573 return false;
1574 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001575
1576 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00001577 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001578 // 2) Their inputs are identical.
1579 bool Equals(HInstruction* other) const;
1580
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001581 virtual InstructionKind GetKind() const = 0;
1582
1583 virtual size_t ComputeHashCode() const {
1584 size_t result = GetKind();
1585 for (size_t i = 0, e = InputCount(); i < e; ++i) {
1586 result = (result * 31) + InputAt(i)->GetId();
1587 }
1588 return result;
1589 }
1590
1591 SideEffects GetSideEffects() const { return side_effects_; }
1592
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001593 size_t GetLifetimePosition() const { return lifetime_position_; }
1594 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
1595 LiveInterval* GetLiveInterval() const { return live_interval_; }
1596 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
1597 bool HasLiveInterval() const { return live_interval_ != nullptr; }
1598
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001599 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
1600
1601 // Returns whether the code generation of the instruction will require to have access
1602 // to the current method. Such instructions are:
1603 // (1): Instructions that require an environment, as calling the runtime requires
1604 // to walk the stack and have the current method stored at a specific stack address.
1605 // (2): Object literals like classes and strings, that are loaded from the dex cache
1606 // fields of the current method.
1607 bool NeedsCurrentMethod() const {
1608 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
1609 }
1610
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001611 virtual bool NeedsDexCache() const { return false; }
1612
David Brazdil1abb4192015-02-17 18:33:36 +00001613 protected:
1614 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
1615 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
1616
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001617 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001618 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); }
1619
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001620 HInstruction* previous_;
1621 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001622 HBasicBlock* block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001623
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001624 // An instruction gets an id when it is added to the graph.
1625 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01001626 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001627 int id_;
1628
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001629 // When doing liveness analysis, instructions that have uses get an SSA index.
1630 int ssa_index_;
1631
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001632 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00001633 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001634
1635 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00001636 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001637
Nicolas Geoffray39468442014-09-02 15:17:15 +01001638 // The environment associated with this instruction. Not null if the instruction
1639 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001640 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001641
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001642 // Set by the code generator.
1643 LocationSummary* locations_;
1644
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001645 // Set by the liveness analysis.
1646 LiveInterval* live_interval_;
1647
1648 // Set by the liveness analysis, this is the position in a linear
1649 // order of blocks where this instruction's live interval start.
1650 size_t lifetime_position_;
1651
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001652 const SideEffects side_effects_;
1653
Calin Juravleacf735c2015-02-12 15:25:22 +00001654 // TODO: for primitive types this should be marked as invalid.
1655 ReferenceTypeInfo reference_type_info_;
1656
David Brazdil1abb4192015-02-17 18:33:36 +00001657 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001658 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00001659 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001660 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001661 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001662
1663 DISALLOW_COPY_AND_ASSIGN(HInstruction);
1664};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001665std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001666
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001667class HInputIterator : public ValueObject {
1668 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001669 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001670
1671 bool Done() const { return index_ == instruction_->InputCount(); }
1672 HInstruction* Current() const { return instruction_->InputAt(index_); }
1673 void Advance() { index_++; }
1674
1675 private:
1676 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001677 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001678
1679 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
1680};
1681
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001682class HInstructionIterator : public ValueObject {
1683 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001684 explicit HInstructionIterator(const HInstructionList& instructions)
1685 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001686 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001687 }
1688
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001689 bool Done() const { return instruction_ == nullptr; }
1690 HInstruction* Current() const { return instruction_; }
1691 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001692 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001693 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001694 }
1695
1696 private:
1697 HInstruction* instruction_;
1698 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001699
1700 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001701};
1702
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001703class HBackwardInstructionIterator : public ValueObject {
1704 public:
1705 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
1706 : instruction_(instructions.last_instruction_) {
1707 next_ = Done() ? nullptr : instruction_->GetPrevious();
1708 }
1709
1710 bool Done() const { return instruction_ == nullptr; }
1711 HInstruction* Current() const { return instruction_; }
1712 void Advance() {
1713 instruction_ = next_;
1714 next_ = Done() ? nullptr : instruction_->GetPrevious();
1715 }
1716
1717 private:
1718 HInstruction* instruction_;
1719 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001720
1721 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001722};
1723
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001724// An embedded container with N elements of type T. Used (with partial
1725// specialization for N=0) because embedded arrays cannot have size 0.
1726template<typename T, intptr_t N>
1727class EmbeddedArray {
1728 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001729 EmbeddedArray() : elements_() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001730
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001731 intptr_t GetLength() const { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001732
1733 const T& operator[](intptr_t i) const {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001734 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001735 return elements_[i];
1736 }
1737
1738 T& operator[](intptr_t i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001739 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001740 return elements_[i];
1741 }
1742
1743 const T& At(intptr_t i) const {
1744 return (*this)[i];
1745 }
1746
1747 void SetAt(intptr_t i, const T& val) {
1748 (*this)[i] = val;
1749 }
1750
1751 private:
1752 T elements_[N];
1753};
1754
1755template<typename T>
1756class EmbeddedArray<T, 0> {
1757 public:
1758 intptr_t length() const { return 0; }
1759 const T& operator[](intptr_t i) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001760 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001761 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001762 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001763 }
1764 T& operator[](intptr_t i) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001765 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001766 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001767 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001768 }
1769};
1770
1771template<intptr_t N>
1772class HTemplateInstruction: public HInstruction {
1773 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001774 HTemplateInstruction<N>(SideEffects side_effects)
1775 : HInstruction(side_effects), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07001776 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001777
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001778 size_t InputCount() const OVERRIDE { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001779
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001780 protected:
David Brazdil1abb4192015-02-17 18:33:36 +00001781 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_[i]; }
1782
1783 void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE {
1784 inputs_[i] = input;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001785 }
1786
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001787 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001788 EmbeddedArray<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001789
1790 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001791};
1792
Dave Allison20dfc792014-06-16 20:44:29 -07001793template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001794class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07001795 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001796 HExpression<N>(Primitive::Type type, SideEffects side_effects)
1797 : HTemplateInstruction<N>(side_effects), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07001798 virtual ~HExpression() {}
1799
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001800 Primitive::Type GetType() const OVERRIDE { return type_; }
Dave Allison20dfc792014-06-16 20:44:29 -07001801
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001802 protected:
1803 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07001804};
1805
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001806// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
1807// instruction that branches to the exit block.
1808class HReturnVoid : public HTemplateInstruction<0> {
1809 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001810 HReturnVoid() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001811
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001812 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001813
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001814 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001815
1816 private:
1817 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
1818};
1819
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001820// Represents dex's RETURN opcodes. A HReturn is a control flow
1821// instruction that branches to the exit block.
1822class HReturn : public HTemplateInstruction<1> {
1823 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001824 explicit HReturn(HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001825 SetRawInputAt(0, value);
1826 }
1827
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001828 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001829
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001830 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001831
1832 private:
1833 DISALLOW_COPY_AND_ASSIGN(HReturn);
1834};
1835
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001836// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001837// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001838// exit block.
1839class HExit : public HTemplateInstruction<0> {
1840 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001841 HExit() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001842
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001843 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001844
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001845 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001846
1847 private:
1848 DISALLOW_COPY_AND_ASSIGN(HExit);
1849};
1850
1851// Jumps from one block to another.
1852class HGoto : public HTemplateInstruction<0> {
1853 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001854 HGoto() : HTemplateInstruction(SideEffects::None()) {}
1855
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001856 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001857
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001858 HBasicBlock* GetSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001859 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001860 }
1861
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001862 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001863
1864 private:
1865 DISALLOW_COPY_AND_ASSIGN(HGoto);
1866};
1867
Dave Allison20dfc792014-06-16 20:44:29 -07001868
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001869// Conditional branch. A block ending with an HIf instruction must have
1870// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001871class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001872 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001873 explicit HIf(HInstruction* input) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001874 SetRawInputAt(0, input);
1875 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001876
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001877 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001878
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001879 HBasicBlock* IfTrueSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001880 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001881 }
1882
1883 HBasicBlock* IfFalseSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001884 return GetBlock()->GetSuccessors().Get(1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001885 }
1886
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001887 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001888
1889 private:
1890 DISALLOW_COPY_AND_ASSIGN(HIf);
1891};
1892
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001893// Deoptimize to interpreter, upon checking a condition.
1894class HDeoptimize : public HTemplateInstruction<1> {
1895 public:
1896 HDeoptimize(HInstruction* cond, uint32_t dex_pc)
1897 : HTemplateInstruction(SideEffects::None()),
1898 dex_pc_(dex_pc) {
1899 SetRawInputAt(0, cond);
1900 }
1901
1902 bool NeedsEnvironment() const OVERRIDE { return true; }
1903 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001904 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001905
1906 DECLARE_INSTRUCTION(Deoptimize);
1907
1908 private:
1909 uint32_t dex_pc_;
1910
1911 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
1912};
1913
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001914// Represents the ArtMethod that was passed as a first argument to
1915// the method. It is used by instructions that depend on it, like
1916// instructions that work with the dex cache.
1917class HCurrentMethod : public HExpression<0> {
1918 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -07001919 explicit HCurrentMethod(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001920
1921 DECLARE_INSTRUCTION(CurrentMethod);
1922
1923 private:
1924 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
1925};
1926
Roland Levillain88cb1752014-10-20 16:36:47 +01001927class HUnaryOperation : public HExpression<1> {
1928 public:
1929 HUnaryOperation(Primitive::Type result_type, HInstruction* input)
1930 : HExpression(result_type, SideEffects::None()) {
1931 SetRawInputAt(0, input);
1932 }
1933
1934 HInstruction* GetInput() const { return InputAt(0); }
1935 Primitive::Type GetResultType() const { return GetType(); }
1936
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001937 bool CanBeMoved() const OVERRIDE { return true; }
1938 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001939 UNUSED(other);
1940 return true;
1941 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001942
Roland Levillain9240d6a2014-10-20 16:47:04 +01001943 // Try to statically evaluate `operation` and return a HConstant
1944 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001945 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01001946 HConstant* TryStaticEvaluation() const;
1947
1948 // Apply this operation to `x`.
1949 virtual int32_t Evaluate(int32_t x) const = 0;
1950 virtual int64_t Evaluate(int64_t x) const = 0;
1951
Roland Levillain88cb1752014-10-20 16:36:47 +01001952 DECLARE_INSTRUCTION(UnaryOperation);
1953
1954 private:
1955 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
1956};
1957
Dave Allison20dfc792014-06-16 20:44:29 -07001958class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001959 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001960 HBinaryOperation(Primitive::Type result_type,
1961 HInstruction* left,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001962 HInstruction* right) : HExpression(result_type, SideEffects::None()) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001963 SetRawInputAt(0, left);
1964 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001965 }
1966
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001967 HInstruction* GetLeft() const { return InputAt(0); }
1968 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07001969 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001970
Mingyao Yangdc5ac732015-02-25 11:28:05 -08001971 virtual bool IsCommutative() const { return false; }
1972
1973 // Put constant on the right.
1974 // Returns whether order is changed.
1975 bool OrderInputsWithConstantOnTheRight() {
1976 HInstruction* left = InputAt(0);
1977 HInstruction* right = InputAt(1);
1978 if (left->IsConstant() && !right->IsConstant()) {
1979 ReplaceInput(right, 0);
1980 ReplaceInput(left, 1);
1981 return true;
1982 }
1983 return false;
1984 }
1985
1986 // Order inputs by instruction id, but favor constant on the right side.
1987 // This helps GVN for commutative ops.
1988 void OrderInputs() {
1989 DCHECK(IsCommutative());
1990 HInstruction* left = InputAt(0);
1991 HInstruction* right = InputAt(1);
1992 if (left == right || (!left->IsConstant() && right->IsConstant())) {
1993 return;
1994 }
1995 if (OrderInputsWithConstantOnTheRight()) {
1996 return;
1997 }
1998 // Order according to instruction id.
1999 if (left->GetId() > right->GetId()) {
2000 ReplaceInput(right, 0);
2001 ReplaceInput(left, 1);
2002 }
2003 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002004
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002005 bool CanBeMoved() const OVERRIDE { return true; }
2006 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002007 UNUSED(other);
2008 return true;
2009 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002010
Roland Levillain9240d6a2014-10-20 16:47:04 +01002011 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01002012 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002013 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002014 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01002015
2016 // Apply this operation to `x` and `y`.
2017 virtual int32_t Evaluate(int32_t x, int32_t y) const = 0;
2018 virtual int64_t Evaluate(int64_t x, int64_t y) const = 0;
2019
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002020 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002021 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002022 HConstant* GetConstantRight() const;
2023
2024 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002025 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002026 HInstruction* GetLeastConstantLeft() const;
2027
Roland Levillainccc07a92014-09-16 14:48:16 +01002028 DECLARE_INSTRUCTION(BinaryOperation);
2029
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002030 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002031 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
2032};
2033
Dave Allison20dfc792014-06-16 20:44:29 -07002034class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002035 public:
Dave Allison20dfc792014-06-16 20:44:29 -07002036 HCondition(HInstruction* first, HInstruction* second)
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002037 : HBinaryOperation(Primitive::kPrimBoolean, first, second),
2038 needs_materialization_(true) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002039
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002040 bool NeedsMaterialization() const { return needs_materialization_; }
2041 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002042
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002043 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002044 // `instruction`, and disregard moves in between.
2045 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002046
Dave Allison20dfc792014-06-16 20:44:29 -07002047 DECLARE_INSTRUCTION(Condition);
2048
2049 virtual IfCondition GetCondition() const = 0;
2050
2051 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002052 // For register allocation purposes, returns whether this instruction needs to be
2053 // materialized (that is, not just be in the processor flags).
2054 bool needs_materialization_;
2055
Dave Allison20dfc792014-06-16 20:44:29 -07002056 DISALLOW_COPY_AND_ASSIGN(HCondition);
2057};
2058
2059// Instruction to check if two inputs are equal to each other.
2060class HEqual : public HCondition {
2061 public:
2062 HEqual(HInstruction* first, HInstruction* second)
2063 : HCondition(first, second) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002064
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002065 bool IsCommutative() const OVERRIDE { return true; }
2066
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002067 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002068 return x == y ? 1 : 0;
2069 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002070 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002071 return x == y ? 1 : 0;
2072 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002073
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002074 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002075
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002076 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002077 return kCondEQ;
2078 }
2079
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002080 private:
2081 DISALLOW_COPY_AND_ASSIGN(HEqual);
2082};
2083
Dave Allison20dfc792014-06-16 20:44:29 -07002084class HNotEqual : public HCondition {
2085 public:
2086 HNotEqual(HInstruction* first, HInstruction* second)
2087 : HCondition(first, second) {}
2088
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002089 bool IsCommutative() const OVERRIDE { return true; }
2090
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002091 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002092 return x != y ? 1 : 0;
2093 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002094 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002095 return x != y ? 1 : 0;
2096 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002097
Dave Allison20dfc792014-06-16 20:44:29 -07002098 DECLARE_INSTRUCTION(NotEqual);
2099
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002100 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002101 return kCondNE;
2102 }
2103
2104 private:
2105 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
2106};
2107
2108class HLessThan : public HCondition {
2109 public:
2110 HLessThan(HInstruction* first, HInstruction* second)
2111 : HCondition(first, second) {}
2112
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002113 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002114 return x < y ? 1 : 0;
2115 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002116 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002117 return x < y ? 1 : 0;
2118 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002119
Dave Allison20dfc792014-06-16 20:44:29 -07002120 DECLARE_INSTRUCTION(LessThan);
2121
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002122 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002123 return kCondLT;
2124 }
2125
2126 private:
2127 DISALLOW_COPY_AND_ASSIGN(HLessThan);
2128};
2129
2130class HLessThanOrEqual : public HCondition {
2131 public:
2132 HLessThanOrEqual(HInstruction* first, HInstruction* second)
2133 : HCondition(first, second) {}
2134
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002135 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002136 return x <= y ? 1 : 0;
2137 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002138 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002139 return x <= y ? 1 : 0;
2140 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002141
Dave Allison20dfc792014-06-16 20:44:29 -07002142 DECLARE_INSTRUCTION(LessThanOrEqual);
2143
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002144 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002145 return kCondLE;
2146 }
2147
2148 private:
2149 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
2150};
2151
2152class HGreaterThan : public HCondition {
2153 public:
2154 HGreaterThan(HInstruction* first, HInstruction* second)
2155 : HCondition(first, second) {}
2156
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002157 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002158 return x > y ? 1 : 0;
2159 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002160 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002161 return x > y ? 1 : 0;
2162 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002163
Dave Allison20dfc792014-06-16 20:44:29 -07002164 DECLARE_INSTRUCTION(GreaterThan);
2165
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002166 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002167 return kCondGT;
2168 }
2169
2170 private:
2171 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
2172};
2173
2174class HGreaterThanOrEqual : public HCondition {
2175 public:
2176 HGreaterThanOrEqual(HInstruction* first, HInstruction* second)
2177 : HCondition(first, second) {}
2178
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002179 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002180 return x >= y ? 1 : 0;
2181 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002182 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002183 return x >= y ? 1 : 0;
2184 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002185
Dave Allison20dfc792014-06-16 20:44:29 -07002186 DECLARE_INSTRUCTION(GreaterThanOrEqual);
2187
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002188 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002189 return kCondGE;
2190 }
2191
2192 private:
2193 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
2194};
2195
2196
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002197// Instruction to check how two inputs compare to each other.
2198// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
2199class HCompare : public HBinaryOperation {
2200 public:
Calin Juravleddb7df22014-11-25 20:56:51 +00002201 // The bias applies for floating point operations and indicates how NaN
2202 // comparisons are treated:
2203 enum Bias {
2204 kNoBias, // bias is not applicable (i.e. for long operation)
2205 kGtBias, // return 1 for NaN comparisons
2206 kLtBias, // return -1 for NaN comparisons
2207 };
2208
Alexey Frunze4dda3372015-06-01 18:31:49 -07002209 HCompare(Primitive::Type type,
2210 HInstruction* first,
2211 HInstruction* second,
2212 Bias bias,
2213 uint32_t dex_pc)
2214 : HBinaryOperation(Primitive::kPrimInt, first, second), bias_(bias), dex_pc_(dex_pc) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002215 DCHECK_EQ(type, first->GetType());
2216 DCHECK_EQ(type, second->GetType());
2217 }
2218
Calin Juravleddb7df22014-11-25 20:56:51 +00002219 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01002220 return
2221 x == y ? 0 :
2222 x > y ? 1 :
2223 -1;
2224 }
Calin Juravleddb7df22014-11-25 20:56:51 +00002225
2226 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01002227 return
2228 x == y ? 0 :
2229 x > y ? 1 :
2230 -1;
2231 }
2232
Calin Juravleddb7df22014-11-25 20:56:51 +00002233 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2234 return bias_ == other->AsCompare()->bias_;
2235 }
2236
2237 bool IsGtBias() { return bias_ == kGtBias; }
2238
Alexey Frunze4dda3372015-06-01 18:31:49 -07002239 uint32_t GetDexPc() const { return dex_pc_; }
2240
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002241 DECLARE_INSTRUCTION(Compare);
2242
2243 private:
Calin Juravleddb7df22014-11-25 20:56:51 +00002244 const Bias bias_;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002245 const uint32_t dex_pc_;
Calin Juravleddb7df22014-11-25 20:56:51 +00002246
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002247 DISALLOW_COPY_AND_ASSIGN(HCompare);
2248};
2249
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002250// A local in the graph. Corresponds to a Dex register.
2251class HLocal : public HTemplateInstruction<0> {
2252 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002253 explicit HLocal(uint16_t reg_number)
2254 : HTemplateInstruction(SideEffects::None()), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002255
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002256 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002257
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002258 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002259
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002260 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002261 // The Dex register number.
2262 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002263
2264 DISALLOW_COPY_AND_ASSIGN(HLocal);
2265};
2266
2267// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07002268class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002269 public:
Roland Levillain5799fc02014-09-25 12:15:20 +01002270 HLoadLocal(HLocal* local, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002271 : HExpression(type, SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002272 SetRawInputAt(0, local);
2273 }
2274
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002275 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
2276
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002277 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002278
2279 private:
2280 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
2281};
2282
2283// Store a value in a given local. This instruction has two inputs: the value
2284// and the local.
2285class HStoreLocal : public HTemplateInstruction<2> {
2286 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002287 HStoreLocal(HLocal* local, HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002288 SetRawInputAt(0, local);
2289 SetRawInputAt(1, value);
2290 }
2291
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002292 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
2293
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002294 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002295
2296 private:
2297 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
2298};
2299
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002300class HConstant : public HExpression<0> {
2301 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002302 explicit HConstant(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
2303
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002304 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002305
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002306 virtual bool IsMinusOne() const { return false; }
2307 virtual bool IsZero() const { return false; }
2308 virtual bool IsOne() const { return false; }
2309
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002310 DECLARE_INSTRUCTION(Constant);
2311
2312 private:
2313 DISALLOW_COPY_AND_ASSIGN(HConstant);
2314};
2315
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002316class HFloatConstant : public HConstant {
2317 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002318 float GetValue() const { return value_; }
2319
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002320 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002321 return bit_cast<uint32_t, float>(other->AsFloatConstant()->value_) ==
2322 bit_cast<uint32_t, float>(value_);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002323 }
2324
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002325 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002326
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002327 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002328 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002329 }
2330 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002331 return value_ == 0.0f;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002332 }
2333 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002334 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
2335 }
2336 bool IsNaN() const {
2337 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002338 }
2339
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002340 DECLARE_INSTRUCTION(FloatConstant);
2341
2342 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002343 explicit HFloatConstant(float value) : HConstant(Primitive::kPrimFloat), value_(value) {}
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002344 explicit HFloatConstant(int32_t value)
2345 : HConstant(Primitive::kPrimFloat), value_(bit_cast<float, int32_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00002346
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002347 const float value_;
2348
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002349 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00002350 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002351 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002352 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2353};
2354
2355class HDoubleConstant : public HConstant {
2356 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002357 double GetValue() const { return value_; }
2358
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002359 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002360 return bit_cast<uint64_t, double>(other->AsDoubleConstant()->value_) ==
2361 bit_cast<uint64_t, double>(value_);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002362 }
2363
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002364 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002365
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002366 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002367 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002368 }
2369 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002370 return value_ == 0.0;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002371 }
2372 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002373 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
2374 }
2375 bool IsNaN() const {
2376 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002377 }
2378
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002379 DECLARE_INSTRUCTION(DoubleConstant);
2380
2381 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002382 explicit HDoubleConstant(double value) : HConstant(Primitive::kPrimDouble), value_(value) {}
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002383 explicit HDoubleConstant(int64_t value)
2384 : HConstant(Primitive::kPrimDouble), value_(bit_cast<double, int64_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00002385
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002386 const double value_;
2387
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002388 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00002389 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002390 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002391 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
2392};
2393
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002394class HNullConstant : public HConstant {
2395 public:
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002396 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2397 return true;
2398 }
2399
2400 size_t ComputeHashCode() const OVERRIDE { return 0; }
2401
2402 DECLARE_INSTRUCTION(NullConstant);
2403
2404 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002405 HNullConstant() : HConstant(Primitive::kPrimNot) {}
2406
2407 friend class HGraph;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002408 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2409};
2410
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002411// Constants of the type int. Those can be from Dex instructions, or
2412// synthesized (for example with the if-eqz instruction).
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002413class HIntConstant : public HConstant {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002414 public:
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002415 int32_t GetValue() const { return value_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002416
Calin Juravle61d544b2015-02-23 16:46:57 +00002417 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002418 return other->AsIntConstant()->value_ == value_;
2419 }
2420
Calin Juravle61d544b2015-02-23 16:46:57 +00002421 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2422
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002423 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2424 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2425 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2426
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002427 DECLARE_INSTRUCTION(IntConstant);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002428
2429 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002430 explicit HIntConstant(int32_t value) : HConstant(Primitive::kPrimInt), value_(value) {}
2431
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002432 const int32_t value_;
2433
David Brazdil8d5b8b22015-03-24 10:51:52 +00002434 friend class HGraph;
2435 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
Zheng Xuad4450e2015-04-17 18:48:56 +08002436 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002437 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2438};
2439
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002440class HLongConstant : public HConstant {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002441 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002442 int64_t GetValue() const { return value_; }
2443
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002444 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002445 return other->AsLongConstant()->value_ == value_;
2446 }
2447
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002448 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002449
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002450 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2451 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2452 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2453
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002454 DECLARE_INSTRUCTION(LongConstant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002455
2456 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002457 explicit HLongConstant(int64_t value) : HConstant(Primitive::kPrimLong), value_(value) {}
2458
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002459 const int64_t value_;
2460
David Brazdil8d5b8b22015-03-24 10:51:52 +00002461 friend class HGraph;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002462 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2463};
2464
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002465enum class Intrinsics {
2466#define OPTIMIZING_INTRINSICS(Name, IsStatic) k ## Name,
2467#include "intrinsics_list.h"
2468 kNone,
2469 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
2470#undef INTRINSICS_LIST
2471#undef OPTIMIZING_INTRINSICS
2472};
2473std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
2474
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002475class HInvoke : public HInstruction {
2476 public:
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002477 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002478
2479 // Runtime needs to walk the stack, so Dex -> Dex calls need to
2480 // know their environment.
Calin Juravle77520bc2015-01-12 18:45:46 +00002481 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002482
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01002483 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002484 SetRawInputAt(index, argument);
2485 }
2486
Roland Levillain3e3d7332015-04-28 11:00:54 +01002487 // Return the number of arguments. This number can be lower than
2488 // the number of inputs returned by InputCount(), as some invoke
2489 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
2490 // inputs at the end of their list of inputs.
2491 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
2492
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002493 Primitive::Type GetType() const OVERRIDE { return return_type_; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002494
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002495 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002496
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002497 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002498 const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002499
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002500 InvokeType GetOriginalInvokeType() const { return original_invoke_type_; }
2501
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01002502 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002503 return intrinsic_;
2504 }
2505
2506 void SetIntrinsic(Intrinsics intrinsic) {
2507 intrinsic_ = intrinsic;
2508 }
2509
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01002510 bool IsFromInlinedInvoke() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002511 return GetEnvironment()->GetParent() != nullptr;
2512 }
2513
2514 bool CanThrow() const OVERRIDE { return true; }
2515
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002516 DECLARE_INSTRUCTION(Invoke);
2517
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002518 protected:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002519 HInvoke(ArenaAllocator* arena,
2520 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01002521 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002522 Primitive::Type return_type,
2523 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002524 uint32_t dex_method_index,
2525 InvokeType original_invoke_type)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002526 : HInstruction(SideEffects::All()),
Roland Levillain3e3d7332015-04-28 11:00:54 +01002527 number_of_arguments_(number_of_arguments),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002528 inputs_(arena, number_of_arguments),
2529 return_type_(return_type),
2530 dex_pc_(dex_pc),
2531 dex_method_index_(dex_method_index),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002532 original_invoke_type_(original_invoke_type),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002533 intrinsic_(Intrinsics::kNone) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01002534 uint32_t number_of_inputs = number_of_arguments + number_of_other_inputs;
2535 inputs_.SetSize(number_of_inputs);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002536 }
2537
David Brazdil1abb4192015-02-17 18:33:36 +00002538 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
2539 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
2540 inputs_.Put(index, input);
2541 }
2542
Roland Levillain3e3d7332015-04-28 11:00:54 +01002543 uint32_t number_of_arguments_;
David Brazdil1abb4192015-02-17 18:33:36 +00002544 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002545 const Primitive::Type return_type_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002546 const uint32_t dex_pc_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002547 const uint32_t dex_method_index_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002548 const InvokeType original_invoke_type_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002549 Intrinsics intrinsic_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002550
2551 private:
2552 DISALLOW_COPY_AND_ASSIGN(HInvoke);
2553};
2554
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002555class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002556 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01002557 // Requirements of this method call regarding the class
2558 // initialization (clinit) check of its declaring class.
2559 enum class ClinitCheckRequirement {
2560 kNone, // Class already initialized.
2561 kExplicit, // Static call having explicit clinit check as last input.
2562 kImplicit, // Static call implicitly requiring a clinit check.
2563 };
2564
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002565 HInvokeStaticOrDirect(ArenaAllocator* arena,
2566 uint32_t number_of_arguments,
2567 Primitive::Type return_type,
2568 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002569 uint32_t dex_method_index,
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002570 bool is_recursive,
Jeff Hao848f70a2014-01-15 13:49:50 -08002571 int32_t string_init_offset,
Nicolas Geoffray79041292015-03-26 10:05:54 +00002572 InvokeType original_invoke_type,
Roland Levillain4c0eb422015-04-24 16:43:49 +01002573 InvokeType invoke_type,
2574 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01002575 : HInvoke(arena,
2576 number_of_arguments,
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002577 // There is one extra argument for the HCurrentMethod node, and
2578 // potentially one other if the clinit check is explicit.
2579 clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 2u : 1u,
Roland Levillain3e3d7332015-04-28 11:00:54 +01002580 return_type,
2581 dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002582 dex_method_index,
2583 original_invoke_type),
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002584 invoke_type_(invoke_type),
Roland Levillain4c0eb422015-04-24 16:43:49 +01002585 is_recursive_(is_recursive),
Jeff Hao848f70a2014-01-15 13:49:50 -08002586 clinit_check_requirement_(clinit_check_requirement),
2587 string_init_offset_(string_init_offset) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002588
Calin Juravle641547a2015-04-21 22:08:51 +01002589 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
2590 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00002591 // We access the method via the dex cache so we can't do an implicit null check.
2592 // TODO: for intrinsics we can generate implicit null checks.
2593 return false;
2594 }
2595
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002596 InvokeType GetInvokeType() const { return invoke_type_; }
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002597 bool IsRecursive() const { return is_recursive_; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002598 bool NeedsDexCache() const OVERRIDE { return !IsRecursive(); }
Jeff Hao848f70a2014-01-15 13:49:50 -08002599 bool IsStringInit() const { return string_init_offset_ != 0; }
2600 int32_t GetStringInitOffset() const { return string_init_offset_; }
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002601 uint32_t GetCurrentMethodInputIndex() const { return GetNumberOfArguments(); }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002602
Roland Levillain4c0eb422015-04-24 16:43:49 +01002603 // Is this instruction a call to a static method?
2604 bool IsStatic() const {
2605 return GetInvokeType() == kStatic;
2606 }
2607
Roland Levillain3e3d7332015-04-28 11:00:54 +01002608 // Remove the art::HLoadClass instruction set as last input by
2609 // art::PrepareForRegisterAllocation::VisitClinitCheck in lieu of
2610 // the initial art::HClinitCheck instruction (only relevant for
2611 // static calls with explicit clinit check).
2612 void RemoveLoadClassAsLastInput() {
Roland Levillain4c0eb422015-04-24 16:43:49 +01002613 DCHECK(IsStaticWithExplicitClinitCheck());
2614 size_t last_input_index = InputCount() - 1;
2615 HInstruction* last_input = InputAt(last_input_index);
2616 DCHECK(last_input != nullptr);
Roland Levillain3e3d7332015-04-28 11:00:54 +01002617 DCHECK(last_input->IsLoadClass()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01002618 RemoveAsUserOfInput(last_input_index);
2619 inputs_.DeleteAt(last_input_index);
2620 clinit_check_requirement_ = ClinitCheckRequirement::kImplicit;
2621 DCHECK(IsStaticWithImplicitClinitCheck());
2622 }
2623
2624 // Is this a call to a static method whose declaring class has an
2625 // explicit intialization check in the graph?
2626 bool IsStaticWithExplicitClinitCheck() const {
2627 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kExplicit);
2628 }
2629
2630 // Is this a call to a static method whose declaring class has an
2631 // implicit intialization check requirement?
2632 bool IsStaticWithImplicitClinitCheck() const {
2633 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kImplicit);
2634 }
2635
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002636 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002637
Roland Levillain4c0eb422015-04-24 16:43:49 +01002638 protected:
2639 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
2640 const HUserRecord<HInstruction*> input_record = HInvoke::InputRecordAt(i);
2641 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck() && (i == InputCount() - 1)) {
2642 HInstruction* input = input_record.GetInstruction();
2643 // `input` is the last input of a static invoke marked as having
2644 // an explicit clinit check. It must either be:
2645 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
2646 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
2647 DCHECK(input != nullptr);
2648 DCHECK(input->IsClinitCheck() || input->IsLoadClass()) << input->DebugName();
2649 }
2650 return input_record;
2651 }
2652
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002653 private:
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002654 const InvokeType invoke_type_;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002655 const bool is_recursive_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01002656 ClinitCheckRequirement clinit_check_requirement_;
Jeff Hao848f70a2014-01-15 13:49:50 -08002657 // Thread entrypoint offset for string init method if this is a string init invoke.
2658 // Note that there are multiple string init methods, each having its own offset.
2659 int32_t string_init_offset_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002660
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002661 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002662};
2663
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002664class HInvokeVirtual : public HInvoke {
2665 public:
2666 HInvokeVirtual(ArenaAllocator* arena,
2667 uint32_t number_of_arguments,
2668 Primitive::Type return_type,
2669 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002670 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002671 uint32_t vtable_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002672 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002673 vtable_index_(vtable_index) {}
2674
Calin Juravle641547a2015-04-21 22:08:51 +01002675 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00002676 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01002677 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00002678 }
2679
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002680 uint32_t GetVTableIndex() const { return vtable_index_; }
2681
2682 DECLARE_INSTRUCTION(InvokeVirtual);
2683
2684 private:
2685 const uint32_t vtable_index_;
2686
2687 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
2688};
2689
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002690class HInvokeInterface : public HInvoke {
2691 public:
2692 HInvokeInterface(ArenaAllocator* arena,
2693 uint32_t number_of_arguments,
2694 Primitive::Type return_type,
2695 uint32_t dex_pc,
2696 uint32_t dex_method_index,
2697 uint32_t imt_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002698 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002699 imt_index_(imt_index) {}
2700
Calin Juravle641547a2015-04-21 22:08:51 +01002701 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00002702 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01002703 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00002704 }
2705
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002706 uint32_t GetImtIndex() const { return imt_index_; }
2707 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
2708
2709 DECLARE_INSTRUCTION(InvokeInterface);
2710
2711 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002712 const uint32_t imt_index_;
2713
2714 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
2715};
2716
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01002717class HNewInstance : public HExpression<1> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002718 public:
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01002719 HNewInstance(HCurrentMethod* current_method,
2720 uint32_t dex_pc,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002721 uint16_t type_index,
2722 const DexFile& dex_file,
2723 QuickEntrypointEnum entrypoint)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002724 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2725 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002726 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002727 dex_file_(dex_file),
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01002728 entrypoint_(entrypoint) {
2729 SetRawInputAt(0, current_method);
2730 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002731
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002732 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002733 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002734 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002735
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002736 // Calls runtime so needs an environment.
Calin Juravle92a6ed22014-12-02 18:58:03 +00002737 bool NeedsEnvironment() const OVERRIDE { return true; }
2738 // It may throw when called on:
2739 // - interfaces
2740 // - abstract/innaccessible/unknown classes
2741 // TODO: optimize when possible.
2742 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002743
Calin Juravle10e244f2015-01-26 18:54:32 +00002744 bool CanBeNull() const OVERRIDE { return false; }
2745
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002746 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
2747
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002748 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002749
2750 private:
2751 const uint32_t dex_pc_;
2752 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002753 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002754 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002755
2756 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
2757};
2758
Roland Levillain88cb1752014-10-20 16:36:47 +01002759class HNeg : public HUnaryOperation {
2760 public:
2761 explicit HNeg(Primitive::Type result_type, HInstruction* input)
2762 : HUnaryOperation(result_type, input) {}
2763
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002764 int32_t Evaluate(int32_t x) const OVERRIDE { return -x; }
2765 int64_t Evaluate(int64_t x) const OVERRIDE { return -x; }
Roland Levillain9240d6a2014-10-20 16:47:04 +01002766
Roland Levillain88cb1752014-10-20 16:36:47 +01002767 DECLARE_INSTRUCTION(Neg);
2768
2769 private:
2770 DISALLOW_COPY_AND_ASSIGN(HNeg);
2771};
2772
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01002773class HNewArray : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002774 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002775 HNewArray(HInstruction* length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01002776 HCurrentMethod* current_method,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002777 uint32_t dex_pc,
2778 uint16_t type_index,
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01002779 const DexFile& dex_file,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002780 QuickEntrypointEnum entrypoint)
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002781 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2782 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002783 type_index_(type_index),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01002784 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002785 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002786 SetRawInputAt(0, length);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01002787 SetRawInputAt(1, current_method);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002788 }
2789
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002790 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002791 uint16_t GetTypeIndex() const { return type_index_; }
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01002792 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002793
2794 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00002795 bool NeedsEnvironment() const OVERRIDE { return true; }
2796
Mingyao Yang0c365e62015-03-31 15:09:29 -07002797 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
2798 bool CanThrow() const OVERRIDE { return true; }
2799
Calin Juravle10e244f2015-01-26 18:54:32 +00002800 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002801
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002802 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
2803
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002804 DECLARE_INSTRUCTION(NewArray);
2805
2806 private:
2807 const uint32_t dex_pc_;
2808 const uint16_t type_index_;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01002809 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002810 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002811
2812 DISALLOW_COPY_AND_ASSIGN(HNewArray);
2813};
2814
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002815class HAdd : public HBinaryOperation {
2816 public:
2817 HAdd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2818 : HBinaryOperation(result_type, left, right) {}
2819
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002820 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002821
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002822 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002823 return x + y;
2824 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002825 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002826 return x + y;
2827 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002828
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002829 DECLARE_INSTRUCTION(Add);
2830
2831 private:
2832 DISALLOW_COPY_AND_ASSIGN(HAdd);
2833};
2834
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002835class HSub : public HBinaryOperation {
2836 public:
2837 HSub(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2838 : HBinaryOperation(result_type, left, right) {}
2839
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002840 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002841 return x - y;
2842 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002843 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002844 return x - y;
2845 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002846
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002847 DECLARE_INSTRUCTION(Sub);
2848
2849 private:
2850 DISALLOW_COPY_AND_ASSIGN(HSub);
2851};
2852
Calin Juravle34bacdf2014-10-07 20:23:36 +01002853class HMul : public HBinaryOperation {
2854 public:
2855 HMul(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2856 : HBinaryOperation(result_type, left, right) {}
2857
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002858 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002859
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002860 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x * y; }
2861 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x * y; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002862
2863 DECLARE_INSTRUCTION(Mul);
2864
2865 private:
2866 DISALLOW_COPY_AND_ASSIGN(HMul);
2867};
2868
Calin Juravle7c4954d2014-10-28 16:57:40 +00002869class HDiv : public HBinaryOperation {
2870 public:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002871 HDiv(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
2872 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00002873
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002874 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00002875 // Our graph structure ensures we never have 0 for `y` during constant folding.
2876 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00002877 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00002878 return (y == -1) ? -x : x / y;
2879 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002880
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002881 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002882 DCHECK_NE(y, 0);
2883 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2884 return (y == -1) ? -x : x / y;
2885 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002886
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002887 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002888
Calin Juravle7c4954d2014-10-28 16:57:40 +00002889 DECLARE_INSTRUCTION(Div);
2890
2891 private:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002892 const uint32_t dex_pc_;
2893
Calin Juravle7c4954d2014-10-28 16:57:40 +00002894 DISALLOW_COPY_AND_ASSIGN(HDiv);
2895};
2896
Calin Juravlebacfec32014-11-14 15:54:36 +00002897class HRem : public HBinaryOperation {
2898 public:
2899 HRem(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
2900 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
2901
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002902 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002903 DCHECK_NE(y, 0);
2904 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2905 return (y == -1) ? 0 : x % y;
2906 }
2907
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002908 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002909 DCHECK_NE(y, 0);
2910 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2911 return (y == -1) ? 0 : x % y;
2912 }
2913
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002914 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Calin Juravlebacfec32014-11-14 15:54:36 +00002915
2916 DECLARE_INSTRUCTION(Rem);
2917
2918 private:
2919 const uint32_t dex_pc_;
2920
2921 DISALLOW_COPY_AND_ASSIGN(HRem);
2922};
2923
Calin Juravled0d48522014-11-04 16:40:20 +00002924class HDivZeroCheck : public HExpression<1> {
2925 public:
2926 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
2927 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
2928 SetRawInputAt(0, value);
2929 }
2930
2931 bool CanBeMoved() const OVERRIDE { return true; }
2932
2933 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2934 UNUSED(other);
2935 return true;
2936 }
2937
2938 bool NeedsEnvironment() const OVERRIDE { return true; }
2939 bool CanThrow() const OVERRIDE { return true; }
2940
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002941 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Calin Juravled0d48522014-11-04 16:40:20 +00002942
2943 DECLARE_INSTRUCTION(DivZeroCheck);
2944
2945 private:
2946 const uint32_t dex_pc_;
2947
2948 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
2949};
2950
Calin Juravle9aec02f2014-11-18 23:06:35 +00002951class HShl : public HBinaryOperation {
2952 public:
2953 HShl(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2954 : HBinaryOperation(result_type, left, right) {}
2955
2956 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x << (y & kMaxIntShiftValue); }
2957 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x << (y & kMaxLongShiftValue); }
2958
2959 DECLARE_INSTRUCTION(Shl);
2960
2961 private:
2962 DISALLOW_COPY_AND_ASSIGN(HShl);
2963};
2964
2965class HShr : public HBinaryOperation {
2966 public:
2967 HShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2968 : HBinaryOperation(result_type, left, right) {}
2969
2970 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x >> (y & kMaxIntShiftValue); }
2971 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x >> (y & kMaxLongShiftValue); }
2972
2973 DECLARE_INSTRUCTION(Shr);
2974
2975 private:
2976 DISALLOW_COPY_AND_ASSIGN(HShr);
2977};
2978
2979class HUShr : public HBinaryOperation {
2980 public:
2981 HUShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2982 : HBinaryOperation(result_type, left, right) {}
2983
2984 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
2985 uint32_t ux = static_cast<uint32_t>(x);
2986 uint32_t uy = static_cast<uint32_t>(y) & kMaxIntShiftValue;
2987 return static_cast<int32_t>(ux >> uy);
2988 }
2989
2990 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
2991 uint64_t ux = static_cast<uint64_t>(x);
2992 uint64_t uy = static_cast<uint64_t>(y) & kMaxLongShiftValue;
2993 return static_cast<int64_t>(ux >> uy);
2994 }
2995
2996 DECLARE_INSTRUCTION(UShr);
2997
2998 private:
2999 DISALLOW_COPY_AND_ASSIGN(HUShr);
3000};
3001
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003002class HAnd : public HBinaryOperation {
3003 public:
3004 HAnd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3005 : HBinaryOperation(result_type, left, right) {}
3006
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003007 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003008
3009 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x & y; }
3010 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x & y; }
3011
3012 DECLARE_INSTRUCTION(And);
3013
3014 private:
3015 DISALLOW_COPY_AND_ASSIGN(HAnd);
3016};
3017
3018class HOr : public HBinaryOperation {
3019 public:
3020 HOr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3021 : HBinaryOperation(result_type, left, right) {}
3022
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003023 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003024
3025 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x | y; }
3026 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x | y; }
3027
3028 DECLARE_INSTRUCTION(Or);
3029
3030 private:
3031 DISALLOW_COPY_AND_ASSIGN(HOr);
3032};
3033
3034class HXor : public HBinaryOperation {
3035 public:
3036 HXor(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3037 : HBinaryOperation(result_type, left, right) {}
3038
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003039 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003040
3041 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x ^ y; }
3042 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x ^ y; }
3043
3044 DECLARE_INSTRUCTION(Xor);
3045
3046 private:
3047 DISALLOW_COPY_AND_ASSIGN(HXor);
3048};
3049
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003050// The value of a parameter in this method. Its location depends on
3051// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07003052class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003053 public:
Calin Juravle10e244f2015-01-26 18:54:32 +00003054 HParameterValue(uint8_t index, Primitive::Type parameter_type, bool is_this = false)
3055 : HExpression(parameter_type, SideEffects::None()), index_(index), is_this_(is_this) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003056
3057 uint8_t GetIndex() const { return index_; }
3058
Calin Juravle10e244f2015-01-26 18:54:32 +00003059 bool CanBeNull() const OVERRIDE { return !is_this_; }
3060
Calin Juravle3cd4fc82015-05-14 15:15:42 +01003061 bool IsThis() const { return is_this_; }
3062
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003063 DECLARE_INSTRUCTION(ParameterValue);
3064
3065 private:
3066 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00003067 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003068 const uint8_t index_;
3069
Calin Juravle10e244f2015-01-26 18:54:32 +00003070 // Whether or not the parameter value corresponds to 'this' argument.
3071 const bool is_this_;
3072
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003073 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
3074};
3075
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003076class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003077 public:
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003078 explicit HNot(Primitive::Type result_type, HInstruction* input)
3079 : HUnaryOperation(result_type, input) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003080
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003081 bool CanBeMoved() const OVERRIDE { return true; }
3082 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003083 UNUSED(other);
3084 return true;
3085 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003086
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003087 int32_t Evaluate(int32_t x) const OVERRIDE { return ~x; }
3088 int64_t Evaluate(int64_t x) const OVERRIDE { return ~x; }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003089
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003090 DECLARE_INSTRUCTION(Not);
3091
3092 private:
3093 DISALLOW_COPY_AND_ASSIGN(HNot);
3094};
3095
David Brazdil66d126e2015-04-03 16:02:44 +01003096class HBooleanNot : public HUnaryOperation {
3097 public:
3098 explicit HBooleanNot(HInstruction* input)
3099 : HUnaryOperation(Primitive::Type::kPrimBoolean, input) {}
3100
3101 bool CanBeMoved() const OVERRIDE { return true; }
3102 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3103 UNUSED(other);
3104 return true;
3105 }
3106
3107 int32_t Evaluate(int32_t x) const OVERRIDE {
3108 DCHECK(IsUint<1>(x));
3109 return !x;
3110 }
3111
3112 int64_t Evaluate(int64_t x ATTRIBUTE_UNUSED) const OVERRIDE {
3113 LOG(FATAL) << DebugName() << " cannot be used with 64-bit values";
3114 UNREACHABLE();
3115 }
3116
3117 DECLARE_INSTRUCTION(BooleanNot);
3118
3119 private:
3120 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
3121};
3122
Roland Levillaindff1f282014-11-05 14:15:05 +00003123class HTypeConversion : public HExpression<1> {
3124 public:
3125 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00003126 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
3127 : HExpression(result_type, SideEffects::None()), dex_pc_(dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00003128 SetRawInputAt(0, input);
3129 DCHECK_NE(input->GetType(), result_type);
3130 }
3131
3132 HInstruction* GetInput() const { return InputAt(0); }
3133 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
3134 Primitive::Type GetResultType() const { return GetType(); }
3135
Roland Levillain624279f2014-12-04 11:54:28 +00003136 // Required by the x86 and ARM code generators when producing calls
3137 // to the runtime.
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003138 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Roland Levillain624279f2014-12-04 11:54:28 +00003139
Roland Levillaindff1f282014-11-05 14:15:05 +00003140 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00003141 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00003142
Mark Mendelle82549b2015-05-06 10:55:34 -04003143 // Try to statically evaluate the conversion and return a HConstant
3144 // containing the result. If the input cannot be converted, return nullptr.
3145 HConstant* TryStaticEvaluation() const;
3146
Roland Levillaindff1f282014-11-05 14:15:05 +00003147 DECLARE_INSTRUCTION(TypeConversion);
3148
3149 private:
Roland Levillain624279f2014-12-04 11:54:28 +00003150 const uint32_t dex_pc_;
3151
Roland Levillaindff1f282014-11-05 14:15:05 +00003152 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
3153};
3154
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00003155static constexpr uint32_t kNoRegNumber = -1;
3156
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003157class HPhi : public HInstruction {
3158 public:
3159 HPhi(ArenaAllocator* arena, uint32_t reg_number, size_t number_of_inputs, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003160 : HInstruction(SideEffects::None()),
3161 inputs_(arena, number_of_inputs),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003162 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01003163 type_(type),
Calin Juravle10e244f2015-01-26 18:54:32 +00003164 is_live_(false),
3165 can_be_null_(true) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003166 inputs_.SetSize(number_of_inputs);
3167 }
3168
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00003169 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
3170 static Primitive::Type ToPhiType(Primitive::Type type) {
3171 switch (type) {
3172 case Primitive::kPrimBoolean:
3173 case Primitive::kPrimByte:
3174 case Primitive::kPrimShort:
3175 case Primitive::kPrimChar:
3176 return Primitive::kPrimInt;
3177 default:
3178 return type;
3179 }
3180 }
3181
Calin Juravle10e244f2015-01-26 18:54:32 +00003182 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003183
3184 void AddInput(HInstruction* input);
David Brazdil2d7352b2015-04-20 14:52:42 +01003185 void RemoveInputAt(size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003186
Calin Juravle10e244f2015-01-26 18:54:32 +00003187 Primitive::Type GetType() const OVERRIDE { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003188 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003189
Calin Juravle10e244f2015-01-26 18:54:32 +00003190 bool CanBeNull() const OVERRIDE { return can_be_null_; }
3191 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
3192
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003193 uint32_t GetRegNumber() const { return reg_number_; }
3194
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01003195 void SetDead() { is_live_ = false; }
3196 void SetLive() { is_live_ = true; }
3197 bool IsDead() const { return !is_live_; }
3198 bool IsLive() const { return is_live_; }
3199
Calin Juravlea4f88312015-04-16 12:57:19 +01003200 // Returns the next equivalent phi (starting from the current one) or null if there is none.
3201 // An equivalent phi is a phi having the same dex register and type.
3202 // It assumes that phis with the same dex register are adjacent.
3203 HPhi* GetNextEquivalentPhiWithSameType() {
3204 HInstruction* next = GetNext();
3205 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
3206 if (next->GetType() == GetType()) {
3207 return next->AsPhi();
3208 }
3209 next = next->GetNext();
3210 }
3211 return nullptr;
3212 }
3213
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003214 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003215
David Brazdil1abb4192015-02-17 18:33:36 +00003216 protected:
3217 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
3218
3219 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
3220 inputs_.Put(index, input);
3221 }
3222
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003223 private:
David Brazdil1abb4192015-02-17 18:33:36 +00003224 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003225 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003226 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01003227 bool is_live_;
Calin Juravle10e244f2015-01-26 18:54:32 +00003228 bool can_be_null_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003229
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003230 DISALLOW_COPY_AND_ASSIGN(HPhi);
3231};
3232
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003233class HNullCheck : public HExpression<1> {
3234 public:
3235 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003236 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003237 SetRawInputAt(0, value);
3238 }
3239
Calin Juravle10e244f2015-01-26 18:54:32 +00003240 bool CanBeMoved() const OVERRIDE { return true; }
3241 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003242 UNUSED(other);
3243 return true;
3244 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003245
Calin Juravle10e244f2015-01-26 18:54:32 +00003246 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003247
Calin Juravle10e244f2015-01-26 18:54:32 +00003248 bool CanThrow() const OVERRIDE { return true; }
3249
3250 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01003251
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003252 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003253
3254 DECLARE_INSTRUCTION(NullCheck);
3255
3256 private:
3257 const uint32_t dex_pc_;
3258
3259 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
3260};
3261
3262class FieldInfo : public ValueObject {
3263 public:
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003264 FieldInfo(MemberOffset field_offset,
3265 Primitive::Type field_type,
3266 bool is_volatile,
3267 uint32_t index,
3268 const DexFile& dex_file)
3269 : field_offset_(field_offset),
3270 field_type_(field_type),
3271 is_volatile_(is_volatile),
3272 index_(index),
3273 dex_file_(dex_file) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003274
3275 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003276 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003277 uint32_t GetFieldIndex() const { return index_; }
3278 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00003279 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003280
3281 private:
3282 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01003283 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00003284 const bool is_volatile_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003285 uint32_t index_;
3286 const DexFile& dex_file_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003287};
3288
3289class HInstanceFieldGet : public HExpression<1> {
3290 public:
3291 HInstanceFieldGet(HInstruction* value,
3292 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003293 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003294 bool is_volatile,
3295 uint32_t field_idx,
3296 const DexFile& dex_file)
Nicolas Geoffray2af23072015-04-30 11:15:40 +00003297 : HExpression(field_type, SideEffects::DependsOnSomething()),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003298 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003299 SetRawInputAt(0, value);
3300 }
3301
Calin Juravle10c9cbe2014-12-19 10:50:19 +00003302 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003303
3304 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3305 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
3306 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003307 }
3308
Calin Juravle641547a2015-04-21 22:08:51 +01003309 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3310 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00003311 }
3312
3313 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01003314 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
3315 }
3316
Calin Juravle52c48962014-12-16 17:02:57 +00003317 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003318 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003319 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003320 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003321
3322 DECLARE_INSTRUCTION(InstanceFieldGet);
3323
3324 private:
3325 const FieldInfo field_info_;
3326
3327 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
3328};
3329
3330class HInstanceFieldSet : public HTemplateInstruction<2> {
3331 public:
3332 HInstanceFieldSet(HInstruction* object,
3333 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01003334 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003335 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003336 bool is_volatile,
3337 uint32_t field_idx,
3338 const DexFile& dex_file)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003339 : HTemplateInstruction(SideEffects::ChangesSomething()),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003340 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003341 value_can_be_null_(true) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003342 SetRawInputAt(0, object);
3343 SetRawInputAt(1, value);
3344 }
3345
Calin Juravle641547a2015-04-21 22:08:51 +01003346 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3347 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00003348 }
3349
Calin Juravle52c48962014-12-16 17:02:57 +00003350 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003351 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003352 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003353 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003354 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003355 bool GetValueCanBeNull() const { return value_can_be_null_; }
3356 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003357
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003358 DECLARE_INSTRUCTION(InstanceFieldSet);
3359
3360 private:
3361 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003362 bool value_can_be_null_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003363
3364 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
3365};
3366
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003367class HArrayGet : public HExpression<2> {
3368 public:
3369 HArrayGet(HInstruction* array, HInstruction* index, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003370 : HExpression(type, SideEffects::DependsOnSomething()) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003371 SetRawInputAt(0, array);
3372 SetRawInputAt(1, index);
3373 }
3374
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003375 bool CanBeMoved() const OVERRIDE { return true; }
3376 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003377 UNUSED(other);
3378 return true;
3379 }
Calin Juravle641547a2015-04-21 22:08:51 +01003380 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3381 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00003382 // TODO: We can be smarter here.
3383 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
3384 // which generates the implicit null check. There are cases when these can be removed
3385 // to produce better code. If we ever add optimizations to do so we should allow an
3386 // implicit check here (as long as the address falls in the first page).
3387 return false;
3388 }
3389
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003390 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003391
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003392 HInstruction* GetArray() const { return InputAt(0); }
3393 HInstruction* GetIndex() const { return InputAt(1); }
3394
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003395 DECLARE_INSTRUCTION(ArrayGet);
3396
3397 private:
3398 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
3399};
3400
3401class HArraySet : public HTemplateInstruction<3> {
3402 public:
3403 HArraySet(HInstruction* array,
3404 HInstruction* index,
3405 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003406 Primitive::Type expected_component_type,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003407 uint32_t dex_pc)
3408 : HTemplateInstruction(SideEffects::ChangesSomething()),
3409 dex_pc_(dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003410 expected_component_type_(expected_component_type),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003411 needs_type_check_(value->GetType() == Primitive::kPrimNot),
3412 value_can_be_null_(true) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003413 SetRawInputAt(0, array);
3414 SetRawInputAt(1, index);
3415 SetRawInputAt(2, value);
3416 }
3417
Calin Juravle77520bc2015-01-12 18:45:46 +00003418 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003419 // We currently always call a runtime method to catch array store
3420 // exceptions.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003421 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003422 }
3423
Mingyao Yang81014cb2015-06-02 03:16:27 -07003424 // Can throw ArrayStoreException.
3425 bool CanThrow() const OVERRIDE { return needs_type_check_; }
3426
Calin Juravle641547a2015-04-21 22:08:51 +01003427 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3428 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00003429 // TODO: Same as for ArrayGet.
3430 return false;
3431 }
3432
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003433 void ClearNeedsTypeCheck() {
3434 needs_type_check_ = false;
3435 }
3436
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003437 void ClearValueCanBeNull() {
3438 value_can_be_null_ = false;
3439 }
3440
3441 bool GetValueCanBeNull() const { return value_can_be_null_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003442 bool NeedsTypeCheck() const { return needs_type_check_; }
3443
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003444 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003445
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003446 HInstruction* GetArray() const { return InputAt(0); }
3447 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003448 HInstruction* GetValue() const { return InputAt(2); }
3449
3450 Primitive::Type GetComponentType() const {
3451 // The Dex format does not type floating point index operations. Since the
3452 // `expected_component_type_` is set during building and can therefore not
3453 // be correct, we also check what is the value type. If it is a floating
3454 // point type, we must use that type.
3455 Primitive::Type value_type = GetValue()->GetType();
3456 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
3457 ? value_type
3458 : expected_component_type_;
3459 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003460
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003461 DECLARE_INSTRUCTION(ArraySet);
3462
3463 private:
3464 const uint32_t dex_pc_;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003465 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003466 bool needs_type_check_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003467 bool value_can_be_null_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003468
3469 DISALLOW_COPY_AND_ASSIGN(HArraySet);
3470};
3471
3472class HArrayLength : public HExpression<1> {
3473 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003474 explicit HArrayLength(HInstruction* array)
3475 : HExpression(Primitive::kPrimInt, SideEffects::None()) {
3476 // Note that arrays do not change length, so the instruction does not
3477 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003478 SetRawInputAt(0, array);
3479 }
3480
Calin Juravle77520bc2015-01-12 18:45:46 +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 return obj == InputAt(0);
3488 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003489
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003490 DECLARE_INSTRUCTION(ArrayLength);
3491
3492 private:
3493 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
3494};
3495
3496class HBoundsCheck : public HExpression<2> {
3497 public:
3498 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003499 : HExpression(index->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003500 DCHECK(index->GetType() == Primitive::kPrimInt);
3501 SetRawInputAt(0, index);
3502 SetRawInputAt(1, length);
3503 }
3504
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003505 bool CanBeMoved() const OVERRIDE { return true; }
3506 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003507 UNUSED(other);
3508 return true;
3509 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003510
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003511 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003512
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003513 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01003514
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003515 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003516
3517 DECLARE_INSTRUCTION(BoundsCheck);
3518
3519 private:
3520 const uint32_t dex_pc_;
3521
3522 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
3523};
3524
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003525/**
3526 * Some DEX instructions are folded into multiple HInstructions that need
3527 * to stay live until the last HInstruction. This class
3528 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00003529 * HInstruction stays live. `index` represents the stack location index of the
3530 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003531 */
3532class HTemporary : public HTemplateInstruction<0> {
3533 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003534 explicit HTemporary(size_t index) : HTemplateInstruction(SideEffects::None()), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003535
3536 size_t GetIndex() const { return index_; }
3537
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00003538 Primitive::Type GetType() const OVERRIDE {
3539 // The previous instruction is the one that will be stored in the temporary location.
3540 DCHECK(GetPrevious() != nullptr);
3541 return GetPrevious()->GetType();
3542 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00003543
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003544 DECLARE_INSTRUCTION(Temporary);
3545
3546 private:
3547 const size_t index_;
3548
3549 DISALLOW_COPY_AND_ASSIGN(HTemporary);
3550};
3551
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003552class HSuspendCheck : public HTemplateInstruction<0> {
3553 public:
3554 explicit HSuspendCheck(uint32_t dex_pc)
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01003555 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003556
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003557 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003558 return true;
3559 }
3560
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003561 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01003562 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
3563 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003564
3565 DECLARE_INSTRUCTION(SuspendCheck);
3566
3567 private:
3568 const uint32_t dex_pc_;
3569
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01003570 // Only used for code generation, in order to share the same slow path between back edges
3571 // of a same loop.
3572 SlowPathCode* slow_path_;
3573
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003574 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
3575};
3576
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003577/**
3578 * Instruction to load a Class object.
3579 */
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003580class HLoadClass : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003581 public:
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003582 HLoadClass(HCurrentMethod* current_method,
3583 uint16_t type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003584 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003585 bool is_referrers_class,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003586 uint32_t dex_pc)
3587 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3588 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003589 dex_file_(dex_file),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003590 is_referrers_class_(is_referrers_class),
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003591 dex_pc_(dex_pc),
Calin Juravleb1498f62015-02-16 13:13:29 +00003592 generate_clinit_check_(false),
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003593 loaded_class_rti_(ReferenceTypeInfo::CreateTop(/* is_exact */ false)) {
3594 SetRawInputAt(0, current_method);
3595 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003596
3597 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003598
3599 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3600 return other->AsLoadClass()->type_index_ == type_index_;
3601 }
3602
3603 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
3604
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003605 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003606 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003607 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003608
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003609 bool NeedsEnvironment() const OVERRIDE {
3610 // Will call runtime and load the class if the class is not loaded yet.
3611 // TODO: finer grain decision.
3612 return !is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003613 }
3614
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003615 bool MustGenerateClinitCheck() const {
3616 return generate_clinit_check_;
3617 }
3618
Calin Juravle0ba218d2015-05-19 18:46:01 +01003619 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
3620 generate_clinit_check_ = generate_clinit_check;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003621 }
3622
3623 bool CanCallRuntime() const {
3624 return MustGenerateClinitCheck() || !is_referrers_class_;
3625 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003626
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003627 bool CanThrow() const OVERRIDE {
3628 // May call runtime and and therefore can throw.
3629 // TODO: finer grain decision.
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01003630 return CanCallRuntime();
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003631 }
3632
Calin Juravleacf735c2015-02-12 15:25:22 +00003633 ReferenceTypeInfo GetLoadedClassRTI() {
3634 return loaded_class_rti_;
3635 }
3636
3637 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
3638 // Make sure we only set exact types (the loaded class should never be merged).
3639 DCHECK(rti.IsExact());
3640 loaded_class_rti_ = rti;
3641 }
3642
3643 bool IsResolved() {
3644 return loaded_class_rti_.IsExact();
3645 }
3646
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003647 const DexFile& GetDexFile() { return dex_file_; }
3648
Nicolas Geoffray9437b782015-03-25 10:08:51 +00003649 bool NeedsDexCache() const OVERRIDE { return !is_referrers_class_; }
3650
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003651 DECLARE_INSTRUCTION(LoadClass);
3652
3653 private:
3654 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003655 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003656 const bool is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003657 const uint32_t dex_pc_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003658 // Whether this instruction must generate the initialization check.
3659 // Used for code generation.
3660 bool generate_clinit_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003661
Calin Juravleacf735c2015-02-12 15:25:22 +00003662 ReferenceTypeInfo loaded_class_rti_;
3663
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003664 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
3665};
3666
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01003667class HLoadString : public HExpression<1> {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003668 public:
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01003669 HLoadString(HCurrentMethod* current_method, uint32_t string_index, uint32_t dex_pc)
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003670 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3671 string_index_(string_index),
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01003672 dex_pc_(dex_pc) {
3673 SetRawInputAt(0, current_method);
3674 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003675
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003676 bool CanBeMoved() const OVERRIDE { return true; }
3677
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003678 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3679 return other->AsLoadString()->string_index_ == string_index_;
3680 }
3681
3682 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
3683
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003684 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003685 uint32_t GetStringIndex() const { return string_index_; }
3686
3687 // TODO: Can we deopt or debug when we resolve a string?
3688 bool NeedsEnvironment() const OVERRIDE { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00003689 bool NeedsDexCache() const OVERRIDE { return true; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003690
3691 DECLARE_INSTRUCTION(LoadString);
3692
3693 private:
3694 const uint32_t string_index_;
3695 const uint32_t dex_pc_;
3696
3697 DISALLOW_COPY_AND_ASSIGN(HLoadString);
3698};
3699
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003700/**
3701 * Performs an initialization check on its Class object input.
3702 */
3703class HClinitCheck : public HExpression<1> {
3704 public:
3705 explicit HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Nicolas Geoffraya0466e12015-03-27 15:00:40 +00003706 : HExpression(Primitive::kPrimNot, SideEffects::ChangesSomething()),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003707 dex_pc_(dex_pc) {
3708 SetRawInputAt(0, constant);
3709 }
3710
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003711 bool CanBeMoved() const OVERRIDE { return true; }
3712 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3713 UNUSED(other);
3714 return true;
3715 }
3716
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003717 bool NeedsEnvironment() const OVERRIDE {
3718 // May call runtime to initialize the class.
3719 return true;
3720 }
3721
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003722 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003723
3724 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
3725
3726 DECLARE_INSTRUCTION(ClinitCheck);
3727
3728 private:
3729 const uint32_t dex_pc_;
3730
3731 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
3732};
3733
3734class HStaticFieldGet : public HExpression<1> {
3735 public:
3736 HStaticFieldGet(HInstruction* cls,
3737 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003738 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003739 bool is_volatile,
3740 uint32_t field_idx,
3741 const DexFile& dex_file)
Nicolas Geoffray2af23072015-04-30 11:15:40 +00003742 : HExpression(field_type, SideEffects::DependsOnSomething()),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003743 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003744 SetRawInputAt(0, cls);
3745 }
3746
Calin Juravle52c48962014-12-16 17:02:57 +00003747
Calin Juravle10c9cbe2014-12-19 10:50:19 +00003748 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003749
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003750 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00003751 HStaticFieldGet* other_get = other->AsStaticFieldGet();
3752 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003753 }
3754
3755 size_t ComputeHashCode() const OVERRIDE {
3756 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
3757 }
3758
Calin Juravle52c48962014-12-16 17:02:57 +00003759 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003760 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
3761 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003762 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003763
3764 DECLARE_INSTRUCTION(StaticFieldGet);
3765
3766 private:
3767 const FieldInfo field_info_;
3768
3769 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
3770};
3771
3772class HStaticFieldSet : public HTemplateInstruction<2> {
3773 public:
3774 HStaticFieldSet(HInstruction* cls,
3775 HInstruction* value,
3776 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003777 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003778 bool is_volatile,
3779 uint32_t field_idx,
3780 const DexFile& dex_file)
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003781 : HTemplateInstruction(SideEffects::ChangesSomething()),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003782 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003783 value_can_be_null_(true) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003784 SetRawInputAt(0, cls);
3785 SetRawInputAt(1, value);
3786 }
3787
Calin Juravle52c48962014-12-16 17:02:57 +00003788 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003789 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
3790 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003791 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003792
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003793 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003794 bool GetValueCanBeNull() const { return value_can_be_null_; }
3795 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003796
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003797 DECLARE_INSTRUCTION(StaticFieldSet);
3798
3799 private:
3800 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003801 bool value_can_be_null_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003802
3803 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
3804};
3805
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003806// Implement the move-exception DEX instruction.
3807class HLoadException : public HExpression<0> {
3808 public:
3809 HLoadException() : HExpression(Primitive::kPrimNot, SideEffects::None()) {}
3810
3811 DECLARE_INSTRUCTION(LoadException);
3812
3813 private:
3814 DISALLOW_COPY_AND_ASSIGN(HLoadException);
3815};
3816
3817class HThrow : public HTemplateInstruction<1> {
3818 public:
3819 HThrow(HInstruction* exception, uint32_t dex_pc)
3820 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {
3821 SetRawInputAt(0, exception);
3822 }
3823
3824 bool IsControlFlow() const OVERRIDE { return true; }
3825
3826 bool NeedsEnvironment() const OVERRIDE { return true; }
3827
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003828 bool CanThrow() const OVERRIDE { return true; }
3829
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003830 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003831
3832 DECLARE_INSTRUCTION(Throw);
3833
3834 private:
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003835 const uint32_t dex_pc_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003836
3837 DISALLOW_COPY_AND_ASSIGN(HThrow);
3838};
3839
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003840class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003841 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003842 HInstanceOf(HInstruction* object,
3843 HLoadClass* constant,
3844 bool class_is_final,
3845 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003846 : HExpression(Primitive::kPrimBoolean, SideEffects::None()),
3847 class_is_final_(class_is_final),
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003848 must_do_null_check_(true),
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003849 dex_pc_(dex_pc) {
3850 SetRawInputAt(0, object);
3851 SetRawInputAt(1, constant);
3852 }
3853
3854 bool CanBeMoved() const OVERRIDE { return true; }
3855
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003856 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003857 return true;
3858 }
3859
3860 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003861 return false;
3862 }
3863
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003864 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003865
3866 bool IsClassFinal() const { return class_is_final_; }
3867
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003868 // Used only in code generation.
3869 bool MustDoNullCheck() const { return must_do_null_check_; }
3870 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
3871
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003872 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003873
3874 private:
3875 const bool class_is_final_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003876 bool must_do_null_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003877 const uint32_t dex_pc_;
3878
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003879 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
3880};
3881
Calin Juravleb1498f62015-02-16 13:13:29 +00003882class HBoundType : public HExpression<1> {
3883 public:
3884 HBoundType(HInstruction* input, ReferenceTypeInfo bound_type)
3885 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3886 bound_type_(bound_type) {
Calin Juravle61d544b2015-02-23 16:46:57 +00003887 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00003888 SetRawInputAt(0, input);
3889 }
3890
3891 const ReferenceTypeInfo& GetBoundType() const { return bound_type_; }
3892
3893 bool CanBeNull() const OVERRIDE {
3894 // `null instanceof ClassX` always return false so we can't be null.
3895 return false;
3896 }
3897
3898 DECLARE_INSTRUCTION(BoundType);
3899
3900 private:
3901 // Encodes the most upper class that this instruction can have. In other words
3902 // it is always the case that GetBoundType().IsSupertypeOf(GetReferenceType()).
3903 // It is used to bound the type in cases like `if (x instanceof ClassX) {}`
3904 const ReferenceTypeInfo bound_type_;
3905
3906 DISALLOW_COPY_AND_ASSIGN(HBoundType);
3907};
3908
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003909class HCheckCast : public HTemplateInstruction<2> {
3910 public:
3911 HCheckCast(HInstruction* object,
3912 HLoadClass* constant,
3913 bool class_is_final,
3914 uint32_t dex_pc)
3915 : HTemplateInstruction(SideEffects::None()),
3916 class_is_final_(class_is_final),
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003917 must_do_null_check_(true),
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003918 dex_pc_(dex_pc) {
3919 SetRawInputAt(0, object);
3920 SetRawInputAt(1, constant);
3921 }
3922
3923 bool CanBeMoved() const OVERRIDE { return true; }
3924
3925 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
3926 return true;
3927 }
3928
3929 bool NeedsEnvironment() const OVERRIDE {
3930 // Instruction may throw a CheckCastError.
3931 return true;
3932 }
3933
3934 bool CanThrow() const OVERRIDE { return true; }
3935
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003936 bool MustDoNullCheck() const { return must_do_null_check_; }
3937 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
3938
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003939 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003940
3941 bool IsClassFinal() const { return class_is_final_; }
3942
3943 DECLARE_INSTRUCTION(CheckCast);
3944
3945 private:
3946 const bool class_is_final_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003947 bool must_do_null_check_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003948 const uint32_t dex_pc_;
3949
3950 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003951};
3952
Calin Juravle27df7582015-04-17 19:12:31 +01003953class HMemoryBarrier : public HTemplateInstruction<0> {
3954 public:
3955 explicit HMemoryBarrier(MemBarrierKind barrier_kind)
3956 : HTemplateInstruction(SideEffects::None()),
3957 barrier_kind_(barrier_kind) {}
3958
3959 MemBarrierKind GetBarrierKind() { return barrier_kind_; }
3960
3961 DECLARE_INSTRUCTION(MemoryBarrier);
3962
3963 private:
3964 const MemBarrierKind barrier_kind_;
3965
3966 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
3967};
3968
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003969class HMonitorOperation : public HTemplateInstruction<1> {
3970 public:
3971 enum OperationKind {
3972 kEnter,
3973 kExit,
3974 };
3975
3976 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
3977 : HTemplateInstruction(SideEffects::None()), kind_(kind), dex_pc_(dex_pc) {
3978 SetRawInputAt(0, object);
3979 }
3980
3981 // Instruction may throw a Java exception, so we need an environment.
3982 bool NeedsEnvironment() const OVERRIDE { return true; }
3983 bool CanThrow() const OVERRIDE { return true; }
3984
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003985 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003986
3987 bool IsEnter() const { return kind_ == kEnter; }
3988
3989 DECLARE_INSTRUCTION(MonitorOperation);
3990
Calin Juravle52c48962014-12-16 17:02:57 +00003991 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003992 const OperationKind kind_;
3993 const uint32_t dex_pc_;
3994
3995 private:
3996 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
3997};
3998
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003999class MoveOperands : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004000 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01004001 MoveOperands(Location source,
4002 Location destination,
4003 Primitive::Type type,
4004 HInstruction* instruction)
4005 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004006
4007 Location GetSource() const { return source_; }
4008 Location GetDestination() const { return destination_; }
4009
4010 void SetSource(Location value) { source_ = value; }
4011 void SetDestination(Location value) { destination_ = value; }
4012
4013 // The parallel move resolver marks moves as "in-progress" by clearing the
4014 // destination (but not the source).
4015 Location MarkPending() {
4016 DCHECK(!IsPending());
4017 Location dest = destination_;
4018 destination_ = Location::NoLocation();
4019 return dest;
4020 }
4021
4022 void ClearPending(Location dest) {
4023 DCHECK(IsPending());
4024 destination_ = dest;
4025 }
4026
4027 bool IsPending() const {
4028 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
4029 return destination_.IsInvalid() && !source_.IsInvalid();
4030 }
4031
4032 // True if this blocks a move from the given location.
4033 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08004034 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004035 }
4036
4037 // A move is redundant if it's been eliminated, if its source and
4038 // destination are the same, or if its destination is unneeded.
4039 bool IsRedundant() const {
4040 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
4041 }
4042
4043 // We clear both operands to indicate move that's been eliminated.
4044 void Eliminate() {
4045 source_ = destination_ = Location::NoLocation();
4046 }
4047
4048 bool IsEliminated() const {
4049 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
4050 return source_.IsInvalid();
4051 }
4052
Alexey Frunze4dda3372015-06-01 18:31:49 -07004053 Primitive::Type GetType() const { return type_; }
4054
Nicolas Geoffray90218252015-04-15 11:56:51 +01004055 bool Is64BitMove() const {
4056 return Primitive::Is64BitType(type_);
4057 }
4058
Nicolas Geoffray740475d2014-09-29 10:33:25 +01004059 HInstruction* GetInstruction() const { return instruction_; }
4060
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004061 private:
4062 Location source_;
4063 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01004064 // The type this move is for.
4065 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01004066 // The instruction this move is assocatied with. Null when this move is
4067 // for moving an input in the expected locations of user (including a phi user).
4068 // This is only used in debug mode, to ensure we do not connect interval siblings
4069 // in the same parallel move.
4070 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004071};
4072
4073static constexpr size_t kDefaultNumberOfMoves = 4;
4074
4075class HParallelMove : public HTemplateInstruction<0> {
4076 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004077 explicit HParallelMove(ArenaAllocator* arena)
4078 : HTemplateInstruction(SideEffects::None()), moves_(arena, kDefaultNumberOfMoves) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004079
Nicolas Geoffray90218252015-04-15 11:56:51 +01004080 void AddMove(Location source,
4081 Location destination,
4082 Primitive::Type type,
4083 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004084 DCHECK(source.IsValid());
4085 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004086 if (kIsDebugBuild) {
4087 if (instruction != nullptr) {
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00004088 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004089 if (moves_.Get(i).GetInstruction() == instruction) {
4090 // Special case the situation where the move is for the spill slot
4091 // of the instruction.
4092 if ((GetPrevious() == instruction)
4093 || ((GetPrevious() == nullptr)
4094 && instruction->IsPhi()
4095 && instruction->GetBlock() == GetBlock())) {
4096 DCHECK_NE(destination.GetKind(), moves_.Get(i).GetDestination().GetKind())
4097 << "Doing parallel moves for the same instruction.";
4098 } else {
4099 DCHECK(false) << "Doing parallel moves for the same instruction.";
4100 }
4101 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00004102 }
4103 }
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004104 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Zheng Xuad4450e2015-04-17 18:48:56 +08004105 DCHECK(!destination.OverlapsWith(moves_.Get(i).GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01004106 << "Overlapped destination for two moves in a parallel move: "
4107 << moves_.Get(i).GetSource() << " ==> " << moves_.Get(i).GetDestination() << " and "
4108 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004109 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01004110 }
Nicolas Geoffray90218252015-04-15 11:56:51 +01004111 moves_.Add(MoveOperands(source, destination, type, instruction));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004112 }
4113
4114 MoveOperands* MoveOperandsAt(size_t index) const {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004115 return moves_.GetRawStorage() + index;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004116 }
4117
4118 size_t NumMoves() const { return moves_.Size(); }
4119
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004120 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004121
4122 private:
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004123 GrowableArray<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004124
4125 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
4126};
4127
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004128class HGraphVisitor : public ValueObject {
4129 public:
Dave Allison20dfc792014-06-16 20:44:29 -07004130 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
4131 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004132
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004133 virtual void VisitInstruction(HInstruction* instruction) { UNUSED(instruction); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004134 virtual void VisitBasicBlock(HBasicBlock* block);
4135
Roland Levillain633021e2014-10-01 14:12:25 +01004136 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004137 void VisitInsertionOrder();
4138
Roland Levillain633021e2014-10-01 14:12:25 +01004139 // Visit the graph following dominator tree reverse post-order.
4140 void VisitReversePostOrder();
4141
Nicolas Geoffray787c3072014-03-17 10:20:19 +00004142 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00004143
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004144 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004145#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004146 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
4147
4148 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
4149
4150#undef DECLARE_VISIT_INSTRUCTION
4151
4152 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07004153 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004154
4155 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
4156};
4157
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004158class HGraphDelegateVisitor : public HGraphVisitor {
4159 public:
4160 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
4161 virtual ~HGraphDelegateVisitor() {}
4162
4163 // Visit functions that delegate to to super class.
4164#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004165 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004166
4167 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
4168
4169#undef DECLARE_VISIT_INSTRUCTION
4170
4171 private:
4172 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
4173};
4174
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004175class HInsertionOrderIterator : public ValueObject {
4176 public:
4177 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
4178
4179 bool Done() const { return index_ == graph_.GetBlocks().Size(); }
4180 HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); }
4181 void Advance() { ++index_; }
4182
4183 private:
4184 const HGraph& graph_;
4185 size_t index_;
4186
4187 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
4188};
4189
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004190class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004191 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00004192 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
4193 // Check that reverse post order of the graph has been built.
4194 DCHECK(!graph.GetReversePostOrder().IsEmpty());
4195 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004196
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004197 bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); }
4198 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004199 void Advance() { ++index_; }
4200
4201 private:
4202 const HGraph& graph_;
4203 size_t index_;
4204
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004205 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004206};
4207
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004208class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004209 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004210 explicit HPostOrderIterator(const HGraph& graph)
David Brazdil10f56cb2015-03-24 18:49:14 +00004211 : graph_(graph), index_(graph_.GetReversePostOrder().Size()) {
4212 // Check that reverse post order of the graph has been built.
4213 DCHECK(!graph.GetReversePostOrder().IsEmpty());
4214 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004215
4216 bool Done() const { return index_ == 0; }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004217 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004218 void Advance() { --index_; }
4219
4220 private:
4221 const HGraph& graph_;
4222 size_t index_;
4223
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004224 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004225};
4226
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01004227class HLinearPostOrderIterator : public ValueObject {
4228 public:
4229 explicit HLinearPostOrderIterator(const HGraph& graph)
4230 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().Size()) {}
4231
4232 bool Done() const { return index_ == 0; }
4233
4234 HBasicBlock* Current() const { return order_.Get(index_ -1); }
4235
4236 void Advance() {
4237 --index_;
4238 DCHECK_GE(index_, 0U);
4239 }
4240
4241 private:
4242 const GrowableArray<HBasicBlock*>& order_;
4243 size_t index_;
4244
4245 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
4246};
4247
4248class HLinearOrderIterator : public ValueObject {
4249 public:
4250 explicit HLinearOrderIterator(const HGraph& graph)
4251 : order_(graph.GetLinearOrder()), index_(0) {}
4252
4253 bool Done() const { return index_ == order_.Size(); }
4254 HBasicBlock* Current() const { return order_.Get(index_); }
4255 void Advance() { ++index_; }
4256
4257 private:
4258 const GrowableArray<HBasicBlock*>& order_;
4259 size_t index_;
4260
4261 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
4262};
4263
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004264// Iterator over the blocks that art part of the loop. Includes blocks part
4265// of an inner loop. The order in which the blocks are iterated is on their
4266// block id.
4267class HBlocksInLoopIterator : public ValueObject {
4268 public:
4269 explicit HBlocksInLoopIterator(const HLoopInformation& info)
4270 : blocks_in_loop_(info.GetBlocks()),
4271 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
4272 index_(0) {
4273 if (!blocks_in_loop_.IsBitSet(index_)) {
4274 Advance();
4275 }
4276 }
4277
4278 bool Done() const { return index_ == blocks_.Size(); }
4279 HBasicBlock* Current() const { return blocks_.Get(index_); }
4280 void Advance() {
4281 ++index_;
4282 for (size_t e = blocks_.Size(); index_ < e; ++index_) {
4283 if (blocks_in_loop_.IsBitSet(index_)) {
4284 break;
4285 }
4286 }
4287 }
4288
4289 private:
4290 const BitVector& blocks_in_loop_;
4291 const GrowableArray<HBasicBlock*>& blocks_;
4292 size_t index_;
4293
4294 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
4295};
4296
Mingyao Yang3584bce2015-05-19 16:01:59 -07004297// Iterator over the blocks that art part of the loop. Includes blocks part
4298// of an inner loop. The order in which the blocks are iterated is reverse
4299// post order.
4300class HBlocksInLoopReversePostOrderIterator : public ValueObject {
4301 public:
4302 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
4303 : blocks_in_loop_(info.GetBlocks()),
4304 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
4305 index_(0) {
4306 if (!blocks_in_loop_.IsBitSet(blocks_.Get(index_)->GetBlockId())) {
4307 Advance();
4308 }
4309 }
4310
4311 bool Done() const { return index_ == blocks_.Size(); }
4312 HBasicBlock* Current() const { return blocks_.Get(index_); }
4313 void Advance() {
4314 ++index_;
4315 for (size_t e = blocks_.Size(); index_ < e; ++index_) {
4316 if (blocks_in_loop_.IsBitSet(blocks_.Get(index_)->GetBlockId())) {
4317 break;
4318 }
4319 }
4320 }
4321
4322 private:
4323 const BitVector& blocks_in_loop_;
4324 const GrowableArray<HBasicBlock*>& blocks_;
4325 size_t index_;
4326
4327 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
4328};
4329
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00004330inline int64_t Int64FromConstant(HConstant* constant) {
4331 DCHECK(constant->IsIntConstant() || constant->IsLongConstant());
4332 return constant->IsIntConstant() ? constant->AsIntConstant()->GetValue()
4333 : constant->AsLongConstant()->GetValue();
4334}
4335
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004336} // namespace art
4337
4338#endif // ART_COMPILER_OPTIMIZING_NODES_H_