blob: 151da7f7b6985290b68d9988a8e81212b6c9106f [file] [log] [blame]
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ART_COMPILER_OPTIMIZING_NODES_H_
18#define ART_COMPILER_OPTIMIZING_NODES_H_
19
David Brazdil8d5b8b22015-03-24 10:51:52 +000020#include "base/arena_containers.h"
Mathieu Chartierb666f482015-02-18 14:33:14 -080021#include "base/arena_object.h"
Calin Juravle27df7582015-04-17 19:12:31 +010022#include "dex/compiler_enums.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000023#include "entrypoints/quick/quick_entrypoints_enum.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000024#include "handle.h"
25#include "handle_scope.h"
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000026#include "invoke_type.h"
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +010027#include "locations.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000028#include "mirror/class.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010029#include "offsets.h"
Ian Rogerse63db272014-07-15 15:36:11 -070030#include "primitive.h"
Nicolas Geoffray0e336432014-02-26 18:24:38 +000031#include "utils/arena_bit_vector.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000032#include "utils/growable_array.h"
33
34namespace art {
35
David Brazdil1abb4192015-02-17 18:33:36 +000036class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000037class HBasicBlock;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010038class HCurrentMethod;
David Brazdil8d5b8b22015-03-24 10:51:52 +000039class HDoubleConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010040class HEnvironment;
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +010041class HFakeString;
David Brazdil8d5b8b22015-03-24 10:51:52 +000042class HFloatConstant;
David Brazdilfc6a86a2015-06-26 10:33:45 +000043class HGraphBuilder;
David Brazdil8d5b8b22015-03-24 10:51:52 +000044class HGraphVisitor;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000045class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000046class HIntConstant;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000047class HInvoke;
David Brazdil8d5b8b22015-03-24 10:51:52 +000048class HLongConstant;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +000049class HNullConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010050class HPhi;
Nicolas Geoffray3c049742014-09-24 18:10:46 +010051class HSuspendCheck;
David Brazdilffee3d32015-07-06 11:48:53 +010052class HTryBoundary;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010053class LiveInterval;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000054class LocationSummary;
Nicolas Geoffraydb216f42015-05-05 17:02:20 +010055class SlowPathCode;
David Brazdil8d5b8b22015-03-24 10:51:52 +000056class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000057
58static const int kDefaultNumberOfBlocks = 8;
59static const int kDefaultNumberOfSuccessors = 2;
60static const int kDefaultNumberOfPredecessors = 2;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010061static const int kDefaultNumberOfDominatedBlocks = 1;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000062static const int kDefaultNumberOfBackEdges = 1;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000063
Calin Juravle9aec02f2014-11-18 23:06:35 +000064static constexpr uint32_t kMaxIntShiftValue = 0x1f;
65static constexpr uint64_t kMaxLongShiftValue = 0x3f;
66
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010067static constexpr uint32_t kUnknownFieldIndex = static_cast<uint32_t>(-1);
68
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +010069static constexpr InvokeType kInvalidInvokeType = static_cast<InvokeType>(-1);
70
Dave Allison20dfc792014-06-16 20:44:29 -070071enum IfCondition {
72 kCondEQ,
73 kCondNE,
74 kCondLT,
75 kCondLE,
76 kCondGT,
77 kCondGE,
78};
79
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010080class HInstructionList {
81 public:
82 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
83
84 void AddInstruction(HInstruction* instruction);
85 void RemoveInstruction(HInstruction* instruction);
86
David Brazdilc3d743f2015-04-22 13:40:50 +010087 // Insert `instruction` before/after an existing instruction `cursor`.
88 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
89 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
90
Roland Levillain6b469232014-09-25 10:10:38 +010091 // Return true if this list contains `instruction`.
92 bool Contains(HInstruction* instruction) const;
93
Roland Levillainccc07a92014-09-16 14:48:16 +010094 // Return true if `instruction1` is found before `instruction2` in
95 // this instruction list and false otherwise. Abort if none
96 // of these instructions is found.
97 bool FoundBefore(const HInstruction* instruction1,
98 const HInstruction* instruction2) const;
99
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000100 bool IsEmpty() const { return first_instruction_ == nullptr; }
101 void Clear() { first_instruction_ = last_instruction_ = nullptr; }
102
103 // Update the block of all instructions to be `block`.
104 void SetBlockOfInstructions(HBasicBlock* block) const;
105
106 void AddAfter(HInstruction* cursor, const HInstructionList& instruction_list);
107 void Add(const HInstructionList& instruction_list);
108
David Brazdil2d7352b2015-04-20 14:52:42 +0100109 // Return the number of instructions in the list. This is an expensive operation.
110 size_t CountSize() const;
111
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100112 private:
113 HInstruction* first_instruction_;
114 HInstruction* last_instruction_;
115
116 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000117 friend class HGraph;
118 friend class HInstruction;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100119 friend class HInstructionIterator;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100120 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100121
122 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
123};
124
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000125// Control-flow graph of a method. Contains a list of basic blocks.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700126class HGraph : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000127 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100128 HGraph(ArenaAllocator* arena,
129 const DexFile& dex_file,
130 uint32_t method_idx,
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100131 bool should_generate_constructor_barrier,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700132 InstructionSet instruction_set,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100133 InvokeType invoke_type = kInvalidInvokeType,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100134 bool debuggable = false,
135 int start_instruction_id = 0)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000136 : arena_(arena),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000137 blocks_(arena, kDefaultNumberOfBlocks),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100138 reverse_post_order_(arena, kDefaultNumberOfBlocks),
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100139 linear_order_(arena, kDefaultNumberOfBlocks),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700140 entry_block_(nullptr),
141 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100142 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100143 number_of_vregs_(0),
144 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000145 temporaries_vreg_slots_(0),
Mark Mendell1152c922015-04-24 17:06:35 -0400146 has_bounds_checks_(false),
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000147 debuggable_(debuggable),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000148 current_instruction_id_(start_instruction_id),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100149 dex_file_(dex_file),
150 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100151 invoke_type_(invoke_type),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100152 in_ssa_form_(false),
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100153 should_generate_constructor_barrier_(should_generate_constructor_barrier),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700154 instruction_set_(instruction_set),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000155 cached_null_constant_(nullptr),
156 cached_int_constants_(std::less<int32_t>(), arena->Adapter()),
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000157 cached_float_constants_(std::less<int32_t>(), arena->Adapter()),
158 cached_long_constants_(std::less<int64_t>(), arena->Adapter()),
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100159 cached_double_constants_(std::less<int64_t>(), arena->Adapter()),
160 cached_current_method_(nullptr) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000161
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000162 ArenaAllocator* GetArena() const { return arena_; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100163 const GrowableArray<HBasicBlock*>& GetBlocks() const { return blocks_; }
Roland Levillain93445682014-10-06 19:24:02 +0100164 HBasicBlock* GetBlock(size_t id) const { return blocks_.Get(id); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000165
David Brazdil69ba7b72015-06-23 18:27:30 +0100166 bool IsInSsaForm() const { return in_ssa_form_; }
167
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000168 HBasicBlock* GetEntryBlock() const { return entry_block_; }
169 HBasicBlock* GetExitBlock() const { return exit_block_; }
David Brazdilc7af85d2015-05-26 12:05:55 +0100170 bool HasExitBlock() const { return exit_block_ != nullptr; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000171
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000172 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
173 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000174
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000175 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100176
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000177 // Try building the SSA form of this graph, with dominance computation and loop
178 // recognition. Returns whether it was successful in doing all these steps.
179 bool TryBuildingSsa() {
180 BuildDominatorTree();
Nicolas Geoffrayd3350832015-03-12 11:16:23 +0000181 // The SSA builder requires loops to all be natural. Specifically, the dead phi
182 // elimination phase checks the consistency of the graph when doing a post-order
183 // visit for eliminating dead phis: a dead phi can only have loop header phi
184 // users remaining when being visited.
185 if (!AnalyzeNaturalLoops()) return false;
David Brazdilffee3d32015-07-06 11:48:53 +0100186 // Precompute per-block try membership before entering the SSA builder,
187 // which needs the information to build catch block phis from values of
188 // locals at throwing instructions inside try blocks.
189 ComputeTryBlockInformation();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000190 TransformToSsa();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100191 in_ssa_form_ = true;
Nicolas Geoffrayd3350832015-03-12 11:16:23 +0000192 return true;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000193 }
194
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100195 void ComputeDominanceInformation();
196 void ClearDominanceInformation();
197
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000198 void BuildDominatorTree();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000199 void TransformToSsa();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100200 void SimplifyCFG();
David Brazdilffee3d32015-07-06 11:48:53 +0100201 void SimplifyCatchBlocks();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000202
Nicolas Geoffrayf5370122014-12-02 11:51:19 +0000203 // Analyze all natural loops in this graph. Returns false if one
204 // loop is not natural, that is the header does not dominate the
205 // back edge.
206 bool AnalyzeNaturalLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100207
David Brazdilffee3d32015-07-06 11:48:53 +0100208 // Iterate over blocks to compute try block membership. Needs reverse post
209 // order and loop information.
210 void ComputeTryBlockInformation();
211
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000212 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
213 void InlineInto(HGraph* outer_graph, HInvoke* invoke);
214
Mingyao Yang3584bce2015-05-19 16:01:59 -0700215 // Need to add a couple of blocks to test if the loop body is entered and
216 // put deoptimization instructions, etc.
217 void TransformLoopHeaderForBCE(HBasicBlock* header);
218
David Brazdil2d7352b2015-04-20 14:52:42 +0100219 // Removes `block` from the graph.
220 void DeleteDeadBlock(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000221
David Brazdilfc6a86a2015-06-26 10:33:45 +0000222 // Splits the edge between `block` and `successor` while preserving the
223 // indices in the predecessor/successor lists. If there are multiple edges
224 // between the blocks, the lowest indices are used.
225 // Returns the new block which is empty and has the same dex pc as `successor`.
226 HBasicBlock* SplitEdge(HBasicBlock* block, HBasicBlock* successor);
227
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100228 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
229 void SimplifyLoop(HBasicBlock* header);
230
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000231 int32_t GetNextInstructionId() {
232 DCHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000233 return current_instruction_id_++;
234 }
235
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000236 int32_t GetCurrentInstructionId() const {
237 return current_instruction_id_;
238 }
239
240 void SetCurrentInstructionId(int32_t id) {
241 current_instruction_id_ = id;
242 }
243
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100244 uint16_t GetMaximumNumberOfOutVRegs() const {
245 return maximum_number_of_out_vregs_;
246 }
247
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000248 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
249 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100250 }
251
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100252 void UpdateMaximumNumberOfOutVRegs(uint16_t other_value) {
253 maximum_number_of_out_vregs_ = std::max(maximum_number_of_out_vregs_, other_value);
254 }
255
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000256 void UpdateTemporariesVRegSlots(size_t slots) {
257 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100258 }
259
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000260 size_t GetTemporariesVRegSlots() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100261 DCHECK(!in_ssa_form_);
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000262 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100263 }
264
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100265 void SetNumberOfVRegs(uint16_t number_of_vregs) {
266 number_of_vregs_ = number_of_vregs;
267 }
268
269 uint16_t GetNumberOfVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100270 DCHECK(!in_ssa_form_);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100271 return number_of_vregs_;
272 }
273
274 void SetNumberOfInVRegs(uint16_t value) {
275 number_of_in_vregs_ = value;
276 }
277
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100278 uint16_t GetNumberOfLocalVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100279 DCHECK(!in_ssa_form_);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100280 return number_of_vregs_ - number_of_in_vregs_;
281 }
282
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100283 const GrowableArray<HBasicBlock*>& GetReversePostOrder() const {
284 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100285 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100286
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100287 const GrowableArray<HBasicBlock*>& GetLinearOrder() const {
288 return linear_order_;
289 }
290
Mark Mendell1152c922015-04-24 17:06:35 -0400291 bool HasBoundsChecks() const {
292 return has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800293 }
294
Mark Mendell1152c922015-04-24 17:06:35 -0400295 void SetHasBoundsChecks(bool value) {
296 has_bounds_checks_ = value;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800297 }
298
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100299 bool ShouldGenerateConstructorBarrier() const {
300 return should_generate_constructor_barrier_;
301 }
302
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000303 bool IsDebuggable() const { return debuggable_; }
304
David Brazdil8d5b8b22015-03-24 10:51:52 +0000305 // Returns a constant of the given type and value. If it does not exist
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000306 // already, it is created and inserted into the graph. This method is only for
307 // integral types.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000308 HConstant* GetConstant(Primitive::Type type, int64_t value);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000309 HNullConstant* GetNullConstant();
David Brazdil8d5b8b22015-03-24 10:51:52 +0000310 HIntConstant* GetIntConstant(int32_t value) {
311 return CreateConstant(value, &cached_int_constants_);
312 }
313 HLongConstant* GetLongConstant(int64_t value) {
314 return CreateConstant(value, &cached_long_constants_);
315 }
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000316 HFloatConstant* GetFloatConstant(float value) {
317 return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_);
318 }
319 HDoubleConstant* GetDoubleConstant(double value) {
320 return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_);
321 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000322
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100323 HCurrentMethod* GetCurrentMethod();
324
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000325 HBasicBlock* FindCommonDominator(HBasicBlock* first, HBasicBlock* second) const;
David Brazdil2d7352b2015-04-20 14:52:42 +0100326
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100327 const DexFile& GetDexFile() const {
328 return dex_file_;
329 }
330
331 uint32_t GetMethodIdx() const {
332 return method_idx_;
333 }
334
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100335 InvokeType GetInvokeType() const {
336 return invoke_type_;
337 }
338
Mark Mendellc4701932015-04-10 13:18:51 -0400339 InstructionSet GetInstructionSet() const {
340 return instruction_set_;
341 }
342
David Brazdil2d7352b2015-04-20 14:52:42 +0100343 private:
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000344 void VisitBlockForDominatorTree(HBasicBlock* block,
345 HBasicBlock* predecessor,
346 GrowableArray<size_t>* visits);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100347 void FindBackEdges(ArenaBitVector* visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000348 void VisitBlockForBackEdges(HBasicBlock* block,
349 ArenaBitVector* visited,
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100350 ArenaBitVector* visiting);
Roland Levillainfc600dc2014-12-02 17:16:31 +0000351 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100352 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000353
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000354 template <class InstructionType, typename ValueType>
355 InstructionType* CreateConstant(ValueType value,
356 ArenaSafeMap<ValueType, InstructionType*>* cache) {
357 // Try to find an existing constant of the given value.
358 InstructionType* constant = nullptr;
359 auto cached_constant = cache->find(value);
360 if (cached_constant != cache->end()) {
361 constant = cached_constant->second;
362 }
363
364 // If not found or previously deleted, create and cache a new instruction.
Nicolas Geoffrayf78848f2015-06-17 11:57:56 +0100365 // Don't bother reviving a previously deleted instruction, for simplicity.
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000366 if (constant == nullptr || constant->GetBlock() == nullptr) {
367 constant = new (arena_) InstructionType(value);
368 cache->Overwrite(value, constant);
369 InsertConstant(constant);
370 }
371 return constant;
372 }
373
David Brazdil8d5b8b22015-03-24 10:51:52 +0000374 void InsertConstant(HConstant* instruction);
375
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000376 // Cache a float constant into the graph. This method should only be
377 // called by the SsaBuilder when creating "equivalent" instructions.
378 void CacheFloatConstant(HFloatConstant* constant);
379
380 // See CacheFloatConstant comment.
381 void CacheDoubleConstant(HDoubleConstant* constant);
382
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000383 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000384
385 // List of blocks in insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000386 GrowableArray<HBasicBlock*> blocks_;
387
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100388 // List of blocks to perform a reverse post order tree traversal.
389 GrowableArray<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000390
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100391 // List of blocks to perform a linear order tree traversal.
392 GrowableArray<HBasicBlock*> linear_order_;
393
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000394 HBasicBlock* entry_block_;
395 HBasicBlock* exit_block_;
396
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100397 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100398 uint16_t maximum_number_of_out_vregs_;
399
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100400 // The number of virtual registers in this method. Contains the parameters.
401 uint16_t number_of_vregs_;
402
403 // The number of virtual registers used by parameters of this method.
404 uint16_t number_of_in_vregs_;
405
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000406 // Number of vreg size slots that the temporaries use (used in baseline compiler).
407 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100408
Mark Mendell1152c922015-04-24 17:06:35 -0400409 // Has bounds checks. We can totally skip BCE if it's false.
410 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800411
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000412 // Indicates whether the graph should be compiled in a way that
413 // ensures full debuggability. If false, we can apply more
414 // aggressive optimizations that may limit the level of debugging.
415 const bool debuggable_;
416
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000417 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000418 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000419
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100420 // The dex file from which the method is from.
421 const DexFile& dex_file_;
422
423 // The method index in the dex file.
424 const uint32_t method_idx_;
425
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100426 // If inlined, this encodes how the callee is being invoked.
427 const InvokeType invoke_type_;
428
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100429 // Whether the graph has been transformed to SSA form. Only used
430 // in debug mode to ensure we are not using properties only valid
431 // for non-SSA form (like the number of temporaries).
432 bool in_ssa_form_;
433
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100434 const bool should_generate_constructor_barrier_;
435
Mathieu Chartiere401d142015-04-22 13:56:20 -0700436 const InstructionSet instruction_set_;
437
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000438 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000439 HNullConstant* cached_null_constant_;
440 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000441 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000442 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000443 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000444
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100445 HCurrentMethod* cached_current_method_;
446
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000447 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100448 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000449 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000450 DISALLOW_COPY_AND_ASSIGN(HGraph);
451};
452
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700453class HLoopInformation : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000454 public:
455 HLoopInformation(HBasicBlock* header, HGraph* graph)
456 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100457 suspend_check_(nullptr),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100458 back_edges_(graph->GetArena(), kDefaultNumberOfBackEdges),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100459 // Make bit vector growable, as the number of blocks may change.
460 blocks_(graph->GetArena(), graph->GetBlocks().Size(), true) {}
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100461
462 HBasicBlock* GetHeader() const {
463 return header_;
464 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000465
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000466 void SetHeader(HBasicBlock* block) {
467 header_ = block;
468 }
469
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100470 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
471 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
472 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
473
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000474 void AddBackEdge(HBasicBlock* back_edge) {
475 back_edges_.Add(back_edge);
476 }
477
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100478 void RemoveBackEdge(HBasicBlock* back_edge) {
479 back_edges_.Delete(back_edge);
480 }
481
David Brazdil46e2a392015-03-16 17:31:52 +0000482 bool IsBackEdge(const HBasicBlock& block) const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100483 for (size_t i = 0, e = back_edges_.Size(); i < e; ++i) {
David Brazdil46e2a392015-03-16 17:31:52 +0000484 if (back_edges_.Get(i) == &block) return true;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100485 }
486 return false;
487 }
488
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000489 size_t NumberOfBackEdges() const {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000490 return back_edges_.Size();
491 }
492
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100493 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100494
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100495 const GrowableArray<HBasicBlock*>& GetBackEdges() const {
496 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100497 }
498
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100499 // Returns the lifetime position of the back edge that has the
500 // greatest lifetime position.
501 size_t GetLifetimeEnd() const;
502
503 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
504 for (size_t i = 0, e = back_edges_.Size(); i < e; ++i) {
505 if (back_edges_.Get(i) == existing) {
506 back_edges_.Put(i, new_back_edge);
507 return;
508 }
509 }
510 UNREACHABLE();
Nicolas Geoffray57902602015-04-21 14:28:41 +0100511 }
512
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100513 // Finds blocks that are part of this loop. Returns whether the loop is a natural loop,
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100514 // that is the header dominates the back edge.
515 bool Populate();
516
David Brazdila4b8c212015-05-07 09:59:30 +0100517 // Reanalyzes the loop by removing loop info from its blocks and re-running
518 // Populate(). If there are no back edges left, the loop info is completely
519 // removed as well as its SuspendCheck instruction. It must be run on nested
520 // inner loops first.
521 void Update();
522
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100523 // Returns whether this loop information contains `block`.
524 // Note that this loop information *must* be populated before entering this function.
525 bool Contains(const HBasicBlock& block) const;
526
527 // Returns whether this loop information is an inner loop of `other`.
528 // Note that `other` *must* be populated before entering this function.
529 bool IsIn(const HLoopInformation& other) const;
530
531 const ArenaBitVector& GetBlocks() const { return blocks_; }
532
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000533 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000534 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000535
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000536 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100537 // Internal recursive implementation of `Populate`.
538 void PopulateRecursive(HBasicBlock* block);
539
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000540 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100541 HSuspendCheck* suspend_check_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000542 GrowableArray<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100543 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000544
545 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
546};
547
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100548static constexpr size_t kNoLifetime = -1;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100549static constexpr uint32_t kNoDexPc = -1;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100550
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000551// A block in a method. Contains the list of instructions represented
552// as a double linked list. Each block knows its predecessors and
553// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100554
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700555class HBasicBlock : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000556 public:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100557 explicit HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000558 : graph_(graph),
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000559 predecessors_(graph->GetArena(), kDefaultNumberOfPredecessors),
560 successors_(graph->GetArena(), kDefaultNumberOfSuccessors),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000561 loop_information_(nullptr),
562 dominator_(nullptr),
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100563 dominated_blocks_(graph->GetArena(), kDefaultNumberOfDominatedBlocks),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100564 block_id_(-1),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100565 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100566 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000567 lifetime_end_(kNoLifetime),
568 is_catch_block_(false) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000569
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100570 const GrowableArray<HBasicBlock*>& GetPredecessors() const {
571 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000572 }
573
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100574 const GrowableArray<HBasicBlock*>& GetSuccessors() const {
575 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000576 }
577
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100578 const GrowableArray<HBasicBlock*>& GetDominatedBlocks() const {
579 return dominated_blocks_;
580 }
581
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100582 bool IsEntryBlock() const {
583 return graph_->GetEntryBlock() == this;
584 }
585
586 bool IsExitBlock() const {
587 return graph_->GetExitBlock() == this;
588 }
589
David Brazdil46e2a392015-03-16 17:31:52 +0000590 bool IsSingleGoto() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000591 bool IsSingleTryBoundary() const;
592
593 // Returns true if this block emits nothing but a jump.
594 bool IsSingleJump() const {
595 HLoopInformation* loop_info = GetLoopInformation();
596 return (IsSingleGoto() || IsSingleTryBoundary())
597 // Back edges generate a suspend check.
598 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
599 }
David Brazdil46e2a392015-03-16 17:31:52 +0000600
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000601 void AddBackEdge(HBasicBlock* back_edge) {
602 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000603 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000604 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100605 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000606 loop_information_->AddBackEdge(back_edge);
607 }
608
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000609 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000610 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000611
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000612 int GetBlockId() const { return block_id_; }
613 void SetBlockId(int id) { block_id_ = id; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000614
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000615 HBasicBlock* GetDominator() const { return dominator_; }
616 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100617 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.Add(block); }
David Brazdil2d7352b2015-04-20 14:52:42 +0100618 void RemoveDominatedBlock(HBasicBlock* block) { dominated_blocks_.Delete(block); }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000619 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
620 for (size_t i = 0, e = dominated_blocks_.Size(); i < e; ++i) {
621 if (dominated_blocks_.Get(i) == existing) {
622 dominated_blocks_.Put(i, new_block);
623 return;
624 }
625 }
626 LOG(FATAL) << "Unreachable";
627 UNREACHABLE();
628 }
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100629 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000630
631 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100632 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000633 }
634
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100635 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
636 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100637 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100638 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +0100639 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
640 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000641
642 void AddSuccessor(HBasicBlock* block) {
643 successors_.Add(block);
644 block->predecessors_.Add(this);
645 }
646
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100647 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
648 size_t successor_index = GetSuccessorIndexOf(existing);
649 DCHECK_NE(successor_index, static_cast<size_t>(-1));
650 existing->RemovePredecessor(this);
651 new_block->predecessors_.Add(this);
652 successors_.Put(successor_index, new_block);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000653 }
654
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000655 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
656 size_t predecessor_index = GetPredecessorIndexOf(existing);
657 DCHECK_NE(predecessor_index, static_cast<size_t>(-1));
658 existing->RemoveSuccessor(this);
659 new_block->successors_.Add(this);
660 predecessors_.Put(predecessor_index, new_block);
661 }
662
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100663 // Insert `this` between `predecessor` and `successor. This method
664 // preserves the indicies, and will update the first edge found between
665 // `predecessor` and `successor`.
666 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
667 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
668 DCHECK_NE(predecessor_index, static_cast<size_t>(-1));
669 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
670 DCHECK_NE(successor_index, static_cast<size_t>(-1));
671 successor->predecessors_.Put(predecessor_index, this);
672 predecessor->successors_.Put(successor_index, this);
673 successors_.Add(successor);
674 predecessors_.Add(predecessor);
675 }
676
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100677 void RemovePredecessor(HBasicBlock* block) {
678 predecessors_.Delete(block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100679 }
680
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000681 void RemoveSuccessor(HBasicBlock* block) {
682 successors_.Delete(block);
683 }
684
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100685 void ClearAllPredecessors() {
686 predecessors_.Reset();
687 }
688
689 void AddPredecessor(HBasicBlock* block) {
690 predecessors_.Add(block);
691 block->successors_.Add(this);
692 }
693
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100694 void SwapPredecessors() {
Nicolas Geoffrayc83d4412014-09-18 16:46:20 +0100695 DCHECK_EQ(predecessors_.Size(), 2u);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100696 HBasicBlock* temp = predecessors_.Get(0);
697 predecessors_.Put(0, predecessors_.Get(1));
698 predecessors_.Put(1, temp);
699 }
700
David Brazdil769c9e52015-04-27 13:54:09 +0100701 void SwapSuccessors() {
702 DCHECK_EQ(successors_.Size(), 2u);
703 HBasicBlock* temp = successors_.Get(0);
704 successors_.Put(0, successors_.Get(1));
705 successors_.Put(1, temp);
706 }
707
David Brazdilfc6a86a2015-06-26 10:33:45 +0000708 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100709 for (size_t i = 0, e = predecessors_.Size(); i < e; ++i) {
710 if (predecessors_.Get(i) == predecessor) {
711 return i;
712 }
713 }
714 return -1;
715 }
716
David Brazdilfc6a86a2015-06-26 10:33:45 +0000717 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100718 for (size_t i = 0, e = successors_.Size(); i < e; ++i) {
719 if (successors_.Get(i) == successor) {
720 return i;
721 }
722 }
723 return -1;
724 }
725
David Brazdilfc6a86a2015-06-26 10:33:45 +0000726 HBasicBlock* GetSinglePredecessor() const {
727 DCHECK_EQ(GetPredecessors().Size(), 1u);
728 return GetPredecessors().Get(0);
729 }
730
731 HBasicBlock* GetSingleSuccessor() const {
732 DCHECK_EQ(GetSuccessors().Size(), 1u);
733 return GetSuccessors().Get(0);
734 }
735
736 // Returns whether the first occurrence of `predecessor` in the list of
737 // predecessors is at index `idx`.
738 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
739 DCHECK_EQ(GetPredecessors().Get(idx), predecessor);
740 return GetPredecessorIndexOf(predecessor) == idx;
741 }
742
David Brazdilffee3d32015-07-06 11:48:53 +0100743 // Returns the number of non-exceptional successors. SsaChecker ensures that
744 // these are stored at the beginning of the successor list.
745 size_t NumberOfNormalSuccessors() const {
746 return EndsWithTryBoundary() ? 1 : GetSuccessors().Size();
747 }
David Brazdilfc6a86a2015-06-26 10:33:45 +0000748
749 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +0100750 // created, latter block. Note that this method will add the block to the
751 // graph, create a Goto at the end of the former block and will create an edge
752 // between the blocks. It will not, however, update the reverse post order or
753 // loop information.
David Brazdilfc6a86a2015-06-26 10:33:45 +0000754 HBasicBlock* SplitBefore(HInstruction* cursor);
755
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000756 // Split the block into two blocks just after `cursor`. Returns the newly
757 // created block. Note that this method just updates raw block information,
758 // like predecessors, successors, dominators, and instruction list. It does not
759 // update the graph, reverse post order, loop information, nor make sure the
760 // blocks are consistent (for example ending with a control flow instruction).
761 HBasicBlock* SplitAfter(HInstruction* cursor);
762
763 // Merge `other` at the end of `this`. Successors and dominated blocks of
764 // `other` are changed to be successors and dominated blocks of `this`. Note
765 // that this method does not update the graph, reverse post order, loop
766 // information, nor make sure the blocks are consistent (for example ending
767 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +0100768 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000769
770 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
771 // of `this` are moved to `other`.
772 // Note that this method does not update the graph, reverse post order, loop
773 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +0000774 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000775 void ReplaceWith(HBasicBlock* other);
776
David Brazdil2d7352b2015-04-20 14:52:42 +0100777 // Merge `other` at the end of `this`. This method updates loops, reverse post
778 // order, links to predecessors, successors, dominators and deletes the block
779 // from the graph. The two blocks must be successive, i.e. `this` the only
780 // predecessor of `other` and vice versa.
781 void MergeWith(HBasicBlock* other);
782
783 // Disconnects `this` from all its predecessors, successors and dominator,
784 // removes it from all loops it is included in and eventually from the graph.
785 // The block must not dominate any other block. Predecessors and successors
786 // are safely updated.
787 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +0000788
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000789 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +0100790 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100791 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +0100792 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +0100793 // Replace instruction `initial` with `replacement` within this block.
794 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
795 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100796 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100797 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +0000798 // RemoveInstruction and RemovePhi delete a given instruction from the respective
799 // instruction list. With 'ensure_safety' set to true, it verifies that the
800 // instruction is not in use and removes it from the use lists of its inputs.
801 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
802 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +0100803 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100804
805 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +0100806 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100807 }
808
Roland Levillain6b879dd2014-09-22 17:13:44 +0100809 bool IsLoopPreHeaderFirstPredecessor() const {
810 DCHECK(IsLoopHeader());
811 DCHECK(!GetPredecessors().IsEmpty());
812 return GetPredecessors().Get(0) == GetLoopInformation()->GetPreHeader();
813 }
814
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100815 HLoopInformation* GetLoopInformation() const {
816 return loop_information_;
817 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000818
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000819 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100820 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000821 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100822 void SetInLoop(HLoopInformation* info) {
823 if (IsLoopHeader()) {
824 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +0100825 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100826 loop_information_ = info;
827 } else if (loop_information_->Contains(*info->GetHeader())) {
828 // Block is currently part of an outer loop. Make it part of this inner loop.
829 // Note that a non loop header having a loop information means this loop information
830 // has already been populated
831 loop_information_ = info;
832 } else {
833 // Block is part of an inner loop. Do not update the loop information.
834 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
835 // at this point, because this method is being called while populating `info`.
836 }
837 }
838
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000839 // Raw update of the loop information.
840 void SetLoopInformation(HLoopInformation* info) {
841 loop_information_ = info;
842 }
843
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100844 bool IsInLoop() const { return loop_information_ != nullptr; }
845
David Brazdilffee3d32015-07-06 11:48:53 +0100846 HTryBoundary* GetTryEntry() const { return try_entry_; }
847 void SetTryEntry(HTryBoundary* try_entry) { try_entry_ = try_entry; }
848 bool IsInTry() const { return try_entry_ != nullptr; }
849
850 // Returns the try entry that this block's successors should have. They will
851 // be in the same try, unless the block ends in a try boundary. In that case,
852 // the appropriate try entry will be returned.
853 HTryBoundary* ComputeTryEntryOfSuccessors() const;
854
David Brazdila4b8c212015-05-07 09:59:30 +0100855 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100856 bool Dominates(HBasicBlock* block) const;
857
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100858 size_t GetLifetimeStart() const { return lifetime_start_; }
859 size_t GetLifetimeEnd() const { return lifetime_end_; }
860
861 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
862 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
863
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100864 uint32_t GetDexPc() const { return dex_pc_; }
865
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000866 bool IsCatchBlock() const { return is_catch_block_; }
867 void SetIsCatchBlock() { is_catch_block_ = true; }
868
David Brazdil8d5b8b22015-03-24 10:51:52 +0000869 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +0000870 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +0100871 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +0000872 bool HasSinglePhi() const;
873
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000874 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000875 HGraph* graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000876 GrowableArray<HBasicBlock*> predecessors_;
877 GrowableArray<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100878 HInstructionList instructions_;
879 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000880 HLoopInformation* loop_information_;
881 HBasicBlock* dominator_;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100882 GrowableArray<HBasicBlock*> dominated_blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000883 int block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100884 // The dex program counter of the first instruction of this block.
885 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100886 size_t lifetime_start_;
887 size_t lifetime_end_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000888 bool is_catch_block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000889
David Brazdilffee3d32015-07-06 11:48:53 +0100890 // If this block is in a try block, `try_entry_` stores one of, possibly
891 // several, TryBoundary instructions entering it.
892 HTryBoundary* try_entry_;
893
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000894 friend class HGraph;
895 friend class HInstruction;
896
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000897 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
898};
899
David Brazdilb2bd1c52015-03-25 11:17:37 +0000900// Iterates over the LoopInformation of all loops which contain 'block'
901// from the innermost to the outermost.
902class HLoopInformationOutwardIterator : public ValueObject {
903 public:
904 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
905 : current_(block.GetLoopInformation()) {}
906
907 bool Done() const { return current_ == nullptr; }
908
909 void Advance() {
910 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +0100911 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +0000912 }
913
914 HLoopInformation* Current() const {
915 DCHECK(!Done());
916 return current_;
917 }
918
919 private:
920 HLoopInformation* current_;
921
922 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
923};
924
Alexandre Ramesef20f712015-06-09 10:29:30 +0100925#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100926 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000927 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000928 M(ArrayGet, Instruction) \
929 M(ArrayLength, Instruction) \
930 M(ArraySet, Instruction) \
David Brazdil66d126e2015-04-03 16:02:44 +0100931 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000932 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +0000933 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000934 M(CheckCast, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100935 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000936 M(Compare, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100937 M(Condition, BinaryOperation) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100938 M(CurrentMethod, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700939 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000940 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +0000941 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000942 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100943 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000944 M(Exit, Instruction) \
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +0100945 M(FakeString, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000946 M(FloatConstant, Constant) \
947 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100948 M(GreaterThan, Condition) \
949 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100950 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000951 M(InstanceFieldGet, Instruction) \
952 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000953 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100954 M(IntConstant, Constant) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +0000955 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000956 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100957 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000958 M(LessThan, Condition) \
959 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000960 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000961 M(LoadException, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100962 M(LoadLocal, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000963 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100964 M(Local, Instruction) \
965 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +0100966 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +0000967 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000968 M(Mul, BinaryOperation) \
969 M(Neg, UnaryOperation) \
970 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100971 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +0100972 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000973 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000974 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000975 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000976 M(Or, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100977 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000978 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100979 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000980 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100981 M(Return, Instruction) \
982 M(ReturnVoid, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000983 M(Shl, BinaryOperation) \
984 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100985 M(StaticFieldGet, Instruction) \
986 M(StaticFieldSet, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100987 M(StoreLocal, Instruction) \
988 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100989 M(SuspendCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000990 M(Temporary, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000991 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +0000992 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +0000993 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000994 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000995 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000996
Alexandre Ramesef20f712015-06-09 10:29:30 +0100997#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
998
999#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
1000
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001001#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1002
Alexandre Ramesef20f712015-06-09 10:29:30 +01001003#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1004
1005#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1006
1007#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1008 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
1009 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1010 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001011 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001012 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1013 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1014
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001015#define FOR_EACH_INSTRUCTION(M) \
1016 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1017 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001018 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001019 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001020 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -07001021
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001022#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001023FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1024#undef FORWARD_DECLARATION
1025
Roland Levillainccc07a92014-09-16 14:48:16 +01001026#define DECLARE_INSTRUCTION(type) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001027 InstructionKind GetKind() const OVERRIDE { return k##type; } \
1028 const char* DebugName() const OVERRIDE { return #type; } \
1029 const H##type* As##type() const OVERRIDE { return this; } \
1030 H##type* As##type() OVERRIDE { return this; } \
1031 bool InstructionTypeEquals(HInstruction* other) const OVERRIDE { \
Roland Levillainccc07a92014-09-16 14:48:16 +01001032 return other->Is##type(); \
1033 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001034 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001035
David Brazdiled596192015-01-23 10:39:45 +00001036template <typename T> class HUseList;
1037
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001038template <typename T>
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001039class HUseListNode : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001040 public:
David Brazdiled596192015-01-23 10:39:45 +00001041 HUseListNode* GetPrevious() const { return prev_; }
1042 HUseListNode* GetNext() const { return next_; }
1043 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001044 size_t GetIndex() const { return index_; }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001045 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001046
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001047 private:
David Brazdiled596192015-01-23 10:39:45 +00001048 HUseListNode(T user, size_t index)
1049 : user_(user), index_(index), prev_(nullptr), next_(nullptr) {}
1050
1051 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001052 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001053 HUseListNode<T>* prev_;
1054 HUseListNode<T>* next_;
1055
1056 friend class HUseList<T>;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001057
1058 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1059};
1060
David Brazdiled596192015-01-23 10:39:45 +00001061template <typename T>
1062class HUseList : public ValueObject {
1063 public:
1064 HUseList() : first_(nullptr) {}
1065
1066 void Clear() {
1067 first_ = nullptr;
1068 }
1069
1070 // Adds a new entry at the beginning of the use list and returns
1071 // the newly created node.
1072 HUseListNode<T>* AddUse(T user, size_t index, ArenaAllocator* arena) {
David Brazdilea55b932015-01-27 17:12:29 +00001073 HUseListNode<T>* new_node = new (arena) HUseListNode<T>(user, index);
David Brazdiled596192015-01-23 10:39:45 +00001074 if (IsEmpty()) {
1075 first_ = new_node;
1076 } else {
1077 first_->prev_ = new_node;
1078 new_node->next_ = first_;
1079 first_ = new_node;
1080 }
1081 return new_node;
1082 }
1083
1084 HUseListNode<T>* GetFirst() const {
1085 return first_;
1086 }
1087
1088 void Remove(HUseListNode<T>* node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001089 DCHECK(node != nullptr);
1090 DCHECK(Contains(node));
1091
David Brazdiled596192015-01-23 10:39:45 +00001092 if (node->prev_ != nullptr) {
1093 node->prev_->next_ = node->next_;
1094 }
1095 if (node->next_ != nullptr) {
1096 node->next_->prev_ = node->prev_;
1097 }
1098 if (node == first_) {
1099 first_ = node->next_;
1100 }
1101 }
1102
David Brazdil1abb4192015-02-17 18:33:36 +00001103 bool Contains(const HUseListNode<T>* node) const {
1104 if (node == nullptr) {
1105 return false;
1106 }
1107 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1108 if (current == node) {
1109 return true;
1110 }
1111 }
1112 return false;
1113 }
1114
David Brazdiled596192015-01-23 10:39:45 +00001115 bool IsEmpty() const {
1116 return first_ == nullptr;
1117 }
1118
1119 bool HasOnlyOneUse() const {
1120 return first_ != nullptr && first_->next_ == nullptr;
1121 }
1122
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001123 size_t SizeSlow() const {
1124 size_t count = 0;
1125 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1126 ++count;
1127 }
1128 return count;
1129 }
1130
David Brazdiled596192015-01-23 10:39:45 +00001131 private:
1132 HUseListNode<T>* first_;
1133};
1134
1135template<typename T>
1136class HUseIterator : public ValueObject {
1137 public:
1138 explicit HUseIterator(const HUseList<T>& uses) : current_(uses.GetFirst()) {}
1139
1140 bool Done() const { return current_ == nullptr; }
1141
1142 void Advance() {
1143 DCHECK(!Done());
1144 current_ = current_->GetNext();
1145 }
1146
1147 HUseListNode<T>* Current() const {
1148 DCHECK(!Done());
1149 return current_;
1150 }
1151
1152 private:
1153 HUseListNode<T>* current_;
1154
1155 friend class HValue;
1156};
1157
David Brazdil1abb4192015-02-17 18:33:36 +00001158// This class is used by HEnvironment and HInstruction classes to record the
1159// instructions they use and pointers to the corresponding HUseListNodes kept
1160// by the used instructions.
1161template <typename T>
1162class HUserRecord : public ValueObject {
1163 public:
1164 HUserRecord() : instruction_(nullptr), use_node_(nullptr) {}
1165 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), use_node_(nullptr) {}
1166
1167 HUserRecord(const HUserRecord<T>& old_record, HUseListNode<T>* use_node)
1168 : instruction_(old_record.instruction_), use_node_(use_node) {
1169 DCHECK(instruction_ != nullptr);
1170 DCHECK(use_node_ != nullptr);
1171 DCHECK(old_record.use_node_ == nullptr);
1172 }
1173
1174 HInstruction* GetInstruction() const { return instruction_; }
1175 HUseListNode<T>* GetUseNode() const { return use_node_; }
1176
1177 private:
1178 // Instruction used by the user.
1179 HInstruction* instruction_;
1180
1181 // Corresponding entry in the use list kept by 'instruction_'.
1182 HUseListNode<T>* use_node_;
1183};
1184
Aart Bik854a02b2015-07-14 16:07:00 -07001185/**
1186 * Side-effects representation for write/read dependences on fields/arrays.
1187 *
1188 * The dependence analysis uses type disambiguation (e.g. a float field write
1189 * cannot modify the value of an integer field read) and the access type (e.g.
1190 * a reference array write cannot modify the value of a reference field read
1191 * [although it may modify the reference fetch prior to reading the field,
1192 * which is represented by its own write/read dependence]). The analysis
1193 * makes conservative points-to assumptions on reference types (e.g. two same
1194 * typed arrays are assumed to be the same, and any reference read depends
1195 * on any reference read without further regard of its type).
1196 *
1197 * The internal representation uses the following 36-bit flags assignments:
1198 *
1199 * |ARRAY-R |FIELD-R |ARRAY-W |FIELD-W |
1200 * +---------+---------+---------+---------+
1201 * |543210987|654321098|765432109|876543210|
1202 * |DFJISCBZL|DFJISCBZL|DFJISCBZL|DFJISCBZL|
1203 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001204class SideEffects : public ValueObject {
1205 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001206 SideEffects() : flags_(0) {}
1207
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001208 static SideEffects None() {
1209 return SideEffects(0);
1210 }
1211
1212 static SideEffects All() {
Aart Bik854a02b2015-07-14 16:07:00 -07001213 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001214 }
1215
Aart Bik34c3ba92015-07-20 14:08:59 -07001216 static SideEffects AllWrites() {
1217 return SideEffects(kAllWrites);
1218 }
1219
1220 static SideEffects AllReads() {
1221 return SideEffects(kAllReads);
1222 }
1223
1224 static SideEffects FieldWriteOfType(Primitive::Type type, bool is_volatile) {
1225 return is_volatile
1226 ? All()
1227 : SideEffects(TypeFlagWithAlias(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001228 }
1229
Aart Bik854a02b2015-07-14 16:07:00 -07001230 static SideEffects ArrayWriteOfType(Primitive::Type type) {
1231 return SideEffects(TypeFlagWithAlias(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001232 }
1233
Aart Bik34c3ba92015-07-20 14:08:59 -07001234 static SideEffects FieldReadOfType(Primitive::Type type, bool is_volatile) {
1235 return is_volatile
1236 ? All()
1237 : SideEffects(TypeFlagWithAlias(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001238 }
1239
1240 static SideEffects ArrayReadOfType(Primitive::Type type) {
1241 return SideEffects(TypeFlagWithAlias(type, kArrayReadOffset));
1242 }
1243
1244 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001245 SideEffects Union(SideEffects other) const {
1246 return SideEffects(flags_ | other.flags_);
1247 }
1248
Aart Bik854a02b2015-07-14 16:07:00 -07001249 // Returns true if something is written.
1250 bool DoesAnyWrite() const {
1251 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001252 }
1253
Aart Bik854a02b2015-07-14 16:07:00 -07001254 // Returns true if something is read.
1255 bool DoesAnyRead() const {
1256 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001257 }
1258
Aart Bik854a02b2015-07-14 16:07:00 -07001259 // Returns true if nothing is written or read.
1260 bool DoesNothing() const {
1261 return flags_ == 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001262 }
1263
Aart Bik854a02b2015-07-14 16:07:00 -07001264 // Returns true if potentially everything is written and read
1265 // (every type and every kind of access).
1266 bool DoesAll() const {
1267 return flags_ == (kAllWrites | kAllReads);
1268 }
1269
1270 // Returns true if this may read something written by other.
1271 bool MayDependOn(SideEffects other) const {
1272 const uint64_t reads = (flags_ & kAllReads) >> kFieldReadOffset;
1273 return (other.flags_ & reads);
1274 }
1275
1276 // Returns string representation of flags (for debugging only).
1277 // Format: |DFJISCBZL|DFJISCBZL|DFJISCBZL|DFJISCBZL|
1278 std::string ToString() const {
1279 static const char *kDebug = "LZBCSIJFD";
1280 std::string flags = "|";
1281 for (int s = 35; s >= 0; s--) {
1282 const int t = s % kBits;
1283 if ((flags_ >> s) & 1)
1284 flags += kDebug[t];
1285 if (t == 0)
1286 flags += "|";
1287 }
1288 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001289 }
1290
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001291 private:
Aart Bik854a02b2015-07-14 16:07:00 -07001292 static constexpr int kBits = 9;
1293 static constexpr int kFieldWriteOffset = 0 * kBits;
1294 static constexpr int kArrayWriteOffset = 1 * kBits;
1295 static constexpr int kFieldReadOffset = 2 * kBits;
1296 static constexpr int kArrayReadOffset = 3 * kBits;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001297
Aart Bik854a02b2015-07-14 16:07:00 -07001298 static constexpr uint64_t kAllWrites = 0x0003ffff;
1299 static constexpr uint64_t kAllReads = kAllWrites << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001300
Aart Bik854a02b2015-07-14 16:07:00 -07001301 // Work around the fact that HIR aliases I/F and J/D.
1302 // TODO: remove this interceptor once HIR types are clean
1303 static uint64_t TypeFlagWithAlias(Primitive::Type type, int offset) {
1304 switch (type) {
1305 case Primitive::kPrimInt:
1306 case Primitive::kPrimFloat:
1307 return TypeFlag(Primitive::kPrimInt, offset) |
1308 TypeFlag(Primitive::kPrimFloat, offset);
1309 case Primitive::kPrimLong:
1310 case Primitive::kPrimDouble:
1311 return TypeFlag(Primitive::kPrimLong, offset) |
1312 TypeFlag(Primitive::kPrimDouble, offset);
1313 default:
1314 return TypeFlag(type, offset);
1315 }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001316 }
1317
Aart Bik854a02b2015-07-14 16:07:00 -07001318 // Translates type to bit flag.
1319 static uint64_t TypeFlag(Primitive::Type type, int offset) {
1320 CHECK_NE(type, Primitive::kPrimVoid);
1321 const uint64_t one = 1;
1322 const int shift = type; // 0-based consecutive enum
1323 DCHECK_LE(kFieldWriteOffset, shift);
1324 DCHECK_LT(shift, kArrayWriteOffset);
1325 return one << (type + offset);
1326 }
1327
1328 // Private constructor on direct flags value.
1329 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1330
1331 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001332};
1333
David Brazdiled596192015-01-23 10:39:45 +00001334// A HEnvironment object contains the values of virtual registers at a given location.
1335class HEnvironment : public ArenaObject<kArenaAllocMisc> {
1336 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001337 HEnvironment(ArenaAllocator* arena,
1338 size_t number_of_vregs,
1339 const DexFile& dex_file,
1340 uint32_t method_idx,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001341 uint32_t dex_pc,
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001342 InvokeType invoke_type,
1343 HInstruction* holder)
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001344 : vregs_(arena, number_of_vregs),
1345 locations_(arena, number_of_vregs),
1346 parent_(nullptr),
1347 dex_file_(dex_file),
1348 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001349 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001350 invoke_type_(invoke_type),
1351 holder_(holder) {
David Brazdiled596192015-01-23 10:39:45 +00001352 vregs_.SetSize(number_of_vregs);
1353 for (size_t i = 0; i < number_of_vregs; i++) {
David Brazdil1abb4192015-02-17 18:33:36 +00001354 vregs_.Put(i, HUserRecord<HEnvironment*>());
David Brazdiled596192015-01-23 10:39:45 +00001355 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001356
1357 locations_.SetSize(number_of_vregs);
1358 for (size_t i = 0; i < number_of_vregs; ++i) {
1359 locations_.Put(i, Location());
1360 }
1361 }
1362
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001363 HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001364 : HEnvironment(arena,
1365 to_copy.Size(),
1366 to_copy.GetDexFile(),
1367 to_copy.GetMethodIdx(),
1368 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001369 to_copy.GetInvokeType(),
1370 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001371
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001372 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001373 if (parent_ != nullptr) {
1374 parent_->SetAndCopyParentChain(allocator, parent);
1375 } else {
1376 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1377 parent_->CopyFrom(parent);
1378 if (parent->GetParent() != nullptr) {
1379 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1380 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001381 }
David Brazdiled596192015-01-23 10:39:45 +00001382 }
1383
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001384 void CopyFrom(const GrowableArray<HInstruction*>& locals);
1385 void CopyFrom(HEnvironment* environment);
1386
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001387 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1388 // input to the loop phi instead. This is for inserting instructions that
1389 // require an environment (like HDeoptimization) in the loop pre-header.
1390 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001391
1392 void SetRawEnvAt(size_t index, HInstruction* instruction) {
David Brazdil1abb4192015-02-17 18:33:36 +00001393 vregs_.Put(index, HUserRecord<HEnvironment*>(instruction));
David Brazdiled596192015-01-23 10:39:45 +00001394 }
1395
1396 HInstruction* GetInstructionAt(size_t index) const {
David Brazdil1abb4192015-02-17 18:33:36 +00001397 return vregs_.Get(index).GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001398 }
1399
David Brazdil1abb4192015-02-17 18:33:36 +00001400 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001401
1402 size_t Size() const { return vregs_.Size(); }
1403
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001404 HEnvironment* GetParent() const { return parent_; }
1405
1406 void SetLocationAt(size_t index, Location location) {
1407 locations_.Put(index, location);
1408 }
1409
1410 Location GetLocationAt(size_t index) const {
1411 return locations_.Get(index);
1412 }
1413
1414 uint32_t GetDexPc() const {
1415 return dex_pc_;
1416 }
1417
1418 uint32_t GetMethodIdx() const {
1419 return method_idx_;
1420 }
1421
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001422 InvokeType GetInvokeType() const {
1423 return invoke_type_;
1424 }
1425
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001426 const DexFile& GetDexFile() const {
1427 return dex_file_;
1428 }
1429
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001430 HInstruction* GetHolder() const {
1431 return holder_;
1432 }
1433
David Brazdiled596192015-01-23 10:39:45 +00001434 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001435 // Record instructions' use entries of this environment for constant-time removal.
1436 // It should only be called by HInstruction when a new environment use is added.
1437 void RecordEnvUse(HUseListNode<HEnvironment*>* env_use) {
1438 DCHECK(env_use->GetUser() == this);
1439 size_t index = env_use->GetIndex();
1440 vregs_.Put(index, HUserRecord<HEnvironment*>(vregs_.Get(index), env_use));
1441 }
David Brazdiled596192015-01-23 10:39:45 +00001442
David Brazdil1abb4192015-02-17 18:33:36 +00001443 GrowableArray<HUserRecord<HEnvironment*> > vregs_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001444 GrowableArray<Location> locations_;
1445 HEnvironment* parent_;
1446 const DexFile& dex_file_;
1447 const uint32_t method_idx_;
1448 const uint32_t dex_pc_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001449 const InvokeType invoke_type_;
David Brazdiled596192015-01-23 10:39:45 +00001450
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001451 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001452 HInstruction* const holder_;
1453
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001454 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001455
1456 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1457};
1458
Calin Juravleacf735c2015-02-12 15:25:22 +00001459class ReferenceTypeInfo : ValueObject {
1460 public:
Calin Juravleb1498f62015-02-16 13:13:29 +00001461 typedef Handle<mirror::Class> TypeHandle;
1462
Calin Juravle80caa142015-07-16 16:51:30 +01001463 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact) {
1464 // The constructor will check that the type_handle is valid.
1465 return ReferenceTypeInfo(type_handle, is_exact);
Calin Juravleb1498f62015-02-16 13:13:29 +00001466 }
1467
Calin Juravle80caa142015-07-16 16:51:30 +01001468 static ReferenceTypeInfo CreateInvalid() { return ReferenceTypeInfo(); }
1469
1470 static bool IsValidHandle(TypeHandle handle) SHARED_REQUIRES(Locks::mutator_lock_) {
1471 return handle.GetReference() != nullptr;
Calin Juravleacf735c2015-02-12 15:25:22 +00001472 }
1473
Calin Juravle80caa142015-07-16 16:51:30 +01001474 bool IsValid() const SHARED_REQUIRES(Locks::mutator_lock_) {
1475 return IsValidHandle(type_handle_);
1476 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001477 bool IsExact() const { return is_exact_; }
Calin Juravle80caa142015-07-16 16:51:30 +01001478
1479 bool IsObjectClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
1480 DCHECK(IsValid());
1481 return GetTypeHandle()->IsObjectClass();
1482 }
Mathieu Chartier90443472015-07-16 20:32:27 -07001483 bool IsInterface() const SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle80caa142015-07-16 16:51:30 +01001484 DCHECK(IsValid());
1485 return GetTypeHandle()->IsInterface();
Guillaume Sanchez222862c2015-06-09 18:33:02 +01001486 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001487
1488 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
1489
Mathieu Chartier90443472015-07-16 20:32:27 -07001490 bool IsSupertypeOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle80caa142015-07-16 16:51:30 +01001491 DCHECK(IsValid());
1492 DCHECK(rti.IsValid());
Calin Juravleacf735c2015-02-12 15:25:22 +00001493 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
1494 }
1495
1496 // Returns true if the type information provide the same amount of details.
1497 // Note that it does not mean that the instructions have the same actual type
Calin Juravle80caa142015-07-16 16:51:30 +01001498 // (because the type can be the result of a merge).
Mathieu Chartier90443472015-07-16 20:32:27 -07001499 bool IsEqual(ReferenceTypeInfo rti) SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle80caa142015-07-16 16:51:30 +01001500 if (!IsValid() && !rti.IsValid()) {
1501 // Invalid types are equal.
Calin Juravle7733bd62015-07-22 17:14:50 +00001502 return true;
1503 }
Calin Juravle80caa142015-07-16 16:51:30 +01001504 if (!IsValid() || !rti.IsValid()) {
1505 // One is valid, the other not.
Calin Juravle7733bd62015-07-22 17:14:50 +00001506 return false;
1507 }
Calin Juravle80caa142015-07-16 16:51:30 +01001508 return IsExact() == rti.IsExact()
1509 && GetTypeHandle().Get() == rti.GetTypeHandle().Get();
Calin Juravleacf735c2015-02-12 15:25:22 +00001510 }
1511
1512 private:
Calin Juravle80caa142015-07-16 16:51:30 +01001513 ReferenceTypeInfo();
1514 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact);
Calin Juravleb1498f62015-02-16 13:13:29 +00001515
Calin Juravleacf735c2015-02-12 15:25:22 +00001516 // The class of the object.
Calin Juravleb1498f62015-02-16 13:13:29 +00001517 TypeHandle type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001518 // Whether or not the type is exact or a superclass of the actual type.
Calin Juravleb1498f62015-02-16 13:13:29 +00001519 // Whether or not we have any information about this type.
Calin Juravleacf735c2015-02-12 15:25:22 +00001520 bool is_exact_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001521};
1522
1523std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
1524
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001525class HInstruction : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001526 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001527 explicit HInstruction(SideEffects side_effects)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001528 : previous_(nullptr),
1529 next_(nullptr),
1530 block_(nullptr),
1531 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001532 ssa_index_(-1),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001533 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001534 locations_(nullptr),
1535 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001536 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001537 side_effects_(side_effects),
Calin Juravle80caa142015-07-16 16:51:30 +01001538 reference_type_info_(ReferenceTypeInfo::CreateInvalid()) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001539
Dave Allison20dfc792014-06-16 20:44:29 -07001540 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001541
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001542#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001543 enum InstructionKind {
1544 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1545 };
1546#undef DECLARE_KIND
1547
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001548 HInstruction* GetNext() const { return next_; }
1549 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001550
Calin Juravle77520bc2015-01-12 18:45:46 +00001551 HInstruction* GetNextDisregardingMoves() const;
1552 HInstruction* GetPreviousDisregardingMoves() const;
1553
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001554 HBasicBlock* GetBlock() const { return block_; }
1555 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001556 bool IsInBlock() const { return block_ != nullptr; }
1557 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +01001558 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001559
Roland Levillain6b879dd2014-09-22 17:13:44 +01001560 virtual size_t InputCount() const = 0;
David Brazdil1abb4192015-02-17 18:33:36 +00001561 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001562
1563 virtual void Accept(HGraphVisitor* visitor) = 0;
1564 virtual const char* DebugName() const = 0;
1565
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001566 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
David Brazdil1abb4192015-02-17 18:33:36 +00001567 void SetRawInputAt(size_t index, HInstruction* input) {
1568 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1569 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001570
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001571 virtual bool NeedsEnvironment() const { return false; }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001572 virtual uint32_t GetDexPc() const {
1573 LOG(FATAL) << "GetDexPc() cannot be called on an instruction that"
1574 " does not need an environment";
1575 UNREACHABLE();
1576 }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001577 virtual bool IsControlFlow() const { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01001578 virtual bool CanThrow() const { return false; }
Aart Bik854a02b2015-07-14 16:07:00 -07001579
1580 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001581
Calin Juravle10e244f2015-01-26 18:54:32 +00001582 // Does not apply for all instructions, but having this at top level greatly
1583 // simplifies the null check elimination.
Calin Juravle00e3b382015-07-15 14:41:29 +01001584 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001585 virtual bool CanBeNull() const {
1586 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1587 return true;
1588 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001589
Calin Juravle641547a2015-04-21 22:08:51 +01001590 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj) const {
1591 UNUSED(obj);
1592 return false;
1593 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001594
Calin Juravle80caa142015-07-16 16:51:30 +01001595 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00001596
Calin Juravle61d544b2015-02-23 16:46:57 +00001597 ReferenceTypeInfo GetReferenceTypeInfo() const {
1598 DCHECK_EQ(GetType(), Primitive::kPrimNot);
1599 return reference_type_info_;
1600 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001601
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001602 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001603 DCHECK(user != nullptr);
1604 HUseListNode<HInstruction*>* use =
1605 uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1606 user->SetRawInputRecordAt(index, HUserRecord<HInstruction*>(user->InputRecordAt(index), use));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001607 }
1608
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001609 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001610 DCHECK(user != nullptr);
David Brazdiled596192015-01-23 10:39:45 +00001611 HUseListNode<HEnvironment*>* env_use =
1612 env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1613 user->RecordEnvUse(env_use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001614 }
1615
David Brazdil1abb4192015-02-17 18:33:36 +00001616 void RemoveAsUserOfInput(size_t input) {
1617 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
1618 input_use.GetInstruction()->uses_.Remove(input_use.GetUseNode());
1619 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001620
David Brazdil1abb4192015-02-17 18:33:36 +00001621 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1622 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001623
David Brazdiled596192015-01-23 10:39:45 +00001624 bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); }
1625 bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); }
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001626 bool HasNonEnvironmentUses() const { return !uses_.IsEmpty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001627 bool HasOnlyOneNonEnvironmentUse() const {
1628 return !HasEnvironmentUses() && GetUses().HasOnlyOneUse();
1629 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001630
Roland Levillain6c82d402014-10-13 16:10:27 +01001631 // Does this instruction strictly dominate `other_instruction`?
1632 // Returns false if this instruction and `other_instruction` are the same.
1633 // Aborts if this instruction and `other_instruction` are both phis.
1634 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001635
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001636 int GetId() const { return id_; }
1637 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001638
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001639 int GetSsaIndex() const { return ssa_index_; }
1640 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1641 bool HasSsaIndex() const { return ssa_index_ != -1; }
1642
1643 bool HasEnvironment() const { return environment_ != nullptr; }
1644 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001645 // Set the `environment_` field. Raw because this method does not
1646 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001647 void SetRawEnvironment(HEnvironment* environment) {
1648 DCHECK(environment_ == nullptr);
1649 DCHECK_EQ(environment->GetHolder(), this);
1650 environment_ = environment;
1651 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001652
1653 // Set the environment of this instruction, copying it from `environment`. While
1654 // copying, the uses lists are being updated.
1655 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001656 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001657 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001658 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001659 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001660 if (environment->GetParent() != nullptr) {
1661 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1662 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001663 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001664
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001665 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
1666 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001667 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001668 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001669 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001670 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001671 if (environment->GetParent() != nullptr) {
1672 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1673 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001674 }
1675
Nicolas Geoffray39468442014-09-02 15:17:15 +01001676 // Returns the number of entries in the environment. Typically, that is the
1677 // number of dex registers in a method. It could be more in case of inlining.
1678 size_t EnvironmentSize() const;
1679
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001680 LocationSummary* GetLocations() const { return locations_; }
1681 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001682
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001683 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001684 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001685
Alexandre Rames188d4312015-04-09 18:30:21 +01001686 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
1687 // uses of this instruction by `other` are *not* updated.
1688 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
1689 ReplaceWith(other);
1690 other->ReplaceInput(this, use_index);
1691 }
1692
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001693 // Move `this` instruction before `cursor`.
1694 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001695
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001696#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01001697 bool Is##type() const { return (As##type() != nullptr); } \
1698 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001699 virtual H##type* As##type() { return nullptr; }
1700
1701 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
1702#undef INSTRUCTION_TYPE_CHECK
1703
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001704 // Returns whether the instruction can be moved within the graph.
1705 virtual bool CanBeMoved() const { return false; }
1706
1707 // Returns whether the two instructions are of the same kind.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001708 virtual bool InstructionTypeEquals(HInstruction* other) const {
1709 UNUSED(other);
1710 return false;
1711 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001712
1713 // Returns whether any data encoded in the two instructions is equal.
1714 // This method does not look at the inputs. Both instructions must be
1715 // of the same type, otherwise the method has undefined behavior.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001716 virtual bool InstructionDataEquals(HInstruction* other) const {
1717 UNUSED(other);
1718 return false;
1719 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001720
1721 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00001722 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001723 // 2) Their inputs are identical.
1724 bool Equals(HInstruction* other) const;
1725
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001726 virtual InstructionKind GetKind() const = 0;
1727
1728 virtual size_t ComputeHashCode() const {
1729 size_t result = GetKind();
1730 for (size_t i = 0, e = InputCount(); i < e; ++i) {
1731 result = (result * 31) + InputAt(i)->GetId();
1732 }
1733 return result;
1734 }
1735
1736 SideEffects GetSideEffects() const { return side_effects_; }
1737
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001738 size_t GetLifetimePosition() const { return lifetime_position_; }
1739 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
1740 LiveInterval* GetLiveInterval() const { return live_interval_; }
1741 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
1742 bool HasLiveInterval() const { return live_interval_ != nullptr; }
1743
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001744 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
1745
1746 // Returns whether the code generation of the instruction will require to have access
1747 // to the current method. Such instructions are:
1748 // (1): Instructions that require an environment, as calling the runtime requires
1749 // to walk the stack and have the current method stored at a specific stack address.
1750 // (2): Object literals like classes and strings, that are loaded from the dex cache
1751 // fields of the current method.
1752 bool NeedsCurrentMethod() const {
1753 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
1754 }
1755
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001756 virtual bool NeedsDexCache() const { return false; }
1757
Mark Mendellc4701932015-04-10 13:18:51 -04001758 // Does this instruction have any use in an environment before
1759 // control flow hits 'other'?
1760 bool HasAnyEnvironmentUseBefore(HInstruction* other);
1761
1762 // Remove all references to environment uses of this instruction.
1763 // The caller must ensure that this is safe to do.
1764 void RemoveEnvironmentUsers();
1765
David Brazdil1abb4192015-02-17 18:33:36 +00001766 protected:
1767 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
1768 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
1769
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001770 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001771 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); }
1772
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001773 HInstruction* previous_;
1774 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001775 HBasicBlock* block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001776
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001777 // An instruction gets an id when it is added to the graph.
1778 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01001779 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001780 int id_;
1781
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001782 // When doing liveness analysis, instructions that have uses get an SSA index.
1783 int ssa_index_;
1784
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001785 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00001786 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001787
1788 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00001789 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001790
Nicolas Geoffray39468442014-09-02 15:17:15 +01001791 // The environment associated with this instruction. Not null if the instruction
1792 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001793 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001794
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001795 // Set by the code generator.
1796 LocationSummary* locations_;
1797
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001798 // Set by the liveness analysis.
1799 LiveInterval* live_interval_;
1800
1801 // Set by the liveness analysis, this is the position in a linear
1802 // order of blocks where this instruction's live interval start.
1803 size_t lifetime_position_;
1804
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001805 const SideEffects side_effects_;
1806
Calin Juravleacf735c2015-02-12 15:25:22 +00001807 // TODO: for primitive types this should be marked as invalid.
1808 ReferenceTypeInfo reference_type_info_;
1809
David Brazdil1abb4192015-02-17 18:33:36 +00001810 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001811 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00001812 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001813 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001814 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001815
1816 DISALLOW_COPY_AND_ASSIGN(HInstruction);
1817};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001818std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001819
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001820class HInputIterator : public ValueObject {
1821 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001822 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001823
1824 bool Done() const { return index_ == instruction_->InputCount(); }
1825 HInstruction* Current() const { return instruction_->InputAt(index_); }
1826 void Advance() { index_++; }
1827
1828 private:
1829 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001830 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001831
1832 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
1833};
1834
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001835class HInstructionIterator : public ValueObject {
1836 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001837 explicit HInstructionIterator(const HInstructionList& instructions)
1838 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001839 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001840 }
1841
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001842 bool Done() const { return instruction_ == nullptr; }
1843 HInstruction* Current() const { return instruction_; }
1844 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001845 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001846 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001847 }
1848
1849 private:
1850 HInstruction* instruction_;
1851 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001852
1853 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001854};
1855
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001856class HBackwardInstructionIterator : public ValueObject {
1857 public:
1858 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
1859 : instruction_(instructions.last_instruction_) {
1860 next_ = Done() ? nullptr : instruction_->GetPrevious();
1861 }
1862
1863 bool Done() const { return instruction_ == nullptr; }
1864 HInstruction* Current() const { return instruction_; }
1865 void Advance() {
1866 instruction_ = next_;
1867 next_ = Done() ? nullptr : instruction_->GetPrevious();
1868 }
1869
1870 private:
1871 HInstruction* instruction_;
1872 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001873
1874 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001875};
1876
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001877// An embedded container with N elements of type T. Used (with partial
1878// specialization for N=0) because embedded arrays cannot have size 0.
1879template<typename T, intptr_t N>
1880class EmbeddedArray {
1881 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001882 EmbeddedArray() : elements_() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001883
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001884 intptr_t GetLength() const { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001885
1886 const T& operator[](intptr_t i) const {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001887 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001888 return elements_[i];
1889 }
1890
1891 T& operator[](intptr_t i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001892 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001893 return elements_[i];
1894 }
1895
1896 const T& At(intptr_t i) const {
1897 return (*this)[i];
1898 }
1899
1900 void SetAt(intptr_t i, const T& val) {
1901 (*this)[i] = val;
1902 }
1903
1904 private:
1905 T elements_[N];
1906};
1907
1908template<typename T>
1909class EmbeddedArray<T, 0> {
1910 public:
1911 intptr_t length() const { return 0; }
1912 const T& operator[](intptr_t i) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001913 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001914 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001915 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001916 }
1917 T& operator[](intptr_t i) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001918 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001919 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001920 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001921 }
1922};
1923
1924template<intptr_t N>
1925class HTemplateInstruction: public HInstruction {
1926 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001927 HTemplateInstruction<N>(SideEffects side_effects)
1928 : HInstruction(side_effects), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07001929 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001930
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001931 size_t InputCount() const OVERRIDE { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001932
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001933 protected:
David Brazdil1abb4192015-02-17 18:33:36 +00001934 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_[i]; }
1935
1936 void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE {
1937 inputs_[i] = input;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001938 }
1939
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001940 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001941 EmbeddedArray<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001942
1943 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001944};
1945
Dave Allison20dfc792014-06-16 20:44:29 -07001946template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001947class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07001948 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001949 HExpression<N>(Primitive::Type type, SideEffects side_effects)
1950 : HTemplateInstruction<N>(side_effects), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07001951 virtual ~HExpression() {}
1952
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001953 Primitive::Type GetType() const OVERRIDE { return type_; }
Dave Allison20dfc792014-06-16 20:44:29 -07001954
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001955 protected:
1956 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07001957};
1958
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001959// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
1960// instruction that branches to the exit block.
1961class HReturnVoid : public HTemplateInstruction<0> {
1962 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001963 HReturnVoid() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001964
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001965 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001966
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001967 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001968
1969 private:
1970 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
1971};
1972
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001973// Represents dex's RETURN opcodes. A HReturn is a control flow
1974// instruction that branches to the exit block.
1975class HReturn : public HTemplateInstruction<1> {
1976 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001977 explicit HReturn(HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001978 SetRawInputAt(0, value);
1979 }
1980
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001981 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001982
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001983 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001984
1985 private:
1986 DISALLOW_COPY_AND_ASSIGN(HReturn);
1987};
1988
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001989// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001990// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001991// exit block.
1992class HExit : public HTemplateInstruction<0> {
1993 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001994 HExit() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001995
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001996 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001997
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001998 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001999
2000 private:
2001 DISALLOW_COPY_AND_ASSIGN(HExit);
2002};
2003
2004// Jumps from one block to another.
2005class HGoto : public HTemplateInstruction<0> {
2006 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002007 HGoto() : HTemplateInstruction(SideEffects::None()) {}
2008
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002009 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002010
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002011 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002012 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002013 }
2014
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002015 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002016
2017 private:
2018 DISALLOW_COPY_AND_ASSIGN(HGoto);
2019};
2020
Dave Allison20dfc792014-06-16 20:44:29 -07002021
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002022// Conditional branch. A block ending with an HIf instruction must have
2023// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002024class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002025 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002026 explicit HIf(HInstruction* input) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002027 SetRawInputAt(0, input);
2028 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002029
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002030 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002031
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002032 HBasicBlock* IfTrueSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002033 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002034 }
2035
2036 HBasicBlock* IfFalseSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002037 return GetBlock()->GetSuccessors().Get(1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002038 }
2039
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002040 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002041
2042 private:
2043 DISALLOW_COPY_AND_ASSIGN(HIf);
2044};
2045
David Brazdilfc6a86a2015-06-26 10:33:45 +00002046
2047// Abstract instruction which marks the beginning and/or end of a try block and
2048// links it to the respective exception handlers. Behaves the same as a Goto in
2049// non-exceptional control flow.
2050// Normal-flow successor is stored at index zero, exception handlers under
2051// higher indices in no particular order.
2052class HTryBoundary : public HTemplateInstruction<0> {
2053 public:
David Brazdil56e1acc2015-06-30 15:41:36 +01002054 enum BoundaryKind {
2055 kEntry,
2056 kExit,
2057 };
2058
2059 explicit HTryBoundary(BoundaryKind kind)
2060 : HTemplateInstruction(SideEffects::None()), kind_(kind) {}
David Brazdilfc6a86a2015-06-26 10:33:45 +00002061
2062 bool IsControlFlow() const OVERRIDE { return true; }
2063
2064 // Returns the block's non-exceptional successor (index zero).
2065 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors().Get(0); }
2066
2067 // Returns whether `handler` is among its exception handlers (non-zero index
2068 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002069 bool HasExceptionHandler(const HBasicBlock& handler) const {
2070 DCHECK(handler.IsCatchBlock());
2071 return GetBlock()->GetSuccessors().Contains(
2072 const_cast<HBasicBlock*>(&handler), /* start_from */ 1);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002073 }
2074
2075 // If not present already, adds `handler` to its block's list of exception
2076 // handlers.
2077 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01002078 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002079 GetBlock()->AddSuccessor(handler);
2080 }
2081 }
2082
David Brazdil56e1acc2015-06-30 15:41:36 +01002083 bool IsEntry() const { return kind_ == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002084
David Brazdilffee3d32015-07-06 11:48:53 +01002085 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
2086
David Brazdilfc6a86a2015-06-26 10:33:45 +00002087 DECLARE_INSTRUCTION(TryBoundary);
2088
2089 private:
David Brazdil56e1acc2015-06-30 15:41:36 +01002090 const BoundaryKind kind_;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002091
2092 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2093};
2094
David Brazdilffee3d32015-07-06 11:48:53 +01002095// Iterator over exception handlers of a given HTryBoundary, i.e. over
2096// exceptional successors of its basic block.
2097class HExceptionHandlerIterator : public ValueObject {
2098 public:
2099 explicit HExceptionHandlerIterator(const HTryBoundary& try_boundary)
2100 : block_(*try_boundary.GetBlock()), index_(block_.NumberOfNormalSuccessors()) {}
2101
2102 bool Done() const { return index_ == block_.GetSuccessors().Size(); }
2103 HBasicBlock* Current() const { return block_.GetSuccessors().Get(index_); }
2104 size_t CurrentSuccessorIndex() const { return index_; }
2105 void Advance() { ++index_; }
2106
2107 private:
2108 const HBasicBlock& block_;
2109 size_t index_;
2110
2111 DISALLOW_COPY_AND_ASSIGN(HExceptionHandlerIterator);
2112};
David Brazdilfc6a86a2015-06-26 10:33:45 +00002113
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002114// Deoptimize to interpreter, upon checking a condition.
2115class HDeoptimize : public HTemplateInstruction<1> {
2116 public:
2117 HDeoptimize(HInstruction* cond, uint32_t dex_pc)
2118 : HTemplateInstruction(SideEffects::None()),
2119 dex_pc_(dex_pc) {
2120 SetRawInputAt(0, cond);
2121 }
2122
2123 bool NeedsEnvironment() const OVERRIDE { return true; }
2124 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002125 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002126
2127 DECLARE_INSTRUCTION(Deoptimize);
2128
2129 private:
2130 uint32_t dex_pc_;
2131
2132 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
2133};
2134
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002135// Represents the ArtMethod that was passed as a first argument to
2136// the method. It is used by instructions that depend on it, like
2137// instructions that work with the dex cache.
2138class HCurrentMethod : public HExpression<0> {
2139 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -07002140 explicit HCurrentMethod(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002141
2142 DECLARE_INSTRUCTION(CurrentMethod);
2143
2144 private:
2145 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
2146};
2147
Roland Levillain88cb1752014-10-20 16:36:47 +01002148class HUnaryOperation : public HExpression<1> {
2149 public:
2150 HUnaryOperation(Primitive::Type result_type, HInstruction* input)
2151 : HExpression(result_type, SideEffects::None()) {
2152 SetRawInputAt(0, input);
2153 }
2154
2155 HInstruction* GetInput() const { return InputAt(0); }
2156 Primitive::Type GetResultType() const { return GetType(); }
2157
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002158 bool CanBeMoved() const OVERRIDE { return true; }
2159 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002160 UNUSED(other);
2161 return true;
2162 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002163
Roland Levillain9240d6a2014-10-20 16:47:04 +01002164 // Try to statically evaluate `operation` and return a HConstant
2165 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002166 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002167 HConstant* TryStaticEvaluation() const;
2168
2169 // Apply this operation to `x`.
2170 virtual int32_t Evaluate(int32_t x) const = 0;
2171 virtual int64_t Evaluate(int64_t x) const = 0;
2172
Roland Levillain88cb1752014-10-20 16:36:47 +01002173 DECLARE_INSTRUCTION(UnaryOperation);
2174
2175 private:
2176 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
2177};
2178
Dave Allison20dfc792014-06-16 20:44:29 -07002179class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002180 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002181 HBinaryOperation(Primitive::Type result_type,
2182 HInstruction* left,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002183 HInstruction* right) : HExpression(result_type, SideEffects::None()) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002184 SetRawInputAt(0, left);
2185 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002186 }
2187
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002188 HInstruction* GetLeft() const { return InputAt(0); }
2189 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07002190 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002191
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002192 virtual bool IsCommutative() const { return false; }
2193
2194 // Put constant on the right.
2195 // Returns whether order is changed.
2196 bool OrderInputsWithConstantOnTheRight() {
2197 HInstruction* left = InputAt(0);
2198 HInstruction* right = InputAt(1);
2199 if (left->IsConstant() && !right->IsConstant()) {
2200 ReplaceInput(right, 0);
2201 ReplaceInput(left, 1);
2202 return true;
2203 }
2204 return false;
2205 }
2206
2207 // Order inputs by instruction id, but favor constant on the right side.
2208 // This helps GVN for commutative ops.
2209 void OrderInputs() {
2210 DCHECK(IsCommutative());
2211 HInstruction* left = InputAt(0);
2212 HInstruction* right = InputAt(1);
2213 if (left == right || (!left->IsConstant() && right->IsConstant())) {
2214 return;
2215 }
2216 if (OrderInputsWithConstantOnTheRight()) {
2217 return;
2218 }
2219 // Order according to instruction id.
2220 if (left->GetId() > right->GetId()) {
2221 ReplaceInput(right, 0);
2222 ReplaceInput(left, 1);
2223 }
2224 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002225
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002226 bool CanBeMoved() const OVERRIDE { return true; }
2227 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002228 UNUSED(other);
2229 return true;
2230 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002231
Roland Levillain9240d6a2014-10-20 16:47:04 +01002232 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01002233 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002234 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002235 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01002236
2237 // Apply this operation to `x` and `y`.
2238 virtual int32_t Evaluate(int32_t x, int32_t y) const = 0;
2239 virtual int64_t Evaluate(int64_t x, int64_t y) const = 0;
2240
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002241 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002242 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002243 HConstant* GetConstantRight() const;
2244
2245 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002246 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002247 HInstruction* GetLeastConstantLeft() const;
2248
Roland Levillainccc07a92014-09-16 14:48:16 +01002249 DECLARE_INSTRUCTION(BinaryOperation);
2250
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002251 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002252 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
2253};
2254
Mark Mendellc4701932015-04-10 13:18:51 -04002255// The comparison bias applies for floating point operations and indicates how NaN
2256// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01002257enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04002258 kNoBias, // bias is not applicable (i.e. for long operation)
2259 kGtBias, // return 1 for NaN comparisons
2260 kLtBias, // return -1 for NaN comparisons
2261};
2262
Dave Allison20dfc792014-06-16 20:44:29 -07002263class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002264 public:
Dave Allison20dfc792014-06-16 20:44:29 -07002265 HCondition(HInstruction* first, HInstruction* second)
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002266 : HBinaryOperation(Primitive::kPrimBoolean, first, second),
Mark Mendellc4701932015-04-10 13:18:51 -04002267 needs_materialization_(true),
Roland Levillain4fa13f62015-07-06 18:11:54 +01002268 bias_(ComparisonBias::kNoBias) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002269
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002270 bool NeedsMaterialization() const { return needs_materialization_; }
2271 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002272
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002273 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002274 // `instruction`, and disregard moves in between.
2275 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002276
Dave Allison20dfc792014-06-16 20:44:29 -07002277 DECLARE_INSTRUCTION(Condition);
2278
2279 virtual IfCondition GetCondition() const = 0;
2280
Mark Mendellc4701932015-04-10 13:18:51 -04002281 virtual IfCondition GetOppositeCondition() const = 0;
2282
Roland Levillain4fa13f62015-07-06 18:11:54 +01002283 bool IsGtBias() const { return bias_ == ComparisonBias::kGtBias; }
Mark Mendellc4701932015-04-10 13:18:51 -04002284
2285 void SetBias(ComparisonBias bias) { bias_ = bias; }
2286
2287 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2288 return bias_ == other->AsCondition()->bias_;
2289 }
2290
Roland Levillain4fa13f62015-07-06 18:11:54 +01002291 bool IsFPConditionTrueIfNaN() const {
2292 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2293 IfCondition if_cond = GetCondition();
2294 return IsGtBias() ? ((if_cond == kCondGT) || (if_cond == kCondGE)) : (if_cond == kCondNE);
2295 }
2296
2297 bool IsFPConditionFalseIfNaN() const {
2298 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2299 IfCondition if_cond = GetCondition();
2300 return IsGtBias() ? ((if_cond == kCondLT) || (if_cond == kCondLE)) : (if_cond == kCondEQ);
2301 }
2302
Dave Allison20dfc792014-06-16 20:44:29 -07002303 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002304 // For register allocation purposes, returns whether this instruction needs to be
2305 // materialized (that is, not just be in the processor flags).
2306 bool needs_materialization_;
2307
Mark Mendellc4701932015-04-10 13:18:51 -04002308 // Needed if we merge a HCompare into a HCondition.
2309 ComparisonBias bias_;
2310
Dave Allison20dfc792014-06-16 20:44:29 -07002311 DISALLOW_COPY_AND_ASSIGN(HCondition);
2312};
2313
2314// Instruction to check if two inputs are equal to each other.
2315class HEqual : public HCondition {
2316 public:
2317 HEqual(HInstruction* first, HInstruction* second)
2318 : HCondition(first, second) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002319
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002320 bool IsCommutative() const OVERRIDE { return true; }
2321
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002322 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002323 return x == y ? 1 : 0;
2324 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002325 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002326 return x == y ? 1 : 0;
2327 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002328
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002329 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002330
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002331 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002332 return kCondEQ;
2333 }
2334
Mark Mendellc4701932015-04-10 13:18:51 -04002335 IfCondition GetOppositeCondition() const OVERRIDE {
2336 return kCondNE;
2337 }
2338
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002339 private:
2340 DISALLOW_COPY_AND_ASSIGN(HEqual);
2341};
2342
Dave Allison20dfc792014-06-16 20:44:29 -07002343class HNotEqual : public HCondition {
2344 public:
2345 HNotEqual(HInstruction* first, HInstruction* second)
2346 : HCondition(first, second) {}
2347
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002348 bool IsCommutative() const OVERRIDE { return true; }
2349
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002350 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002351 return x != y ? 1 : 0;
2352 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002353 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002354 return x != y ? 1 : 0;
2355 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002356
Dave Allison20dfc792014-06-16 20:44:29 -07002357 DECLARE_INSTRUCTION(NotEqual);
2358
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002359 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002360 return kCondNE;
2361 }
2362
Mark Mendellc4701932015-04-10 13:18:51 -04002363 IfCondition GetOppositeCondition() const OVERRIDE {
2364 return kCondEQ;
2365 }
2366
Dave Allison20dfc792014-06-16 20:44:29 -07002367 private:
2368 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
2369};
2370
2371class HLessThan : public HCondition {
2372 public:
2373 HLessThan(HInstruction* first, HInstruction* second)
2374 : HCondition(first, second) {}
2375
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002376 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002377 return x < y ? 1 : 0;
2378 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002379 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002380 return x < y ? 1 : 0;
2381 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002382
Dave Allison20dfc792014-06-16 20:44:29 -07002383 DECLARE_INSTRUCTION(LessThan);
2384
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002385 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002386 return kCondLT;
2387 }
2388
Mark Mendellc4701932015-04-10 13:18:51 -04002389 IfCondition GetOppositeCondition() const OVERRIDE {
2390 return kCondGE;
2391 }
2392
Dave Allison20dfc792014-06-16 20:44:29 -07002393 private:
2394 DISALLOW_COPY_AND_ASSIGN(HLessThan);
2395};
2396
2397class HLessThanOrEqual : public HCondition {
2398 public:
2399 HLessThanOrEqual(HInstruction* first, HInstruction* second)
2400 : HCondition(first, second) {}
2401
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002402 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002403 return x <= y ? 1 : 0;
2404 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002405 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002406 return x <= y ? 1 : 0;
2407 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002408
Dave Allison20dfc792014-06-16 20:44:29 -07002409 DECLARE_INSTRUCTION(LessThanOrEqual);
2410
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002411 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002412 return kCondLE;
2413 }
2414
Mark Mendellc4701932015-04-10 13:18:51 -04002415 IfCondition GetOppositeCondition() const OVERRIDE {
2416 return kCondGT;
2417 }
2418
Dave Allison20dfc792014-06-16 20:44:29 -07002419 private:
2420 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
2421};
2422
2423class HGreaterThan : public HCondition {
2424 public:
2425 HGreaterThan(HInstruction* first, HInstruction* second)
2426 : HCondition(first, second) {}
2427
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002428 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002429 return x > y ? 1 : 0;
2430 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002431 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002432 return x > y ? 1 : 0;
2433 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002434
Dave Allison20dfc792014-06-16 20:44:29 -07002435 DECLARE_INSTRUCTION(GreaterThan);
2436
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002437 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002438 return kCondGT;
2439 }
2440
Mark Mendellc4701932015-04-10 13:18:51 -04002441 IfCondition GetOppositeCondition() const OVERRIDE {
2442 return kCondLE;
2443 }
2444
Dave Allison20dfc792014-06-16 20:44:29 -07002445 private:
2446 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
2447};
2448
2449class HGreaterThanOrEqual : public HCondition {
2450 public:
2451 HGreaterThanOrEqual(HInstruction* first, HInstruction* second)
2452 : HCondition(first, second) {}
2453
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002454 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002455 return x >= y ? 1 : 0;
2456 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002457 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002458 return x >= y ? 1 : 0;
2459 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002460
Dave Allison20dfc792014-06-16 20:44:29 -07002461 DECLARE_INSTRUCTION(GreaterThanOrEqual);
2462
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002463 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002464 return kCondGE;
2465 }
2466
Mark Mendellc4701932015-04-10 13:18:51 -04002467 IfCondition GetOppositeCondition() const OVERRIDE {
2468 return kCondLT;
2469 }
2470
Dave Allison20dfc792014-06-16 20:44:29 -07002471 private:
2472 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
2473};
2474
2475
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002476// Instruction to check how two inputs compare to each other.
2477// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
2478class HCompare : public HBinaryOperation {
2479 public:
Alexey Frunze4dda3372015-06-01 18:31:49 -07002480 HCompare(Primitive::Type type,
2481 HInstruction* first,
2482 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04002483 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07002484 uint32_t dex_pc)
2485 : HBinaryOperation(Primitive::kPrimInt, first, second), bias_(bias), dex_pc_(dex_pc) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002486 DCHECK_EQ(type, first->GetType());
2487 DCHECK_EQ(type, second->GetType());
2488 }
2489
Calin Juravleddb7df22014-11-25 20:56:51 +00002490 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01002491 return
2492 x == y ? 0 :
2493 x > y ? 1 :
2494 -1;
2495 }
Calin Juravleddb7df22014-11-25 20:56:51 +00002496
2497 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01002498 return
2499 x == y ? 0 :
2500 x > y ? 1 :
2501 -1;
2502 }
2503
Calin Juravleddb7df22014-11-25 20:56:51 +00002504 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2505 return bias_ == other->AsCompare()->bias_;
2506 }
2507
Mark Mendellc4701932015-04-10 13:18:51 -04002508 ComparisonBias GetBias() const { return bias_; }
2509
Roland Levillain4fa13f62015-07-06 18:11:54 +01002510 bool IsGtBias() { return bias_ == ComparisonBias::kGtBias; }
Calin Juravleddb7df22014-11-25 20:56:51 +00002511
Alexey Frunze4dda3372015-06-01 18:31:49 -07002512 uint32_t GetDexPc() const { return dex_pc_; }
2513
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002514 DECLARE_INSTRUCTION(Compare);
2515
2516 private:
Mark Mendellc4701932015-04-10 13:18:51 -04002517 const ComparisonBias bias_;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002518 const uint32_t dex_pc_;
Calin Juravleddb7df22014-11-25 20:56:51 +00002519
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002520 DISALLOW_COPY_AND_ASSIGN(HCompare);
2521};
2522
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002523// A local in the graph. Corresponds to a Dex register.
2524class HLocal : public HTemplateInstruction<0> {
2525 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002526 explicit HLocal(uint16_t reg_number)
2527 : HTemplateInstruction(SideEffects::None()), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002528
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002529 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002530
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002531 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002532
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002533 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002534 // The Dex register number.
2535 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002536
2537 DISALLOW_COPY_AND_ASSIGN(HLocal);
2538};
2539
2540// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07002541class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002542 public:
Roland Levillain5799fc02014-09-25 12:15:20 +01002543 HLoadLocal(HLocal* local, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002544 : HExpression(type, SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002545 SetRawInputAt(0, local);
2546 }
2547
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002548 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
2549
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002550 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002551
2552 private:
2553 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
2554};
2555
2556// Store a value in a given local. This instruction has two inputs: the value
2557// and the local.
2558class HStoreLocal : public HTemplateInstruction<2> {
2559 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002560 HStoreLocal(HLocal* local, HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002561 SetRawInputAt(0, local);
2562 SetRawInputAt(1, value);
2563 }
2564
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002565 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
2566
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002567 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002568
2569 private:
2570 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
2571};
2572
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002573class HConstant : public HExpression<0> {
2574 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002575 explicit HConstant(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
2576
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002577 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002578
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002579 virtual bool IsMinusOne() const { return false; }
2580 virtual bool IsZero() const { return false; }
2581 virtual bool IsOne() const { return false; }
2582
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002583 DECLARE_INSTRUCTION(Constant);
2584
2585 private:
2586 DISALLOW_COPY_AND_ASSIGN(HConstant);
2587};
2588
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002589class HFloatConstant : public HConstant {
2590 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002591 float GetValue() const { return value_; }
2592
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002593 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002594 return bit_cast<uint32_t, float>(other->AsFloatConstant()->value_) ==
2595 bit_cast<uint32_t, float>(value_);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002596 }
2597
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002598 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002599
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002600 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002601 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002602 }
2603 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002604 return value_ == 0.0f;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002605 }
2606 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002607 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
2608 }
2609 bool IsNaN() const {
2610 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002611 }
2612
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002613 DECLARE_INSTRUCTION(FloatConstant);
2614
2615 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002616 explicit HFloatConstant(float value) : HConstant(Primitive::kPrimFloat), value_(value) {}
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002617 explicit HFloatConstant(int32_t value)
2618 : HConstant(Primitive::kPrimFloat), value_(bit_cast<float, int32_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00002619
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002620 const float value_;
2621
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002622 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00002623 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002624 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002625 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2626};
2627
2628class HDoubleConstant : public HConstant {
2629 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002630 double GetValue() const { return value_; }
2631
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002632 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002633 return bit_cast<uint64_t, double>(other->AsDoubleConstant()->value_) ==
2634 bit_cast<uint64_t, double>(value_);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002635 }
2636
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002637 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002638
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002639 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002640 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002641 }
2642 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002643 return value_ == 0.0;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002644 }
2645 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002646 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
2647 }
2648 bool IsNaN() const {
2649 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002650 }
2651
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002652 DECLARE_INSTRUCTION(DoubleConstant);
2653
2654 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002655 explicit HDoubleConstant(double value) : HConstant(Primitive::kPrimDouble), value_(value) {}
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002656 explicit HDoubleConstant(int64_t value)
2657 : HConstant(Primitive::kPrimDouble), value_(bit_cast<double, int64_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00002658
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002659 const double value_;
2660
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002661 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00002662 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002663 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002664 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
2665};
2666
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002667class HNullConstant : public HConstant {
2668 public:
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002669 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2670 return true;
2671 }
2672
2673 size_t ComputeHashCode() const OVERRIDE { return 0; }
2674
2675 DECLARE_INSTRUCTION(NullConstant);
2676
2677 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002678 HNullConstant() : HConstant(Primitive::kPrimNot) {}
2679
2680 friend class HGraph;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002681 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2682};
2683
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002684// Constants of the type int. Those can be from Dex instructions, or
2685// synthesized (for example with the if-eqz instruction).
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002686class HIntConstant : public HConstant {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002687 public:
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002688 int32_t GetValue() const { return value_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002689
Calin Juravle61d544b2015-02-23 16:46:57 +00002690 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002691 return other->AsIntConstant()->value_ == value_;
2692 }
2693
Calin Juravle61d544b2015-02-23 16:46:57 +00002694 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2695
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002696 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2697 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2698 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2699
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002700 DECLARE_INSTRUCTION(IntConstant);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002701
2702 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002703 explicit HIntConstant(int32_t value) : HConstant(Primitive::kPrimInt), value_(value) {}
2704
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002705 const int32_t value_;
2706
David Brazdil8d5b8b22015-03-24 10:51:52 +00002707 friend class HGraph;
2708 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
Zheng Xuad4450e2015-04-17 18:48:56 +08002709 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002710 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2711};
2712
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002713class HLongConstant : public HConstant {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002714 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002715 int64_t GetValue() const { return value_; }
2716
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002717 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002718 return other->AsLongConstant()->value_ == value_;
2719 }
2720
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002721 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002722
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002723 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2724 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2725 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2726
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002727 DECLARE_INSTRUCTION(LongConstant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002728
2729 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002730 explicit HLongConstant(int64_t value) : HConstant(Primitive::kPrimLong), value_(value) {}
2731
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002732 const int64_t value_;
2733
David Brazdil8d5b8b22015-03-24 10:51:52 +00002734 friend class HGraph;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002735 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2736};
2737
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002738enum class Intrinsics {
2739#define OPTIMIZING_INTRINSICS(Name, IsStatic) k ## Name,
2740#include "intrinsics_list.h"
2741 kNone,
2742 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
2743#undef INTRINSICS_LIST
2744#undef OPTIMIZING_INTRINSICS
2745};
2746std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
2747
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002748class HInvoke : public HInstruction {
2749 public:
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002750 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002751
2752 // Runtime needs to walk the stack, so Dex -> Dex calls need to
2753 // know their environment.
Calin Juravle77520bc2015-01-12 18:45:46 +00002754 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002755
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01002756 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002757 SetRawInputAt(index, argument);
2758 }
2759
Roland Levillain3e3d7332015-04-28 11:00:54 +01002760 // Return the number of arguments. This number can be lower than
2761 // the number of inputs returned by InputCount(), as some invoke
2762 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
2763 // inputs at the end of their list of inputs.
2764 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
2765
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002766 Primitive::Type GetType() const OVERRIDE { return return_type_; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002767
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002768 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002769
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002770 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002771 const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002772
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002773 InvokeType GetOriginalInvokeType() const { return original_invoke_type_; }
2774
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01002775 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002776 return intrinsic_;
2777 }
2778
2779 void SetIntrinsic(Intrinsics intrinsic) {
2780 intrinsic_ = intrinsic;
2781 }
2782
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01002783 bool IsFromInlinedInvoke() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002784 return GetEnvironment()->GetParent() != nullptr;
2785 }
2786
2787 bool CanThrow() const OVERRIDE { return true; }
2788
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002789 DECLARE_INSTRUCTION(Invoke);
2790
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002791 protected:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002792 HInvoke(ArenaAllocator* arena,
2793 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01002794 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002795 Primitive::Type return_type,
2796 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002797 uint32_t dex_method_index,
2798 InvokeType original_invoke_type)
Aart Bik854a02b2015-07-14 16:07:00 -07002799 : HInstruction(SideEffects::All()), // assume write/read on all fields/arrays
Roland Levillain3e3d7332015-04-28 11:00:54 +01002800 number_of_arguments_(number_of_arguments),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002801 inputs_(arena, number_of_arguments),
2802 return_type_(return_type),
2803 dex_pc_(dex_pc),
2804 dex_method_index_(dex_method_index),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002805 original_invoke_type_(original_invoke_type),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002806 intrinsic_(Intrinsics::kNone) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01002807 uint32_t number_of_inputs = number_of_arguments + number_of_other_inputs;
2808 inputs_.SetSize(number_of_inputs);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002809 }
2810
David Brazdil1abb4192015-02-17 18:33:36 +00002811 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
2812 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
2813 inputs_.Put(index, input);
2814 }
2815
Roland Levillain3e3d7332015-04-28 11:00:54 +01002816 uint32_t number_of_arguments_;
David Brazdil1abb4192015-02-17 18:33:36 +00002817 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002818 const Primitive::Type return_type_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002819 const uint32_t dex_pc_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002820 const uint32_t dex_method_index_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002821 const InvokeType original_invoke_type_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002822 Intrinsics intrinsic_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002823
2824 private:
2825 DISALLOW_COPY_AND_ASSIGN(HInvoke);
2826};
2827
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002828class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002829 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01002830 // Requirements of this method call regarding the class
2831 // initialization (clinit) check of its declaring class.
2832 enum class ClinitCheckRequirement {
2833 kNone, // Class already initialized.
2834 kExplicit, // Static call having explicit clinit check as last input.
2835 kImplicit, // Static call implicitly requiring a clinit check.
2836 };
2837
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002838 HInvokeStaticOrDirect(ArenaAllocator* arena,
2839 uint32_t number_of_arguments,
2840 Primitive::Type return_type,
2841 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002842 uint32_t dex_method_index,
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002843 bool is_recursive,
Jeff Hao848f70a2014-01-15 13:49:50 -08002844 int32_t string_init_offset,
Nicolas Geoffray79041292015-03-26 10:05:54 +00002845 InvokeType original_invoke_type,
Roland Levillain4c0eb422015-04-24 16:43:49 +01002846 InvokeType invoke_type,
2847 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01002848 : HInvoke(arena,
2849 number_of_arguments,
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01002850 // There is one extra argument for the HCurrentMethod node, and
2851 // potentially one other if the clinit check is explicit, and one other
2852 // if the method is a string factory.
2853 1u + (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u)
2854 + (string_init_offset ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01002855 return_type,
2856 dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002857 dex_method_index,
2858 original_invoke_type),
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002859 invoke_type_(invoke_type),
Roland Levillain4c0eb422015-04-24 16:43:49 +01002860 is_recursive_(is_recursive),
Jeff Hao848f70a2014-01-15 13:49:50 -08002861 clinit_check_requirement_(clinit_check_requirement),
2862 string_init_offset_(string_init_offset) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002863
Calin Juravle641547a2015-04-21 22:08:51 +01002864 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
2865 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00002866 // We access the method via the dex cache so we can't do an implicit null check.
2867 // TODO: for intrinsics we can generate implicit null checks.
2868 return false;
2869 }
2870
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002871 InvokeType GetInvokeType() const { return invoke_type_; }
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002872 bool IsRecursive() const { return is_recursive_; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002873 bool NeedsDexCache() const OVERRIDE { return !IsRecursive(); }
Jeff Hao848f70a2014-01-15 13:49:50 -08002874 bool IsStringInit() const { return string_init_offset_ != 0; }
2875 int32_t GetStringInitOffset() const { return string_init_offset_; }
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002876 uint32_t GetCurrentMethodInputIndex() const { return GetNumberOfArguments(); }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002877
Roland Levillain4c0eb422015-04-24 16:43:49 +01002878 // Is this instruction a call to a static method?
2879 bool IsStatic() const {
2880 return GetInvokeType() == kStatic;
2881 }
2882
Roland Levillain3e3d7332015-04-28 11:00:54 +01002883 // Remove the art::HLoadClass instruction set as last input by
2884 // art::PrepareForRegisterAllocation::VisitClinitCheck in lieu of
2885 // the initial art::HClinitCheck instruction (only relevant for
2886 // static calls with explicit clinit check).
2887 void RemoveLoadClassAsLastInput() {
Roland Levillain4c0eb422015-04-24 16:43:49 +01002888 DCHECK(IsStaticWithExplicitClinitCheck());
2889 size_t last_input_index = InputCount() - 1;
2890 HInstruction* last_input = InputAt(last_input_index);
2891 DCHECK(last_input != nullptr);
Roland Levillain3e3d7332015-04-28 11:00:54 +01002892 DCHECK(last_input->IsLoadClass()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01002893 RemoveAsUserOfInput(last_input_index);
2894 inputs_.DeleteAt(last_input_index);
2895 clinit_check_requirement_ = ClinitCheckRequirement::kImplicit;
2896 DCHECK(IsStaticWithImplicitClinitCheck());
2897 }
2898
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01002899 bool IsStringFactoryFor(HFakeString* str) const {
2900 if (!IsStringInit()) return false;
2901 // +1 for the current method.
2902 if (InputCount() == (number_of_arguments_ + 1)) return false;
2903 return InputAt(InputCount() - 1)->AsFakeString() == str;
2904 }
2905
2906 void RemoveFakeStringArgumentAsLastInput() {
2907 DCHECK(IsStringInit());
2908 size_t last_input_index = InputCount() - 1;
2909 HInstruction* last_input = InputAt(last_input_index);
2910 DCHECK(last_input != nullptr);
2911 DCHECK(last_input->IsFakeString()) << last_input->DebugName();
2912 RemoveAsUserOfInput(last_input_index);
2913 inputs_.DeleteAt(last_input_index);
2914 }
2915
Roland Levillain4c0eb422015-04-24 16:43:49 +01002916 // Is this a call to a static method whose declaring class has an
2917 // explicit intialization check in the graph?
2918 bool IsStaticWithExplicitClinitCheck() const {
2919 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kExplicit);
2920 }
2921
2922 // Is this a call to a static method whose declaring class has an
2923 // implicit intialization check requirement?
2924 bool IsStaticWithImplicitClinitCheck() const {
2925 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kImplicit);
2926 }
2927
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002928 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002929
Roland Levillain4c0eb422015-04-24 16:43:49 +01002930 protected:
2931 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
2932 const HUserRecord<HInstruction*> input_record = HInvoke::InputRecordAt(i);
2933 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck() && (i == InputCount() - 1)) {
2934 HInstruction* input = input_record.GetInstruction();
2935 // `input` is the last input of a static invoke marked as having
2936 // an explicit clinit check. It must either be:
2937 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
2938 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
2939 DCHECK(input != nullptr);
2940 DCHECK(input->IsClinitCheck() || input->IsLoadClass()) << input->DebugName();
2941 }
2942 return input_record;
2943 }
2944
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002945 private:
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002946 const InvokeType invoke_type_;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002947 const bool is_recursive_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01002948 ClinitCheckRequirement clinit_check_requirement_;
Jeff Hao848f70a2014-01-15 13:49:50 -08002949 // Thread entrypoint offset for string init method if this is a string init invoke.
2950 // Note that there are multiple string init methods, each having its own offset.
2951 int32_t string_init_offset_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002952
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002953 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002954};
2955
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002956class HInvokeVirtual : public HInvoke {
2957 public:
2958 HInvokeVirtual(ArenaAllocator* arena,
2959 uint32_t number_of_arguments,
2960 Primitive::Type return_type,
2961 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002962 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002963 uint32_t vtable_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002964 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002965 vtable_index_(vtable_index) {}
2966
Calin Juravle641547a2015-04-21 22:08:51 +01002967 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00002968 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01002969 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00002970 }
2971
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002972 uint32_t GetVTableIndex() const { return vtable_index_; }
2973
2974 DECLARE_INSTRUCTION(InvokeVirtual);
2975
2976 private:
2977 const uint32_t vtable_index_;
2978
2979 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
2980};
2981
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002982class HInvokeInterface : public HInvoke {
2983 public:
2984 HInvokeInterface(ArenaAllocator* arena,
2985 uint32_t number_of_arguments,
2986 Primitive::Type return_type,
2987 uint32_t dex_pc,
2988 uint32_t dex_method_index,
2989 uint32_t imt_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002990 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002991 imt_index_(imt_index) {}
2992
Calin Juravle641547a2015-04-21 22:08:51 +01002993 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00002994 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01002995 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00002996 }
2997
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002998 uint32_t GetImtIndex() const { return imt_index_; }
2999 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
3000
3001 DECLARE_INSTRUCTION(InvokeInterface);
3002
3003 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003004 const uint32_t imt_index_;
3005
3006 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
3007};
3008
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003009class HNewInstance : public HExpression<1> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003010 public:
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003011 HNewInstance(HCurrentMethod* current_method,
3012 uint32_t dex_pc,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003013 uint16_t type_index,
3014 const DexFile& dex_file,
3015 QuickEntrypointEnum entrypoint)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003016 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3017 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003018 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003019 dex_file_(dex_file),
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003020 entrypoint_(entrypoint) {
3021 SetRawInputAt(0, current_method);
3022 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003023
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003024 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003025 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003026 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003027
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003028 // Calls runtime so needs an environment.
Calin Juravle92a6ed22014-12-02 18:58:03 +00003029 bool NeedsEnvironment() const OVERRIDE { return true; }
3030 // It may throw when called on:
3031 // - interfaces
3032 // - abstract/innaccessible/unknown classes
3033 // TODO: optimize when possible.
3034 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003035
Calin Juravle10e244f2015-01-26 18:54:32 +00003036 bool CanBeNull() const OVERRIDE { return false; }
3037
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003038 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3039
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003040 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003041
3042 private:
3043 const uint32_t dex_pc_;
3044 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003045 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003046 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003047
3048 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3049};
3050
Roland Levillain88cb1752014-10-20 16:36:47 +01003051class HNeg : public HUnaryOperation {
3052 public:
3053 explicit HNeg(Primitive::Type result_type, HInstruction* input)
3054 : HUnaryOperation(result_type, input) {}
3055
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003056 int32_t Evaluate(int32_t x) const OVERRIDE { return -x; }
3057 int64_t Evaluate(int64_t x) const OVERRIDE { return -x; }
Roland Levillain9240d6a2014-10-20 16:47:04 +01003058
Roland Levillain88cb1752014-10-20 16:36:47 +01003059 DECLARE_INSTRUCTION(Neg);
3060
3061 private:
3062 DISALLOW_COPY_AND_ASSIGN(HNeg);
3063};
3064
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003065class HNewArray : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003066 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003067 HNewArray(HInstruction* length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003068 HCurrentMethod* current_method,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003069 uint32_t dex_pc,
3070 uint16_t type_index,
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003071 const DexFile& dex_file,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003072 QuickEntrypointEnum entrypoint)
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003073 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3074 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003075 type_index_(type_index),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003076 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003077 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003078 SetRawInputAt(0, length);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003079 SetRawInputAt(1, current_method);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003080 }
3081
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003082 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003083 uint16_t GetTypeIndex() const { return type_index_; }
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003084 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003085
3086 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00003087 bool NeedsEnvironment() const OVERRIDE { return true; }
3088
Mingyao Yang0c365e62015-03-31 15:09:29 -07003089 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
3090 bool CanThrow() const OVERRIDE { return true; }
3091
Calin Juravle10e244f2015-01-26 18:54:32 +00003092 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003093
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003094 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3095
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003096 DECLARE_INSTRUCTION(NewArray);
3097
3098 private:
3099 const uint32_t dex_pc_;
3100 const uint16_t type_index_;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003101 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003102 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003103
3104 DISALLOW_COPY_AND_ASSIGN(HNewArray);
3105};
3106
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003107class HAdd : public HBinaryOperation {
3108 public:
3109 HAdd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3110 : HBinaryOperation(result_type, left, right) {}
3111
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003112 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003113
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003114 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01003115 return x + y;
3116 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003117 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01003118 return x + y;
3119 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003120
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003121 DECLARE_INSTRUCTION(Add);
3122
3123 private:
3124 DISALLOW_COPY_AND_ASSIGN(HAdd);
3125};
3126
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003127class HSub : public HBinaryOperation {
3128 public:
3129 HSub(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3130 : HBinaryOperation(result_type, left, right) {}
3131
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003132 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01003133 return x - y;
3134 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003135 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01003136 return x - y;
3137 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003138
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003139 DECLARE_INSTRUCTION(Sub);
3140
3141 private:
3142 DISALLOW_COPY_AND_ASSIGN(HSub);
3143};
3144
Calin Juravle34bacdf2014-10-07 20:23:36 +01003145class HMul : public HBinaryOperation {
3146 public:
3147 HMul(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3148 : HBinaryOperation(result_type, left, right) {}
3149
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003150 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003151
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003152 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x * y; }
3153 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x * y; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003154
3155 DECLARE_INSTRUCTION(Mul);
3156
3157 private:
3158 DISALLOW_COPY_AND_ASSIGN(HMul);
3159};
3160
Calin Juravle7c4954d2014-10-28 16:57:40 +00003161class HDiv : public HBinaryOperation {
3162 public:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003163 HDiv(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
3164 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00003165
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003166 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00003167 // Our graph structure ensures we never have 0 for `y` during constant folding.
3168 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00003169 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00003170 return (y == -1) ? -x : x / y;
3171 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003172
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003173 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00003174 DCHECK_NE(y, 0);
3175 // Special case -1 to avoid getting a SIGFPE on x86(_64).
3176 return (y == -1) ? -x : x / y;
3177 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003178
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003179 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003180
Calin Juravle7c4954d2014-10-28 16:57:40 +00003181 DECLARE_INSTRUCTION(Div);
3182
3183 private:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003184 const uint32_t dex_pc_;
3185
Calin Juravle7c4954d2014-10-28 16:57:40 +00003186 DISALLOW_COPY_AND_ASSIGN(HDiv);
3187};
3188
Calin Juravlebacfec32014-11-14 15:54:36 +00003189class HRem : public HBinaryOperation {
3190 public:
3191 HRem(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
3192 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
3193
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003194 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00003195 DCHECK_NE(y, 0);
3196 // Special case -1 to avoid getting a SIGFPE on x86(_64).
3197 return (y == -1) ? 0 : x % y;
3198 }
3199
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003200 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00003201 DCHECK_NE(y, 0);
3202 // Special case -1 to avoid getting a SIGFPE on x86(_64).
3203 return (y == -1) ? 0 : x % y;
3204 }
3205
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003206 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Calin Juravlebacfec32014-11-14 15:54:36 +00003207
3208 DECLARE_INSTRUCTION(Rem);
3209
3210 private:
3211 const uint32_t dex_pc_;
3212
3213 DISALLOW_COPY_AND_ASSIGN(HRem);
3214};
3215
Calin Juravled0d48522014-11-04 16:40:20 +00003216class HDivZeroCheck : public HExpression<1> {
3217 public:
3218 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
3219 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
3220 SetRawInputAt(0, value);
3221 }
3222
3223 bool CanBeMoved() const OVERRIDE { return true; }
3224
3225 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3226 UNUSED(other);
3227 return true;
3228 }
3229
3230 bool NeedsEnvironment() const OVERRIDE { return true; }
3231 bool CanThrow() const OVERRIDE { return true; }
3232
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003233 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Calin Juravled0d48522014-11-04 16:40:20 +00003234
3235 DECLARE_INSTRUCTION(DivZeroCheck);
3236
3237 private:
3238 const uint32_t dex_pc_;
3239
3240 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
3241};
3242
Calin Juravle9aec02f2014-11-18 23:06:35 +00003243class HShl : public HBinaryOperation {
3244 public:
3245 HShl(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3246 : HBinaryOperation(result_type, left, right) {}
3247
3248 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x << (y & kMaxIntShiftValue); }
3249 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x << (y & kMaxLongShiftValue); }
3250
3251 DECLARE_INSTRUCTION(Shl);
3252
3253 private:
3254 DISALLOW_COPY_AND_ASSIGN(HShl);
3255};
3256
3257class HShr : public HBinaryOperation {
3258 public:
3259 HShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3260 : HBinaryOperation(result_type, left, right) {}
3261
3262 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x >> (y & kMaxIntShiftValue); }
3263 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x >> (y & kMaxLongShiftValue); }
3264
3265 DECLARE_INSTRUCTION(Shr);
3266
3267 private:
3268 DISALLOW_COPY_AND_ASSIGN(HShr);
3269};
3270
3271class HUShr : public HBinaryOperation {
3272 public:
3273 HUShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3274 : HBinaryOperation(result_type, left, right) {}
3275
3276 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
3277 uint32_t ux = static_cast<uint32_t>(x);
3278 uint32_t uy = static_cast<uint32_t>(y) & kMaxIntShiftValue;
3279 return static_cast<int32_t>(ux >> uy);
3280 }
3281
3282 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
3283 uint64_t ux = static_cast<uint64_t>(x);
3284 uint64_t uy = static_cast<uint64_t>(y) & kMaxLongShiftValue;
3285 return static_cast<int64_t>(ux >> uy);
3286 }
3287
3288 DECLARE_INSTRUCTION(UShr);
3289
3290 private:
3291 DISALLOW_COPY_AND_ASSIGN(HUShr);
3292};
3293
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003294class HAnd : public HBinaryOperation {
3295 public:
3296 HAnd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3297 : HBinaryOperation(result_type, left, right) {}
3298
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003299 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003300
3301 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x & y; }
3302 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x & y; }
3303
3304 DECLARE_INSTRUCTION(And);
3305
3306 private:
3307 DISALLOW_COPY_AND_ASSIGN(HAnd);
3308};
3309
3310class HOr : public HBinaryOperation {
3311 public:
3312 HOr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3313 : HBinaryOperation(result_type, left, right) {}
3314
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003315 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003316
3317 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x | y; }
3318 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x | y; }
3319
3320 DECLARE_INSTRUCTION(Or);
3321
3322 private:
3323 DISALLOW_COPY_AND_ASSIGN(HOr);
3324};
3325
3326class HXor : public HBinaryOperation {
3327 public:
3328 HXor(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3329 : HBinaryOperation(result_type, left, right) {}
3330
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003331 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003332
3333 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x ^ y; }
3334 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x ^ y; }
3335
3336 DECLARE_INSTRUCTION(Xor);
3337
3338 private:
3339 DISALLOW_COPY_AND_ASSIGN(HXor);
3340};
3341
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003342// The value of a parameter in this method. Its location depends on
3343// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07003344class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003345 public:
Calin Juravle10e244f2015-01-26 18:54:32 +00003346 HParameterValue(uint8_t index, Primitive::Type parameter_type, bool is_this = false)
3347 : HExpression(parameter_type, SideEffects::None()), index_(index), is_this_(is_this) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003348
3349 uint8_t GetIndex() const { return index_; }
3350
Calin Juravle10e244f2015-01-26 18:54:32 +00003351 bool CanBeNull() const OVERRIDE { return !is_this_; }
3352
Calin Juravle3cd4fc82015-05-14 15:15:42 +01003353 bool IsThis() const { return is_this_; }
3354
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003355 DECLARE_INSTRUCTION(ParameterValue);
3356
3357 private:
3358 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00003359 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003360 const uint8_t index_;
3361
Calin Juravle10e244f2015-01-26 18:54:32 +00003362 // Whether or not the parameter value corresponds to 'this' argument.
3363 const bool is_this_;
3364
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003365 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
3366};
3367
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003368class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003369 public:
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003370 explicit HNot(Primitive::Type result_type, HInstruction* input)
3371 : HUnaryOperation(result_type, input) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003372
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003373 bool CanBeMoved() const OVERRIDE { return true; }
3374 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003375 UNUSED(other);
3376 return true;
3377 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003378
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003379 int32_t Evaluate(int32_t x) const OVERRIDE { return ~x; }
3380 int64_t Evaluate(int64_t x) const OVERRIDE { return ~x; }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003381
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003382 DECLARE_INSTRUCTION(Not);
3383
3384 private:
3385 DISALLOW_COPY_AND_ASSIGN(HNot);
3386};
3387
David Brazdil66d126e2015-04-03 16:02:44 +01003388class HBooleanNot : public HUnaryOperation {
3389 public:
3390 explicit HBooleanNot(HInstruction* input)
3391 : HUnaryOperation(Primitive::Type::kPrimBoolean, input) {}
3392
3393 bool CanBeMoved() const OVERRIDE { return true; }
3394 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3395 UNUSED(other);
3396 return true;
3397 }
3398
3399 int32_t Evaluate(int32_t x) const OVERRIDE {
3400 DCHECK(IsUint<1>(x));
3401 return !x;
3402 }
3403
3404 int64_t Evaluate(int64_t x ATTRIBUTE_UNUSED) const OVERRIDE {
3405 LOG(FATAL) << DebugName() << " cannot be used with 64-bit values";
3406 UNREACHABLE();
3407 }
3408
3409 DECLARE_INSTRUCTION(BooleanNot);
3410
3411 private:
3412 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
3413};
3414
Roland Levillaindff1f282014-11-05 14:15:05 +00003415class HTypeConversion : public HExpression<1> {
3416 public:
3417 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00003418 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
3419 : HExpression(result_type, SideEffects::None()), dex_pc_(dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00003420 SetRawInputAt(0, input);
3421 DCHECK_NE(input->GetType(), result_type);
3422 }
3423
3424 HInstruction* GetInput() const { return InputAt(0); }
3425 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
3426 Primitive::Type GetResultType() const { return GetType(); }
3427
Roland Levillain624279f2014-12-04 11:54:28 +00003428 // Required by the x86 and ARM code generators when producing calls
3429 // to the runtime.
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003430 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Roland Levillain624279f2014-12-04 11:54:28 +00003431
Roland Levillaindff1f282014-11-05 14:15:05 +00003432 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00003433 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00003434
Mark Mendelle82549b2015-05-06 10:55:34 -04003435 // Try to statically evaluate the conversion and return a HConstant
3436 // containing the result. If the input cannot be converted, return nullptr.
3437 HConstant* TryStaticEvaluation() const;
3438
Roland Levillaindff1f282014-11-05 14:15:05 +00003439 DECLARE_INSTRUCTION(TypeConversion);
3440
3441 private:
Roland Levillain624279f2014-12-04 11:54:28 +00003442 const uint32_t dex_pc_;
3443
Roland Levillaindff1f282014-11-05 14:15:05 +00003444 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
3445};
3446
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00003447static constexpr uint32_t kNoRegNumber = -1;
3448
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003449class HPhi : public HInstruction {
3450 public:
3451 HPhi(ArenaAllocator* arena, uint32_t reg_number, size_t number_of_inputs, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003452 : HInstruction(SideEffects::None()),
3453 inputs_(arena, number_of_inputs),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003454 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01003455 type_(type),
Calin Juravle10e244f2015-01-26 18:54:32 +00003456 is_live_(false),
3457 can_be_null_(true) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003458 inputs_.SetSize(number_of_inputs);
3459 }
3460
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00003461 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
3462 static Primitive::Type ToPhiType(Primitive::Type type) {
3463 switch (type) {
3464 case Primitive::kPrimBoolean:
3465 case Primitive::kPrimByte:
3466 case Primitive::kPrimShort:
3467 case Primitive::kPrimChar:
3468 return Primitive::kPrimInt;
3469 default:
3470 return type;
3471 }
3472 }
3473
David Brazdilffee3d32015-07-06 11:48:53 +01003474 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
3475
Calin Juravle10e244f2015-01-26 18:54:32 +00003476 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003477
3478 void AddInput(HInstruction* input);
David Brazdil2d7352b2015-04-20 14:52:42 +01003479 void RemoveInputAt(size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003480
Calin Juravle10e244f2015-01-26 18:54:32 +00003481 Primitive::Type GetType() const OVERRIDE { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003482 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003483
Calin Juravle10e244f2015-01-26 18:54:32 +00003484 bool CanBeNull() const OVERRIDE { return can_be_null_; }
3485 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
3486
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003487 uint32_t GetRegNumber() const { return reg_number_; }
3488
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01003489 void SetDead() { is_live_ = false; }
3490 void SetLive() { is_live_ = true; }
3491 bool IsDead() const { return !is_live_; }
3492 bool IsLive() const { return is_live_; }
3493
Calin Juravlea4f88312015-04-16 12:57:19 +01003494 // Returns the next equivalent phi (starting from the current one) or null if there is none.
3495 // An equivalent phi is a phi having the same dex register and type.
3496 // It assumes that phis with the same dex register are adjacent.
3497 HPhi* GetNextEquivalentPhiWithSameType() {
3498 HInstruction* next = GetNext();
3499 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
3500 if (next->GetType() == GetType()) {
3501 return next->AsPhi();
3502 }
3503 next = next->GetNext();
3504 }
3505 return nullptr;
3506 }
3507
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003508 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003509
David Brazdil1abb4192015-02-17 18:33:36 +00003510 protected:
3511 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
3512
3513 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
3514 inputs_.Put(index, input);
3515 }
3516
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003517 private:
David Brazdil1abb4192015-02-17 18:33:36 +00003518 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003519 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003520 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01003521 bool is_live_;
Calin Juravle10e244f2015-01-26 18:54:32 +00003522 bool can_be_null_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003523
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003524 DISALLOW_COPY_AND_ASSIGN(HPhi);
3525};
3526
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003527class HNullCheck : public HExpression<1> {
3528 public:
3529 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003530 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003531 SetRawInputAt(0, value);
3532 }
3533
Calin Juravle10e244f2015-01-26 18:54:32 +00003534 bool CanBeMoved() const OVERRIDE { return true; }
3535 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003536 UNUSED(other);
3537 return true;
3538 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003539
Calin Juravle10e244f2015-01-26 18:54:32 +00003540 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003541
Calin Juravle10e244f2015-01-26 18:54:32 +00003542 bool CanThrow() const OVERRIDE { return true; }
3543
3544 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01003545
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003546 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003547
3548 DECLARE_INSTRUCTION(NullCheck);
3549
3550 private:
3551 const uint32_t dex_pc_;
3552
3553 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
3554};
3555
3556class FieldInfo : public ValueObject {
3557 public:
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003558 FieldInfo(MemberOffset field_offset,
3559 Primitive::Type field_type,
3560 bool is_volatile,
3561 uint32_t index,
3562 const DexFile& dex_file)
3563 : field_offset_(field_offset),
3564 field_type_(field_type),
3565 is_volatile_(is_volatile),
3566 index_(index),
3567 dex_file_(dex_file) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003568
3569 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003570 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003571 uint32_t GetFieldIndex() const { return index_; }
3572 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00003573 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003574
3575 private:
3576 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01003577 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00003578 const bool is_volatile_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003579 uint32_t index_;
3580 const DexFile& dex_file_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003581};
3582
3583class HInstanceFieldGet : public HExpression<1> {
3584 public:
3585 HInstanceFieldGet(HInstruction* value,
3586 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003587 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003588 bool is_volatile,
3589 uint32_t field_idx,
3590 const DexFile& dex_file)
Aart Bik34c3ba92015-07-20 14:08:59 -07003591 : HExpression(
3592 field_type,
Alexandre Rames1c4ccea2015-07-22 11:32:58 +01003593 SideEffects::FieldReadOfType(field_type, is_volatile)),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003594 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003595 SetRawInputAt(0, value);
3596 }
3597
Calin Juravle10c9cbe2014-12-19 10:50:19 +00003598 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003599
3600 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3601 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
3602 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003603 }
3604
Calin Juravle641547a2015-04-21 22:08:51 +01003605 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3606 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00003607 }
3608
3609 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01003610 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
3611 }
3612
Calin Juravle52c48962014-12-16 17:02:57 +00003613 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003614 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003615 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003616 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003617
3618 DECLARE_INSTRUCTION(InstanceFieldGet);
3619
3620 private:
3621 const FieldInfo field_info_;
3622
3623 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
3624};
3625
3626class HInstanceFieldSet : public HTemplateInstruction<2> {
3627 public:
3628 HInstanceFieldSet(HInstruction* object,
3629 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01003630 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003631 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003632 bool is_volatile,
3633 uint32_t field_idx,
3634 const DexFile& dex_file)
Aart Bik34c3ba92015-07-20 14:08:59 -07003635 : HTemplateInstruction(
3636 SideEffects::FieldWriteOfType(field_type, is_volatile)),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003637 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003638 value_can_be_null_(true) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003639 SetRawInputAt(0, object);
3640 SetRawInputAt(1, value);
3641 }
3642
Calin Juravle641547a2015-04-21 22:08:51 +01003643 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3644 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00003645 }
3646
Calin Juravle52c48962014-12-16 17:02:57 +00003647 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003648 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003649 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003650 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003651 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003652 bool GetValueCanBeNull() const { return value_can_be_null_; }
3653 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003654
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003655 DECLARE_INSTRUCTION(InstanceFieldSet);
3656
3657 private:
3658 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003659 bool value_can_be_null_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003660
3661 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
3662};
3663
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003664class HArrayGet : public HExpression<2> {
3665 public:
3666 HArrayGet(HInstruction* array, HInstruction* index, Primitive::Type type)
Aart Bik854a02b2015-07-14 16:07:00 -07003667 : HExpression(type, SideEffects::ArrayReadOfType(type)) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003668 SetRawInputAt(0, array);
3669 SetRawInputAt(1, index);
3670 }
3671
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003672 bool CanBeMoved() const OVERRIDE { return true; }
3673 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003674 UNUSED(other);
3675 return true;
3676 }
Calin Juravle641547a2015-04-21 22:08:51 +01003677 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3678 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00003679 // TODO: We can be smarter here.
3680 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
3681 // which generates the implicit null check. There are cases when these can be removed
3682 // to produce better code. If we ever add optimizations to do so we should allow an
3683 // implicit check here (as long as the address falls in the first page).
3684 return false;
3685 }
3686
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003687 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003688
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003689 HInstruction* GetArray() const { return InputAt(0); }
3690 HInstruction* GetIndex() const { return InputAt(1); }
3691
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003692 DECLARE_INSTRUCTION(ArrayGet);
3693
3694 private:
3695 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
3696};
3697
3698class HArraySet : public HTemplateInstruction<3> {
3699 public:
3700 HArraySet(HInstruction* array,
3701 HInstruction* index,
3702 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003703 Primitive::Type expected_component_type,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003704 uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07003705 : HTemplateInstruction(SideEffects::ArrayWriteOfType(expected_component_type)),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003706 dex_pc_(dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003707 expected_component_type_(expected_component_type),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003708 needs_type_check_(value->GetType() == Primitive::kPrimNot),
3709 value_can_be_null_(true) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003710 SetRawInputAt(0, array);
3711 SetRawInputAt(1, index);
3712 SetRawInputAt(2, value);
3713 }
3714
Calin Juravle77520bc2015-01-12 18:45:46 +00003715 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003716 // We currently always call a runtime method to catch array store
3717 // exceptions.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003718 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003719 }
3720
Mingyao Yang81014cb2015-06-02 03:16:27 -07003721 // Can throw ArrayStoreException.
3722 bool CanThrow() const OVERRIDE { return needs_type_check_; }
3723
Calin Juravle641547a2015-04-21 22:08:51 +01003724 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3725 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00003726 // TODO: Same as for ArrayGet.
3727 return false;
3728 }
3729
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003730 void ClearNeedsTypeCheck() {
3731 needs_type_check_ = false;
3732 }
3733
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003734 void ClearValueCanBeNull() {
3735 value_can_be_null_ = false;
3736 }
3737
3738 bool GetValueCanBeNull() const { return value_can_be_null_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003739 bool NeedsTypeCheck() const { return needs_type_check_; }
3740
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003741 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003742
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003743 HInstruction* GetArray() const { return InputAt(0); }
3744 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003745 HInstruction* GetValue() const { return InputAt(2); }
3746
3747 Primitive::Type GetComponentType() const {
3748 // The Dex format does not type floating point index operations. Since the
3749 // `expected_component_type_` is set during building and can therefore not
3750 // be correct, we also check what is the value type. If it is a floating
3751 // point type, we must use that type.
3752 Primitive::Type value_type = GetValue()->GetType();
3753 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
3754 ? value_type
3755 : expected_component_type_;
3756 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003757
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003758 DECLARE_INSTRUCTION(ArraySet);
3759
3760 private:
3761 const uint32_t dex_pc_;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003762 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003763 bool needs_type_check_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003764 bool value_can_be_null_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003765
3766 DISALLOW_COPY_AND_ASSIGN(HArraySet);
3767};
3768
3769class HArrayLength : public HExpression<1> {
3770 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003771 explicit HArrayLength(HInstruction* array)
3772 : HExpression(Primitive::kPrimInt, SideEffects::None()) {
3773 // Note that arrays do not change length, so the instruction does not
3774 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003775 SetRawInputAt(0, array);
3776 }
3777
Calin Juravle77520bc2015-01-12 18:45:46 +00003778 bool CanBeMoved() const OVERRIDE { return true; }
3779 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003780 UNUSED(other);
3781 return true;
3782 }
Calin Juravle641547a2015-04-21 22:08:51 +01003783 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3784 return obj == InputAt(0);
3785 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003786
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003787 DECLARE_INSTRUCTION(ArrayLength);
3788
3789 private:
3790 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
3791};
3792
3793class HBoundsCheck : public HExpression<2> {
3794 public:
3795 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003796 : HExpression(index->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003797 DCHECK(index->GetType() == Primitive::kPrimInt);
3798 SetRawInputAt(0, index);
3799 SetRawInputAt(1, length);
3800 }
3801
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003802 bool CanBeMoved() const OVERRIDE { return true; }
3803 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003804 UNUSED(other);
3805 return true;
3806 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003807
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003808 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003809
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003810 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01003811
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003812 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003813
3814 DECLARE_INSTRUCTION(BoundsCheck);
3815
3816 private:
3817 const uint32_t dex_pc_;
3818
3819 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
3820};
3821
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003822/**
3823 * Some DEX instructions are folded into multiple HInstructions that need
3824 * to stay live until the last HInstruction. This class
3825 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00003826 * HInstruction stays live. `index` represents the stack location index of the
3827 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003828 */
3829class HTemporary : public HTemplateInstruction<0> {
3830 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003831 explicit HTemporary(size_t index) : HTemplateInstruction(SideEffects::None()), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003832
3833 size_t GetIndex() const { return index_; }
3834
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00003835 Primitive::Type GetType() const OVERRIDE {
3836 // The previous instruction is the one that will be stored in the temporary location.
3837 DCHECK(GetPrevious() != nullptr);
3838 return GetPrevious()->GetType();
3839 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00003840
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003841 DECLARE_INSTRUCTION(Temporary);
3842
3843 private:
3844 const size_t index_;
3845
3846 DISALLOW_COPY_AND_ASSIGN(HTemporary);
3847};
3848
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003849class HSuspendCheck : public HTemplateInstruction<0> {
3850 public:
3851 explicit HSuspendCheck(uint32_t dex_pc)
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01003852 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003853
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003854 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003855 return true;
3856 }
3857
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003858 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01003859 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
3860 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003861
3862 DECLARE_INSTRUCTION(SuspendCheck);
3863
3864 private:
3865 const uint32_t dex_pc_;
3866
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01003867 // Only used for code generation, in order to share the same slow path between back edges
3868 // of a same loop.
3869 SlowPathCode* slow_path_;
3870
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003871 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
3872};
3873
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003874/**
3875 * Instruction to load a Class object.
3876 */
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003877class HLoadClass : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003878 public:
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003879 HLoadClass(HCurrentMethod* current_method,
3880 uint16_t type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003881 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003882 bool is_referrers_class,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003883 uint32_t dex_pc)
3884 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3885 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003886 dex_file_(dex_file),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003887 is_referrers_class_(is_referrers_class),
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003888 dex_pc_(dex_pc),
Calin Juravleb1498f62015-02-16 13:13:29 +00003889 generate_clinit_check_(false),
Calin Juravle80caa142015-07-16 16:51:30 +01003890 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003891 SetRawInputAt(0, current_method);
3892 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003893
3894 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003895
3896 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3897 return other->AsLoadClass()->type_index_ == type_index_;
3898 }
3899
3900 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
3901
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003902 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003903 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003904 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01003905 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003906
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003907 bool NeedsEnvironment() const OVERRIDE {
3908 // Will call runtime and load the class if the class is not loaded yet.
3909 // TODO: finer grain decision.
3910 return !is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003911 }
3912
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003913 bool MustGenerateClinitCheck() const {
3914 return generate_clinit_check_;
3915 }
3916
Calin Juravle0ba218d2015-05-19 18:46:01 +01003917 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
3918 generate_clinit_check_ = generate_clinit_check;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003919 }
3920
3921 bool CanCallRuntime() const {
3922 return MustGenerateClinitCheck() || !is_referrers_class_;
3923 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003924
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003925 bool CanThrow() const OVERRIDE {
3926 // May call runtime and and therefore can throw.
3927 // TODO: finer grain decision.
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01003928 return CanCallRuntime();
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003929 }
3930
Calin Juravleacf735c2015-02-12 15:25:22 +00003931 ReferenceTypeInfo GetLoadedClassRTI() {
3932 return loaded_class_rti_;
3933 }
3934
3935 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
3936 // Make sure we only set exact types (the loaded class should never be merged).
3937 DCHECK(rti.IsExact());
3938 loaded_class_rti_ = rti;
3939 }
3940
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003941 const DexFile& GetDexFile() { return dex_file_; }
3942
Nicolas Geoffray9437b782015-03-25 10:08:51 +00003943 bool NeedsDexCache() const OVERRIDE { return !is_referrers_class_; }
3944
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003945 DECLARE_INSTRUCTION(LoadClass);
3946
3947 private:
3948 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003949 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003950 const bool is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003951 const uint32_t dex_pc_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003952 // Whether this instruction must generate the initialization check.
3953 // Used for code generation.
3954 bool generate_clinit_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003955
Calin Juravleacf735c2015-02-12 15:25:22 +00003956 ReferenceTypeInfo loaded_class_rti_;
3957
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003958 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
3959};
3960
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01003961class HLoadString : public HExpression<1> {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003962 public:
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01003963 HLoadString(HCurrentMethod* current_method, uint32_t string_index, uint32_t dex_pc)
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003964 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3965 string_index_(string_index),
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01003966 dex_pc_(dex_pc) {
3967 SetRawInputAt(0, current_method);
3968 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003969
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003970 bool CanBeMoved() const OVERRIDE { return true; }
3971
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003972 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3973 return other->AsLoadString()->string_index_ == string_index_;
3974 }
3975
3976 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
3977
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003978 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003979 uint32_t GetStringIndex() const { return string_index_; }
3980
3981 // TODO: Can we deopt or debug when we resolve a string?
3982 bool NeedsEnvironment() const OVERRIDE { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00003983 bool NeedsDexCache() const OVERRIDE { return true; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003984
3985 DECLARE_INSTRUCTION(LoadString);
3986
3987 private:
3988 const uint32_t string_index_;
3989 const uint32_t dex_pc_;
3990
3991 DISALLOW_COPY_AND_ASSIGN(HLoadString);
3992};
3993
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003994/**
3995 * Performs an initialization check on its Class object input.
3996 */
3997class HClinitCheck : public HExpression<1> {
3998 public:
3999 explicit HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07004000 : HExpression(
4001 Primitive::kPrimNot,
Aart Bik34c3ba92015-07-20 14:08:59 -07004002 SideEffects::AllWrites()), // assume write on all fields/arrays
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004003 dex_pc_(dex_pc) {
4004 SetRawInputAt(0, constant);
4005 }
4006
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004007 bool CanBeMoved() const OVERRIDE { return true; }
4008 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4009 UNUSED(other);
4010 return true;
4011 }
4012
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004013 bool NeedsEnvironment() const OVERRIDE {
4014 // May call runtime to initialize the class.
4015 return true;
4016 }
4017
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004018 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004019
4020 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
4021
4022 DECLARE_INSTRUCTION(ClinitCheck);
4023
4024 private:
4025 const uint32_t dex_pc_;
4026
4027 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
4028};
4029
4030class HStaticFieldGet : public HExpression<1> {
4031 public:
4032 HStaticFieldGet(HInstruction* cls,
4033 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004034 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004035 bool is_volatile,
4036 uint32_t field_idx,
4037 const DexFile& dex_file)
Aart Bik34c3ba92015-07-20 14:08:59 -07004038 : HExpression(
4039 field_type,
Alexandre Rames1c4ccea2015-07-22 11:32:58 +01004040 SideEffects::FieldReadOfType(field_type, is_volatile)),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004041 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004042 SetRawInputAt(0, cls);
4043 }
4044
Calin Juravle52c48962014-12-16 17:02:57 +00004045
Calin Juravle10c9cbe2014-12-19 10:50:19 +00004046 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004047
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004048 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00004049 HStaticFieldGet* other_get = other->AsStaticFieldGet();
4050 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004051 }
4052
4053 size_t ComputeHashCode() const OVERRIDE {
4054 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
4055 }
4056
Calin Juravle52c48962014-12-16 17:02:57 +00004057 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004058 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
4059 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004060 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004061
4062 DECLARE_INSTRUCTION(StaticFieldGet);
4063
4064 private:
4065 const FieldInfo field_info_;
4066
4067 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
4068};
4069
4070class HStaticFieldSet : public HTemplateInstruction<2> {
4071 public:
4072 HStaticFieldSet(HInstruction* cls,
4073 HInstruction* value,
4074 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004075 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004076 bool is_volatile,
4077 uint32_t field_idx,
4078 const DexFile& dex_file)
Aart Bik34c3ba92015-07-20 14:08:59 -07004079 : HTemplateInstruction(
4080 SideEffects::FieldWriteOfType(field_type, is_volatile)),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004081 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004082 value_can_be_null_(true) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004083 SetRawInputAt(0, cls);
4084 SetRawInputAt(1, value);
4085 }
4086
Calin Juravle52c48962014-12-16 17:02:57 +00004087 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004088 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
4089 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004090 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004091
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004092 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004093 bool GetValueCanBeNull() const { return value_can_be_null_; }
4094 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004095
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004096 DECLARE_INSTRUCTION(StaticFieldSet);
4097
4098 private:
4099 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004100 bool value_can_be_null_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004101
4102 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
4103};
4104
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004105// Implement the move-exception DEX instruction.
4106class HLoadException : public HExpression<0> {
4107 public:
4108 HLoadException() : HExpression(Primitive::kPrimNot, SideEffects::None()) {}
4109
4110 DECLARE_INSTRUCTION(LoadException);
4111
4112 private:
4113 DISALLOW_COPY_AND_ASSIGN(HLoadException);
4114};
4115
4116class HThrow : public HTemplateInstruction<1> {
4117 public:
4118 HThrow(HInstruction* exception, uint32_t dex_pc)
4119 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {
4120 SetRawInputAt(0, exception);
4121 }
4122
4123 bool IsControlFlow() const OVERRIDE { return true; }
4124
4125 bool NeedsEnvironment() const OVERRIDE { return true; }
4126
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004127 bool CanThrow() const OVERRIDE { return true; }
4128
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004129 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004130
4131 DECLARE_INSTRUCTION(Throw);
4132
4133 private:
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004134 const uint32_t dex_pc_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004135
4136 DISALLOW_COPY_AND_ASSIGN(HThrow);
4137};
4138
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004139class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004140 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004141 HInstanceOf(HInstruction* object,
4142 HLoadClass* constant,
4143 bool class_is_final,
4144 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004145 : HExpression(Primitive::kPrimBoolean, SideEffects::None()),
4146 class_is_final_(class_is_final),
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004147 must_do_null_check_(true),
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004148 dex_pc_(dex_pc) {
4149 SetRawInputAt(0, object);
4150 SetRawInputAt(1, constant);
4151 }
4152
4153 bool CanBeMoved() const OVERRIDE { return true; }
4154
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004155 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004156 return true;
4157 }
4158
4159 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004160 return false;
4161 }
4162
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004163 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004164
4165 bool IsClassFinal() const { return class_is_final_; }
4166
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004167 // Used only in code generation.
4168 bool MustDoNullCheck() const { return must_do_null_check_; }
4169 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
4170
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004171 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004172
4173 private:
4174 const bool class_is_final_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004175 bool must_do_null_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004176 const uint32_t dex_pc_;
4177
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004178 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
4179};
4180
Calin Juravleb1498f62015-02-16 13:13:29 +00004181class HBoundType : public HExpression<1> {
4182 public:
Calin Juravle80caa142015-07-16 16:51:30 +01004183 // Constructs an HBoundType with the given upper_bound.
4184 // Ensures that the upper_bound is valid.
Calin Juravle00e3b382015-07-15 14:41:29 +01004185 HBoundType(HInstruction* input, ReferenceTypeInfo upper_bound, bool upper_can_be_null)
Calin Juravleb1498f62015-02-16 13:13:29 +00004186 : HExpression(Primitive::kPrimNot, SideEffects::None()),
Calin Juravle00e3b382015-07-15 14:41:29 +01004187 upper_bound_(upper_bound),
4188 upper_can_be_null_(upper_can_be_null),
4189 can_be_null_(upper_can_be_null) {
Calin Juravle61d544b2015-02-23 16:46:57 +00004190 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00004191 SetRawInputAt(0, input);
Calin Juravle80caa142015-07-16 16:51:30 +01004192 SetReferenceTypeInfo(upper_bound_);
Calin Juravleb1498f62015-02-16 13:13:29 +00004193 }
4194
Calin Juravle00e3b382015-07-15 14:41:29 +01004195 // GetUpper* should only be used in reference type propagation.
4196 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
4197 bool GetUpperCanBeNull() const { return upper_can_be_null_; }
Calin Juravleb1498f62015-02-16 13:13:29 +00004198
Calin Juravle00e3b382015-07-15 14:41:29 +01004199 void SetCanBeNull(bool can_be_null) {
4200 DCHECK(upper_can_be_null_ || !can_be_null);
4201 can_be_null_ = can_be_null;
Calin Juravleb1498f62015-02-16 13:13:29 +00004202 }
4203
Calin Juravle00e3b382015-07-15 14:41:29 +01004204 bool CanBeNull() const OVERRIDE { return can_be_null_; }
4205
Calin Juravleb1498f62015-02-16 13:13:29 +00004206 DECLARE_INSTRUCTION(BoundType);
4207
4208 private:
4209 // Encodes the most upper class that this instruction can have. In other words
Calin Juravle00e3b382015-07-15 14:41:29 +01004210 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
4211 // It is used to bound the type in cases like:
4212 // if (x instanceof ClassX) {
4213 // // uper_bound_ will be ClassX
4214 // }
4215 const ReferenceTypeInfo upper_bound_;
4216 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
4217 // is false then can_be_null_ cannot be true).
4218 const bool upper_can_be_null_;
4219 bool can_be_null_;
Calin Juravleb1498f62015-02-16 13:13:29 +00004220
4221 DISALLOW_COPY_AND_ASSIGN(HBoundType);
4222};
4223
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004224class HCheckCast : public HTemplateInstruction<2> {
4225 public:
4226 HCheckCast(HInstruction* object,
4227 HLoadClass* constant,
4228 bool class_is_final,
4229 uint32_t dex_pc)
4230 : HTemplateInstruction(SideEffects::None()),
4231 class_is_final_(class_is_final),
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004232 must_do_null_check_(true),
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004233 dex_pc_(dex_pc) {
4234 SetRawInputAt(0, object);
4235 SetRawInputAt(1, constant);
4236 }
4237
4238 bool CanBeMoved() const OVERRIDE { return true; }
4239
4240 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
4241 return true;
4242 }
4243
4244 bool NeedsEnvironment() const OVERRIDE {
4245 // Instruction may throw a CheckCastError.
4246 return true;
4247 }
4248
4249 bool CanThrow() const OVERRIDE { return true; }
4250
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004251 bool MustDoNullCheck() const { return must_do_null_check_; }
4252 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
4253
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004254 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004255
4256 bool IsClassFinal() const { return class_is_final_; }
4257
4258 DECLARE_INSTRUCTION(CheckCast);
4259
4260 private:
4261 const bool class_is_final_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004262 bool must_do_null_check_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004263 const uint32_t dex_pc_;
4264
4265 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004266};
4267
Calin Juravle27df7582015-04-17 19:12:31 +01004268class HMemoryBarrier : public HTemplateInstruction<0> {
4269 public:
4270 explicit HMemoryBarrier(MemBarrierKind barrier_kind)
Aart Bik34c3ba92015-07-20 14:08:59 -07004271 : HTemplateInstruction(
4272 SideEffects::All()), // assume write/read on all fields/arrays
Calin Juravle27df7582015-04-17 19:12:31 +01004273 barrier_kind_(barrier_kind) {}
4274
4275 MemBarrierKind GetBarrierKind() { return barrier_kind_; }
4276
4277 DECLARE_INSTRUCTION(MemoryBarrier);
4278
4279 private:
4280 const MemBarrierKind barrier_kind_;
4281
4282 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
4283};
4284
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004285class HMonitorOperation : public HTemplateInstruction<1> {
4286 public:
4287 enum OperationKind {
4288 kEnter,
4289 kExit,
4290 };
4291
4292 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07004293 : HTemplateInstruction(SideEffects::All()), // assume write/read on all fields/arrays
4294 kind_(kind), dex_pc_(dex_pc) {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004295 SetRawInputAt(0, object);
4296 }
4297
4298 // Instruction may throw a Java exception, so we need an environment.
David Brazdilbff75032015-07-08 17:26:51 +00004299 bool NeedsEnvironment() const OVERRIDE { return CanThrow(); }
4300
4301 bool CanThrow() const OVERRIDE {
4302 // Verifier guarantees that monitor-exit cannot throw.
4303 // This is important because it allows the HGraphBuilder to remove
4304 // a dead throw-catch loop generated for `synchronized` blocks/methods.
4305 return IsEnter();
4306 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004307
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004308 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004309
4310 bool IsEnter() const { return kind_ == kEnter; }
4311
4312 DECLARE_INSTRUCTION(MonitorOperation);
4313
Calin Juravle52c48962014-12-16 17:02:57 +00004314 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004315 const OperationKind kind_;
4316 const uint32_t dex_pc_;
4317
4318 private:
4319 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
4320};
4321
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01004322/**
4323 * A HInstruction used as a marker for the replacement of new + <init>
4324 * of a String to a call to a StringFactory. Only baseline will see
4325 * the node at code generation, where it will be be treated as null.
4326 * When compiling non-baseline, `HFakeString` instructions are being removed
4327 * in the instruction simplifier.
4328 */
4329class HFakeString : public HTemplateInstruction<0> {
4330 public:
4331 HFakeString() : HTemplateInstruction(SideEffects::None()) {}
4332
4333 Primitive::Type GetType() const OVERRIDE { return Primitive::kPrimNot; }
4334
4335 DECLARE_INSTRUCTION(FakeString);
4336
4337 private:
4338 DISALLOW_COPY_AND_ASSIGN(HFakeString);
4339};
4340
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004341class MoveOperands : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004342 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01004343 MoveOperands(Location source,
4344 Location destination,
4345 Primitive::Type type,
4346 HInstruction* instruction)
4347 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004348
4349 Location GetSource() const { return source_; }
4350 Location GetDestination() const { return destination_; }
4351
4352 void SetSource(Location value) { source_ = value; }
4353 void SetDestination(Location value) { destination_ = value; }
4354
4355 // The parallel move resolver marks moves as "in-progress" by clearing the
4356 // destination (but not the source).
4357 Location MarkPending() {
4358 DCHECK(!IsPending());
4359 Location dest = destination_;
4360 destination_ = Location::NoLocation();
4361 return dest;
4362 }
4363
4364 void ClearPending(Location dest) {
4365 DCHECK(IsPending());
4366 destination_ = dest;
4367 }
4368
4369 bool IsPending() const {
4370 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
4371 return destination_.IsInvalid() && !source_.IsInvalid();
4372 }
4373
4374 // True if this blocks a move from the given location.
4375 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08004376 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004377 }
4378
4379 // A move is redundant if it's been eliminated, if its source and
4380 // destination are the same, or if its destination is unneeded.
4381 bool IsRedundant() const {
4382 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
4383 }
4384
4385 // We clear both operands to indicate move that's been eliminated.
4386 void Eliminate() {
4387 source_ = destination_ = Location::NoLocation();
4388 }
4389
4390 bool IsEliminated() const {
4391 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
4392 return source_.IsInvalid();
4393 }
4394
Alexey Frunze4dda3372015-06-01 18:31:49 -07004395 Primitive::Type GetType() const { return type_; }
4396
Nicolas Geoffray90218252015-04-15 11:56:51 +01004397 bool Is64BitMove() const {
4398 return Primitive::Is64BitType(type_);
4399 }
4400
Nicolas Geoffray740475d2014-09-29 10:33:25 +01004401 HInstruction* GetInstruction() const { return instruction_; }
4402
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004403 private:
4404 Location source_;
4405 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01004406 // The type this move is for.
4407 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01004408 // The instruction this move is assocatied with. Null when this move is
4409 // for moving an input in the expected locations of user (including a phi user).
4410 // This is only used in debug mode, to ensure we do not connect interval siblings
4411 // in the same parallel move.
4412 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004413};
4414
4415static constexpr size_t kDefaultNumberOfMoves = 4;
4416
4417class HParallelMove : public HTemplateInstruction<0> {
4418 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004419 explicit HParallelMove(ArenaAllocator* arena)
4420 : HTemplateInstruction(SideEffects::None()), moves_(arena, kDefaultNumberOfMoves) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004421
Nicolas Geoffray90218252015-04-15 11:56:51 +01004422 void AddMove(Location source,
4423 Location destination,
4424 Primitive::Type type,
4425 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004426 DCHECK(source.IsValid());
4427 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004428 if (kIsDebugBuild) {
4429 if (instruction != nullptr) {
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00004430 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004431 if (moves_.Get(i).GetInstruction() == instruction) {
4432 // Special case the situation where the move is for the spill slot
4433 // of the instruction.
4434 if ((GetPrevious() == instruction)
4435 || ((GetPrevious() == nullptr)
4436 && instruction->IsPhi()
4437 && instruction->GetBlock() == GetBlock())) {
4438 DCHECK_NE(destination.GetKind(), moves_.Get(i).GetDestination().GetKind())
4439 << "Doing parallel moves for the same instruction.";
4440 } else {
4441 DCHECK(false) << "Doing parallel moves for the same instruction.";
4442 }
4443 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00004444 }
4445 }
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004446 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Zheng Xuad4450e2015-04-17 18:48:56 +08004447 DCHECK(!destination.OverlapsWith(moves_.Get(i).GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01004448 << "Overlapped destination for two moves in a parallel move: "
4449 << moves_.Get(i).GetSource() << " ==> " << moves_.Get(i).GetDestination() << " and "
4450 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004451 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01004452 }
Nicolas Geoffray90218252015-04-15 11:56:51 +01004453 moves_.Add(MoveOperands(source, destination, type, instruction));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004454 }
4455
4456 MoveOperands* MoveOperandsAt(size_t index) const {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004457 return moves_.GetRawStorage() + index;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004458 }
4459
4460 size_t NumMoves() const { return moves_.Size(); }
4461
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004462 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004463
4464 private:
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004465 GrowableArray<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004466
4467 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
4468};
4469
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004470class HGraphVisitor : public ValueObject {
4471 public:
Dave Allison20dfc792014-06-16 20:44:29 -07004472 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
4473 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004474
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004475 virtual void VisitInstruction(HInstruction* instruction) { UNUSED(instruction); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004476 virtual void VisitBasicBlock(HBasicBlock* block);
4477
Roland Levillain633021e2014-10-01 14:12:25 +01004478 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004479 void VisitInsertionOrder();
4480
Roland Levillain633021e2014-10-01 14:12:25 +01004481 // Visit the graph following dominator tree reverse post-order.
4482 void VisitReversePostOrder();
4483
Nicolas Geoffray787c3072014-03-17 10:20:19 +00004484 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00004485
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004486 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004487#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004488 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
4489
4490 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
4491
4492#undef DECLARE_VISIT_INSTRUCTION
4493
4494 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07004495 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004496
4497 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
4498};
4499
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004500class HGraphDelegateVisitor : public HGraphVisitor {
4501 public:
4502 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
4503 virtual ~HGraphDelegateVisitor() {}
4504
4505 // Visit functions that delegate to to super class.
4506#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004507 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004508
4509 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
4510
4511#undef DECLARE_VISIT_INSTRUCTION
4512
4513 private:
4514 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
4515};
4516
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004517class HInsertionOrderIterator : public ValueObject {
4518 public:
4519 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
4520
4521 bool Done() const { return index_ == graph_.GetBlocks().Size(); }
4522 HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); }
4523 void Advance() { ++index_; }
4524
4525 private:
4526 const HGraph& graph_;
4527 size_t index_;
4528
4529 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
4530};
4531
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004532class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004533 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00004534 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
4535 // Check that reverse post order of the graph has been built.
4536 DCHECK(!graph.GetReversePostOrder().IsEmpty());
4537 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004538
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004539 bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); }
4540 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004541 void Advance() { ++index_; }
4542
4543 private:
4544 const HGraph& graph_;
4545 size_t index_;
4546
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004547 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004548};
4549
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004550class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004551 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004552 explicit HPostOrderIterator(const HGraph& graph)
David Brazdil10f56cb2015-03-24 18:49:14 +00004553 : graph_(graph), index_(graph_.GetReversePostOrder().Size()) {
4554 // Check that reverse post order of the graph has been built.
4555 DCHECK(!graph.GetReversePostOrder().IsEmpty());
4556 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004557
4558 bool Done() const { return index_ == 0; }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004559 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004560 void Advance() { --index_; }
4561
4562 private:
4563 const HGraph& graph_;
4564 size_t index_;
4565
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004566 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004567};
4568
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01004569class HLinearPostOrderIterator : public ValueObject {
4570 public:
4571 explicit HLinearPostOrderIterator(const HGraph& graph)
4572 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().Size()) {}
4573
4574 bool Done() const { return index_ == 0; }
4575
4576 HBasicBlock* Current() const { return order_.Get(index_ -1); }
4577
4578 void Advance() {
4579 --index_;
4580 DCHECK_GE(index_, 0U);
4581 }
4582
4583 private:
4584 const GrowableArray<HBasicBlock*>& order_;
4585 size_t index_;
4586
4587 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
4588};
4589
4590class HLinearOrderIterator : public ValueObject {
4591 public:
4592 explicit HLinearOrderIterator(const HGraph& graph)
4593 : order_(graph.GetLinearOrder()), index_(0) {}
4594
4595 bool Done() const { return index_ == order_.Size(); }
4596 HBasicBlock* Current() const { return order_.Get(index_); }
4597 void Advance() { ++index_; }
4598
4599 private:
4600 const GrowableArray<HBasicBlock*>& order_;
4601 size_t index_;
4602
4603 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
4604};
4605
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004606// Iterator over the blocks that art part of the loop. Includes blocks part
4607// of an inner loop. The order in which the blocks are iterated is on their
4608// block id.
4609class HBlocksInLoopIterator : public ValueObject {
4610 public:
4611 explicit HBlocksInLoopIterator(const HLoopInformation& info)
4612 : blocks_in_loop_(info.GetBlocks()),
4613 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
4614 index_(0) {
4615 if (!blocks_in_loop_.IsBitSet(index_)) {
4616 Advance();
4617 }
4618 }
4619
4620 bool Done() const { return index_ == blocks_.Size(); }
4621 HBasicBlock* Current() const { return blocks_.Get(index_); }
4622 void Advance() {
4623 ++index_;
4624 for (size_t e = blocks_.Size(); index_ < e; ++index_) {
4625 if (blocks_in_loop_.IsBitSet(index_)) {
4626 break;
4627 }
4628 }
4629 }
4630
4631 private:
4632 const BitVector& blocks_in_loop_;
4633 const GrowableArray<HBasicBlock*>& blocks_;
4634 size_t index_;
4635
4636 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
4637};
4638
Mingyao Yang3584bce2015-05-19 16:01:59 -07004639// Iterator over the blocks that art part of the loop. Includes blocks part
4640// of an inner loop. The order in which the blocks are iterated is reverse
4641// post order.
4642class HBlocksInLoopReversePostOrderIterator : public ValueObject {
4643 public:
4644 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
4645 : blocks_in_loop_(info.GetBlocks()),
4646 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
4647 index_(0) {
4648 if (!blocks_in_loop_.IsBitSet(blocks_.Get(index_)->GetBlockId())) {
4649 Advance();
4650 }
4651 }
4652
4653 bool Done() const { return index_ == blocks_.Size(); }
4654 HBasicBlock* Current() const { return blocks_.Get(index_); }
4655 void Advance() {
4656 ++index_;
4657 for (size_t e = blocks_.Size(); index_ < e; ++index_) {
4658 if (blocks_in_loop_.IsBitSet(blocks_.Get(index_)->GetBlockId())) {
4659 break;
4660 }
4661 }
4662 }
4663
4664 private:
4665 const BitVector& blocks_in_loop_;
4666 const GrowableArray<HBasicBlock*>& blocks_;
4667 size_t index_;
4668
4669 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
4670};
4671
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00004672inline int64_t Int64FromConstant(HConstant* constant) {
4673 DCHECK(constant->IsIntConstant() || constant->IsLongConstant());
4674 return constant->IsIntConstant() ? constant->AsIntConstant()->GetValue()
4675 : constant->AsLongConstant()->GetValue();
4676}
4677
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004678} // namespace art
4679
4680#endif // ART_COMPILER_OPTIMIZING_NODES_H_