blob: badb2bfe134cfa4ace2860bd2298f061948bbd69 [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 Bik854a02b2015-07-14 16:07:00 -07001216 static SideEffects FieldWriteOfType(Primitive::Type type) {
1217 return SideEffects(TypeFlagWithAlias(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001218 }
1219
Aart Bik854a02b2015-07-14 16:07:00 -07001220 static SideEffects ArrayWriteOfType(Primitive::Type type) {
1221 return SideEffects(TypeFlagWithAlias(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001222 }
1223
Aart Bik854a02b2015-07-14 16:07:00 -07001224 static SideEffects FieldReadOfType(Primitive::Type type) {
1225 return SideEffects(TypeFlagWithAlias(type, kFieldReadOffset));
1226 }
1227
1228 static SideEffects ArrayReadOfType(Primitive::Type type) {
1229 return SideEffects(TypeFlagWithAlias(type, kArrayReadOffset));
1230 }
1231
1232 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001233 SideEffects Union(SideEffects other) const {
1234 return SideEffects(flags_ | other.flags_);
1235 }
1236
Aart Bik854a02b2015-07-14 16:07:00 -07001237 // Returns true if something is written.
1238 bool DoesAnyWrite() const {
1239 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001240 }
1241
Aart Bik854a02b2015-07-14 16:07:00 -07001242 // Returns true if something is read.
1243 bool DoesAnyRead() const {
1244 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001245 }
1246
Aart Bik854a02b2015-07-14 16:07:00 -07001247 // Returns true if nothing is written or read.
1248 bool DoesNothing() const {
1249 return flags_ == 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001250 }
1251
Aart Bik854a02b2015-07-14 16:07:00 -07001252 // Returns true if potentially everything is written and read
1253 // (every type and every kind of access).
1254 bool DoesAll() const {
1255 return flags_ == (kAllWrites | kAllReads);
1256 }
1257
1258 // Returns true if this may read something written by other.
1259 bool MayDependOn(SideEffects other) const {
1260 const uint64_t reads = (flags_ & kAllReads) >> kFieldReadOffset;
1261 return (other.flags_ & reads);
1262 }
1263
1264 // Returns string representation of flags (for debugging only).
1265 // Format: |DFJISCBZL|DFJISCBZL|DFJISCBZL|DFJISCBZL|
1266 std::string ToString() const {
1267 static const char *kDebug = "LZBCSIJFD";
1268 std::string flags = "|";
1269 for (int s = 35; s >= 0; s--) {
1270 const int t = s % kBits;
1271 if ((flags_ >> s) & 1)
1272 flags += kDebug[t];
1273 if (t == 0)
1274 flags += "|";
1275 }
1276 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001277 }
1278
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001279 private:
Aart Bik854a02b2015-07-14 16:07:00 -07001280 static constexpr int kBits = 9;
1281 static constexpr int kFieldWriteOffset = 0 * kBits;
1282 static constexpr int kArrayWriteOffset = 1 * kBits;
1283 static constexpr int kFieldReadOffset = 2 * kBits;
1284 static constexpr int kArrayReadOffset = 3 * kBits;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001285
Aart Bik854a02b2015-07-14 16:07:00 -07001286 static constexpr uint64_t kAllWrites = 0x0003ffff;
1287 static constexpr uint64_t kAllReads = kAllWrites << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001288
Aart Bik854a02b2015-07-14 16:07:00 -07001289 // Work around the fact that HIR aliases I/F and J/D.
1290 // TODO: remove this interceptor once HIR types are clean
1291 static uint64_t TypeFlagWithAlias(Primitive::Type type, int offset) {
1292 switch (type) {
1293 case Primitive::kPrimInt:
1294 case Primitive::kPrimFloat:
1295 return TypeFlag(Primitive::kPrimInt, offset) |
1296 TypeFlag(Primitive::kPrimFloat, offset);
1297 case Primitive::kPrimLong:
1298 case Primitive::kPrimDouble:
1299 return TypeFlag(Primitive::kPrimLong, offset) |
1300 TypeFlag(Primitive::kPrimDouble, offset);
1301 default:
1302 return TypeFlag(type, offset);
1303 }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001304 }
1305
Aart Bik854a02b2015-07-14 16:07:00 -07001306 // Translates type to bit flag.
1307 static uint64_t TypeFlag(Primitive::Type type, int offset) {
1308 CHECK_NE(type, Primitive::kPrimVoid);
1309 const uint64_t one = 1;
1310 const int shift = type; // 0-based consecutive enum
1311 DCHECK_LE(kFieldWriteOffset, shift);
1312 DCHECK_LT(shift, kArrayWriteOffset);
1313 return one << (type + offset);
1314 }
1315
1316 // Private constructor on direct flags value.
1317 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1318
1319 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001320};
1321
David Brazdiled596192015-01-23 10:39:45 +00001322// A HEnvironment object contains the values of virtual registers at a given location.
1323class HEnvironment : public ArenaObject<kArenaAllocMisc> {
1324 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001325 HEnvironment(ArenaAllocator* arena,
1326 size_t number_of_vregs,
1327 const DexFile& dex_file,
1328 uint32_t method_idx,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001329 uint32_t dex_pc,
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001330 InvokeType invoke_type,
1331 HInstruction* holder)
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001332 : vregs_(arena, number_of_vregs),
1333 locations_(arena, number_of_vregs),
1334 parent_(nullptr),
1335 dex_file_(dex_file),
1336 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001337 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001338 invoke_type_(invoke_type),
1339 holder_(holder) {
David Brazdiled596192015-01-23 10:39:45 +00001340 vregs_.SetSize(number_of_vregs);
1341 for (size_t i = 0; i < number_of_vregs; i++) {
David Brazdil1abb4192015-02-17 18:33:36 +00001342 vregs_.Put(i, HUserRecord<HEnvironment*>());
David Brazdiled596192015-01-23 10:39:45 +00001343 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001344
1345 locations_.SetSize(number_of_vregs);
1346 for (size_t i = 0; i < number_of_vregs; ++i) {
1347 locations_.Put(i, Location());
1348 }
1349 }
1350
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001351 HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001352 : HEnvironment(arena,
1353 to_copy.Size(),
1354 to_copy.GetDexFile(),
1355 to_copy.GetMethodIdx(),
1356 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001357 to_copy.GetInvokeType(),
1358 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001359
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001360 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001361 if (parent_ != nullptr) {
1362 parent_->SetAndCopyParentChain(allocator, parent);
1363 } else {
1364 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1365 parent_->CopyFrom(parent);
1366 if (parent->GetParent() != nullptr) {
1367 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1368 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001369 }
David Brazdiled596192015-01-23 10:39:45 +00001370 }
1371
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001372 void CopyFrom(const GrowableArray<HInstruction*>& locals);
1373 void CopyFrom(HEnvironment* environment);
1374
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001375 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1376 // input to the loop phi instead. This is for inserting instructions that
1377 // require an environment (like HDeoptimization) in the loop pre-header.
1378 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001379
1380 void SetRawEnvAt(size_t index, HInstruction* instruction) {
David Brazdil1abb4192015-02-17 18:33:36 +00001381 vregs_.Put(index, HUserRecord<HEnvironment*>(instruction));
David Brazdiled596192015-01-23 10:39:45 +00001382 }
1383
1384 HInstruction* GetInstructionAt(size_t index) const {
David Brazdil1abb4192015-02-17 18:33:36 +00001385 return vregs_.Get(index).GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001386 }
1387
David Brazdil1abb4192015-02-17 18:33:36 +00001388 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001389
1390 size_t Size() const { return vregs_.Size(); }
1391
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001392 HEnvironment* GetParent() const { return parent_; }
1393
1394 void SetLocationAt(size_t index, Location location) {
1395 locations_.Put(index, location);
1396 }
1397
1398 Location GetLocationAt(size_t index) const {
1399 return locations_.Get(index);
1400 }
1401
1402 uint32_t GetDexPc() const {
1403 return dex_pc_;
1404 }
1405
1406 uint32_t GetMethodIdx() const {
1407 return method_idx_;
1408 }
1409
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001410 InvokeType GetInvokeType() const {
1411 return invoke_type_;
1412 }
1413
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001414 const DexFile& GetDexFile() const {
1415 return dex_file_;
1416 }
1417
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001418 HInstruction* GetHolder() const {
1419 return holder_;
1420 }
1421
David Brazdiled596192015-01-23 10:39:45 +00001422 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001423 // Record instructions' use entries of this environment for constant-time removal.
1424 // It should only be called by HInstruction when a new environment use is added.
1425 void RecordEnvUse(HUseListNode<HEnvironment*>* env_use) {
1426 DCHECK(env_use->GetUser() == this);
1427 size_t index = env_use->GetIndex();
1428 vregs_.Put(index, HUserRecord<HEnvironment*>(vregs_.Get(index), env_use));
1429 }
David Brazdiled596192015-01-23 10:39:45 +00001430
David Brazdil1abb4192015-02-17 18:33:36 +00001431 GrowableArray<HUserRecord<HEnvironment*> > vregs_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001432 GrowableArray<Location> locations_;
1433 HEnvironment* parent_;
1434 const DexFile& dex_file_;
1435 const uint32_t method_idx_;
1436 const uint32_t dex_pc_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001437 const InvokeType invoke_type_;
David Brazdiled596192015-01-23 10:39:45 +00001438
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001439 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001440 HInstruction* const holder_;
1441
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001442 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001443
1444 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1445};
1446
Calin Juravleacf735c2015-02-12 15:25:22 +00001447class ReferenceTypeInfo : ValueObject {
1448 public:
Calin Juravleb1498f62015-02-16 13:13:29 +00001449 typedef Handle<mirror::Class> TypeHandle;
1450
1451 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact)
1452 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1453 if (type_handle->IsObjectClass()) {
1454 // Override the type handle to be consistent with the case when we get to
1455 // Top but don't have the Object class available. It avoids having to guess
1456 // what value the type_handle has when it's Top.
1457 return ReferenceTypeInfo(TypeHandle(), is_exact, true);
1458 } else {
1459 return ReferenceTypeInfo(type_handle, is_exact, false);
1460 }
1461 }
1462
1463 static ReferenceTypeInfo CreateTop(bool is_exact) {
1464 return ReferenceTypeInfo(TypeHandle(), is_exact, true);
Calin Juravleacf735c2015-02-12 15:25:22 +00001465 }
1466
1467 bool IsExact() const { return is_exact_; }
1468 bool IsTop() const { return is_top_; }
Guillaume Sanchez222862c2015-06-09 18:33:02 +01001469 bool IsInterface() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1470 return !IsTop() && GetTypeHandle()->IsInterface();
1471 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001472
1473 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
1474
Calin Juravleb1498f62015-02-16 13:13:29 +00001475 bool IsSupertypeOf(ReferenceTypeInfo rti) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Calin Juravleacf735c2015-02-12 15:25:22 +00001476 if (IsTop()) {
1477 // Top (equivalent for java.lang.Object) is supertype of anything.
1478 return true;
1479 }
1480 if (rti.IsTop()) {
1481 // If we get here `this` is not Top() so it can't be a supertype.
1482 return false;
1483 }
1484 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
1485 }
1486
1487 // Returns true if the type information provide the same amount of details.
1488 // Note that it does not mean that the instructions have the same actual type
1489 // (e.g. tops are equal but they can be the result of a merge).
1490 bool IsEqual(ReferenceTypeInfo rti) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1491 if (IsExact() != rti.IsExact()) {
1492 return false;
1493 }
1494 if (IsTop() && rti.IsTop()) {
1495 // `Top` means java.lang.Object, so the types are equivalent.
1496 return true;
1497 }
1498 if (IsTop() || rti.IsTop()) {
1499 // If only one is top or object than they are not equivalent.
1500 // NB: We need this extra check because the type_handle of `Top` is invalid
1501 // and we cannot inspect its reference.
1502 return false;
1503 }
1504
1505 // Finally check the types.
1506 return GetTypeHandle().Get() == rti.GetTypeHandle().Get();
1507 }
1508
1509 private:
Calin Juravleb1498f62015-02-16 13:13:29 +00001510 ReferenceTypeInfo() : ReferenceTypeInfo(TypeHandle(), false, true) {}
1511 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact, bool is_top)
1512 : type_handle_(type_handle), is_exact_(is_exact), is_top_(is_top) {}
1513
Calin Juravleacf735c2015-02-12 15:25:22 +00001514 // The class of the object.
Calin Juravleb1498f62015-02-16 13:13:29 +00001515 TypeHandle type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001516 // Whether or not the type is exact or a superclass of the actual type.
Calin Juravleb1498f62015-02-16 13:13:29 +00001517 // Whether or not we have any information about this type.
Calin Juravleacf735c2015-02-12 15:25:22 +00001518 bool is_exact_;
1519 // A true value here means that the object type should be java.lang.Object.
1520 // We don't have access to the corresponding mirror object every time so this
1521 // flag acts as a substitute. When true, the TypeHandle refers to a null
1522 // pointer and should not be used.
1523 bool is_top_;
1524};
1525
1526std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
1527
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001528class HInstruction : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001529 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001530 explicit HInstruction(SideEffects side_effects)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001531 : previous_(nullptr),
1532 next_(nullptr),
1533 block_(nullptr),
1534 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001535 ssa_index_(-1),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001536 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001537 locations_(nullptr),
1538 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001539 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001540 side_effects_(side_effects),
1541 reference_type_info_(ReferenceTypeInfo::CreateTop(/* is_exact */ false)) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001542
Dave Allison20dfc792014-06-16 20:44:29 -07001543 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001544
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001545#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001546 enum InstructionKind {
1547 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1548 };
1549#undef DECLARE_KIND
1550
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001551 HInstruction* GetNext() const { return next_; }
1552 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001553
Calin Juravle77520bc2015-01-12 18:45:46 +00001554 HInstruction* GetNextDisregardingMoves() const;
1555 HInstruction* GetPreviousDisregardingMoves() const;
1556
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001557 HBasicBlock* GetBlock() const { return block_; }
1558 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001559 bool IsInBlock() const { return block_ != nullptr; }
1560 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +01001561 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001562
Roland Levillain6b879dd2014-09-22 17:13:44 +01001563 virtual size_t InputCount() const = 0;
David Brazdil1abb4192015-02-17 18:33:36 +00001564 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001565
1566 virtual void Accept(HGraphVisitor* visitor) = 0;
1567 virtual const char* DebugName() const = 0;
1568
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001569 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
David Brazdil1abb4192015-02-17 18:33:36 +00001570 void SetRawInputAt(size_t index, HInstruction* input) {
1571 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1572 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001573
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001574 virtual bool NeedsEnvironment() const { return false; }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001575 virtual uint32_t GetDexPc() const {
1576 LOG(FATAL) << "GetDexPc() cannot be called on an instruction that"
1577 " does not need an environment";
1578 UNREACHABLE();
1579 }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001580 virtual bool IsControlFlow() const { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01001581 virtual bool CanThrow() const { return false; }
Aart Bik854a02b2015-07-14 16:07:00 -07001582
1583 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001584
Calin Juravle10e244f2015-01-26 18:54:32 +00001585 // Does not apply for all instructions, but having this at top level greatly
1586 // simplifies the null check elimination.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001587 virtual bool CanBeNull() const {
1588 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1589 return true;
1590 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001591
Calin Juravle641547a2015-04-21 22:08:51 +01001592 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj) const {
1593 UNUSED(obj);
1594 return false;
1595 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001596
Calin Juravleacf735c2015-02-12 15:25:22 +00001597 void SetReferenceTypeInfo(ReferenceTypeInfo reference_type_info) {
Calin Juravle61d544b2015-02-23 16:46:57 +00001598 DCHECK_EQ(GetType(), Primitive::kPrimNot);
Calin Juravleacf735c2015-02-12 15:25:22 +00001599 reference_type_info_ = reference_type_info;
1600 }
1601
Calin Juravle61d544b2015-02-23 16:46:57 +00001602 ReferenceTypeInfo GetReferenceTypeInfo() const {
1603 DCHECK_EQ(GetType(), Primitive::kPrimNot);
1604 return reference_type_info_;
1605 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001606
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001607 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001608 DCHECK(user != nullptr);
1609 HUseListNode<HInstruction*>* use =
1610 uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1611 user->SetRawInputRecordAt(index, HUserRecord<HInstruction*>(user->InputRecordAt(index), use));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001612 }
1613
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001614 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001615 DCHECK(user != nullptr);
David Brazdiled596192015-01-23 10:39:45 +00001616 HUseListNode<HEnvironment*>* env_use =
1617 env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1618 user->RecordEnvUse(env_use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001619 }
1620
David Brazdil1abb4192015-02-17 18:33:36 +00001621 void RemoveAsUserOfInput(size_t input) {
1622 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
1623 input_use.GetInstruction()->uses_.Remove(input_use.GetUseNode());
1624 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001625
David Brazdil1abb4192015-02-17 18:33:36 +00001626 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1627 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001628
David Brazdiled596192015-01-23 10:39:45 +00001629 bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); }
1630 bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); }
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001631 bool HasNonEnvironmentUses() const { return !uses_.IsEmpty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001632 bool HasOnlyOneNonEnvironmentUse() const {
1633 return !HasEnvironmentUses() && GetUses().HasOnlyOneUse();
1634 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001635
Roland Levillain6c82d402014-10-13 16:10:27 +01001636 // Does this instruction strictly dominate `other_instruction`?
1637 // Returns false if this instruction and `other_instruction` are the same.
1638 // Aborts if this instruction and `other_instruction` are both phis.
1639 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001640
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001641 int GetId() const { return id_; }
1642 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001643
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001644 int GetSsaIndex() const { return ssa_index_; }
1645 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1646 bool HasSsaIndex() const { return ssa_index_ != -1; }
1647
1648 bool HasEnvironment() const { return environment_ != nullptr; }
1649 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001650 // Set the `environment_` field. Raw because this method does not
1651 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001652 void SetRawEnvironment(HEnvironment* environment) {
1653 DCHECK(environment_ == nullptr);
1654 DCHECK_EQ(environment->GetHolder(), this);
1655 environment_ = environment;
1656 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001657
1658 // Set the environment of this instruction, copying it from `environment`. While
1659 // copying, the uses lists are being updated.
1660 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001661 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001662 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001663 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001664 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001665 if (environment->GetParent() != nullptr) {
1666 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1667 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001668 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001669
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001670 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
1671 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001672 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001673 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001674 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001675 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001676 if (environment->GetParent() != nullptr) {
1677 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1678 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001679 }
1680
Nicolas Geoffray39468442014-09-02 15:17:15 +01001681 // Returns the number of entries in the environment. Typically, that is the
1682 // number of dex registers in a method. It could be more in case of inlining.
1683 size_t EnvironmentSize() const;
1684
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001685 LocationSummary* GetLocations() const { return locations_; }
1686 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001687
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001688 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001689 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001690
Alexandre Rames188d4312015-04-09 18:30:21 +01001691 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
1692 // uses of this instruction by `other` are *not* updated.
1693 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
1694 ReplaceWith(other);
1695 other->ReplaceInput(this, use_index);
1696 }
1697
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001698 // Move `this` instruction before `cursor`.
1699 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001700
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001701#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01001702 bool Is##type() const { return (As##type() != nullptr); } \
1703 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001704 virtual H##type* As##type() { return nullptr; }
1705
1706 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
1707#undef INSTRUCTION_TYPE_CHECK
1708
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001709 // Returns whether the instruction can be moved within the graph.
1710 virtual bool CanBeMoved() const { return false; }
1711
1712 // Returns whether the two instructions are of the same kind.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001713 virtual bool InstructionTypeEquals(HInstruction* other) const {
1714 UNUSED(other);
1715 return false;
1716 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001717
1718 // Returns whether any data encoded in the two instructions is equal.
1719 // This method does not look at the inputs. Both instructions must be
1720 // of the same type, otherwise the method has undefined behavior.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001721 virtual bool InstructionDataEquals(HInstruction* other) const {
1722 UNUSED(other);
1723 return false;
1724 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001725
1726 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00001727 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001728 // 2) Their inputs are identical.
1729 bool Equals(HInstruction* other) const;
1730
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001731 virtual InstructionKind GetKind() const = 0;
1732
1733 virtual size_t ComputeHashCode() const {
1734 size_t result = GetKind();
1735 for (size_t i = 0, e = InputCount(); i < e; ++i) {
1736 result = (result * 31) + InputAt(i)->GetId();
1737 }
1738 return result;
1739 }
1740
1741 SideEffects GetSideEffects() const { return side_effects_; }
1742
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001743 size_t GetLifetimePosition() const { return lifetime_position_; }
1744 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
1745 LiveInterval* GetLiveInterval() const { return live_interval_; }
1746 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
1747 bool HasLiveInterval() const { return live_interval_ != nullptr; }
1748
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001749 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
1750
1751 // Returns whether the code generation of the instruction will require to have access
1752 // to the current method. Such instructions are:
1753 // (1): Instructions that require an environment, as calling the runtime requires
1754 // to walk the stack and have the current method stored at a specific stack address.
1755 // (2): Object literals like classes and strings, that are loaded from the dex cache
1756 // fields of the current method.
1757 bool NeedsCurrentMethod() const {
1758 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
1759 }
1760
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001761 virtual bool NeedsDexCache() const { return false; }
1762
Mark Mendellc4701932015-04-10 13:18:51 -04001763 // Does this instruction have any use in an environment before
1764 // control flow hits 'other'?
1765 bool HasAnyEnvironmentUseBefore(HInstruction* other);
1766
1767 // Remove all references to environment uses of this instruction.
1768 // The caller must ensure that this is safe to do.
1769 void RemoveEnvironmentUsers();
1770
David Brazdil1abb4192015-02-17 18:33:36 +00001771 protected:
1772 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
1773 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
1774
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001775 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001776 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); }
1777
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001778 HInstruction* previous_;
1779 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001780 HBasicBlock* block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001781
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001782 // An instruction gets an id when it is added to the graph.
1783 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01001784 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001785 int id_;
1786
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001787 // When doing liveness analysis, instructions that have uses get an SSA index.
1788 int ssa_index_;
1789
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001790 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00001791 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001792
1793 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00001794 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001795
Nicolas Geoffray39468442014-09-02 15:17:15 +01001796 // The environment associated with this instruction. Not null if the instruction
1797 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001798 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001799
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001800 // Set by the code generator.
1801 LocationSummary* locations_;
1802
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001803 // Set by the liveness analysis.
1804 LiveInterval* live_interval_;
1805
1806 // Set by the liveness analysis, this is the position in a linear
1807 // order of blocks where this instruction's live interval start.
1808 size_t lifetime_position_;
1809
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001810 const SideEffects side_effects_;
1811
Calin Juravleacf735c2015-02-12 15:25:22 +00001812 // TODO: for primitive types this should be marked as invalid.
1813 ReferenceTypeInfo reference_type_info_;
1814
David Brazdil1abb4192015-02-17 18:33:36 +00001815 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001816 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00001817 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001818 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001819 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001820
1821 DISALLOW_COPY_AND_ASSIGN(HInstruction);
1822};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001823std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001824
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001825class HInputIterator : public ValueObject {
1826 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001827 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001828
1829 bool Done() const { return index_ == instruction_->InputCount(); }
1830 HInstruction* Current() const { return instruction_->InputAt(index_); }
1831 void Advance() { index_++; }
1832
1833 private:
1834 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001835 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001836
1837 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
1838};
1839
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001840class HInstructionIterator : public ValueObject {
1841 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001842 explicit HInstructionIterator(const HInstructionList& instructions)
1843 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001844 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001845 }
1846
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001847 bool Done() const { return instruction_ == nullptr; }
1848 HInstruction* Current() const { return instruction_; }
1849 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001850 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001851 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001852 }
1853
1854 private:
1855 HInstruction* instruction_;
1856 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001857
1858 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001859};
1860
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001861class HBackwardInstructionIterator : public ValueObject {
1862 public:
1863 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
1864 : instruction_(instructions.last_instruction_) {
1865 next_ = Done() ? nullptr : instruction_->GetPrevious();
1866 }
1867
1868 bool Done() const { return instruction_ == nullptr; }
1869 HInstruction* Current() const { return instruction_; }
1870 void Advance() {
1871 instruction_ = next_;
1872 next_ = Done() ? nullptr : instruction_->GetPrevious();
1873 }
1874
1875 private:
1876 HInstruction* instruction_;
1877 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001878
1879 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001880};
1881
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001882// An embedded container with N elements of type T. Used (with partial
1883// specialization for N=0) because embedded arrays cannot have size 0.
1884template<typename T, intptr_t N>
1885class EmbeddedArray {
1886 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001887 EmbeddedArray() : elements_() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001888
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001889 intptr_t GetLength() const { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001890
1891 const T& operator[](intptr_t i) const {
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 T& operator[](intptr_t i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001897 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001898 return elements_[i];
1899 }
1900
1901 const T& At(intptr_t i) const {
1902 return (*this)[i];
1903 }
1904
1905 void SetAt(intptr_t i, const T& val) {
1906 (*this)[i] = val;
1907 }
1908
1909 private:
1910 T elements_[N];
1911};
1912
1913template<typename T>
1914class EmbeddedArray<T, 0> {
1915 public:
1916 intptr_t length() const { return 0; }
1917 const T& operator[](intptr_t i) const {
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 T& operator[](intptr_t i) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001923 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001924 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001925 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001926 }
1927};
1928
1929template<intptr_t N>
1930class HTemplateInstruction: public HInstruction {
1931 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001932 HTemplateInstruction<N>(SideEffects side_effects)
1933 : HInstruction(side_effects), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07001934 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001935
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001936 size_t InputCount() const OVERRIDE { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001937
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001938 protected:
David Brazdil1abb4192015-02-17 18:33:36 +00001939 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_[i]; }
1940
1941 void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE {
1942 inputs_[i] = input;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001943 }
1944
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001945 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001946 EmbeddedArray<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001947
1948 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001949};
1950
Dave Allison20dfc792014-06-16 20:44:29 -07001951template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001952class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07001953 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001954 HExpression<N>(Primitive::Type type, SideEffects side_effects)
1955 : HTemplateInstruction<N>(side_effects), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07001956 virtual ~HExpression() {}
1957
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001958 Primitive::Type GetType() const OVERRIDE { return type_; }
Dave Allison20dfc792014-06-16 20:44:29 -07001959
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001960 protected:
1961 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07001962};
1963
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001964// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
1965// instruction that branches to the exit block.
1966class HReturnVoid : public HTemplateInstruction<0> {
1967 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001968 HReturnVoid() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001969
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001970 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001971
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001972 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001973
1974 private:
1975 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
1976};
1977
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001978// Represents dex's RETURN opcodes. A HReturn is a control flow
1979// instruction that branches to the exit block.
1980class HReturn : public HTemplateInstruction<1> {
1981 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001982 explicit HReturn(HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001983 SetRawInputAt(0, value);
1984 }
1985
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001986 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001987
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001988 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001989
1990 private:
1991 DISALLOW_COPY_AND_ASSIGN(HReturn);
1992};
1993
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001994// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001995// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001996// exit block.
1997class HExit : public HTemplateInstruction<0> {
1998 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001999 HExit() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002000
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002001 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002002
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002003 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002004
2005 private:
2006 DISALLOW_COPY_AND_ASSIGN(HExit);
2007};
2008
2009// Jumps from one block to another.
2010class HGoto : public HTemplateInstruction<0> {
2011 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002012 HGoto() : HTemplateInstruction(SideEffects::None()) {}
2013
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002014 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002015
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002016 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002017 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002018 }
2019
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002020 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002021
2022 private:
2023 DISALLOW_COPY_AND_ASSIGN(HGoto);
2024};
2025
Dave Allison20dfc792014-06-16 20:44:29 -07002026
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002027// Conditional branch. A block ending with an HIf instruction must have
2028// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002029class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002030 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002031 explicit HIf(HInstruction* input) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002032 SetRawInputAt(0, input);
2033 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002034
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002035 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002036
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002037 HBasicBlock* IfTrueSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002038 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002039 }
2040
2041 HBasicBlock* IfFalseSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002042 return GetBlock()->GetSuccessors().Get(1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002043 }
2044
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002045 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002046
2047 private:
2048 DISALLOW_COPY_AND_ASSIGN(HIf);
2049};
2050
David Brazdilfc6a86a2015-06-26 10:33:45 +00002051
2052// Abstract instruction which marks the beginning and/or end of a try block and
2053// links it to the respective exception handlers. Behaves the same as a Goto in
2054// non-exceptional control flow.
2055// Normal-flow successor is stored at index zero, exception handlers under
2056// higher indices in no particular order.
2057class HTryBoundary : public HTemplateInstruction<0> {
2058 public:
David Brazdil56e1acc2015-06-30 15:41:36 +01002059 enum BoundaryKind {
2060 kEntry,
2061 kExit,
2062 };
2063
2064 explicit HTryBoundary(BoundaryKind kind)
2065 : HTemplateInstruction(SideEffects::None()), kind_(kind) {}
David Brazdilfc6a86a2015-06-26 10:33:45 +00002066
2067 bool IsControlFlow() const OVERRIDE { return true; }
2068
2069 // Returns the block's non-exceptional successor (index zero).
2070 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors().Get(0); }
2071
2072 // Returns whether `handler` is among its exception handlers (non-zero index
2073 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002074 bool HasExceptionHandler(const HBasicBlock& handler) const {
2075 DCHECK(handler.IsCatchBlock());
2076 return GetBlock()->GetSuccessors().Contains(
2077 const_cast<HBasicBlock*>(&handler), /* start_from */ 1);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002078 }
2079
2080 // If not present already, adds `handler` to its block's list of exception
2081 // handlers.
2082 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01002083 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002084 GetBlock()->AddSuccessor(handler);
2085 }
2086 }
2087
David Brazdil56e1acc2015-06-30 15:41:36 +01002088 bool IsEntry() const { return kind_ == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002089
David Brazdilffee3d32015-07-06 11:48:53 +01002090 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
2091
David Brazdilfc6a86a2015-06-26 10:33:45 +00002092 DECLARE_INSTRUCTION(TryBoundary);
2093
2094 private:
David Brazdil56e1acc2015-06-30 15:41:36 +01002095 const BoundaryKind kind_;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002096
2097 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2098};
2099
David Brazdilffee3d32015-07-06 11:48:53 +01002100// Iterator over exception handlers of a given HTryBoundary, i.e. over
2101// exceptional successors of its basic block.
2102class HExceptionHandlerIterator : public ValueObject {
2103 public:
2104 explicit HExceptionHandlerIterator(const HTryBoundary& try_boundary)
2105 : block_(*try_boundary.GetBlock()), index_(block_.NumberOfNormalSuccessors()) {}
2106
2107 bool Done() const { return index_ == block_.GetSuccessors().Size(); }
2108 HBasicBlock* Current() const { return block_.GetSuccessors().Get(index_); }
2109 size_t CurrentSuccessorIndex() const { return index_; }
2110 void Advance() { ++index_; }
2111
2112 private:
2113 const HBasicBlock& block_;
2114 size_t index_;
2115
2116 DISALLOW_COPY_AND_ASSIGN(HExceptionHandlerIterator);
2117};
David Brazdilfc6a86a2015-06-26 10:33:45 +00002118
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002119// Deoptimize to interpreter, upon checking a condition.
2120class HDeoptimize : public HTemplateInstruction<1> {
2121 public:
2122 HDeoptimize(HInstruction* cond, uint32_t dex_pc)
2123 : HTemplateInstruction(SideEffects::None()),
2124 dex_pc_(dex_pc) {
2125 SetRawInputAt(0, cond);
2126 }
2127
2128 bool NeedsEnvironment() const OVERRIDE { return true; }
2129 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002130 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002131
2132 DECLARE_INSTRUCTION(Deoptimize);
2133
2134 private:
2135 uint32_t dex_pc_;
2136
2137 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
2138};
2139
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002140// Represents the ArtMethod that was passed as a first argument to
2141// the method. It is used by instructions that depend on it, like
2142// instructions that work with the dex cache.
2143class HCurrentMethod : public HExpression<0> {
2144 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -07002145 explicit HCurrentMethod(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002146
2147 DECLARE_INSTRUCTION(CurrentMethod);
2148
2149 private:
2150 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
2151};
2152
Roland Levillain88cb1752014-10-20 16:36:47 +01002153class HUnaryOperation : public HExpression<1> {
2154 public:
2155 HUnaryOperation(Primitive::Type result_type, HInstruction* input)
2156 : HExpression(result_type, SideEffects::None()) {
2157 SetRawInputAt(0, input);
2158 }
2159
2160 HInstruction* GetInput() const { return InputAt(0); }
2161 Primitive::Type GetResultType() const { return GetType(); }
2162
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002163 bool CanBeMoved() const OVERRIDE { return true; }
2164 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002165 UNUSED(other);
2166 return true;
2167 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002168
Roland Levillain9240d6a2014-10-20 16:47:04 +01002169 // Try to statically evaluate `operation` and return a HConstant
2170 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002171 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002172 HConstant* TryStaticEvaluation() const;
2173
2174 // Apply this operation to `x`.
2175 virtual int32_t Evaluate(int32_t x) const = 0;
2176 virtual int64_t Evaluate(int64_t x) const = 0;
2177
Roland Levillain88cb1752014-10-20 16:36:47 +01002178 DECLARE_INSTRUCTION(UnaryOperation);
2179
2180 private:
2181 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
2182};
2183
Dave Allison20dfc792014-06-16 20:44:29 -07002184class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002185 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002186 HBinaryOperation(Primitive::Type result_type,
2187 HInstruction* left,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002188 HInstruction* right) : HExpression(result_type, SideEffects::None()) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002189 SetRawInputAt(0, left);
2190 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002191 }
2192
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002193 HInstruction* GetLeft() const { return InputAt(0); }
2194 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07002195 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002196
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002197 virtual bool IsCommutative() const { return false; }
2198
2199 // Put constant on the right.
2200 // Returns whether order is changed.
2201 bool OrderInputsWithConstantOnTheRight() {
2202 HInstruction* left = InputAt(0);
2203 HInstruction* right = InputAt(1);
2204 if (left->IsConstant() && !right->IsConstant()) {
2205 ReplaceInput(right, 0);
2206 ReplaceInput(left, 1);
2207 return true;
2208 }
2209 return false;
2210 }
2211
2212 // Order inputs by instruction id, but favor constant on the right side.
2213 // This helps GVN for commutative ops.
2214 void OrderInputs() {
2215 DCHECK(IsCommutative());
2216 HInstruction* left = InputAt(0);
2217 HInstruction* right = InputAt(1);
2218 if (left == right || (!left->IsConstant() && right->IsConstant())) {
2219 return;
2220 }
2221 if (OrderInputsWithConstantOnTheRight()) {
2222 return;
2223 }
2224 // Order according to instruction id.
2225 if (left->GetId() > right->GetId()) {
2226 ReplaceInput(right, 0);
2227 ReplaceInput(left, 1);
2228 }
2229 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002230
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002231 bool CanBeMoved() const OVERRIDE { return true; }
2232 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002233 UNUSED(other);
2234 return true;
2235 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002236
Roland Levillain9240d6a2014-10-20 16:47:04 +01002237 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01002238 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002239 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002240 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01002241
2242 // Apply this operation to `x` and `y`.
2243 virtual int32_t Evaluate(int32_t x, int32_t y) const = 0;
2244 virtual int64_t Evaluate(int64_t x, int64_t y) const = 0;
2245
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002246 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002247 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002248 HConstant* GetConstantRight() const;
2249
2250 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002251 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002252 HInstruction* GetLeastConstantLeft() const;
2253
Roland Levillainccc07a92014-09-16 14:48:16 +01002254 DECLARE_INSTRUCTION(BinaryOperation);
2255
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002256 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002257 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
2258};
2259
Mark Mendellc4701932015-04-10 13:18:51 -04002260// The comparison bias applies for floating point operations and indicates how NaN
2261// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01002262enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04002263 kNoBias, // bias is not applicable (i.e. for long operation)
2264 kGtBias, // return 1 for NaN comparisons
2265 kLtBias, // return -1 for NaN comparisons
2266};
2267
Dave Allison20dfc792014-06-16 20:44:29 -07002268class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002269 public:
Dave Allison20dfc792014-06-16 20:44:29 -07002270 HCondition(HInstruction* first, HInstruction* second)
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002271 : HBinaryOperation(Primitive::kPrimBoolean, first, second),
Mark Mendellc4701932015-04-10 13:18:51 -04002272 needs_materialization_(true),
Roland Levillain4fa13f62015-07-06 18:11:54 +01002273 bias_(ComparisonBias::kNoBias) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002274
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002275 bool NeedsMaterialization() const { return needs_materialization_; }
2276 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002277
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002278 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002279 // `instruction`, and disregard moves in between.
2280 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002281
Dave Allison20dfc792014-06-16 20:44:29 -07002282 DECLARE_INSTRUCTION(Condition);
2283
2284 virtual IfCondition GetCondition() const = 0;
2285
Mark Mendellc4701932015-04-10 13:18:51 -04002286 virtual IfCondition GetOppositeCondition() const = 0;
2287
Roland Levillain4fa13f62015-07-06 18:11:54 +01002288 bool IsGtBias() const { return bias_ == ComparisonBias::kGtBias; }
Mark Mendellc4701932015-04-10 13:18:51 -04002289
2290 void SetBias(ComparisonBias bias) { bias_ = bias; }
2291
2292 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2293 return bias_ == other->AsCondition()->bias_;
2294 }
2295
Roland Levillain4fa13f62015-07-06 18:11:54 +01002296 bool IsFPConditionTrueIfNaN() const {
2297 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2298 IfCondition if_cond = GetCondition();
2299 return IsGtBias() ? ((if_cond == kCondGT) || (if_cond == kCondGE)) : (if_cond == kCondNE);
2300 }
2301
2302 bool IsFPConditionFalseIfNaN() const {
2303 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2304 IfCondition if_cond = GetCondition();
2305 return IsGtBias() ? ((if_cond == kCondLT) || (if_cond == kCondLE)) : (if_cond == kCondEQ);
2306 }
2307
Dave Allison20dfc792014-06-16 20:44:29 -07002308 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002309 // For register allocation purposes, returns whether this instruction needs to be
2310 // materialized (that is, not just be in the processor flags).
2311 bool needs_materialization_;
2312
Mark Mendellc4701932015-04-10 13:18:51 -04002313 // Needed if we merge a HCompare into a HCondition.
2314 ComparisonBias bias_;
2315
Dave Allison20dfc792014-06-16 20:44:29 -07002316 DISALLOW_COPY_AND_ASSIGN(HCondition);
2317};
2318
2319// Instruction to check if two inputs are equal to each other.
2320class HEqual : public HCondition {
2321 public:
2322 HEqual(HInstruction* first, HInstruction* second)
2323 : HCondition(first, second) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002324
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002325 bool IsCommutative() const OVERRIDE { return true; }
2326
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002327 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002328 return x == y ? 1 : 0;
2329 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002330 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002331 return x == y ? 1 : 0;
2332 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002333
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002334 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002335
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002336 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002337 return kCondEQ;
2338 }
2339
Mark Mendellc4701932015-04-10 13:18:51 -04002340 IfCondition GetOppositeCondition() const OVERRIDE {
2341 return kCondNE;
2342 }
2343
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002344 private:
2345 DISALLOW_COPY_AND_ASSIGN(HEqual);
2346};
2347
Dave Allison20dfc792014-06-16 20:44:29 -07002348class HNotEqual : public HCondition {
2349 public:
2350 HNotEqual(HInstruction* first, HInstruction* second)
2351 : HCondition(first, second) {}
2352
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002353 bool IsCommutative() const OVERRIDE { return true; }
2354
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002355 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002356 return x != y ? 1 : 0;
2357 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002358 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002359 return x != y ? 1 : 0;
2360 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002361
Dave Allison20dfc792014-06-16 20:44:29 -07002362 DECLARE_INSTRUCTION(NotEqual);
2363
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002364 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002365 return kCondNE;
2366 }
2367
Mark Mendellc4701932015-04-10 13:18:51 -04002368 IfCondition GetOppositeCondition() const OVERRIDE {
2369 return kCondEQ;
2370 }
2371
Dave Allison20dfc792014-06-16 20:44:29 -07002372 private:
2373 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
2374};
2375
2376class HLessThan : public HCondition {
2377 public:
2378 HLessThan(HInstruction* first, HInstruction* second)
2379 : HCondition(first, second) {}
2380
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002381 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002382 return x < y ? 1 : 0;
2383 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002384 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002385 return x < y ? 1 : 0;
2386 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002387
Dave Allison20dfc792014-06-16 20:44:29 -07002388 DECLARE_INSTRUCTION(LessThan);
2389
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002390 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002391 return kCondLT;
2392 }
2393
Mark Mendellc4701932015-04-10 13:18:51 -04002394 IfCondition GetOppositeCondition() const OVERRIDE {
2395 return kCondGE;
2396 }
2397
Dave Allison20dfc792014-06-16 20:44:29 -07002398 private:
2399 DISALLOW_COPY_AND_ASSIGN(HLessThan);
2400};
2401
2402class HLessThanOrEqual : public HCondition {
2403 public:
2404 HLessThanOrEqual(HInstruction* first, HInstruction* second)
2405 : HCondition(first, second) {}
2406
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002407 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002408 return x <= y ? 1 : 0;
2409 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002410 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002411 return x <= y ? 1 : 0;
2412 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002413
Dave Allison20dfc792014-06-16 20:44:29 -07002414 DECLARE_INSTRUCTION(LessThanOrEqual);
2415
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002416 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002417 return kCondLE;
2418 }
2419
Mark Mendellc4701932015-04-10 13:18:51 -04002420 IfCondition GetOppositeCondition() const OVERRIDE {
2421 return kCondGT;
2422 }
2423
Dave Allison20dfc792014-06-16 20:44:29 -07002424 private:
2425 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
2426};
2427
2428class HGreaterThan : public HCondition {
2429 public:
2430 HGreaterThan(HInstruction* first, HInstruction* second)
2431 : HCondition(first, second) {}
2432
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002433 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002434 return x > y ? 1 : 0;
2435 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002436 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002437 return x > y ? 1 : 0;
2438 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002439
Dave Allison20dfc792014-06-16 20:44:29 -07002440 DECLARE_INSTRUCTION(GreaterThan);
2441
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002442 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002443 return kCondGT;
2444 }
2445
Mark Mendellc4701932015-04-10 13:18:51 -04002446 IfCondition GetOppositeCondition() const OVERRIDE {
2447 return kCondLE;
2448 }
2449
Dave Allison20dfc792014-06-16 20:44:29 -07002450 private:
2451 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
2452};
2453
2454class HGreaterThanOrEqual : public HCondition {
2455 public:
2456 HGreaterThanOrEqual(HInstruction* first, HInstruction* second)
2457 : HCondition(first, second) {}
2458
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002459 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002460 return x >= y ? 1 : 0;
2461 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002462 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002463 return x >= y ? 1 : 0;
2464 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002465
Dave Allison20dfc792014-06-16 20:44:29 -07002466 DECLARE_INSTRUCTION(GreaterThanOrEqual);
2467
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002468 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002469 return kCondGE;
2470 }
2471
Mark Mendellc4701932015-04-10 13:18:51 -04002472 IfCondition GetOppositeCondition() const OVERRIDE {
2473 return kCondLT;
2474 }
2475
Dave Allison20dfc792014-06-16 20:44:29 -07002476 private:
2477 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
2478};
2479
2480
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002481// Instruction to check how two inputs compare to each other.
2482// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
2483class HCompare : public HBinaryOperation {
2484 public:
Alexey Frunze4dda3372015-06-01 18:31:49 -07002485 HCompare(Primitive::Type type,
2486 HInstruction* first,
2487 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04002488 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07002489 uint32_t dex_pc)
2490 : HBinaryOperation(Primitive::kPrimInt, first, second), bias_(bias), dex_pc_(dex_pc) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002491 DCHECK_EQ(type, first->GetType());
2492 DCHECK_EQ(type, second->GetType());
2493 }
2494
Calin Juravleddb7df22014-11-25 20:56:51 +00002495 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01002496 return
2497 x == y ? 0 :
2498 x > y ? 1 :
2499 -1;
2500 }
Calin Juravleddb7df22014-11-25 20:56:51 +00002501
2502 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01002503 return
2504 x == y ? 0 :
2505 x > y ? 1 :
2506 -1;
2507 }
2508
Calin Juravleddb7df22014-11-25 20:56:51 +00002509 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2510 return bias_ == other->AsCompare()->bias_;
2511 }
2512
Mark Mendellc4701932015-04-10 13:18:51 -04002513 ComparisonBias GetBias() const { return bias_; }
2514
Roland Levillain4fa13f62015-07-06 18:11:54 +01002515 bool IsGtBias() { return bias_ == ComparisonBias::kGtBias; }
Calin Juravleddb7df22014-11-25 20:56:51 +00002516
Alexey Frunze4dda3372015-06-01 18:31:49 -07002517 uint32_t GetDexPc() const { return dex_pc_; }
2518
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002519 DECLARE_INSTRUCTION(Compare);
2520
2521 private:
Mark Mendellc4701932015-04-10 13:18:51 -04002522 const ComparisonBias bias_;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002523 const uint32_t dex_pc_;
Calin Juravleddb7df22014-11-25 20:56:51 +00002524
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002525 DISALLOW_COPY_AND_ASSIGN(HCompare);
2526};
2527
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002528// A local in the graph. Corresponds to a Dex register.
2529class HLocal : public HTemplateInstruction<0> {
2530 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002531 explicit HLocal(uint16_t reg_number)
2532 : HTemplateInstruction(SideEffects::None()), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002533
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002534 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002535
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002536 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002537
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002538 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002539 // The Dex register number.
2540 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002541
2542 DISALLOW_COPY_AND_ASSIGN(HLocal);
2543};
2544
2545// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07002546class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002547 public:
Roland Levillain5799fc02014-09-25 12:15:20 +01002548 HLoadLocal(HLocal* local, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002549 : HExpression(type, SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002550 SetRawInputAt(0, local);
2551 }
2552
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002553 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
2554
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002555 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002556
2557 private:
2558 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
2559};
2560
2561// Store a value in a given local. This instruction has two inputs: the value
2562// and the local.
2563class HStoreLocal : public HTemplateInstruction<2> {
2564 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002565 HStoreLocal(HLocal* local, HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002566 SetRawInputAt(0, local);
2567 SetRawInputAt(1, value);
2568 }
2569
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002570 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
2571
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002572 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002573
2574 private:
2575 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
2576};
2577
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002578class HConstant : public HExpression<0> {
2579 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002580 explicit HConstant(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
2581
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002582 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002583
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002584 virtual bool IsMinusOne() const { return false; }
2585 virtual bool IsZero() const { return false; }
2586 virtual bool IsOne() const { return false; }
2587
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002588 DECLARE_INSTRUCTION(Constant);
2589
2590 private:
2591 DISALLOW_COPY_AND_ASSIGN(HConstant);
2592};
2593
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002594class HFloatConstant : public HConstant {
2595 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002596 float GetValue() const { return value_; }
2597
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002598 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002599 return bit_cast<uint32_t, float>(other->AsFloatConstant()->value_) ==
2600 bit_cast<uint32_t, float>(value_);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002601 }
2602
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002603 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002604
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002605 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002606 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002607 }
2608 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002609 return value_ == 0.0f;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002610 }
2611 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002612 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
2613 }
2614 bool IsNaN() const {
2615 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002616 }
2617
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002618 DECLARE_INSTRUCTION(FloatConstant);
2619
2620 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002621 explicit HFloatConstant(float value) : HConstant(Primitive::kPrimFloat), value_(value) {}
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002622 explicit HFloatConstant(int32_t value)
2623 : HConstant(Primitive::kPrimFloat), value_(bit_cast<float, int32_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00002624
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002625 const float value_;
2626
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002627 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00002628 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002629 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002630 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2631};
2632
2633class HDoubleConstant : public HConstant {
2634 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002635 double GetValue() const { return value_; }
2636
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002637 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002638 return bit_cast<uint64_t, double>(other->AsDoubleConstant()->value_) ==
2639 bit_cast<uint64_t, double>(value_);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002640 }
2641
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002642 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002643
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002644 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002645 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002646 }
2647 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002648 return value_ == 0.0;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002649 }
2650 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002651 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
2652 }
2653 bool IsNaN() const {
2654 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002655 }
2656
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002657 DECLARE_INSTRUCTION(DoubleConstant);
2658
2659 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002660 explicit HDoubleConstant(double value) : HConstant(Primitive::kPrimDouble), value_(value) {}
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002661 explicit HDoubleConstant(int64_t value)
2662 : HConstant(Primitive::kPrimDouble), value_(bit_cast<double, int64_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00002663
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002664 const double value_;
2665
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002666 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00002667 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002668 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002669 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
2670};
2671
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002672class HNullConstant : public HConstant {
2673 public:
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002674 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2675 return true;
2676 }
2677
2678 size_t ComputeHashCode() const OVERRIDE { return 0; }
2679
2680 DECLARE_INSTRUCTION(NullConstant);
2681
2682 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002683 HNullConstant() : HConstant(Primitive::kPrimNot) {}
2684
2685 friend class HGraph;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002686 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2687};
2688
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002689// Constants of the type int. Those can be from Dex instructions, or
2690// synthesized (for example with the if-eqz instruction).
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002691class HIntConstant : public HConstant {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002692 public:
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002693 int32_t GetValue() const { return value_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002694
Calin Juravle61d544b2015-02-23 16:46:57 +00002695 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002696 return other->AsIntConstant()->value_ == value_;
2697 }
2698
Calin Juravle61d544b2015-02-23 16:46:57 +00002699 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2700
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002701 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2702 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2703 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2704
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002705 DECLARE_INSTRUCTION(IntConstant);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002706
2707 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002708 explicit HIntConstant(int32_t value) : HConstant(Primitive::kPrimInt), value_(value) {}
2709
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002710 const int32_t value_;
2711
David Brazdil8d5b8b22015-03-24 10:51:52 +00002712 friend class HGraph;
2713 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
Zheng Xuad4450e2015-04-17 18:48:56 +08002714 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002715 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2716};
2717
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002718class HLongConstant : public HConstant {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002719 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002720 int64_t GetValue() const { return value_; }
2721
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002722 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002723 return other->AsLongConstant()->value_ == value_;
2724 }
2725
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002726 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002727
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002728 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2729 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2730 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2731
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002732 DECLARE_INSTRUCTION(LongConstant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002733
2734 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002735 explicit HLongConstant(int64_t value) : HConstant(Primitive::kPrimLong), value_(value) {}
2736
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002737 const int64_t value_;
2738
David Brazdil8d5b8b22015-03-24 10:51:52 +00002739 friend class HGraph;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002740 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2741};
2742
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002743enum class Intrinsics {
2744#define OPTIMIZING_INTRINSICS(Name, IsStatic) k ## Name,
2745#include "intrinsics_list.h"
2746 kNone,
2747 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
2748#undef INTRINSICS_LIST
2749#undef OPTIMIZING_INTRINSICS
2750};
2751std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
2752
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002753class HInvoke : public HInstruction {
2754 public:
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002755 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002756
2757 // Runtime needs to walk the stack, so Dex -> Dex calls need to
2758 // know their environment.
Calin Juravle77520bc2015-01-12 18:45:46 +00002759 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002760
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01002761 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002762 SetRawInputAt(index, argument);
2763 }
2764
Roland Levillain3e3d7332015-04-28 11:00:54 +01002765 // Return the number of arguments. This number can be lower than
2766 // the number of inputs returned by InputCount(), as some invoke
2767 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
2768 // inputs at the end of their list of inputs.
2769 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
2770
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002771 Primitive::Type GetType() const OVERRIDE { return return_type_; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002772
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002773 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002774
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002775 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002776 const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002777
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002778 InvokeType GetOriginalInvokeType() const { return original_invoke_type_; }
2779
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01002780 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002781 return intrinsic_;
2782 }
2783
2784 void SetIntrinsic(Intrinsics intrinsic) {
2785 intrinsic_ = intrinsic;
2786 }
2787
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01002788 bool IsFromInlinedInvoke() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002789 return GetEnvironment()->GetParent() != nullptr;
2790 }
2791
2792 bool CanThrow() const OVERRIDE { return true; }
2793
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002794 DECLARE_INSTRUCTION(Invoke);
2795
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002796 protected:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002797 HInvoke(ArenaAllocator* arena,
2798 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01002799 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002800 Primitive::Type return_type,
2801 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002802 uint32_t dex_method_index,
2803 InvokeType original_invoke_type)
Aart Bik854a02b2015-07-14 16:07:00 -07002804 : HInstruction(SideEffects::All()), // assume write/read on all fields/arrays
Roland Levillain3e3d7332015-04-28 11:00:54 +01002805 number_of_arguments_(number_of_arguments),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002806 inputs_(arena, number_of_arguments),
2807 return_type_(return_type),
2808 dex_pc_(dex_pc),
2809 dex_method_index_(dex_method_index),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002810 original_invoke_type_(original_invoke_type),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002811 intrinsic_(Intrinsics::kNone) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01002812 uint32_t number_of_inputs = number_of_arguments + number_of_other_inputs;
2813 inputs_.SetSize(number_of_inputs);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002814 }
2815
David Brazdil1abb4192015-02-17 18:33:36 +00002816 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
2817 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
2818 inputs_.Put(index, input);
2819 }
2820
Roland Levillain3e3d7332015-04-28 11:00:54 +01002821 uint32_t number_of_arguments_;
David Brazdil1abb4192015-02-17 18:33:36 +00002822 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002823 const Primitive::Type return_type_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002824 const uint32_t dex_pc_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002825 const uint32_t dex_method_index_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002826 const InvokeType original_invoke_type_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002827 Intrinsics intrinsic_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002828
2829 private:
2830 DISALLOW_COPY_AND_ASSIGN(HInvoke);
2831};
2832
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002833class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002834 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01002835 // Requirements of this method call regarding the class
2836 // initialization (clinit) check of its declaring class.
2837 enum class ClinitCheckRequirement {
2838 kNone, // Class already initialized.
2839 kExplicit, // Static call having explicit clinit check as last input.
2840 kImplicit, // Static call implicitly requiring a clinit check.
2841 };
2842
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002843 HInvokeStaticOrDirect(ArenaAllocator* arena,
2844 uint32_t number_of_arguments,
2845 Primitive::Type return_type,
2846 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002847 uint32_t dex_method_index,
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002848 bool is_recursive,
Jeff Hao848f70a2014-01-15 13:49:50 -08002849 int32_t string_init_offset,
Nicolas Geoffray79041292015-03-26 10:05:54 +00002850 InvokeType original_invoke_type,
Roland Levillain4c0eb422015-04-24 16:43:49 +01002851 InvokeType invoke_type,
2852 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01002853 : HInvoke(arena,
2854 number_of_arguments,
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01002855 // There is one extra argument for the HCurrentMethod node, and
2856 // potentially one other if the clinit check is explicit, and one other
2857 // if the method is a string factory.
2858 1u + (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u)
2859 + (string_init_offset ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01002860 return_type,
2861 dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002862 dex_method_index,
2863 original_invoke_type),
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002864 invoke_type_(invoke_type),
Roland Levillain4c0eb422015-04-24 16:43:49 +01002865 is_recursive_(is_recursive),
Jeff Hao848f70a2014-01-15 13:49:50 -08002866 clinit_check_requirement_(clinit_check_requirement),
2867 string_init_offset_(string_init_offset) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002868
Calin Juravle641547a2015-04-21 22:08:51 +01002869 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
2870 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00002871 // We access the method via the dex cache so we can't do an implicit null check.
2872 // TODO: for intrinsics we can generate implicit null checks.
2873 return false;
2874 }
2875
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002876 InvokeType GetInvokeType() const { return invoke_type_; }
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002877 bool IsRecursive() const { return is_recursive_; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002878 bool NeedsDexCache() const OVERRIDE { return !IsRecursive(); }
Jeff Hao848f70a2014-01-15 13:49:50 -08002879 bool IsStringInit() const { return string_init_offset_ != 0; }
2880 int32_t GetStringInitOffset() const { return string_init_offset_; }
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002881 uint32_t GetCurrentMethodInputIndex() const { return GetNumberOfArguments(); }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002882
Roland Levillain4c0eb422015-04-24 16:43:49 +01002883 // Is this instruction a call to a static method?
2884 bool IsStatic() const {
2885 return GetInvokeType() == kStatic;
2886 }
2887
Roland Levillain3e3d7332015-04-28 11:00:54 +01002888 // Remove the art::HLoadClass instruction set as last input by
2889 // art::PrepareForRegisterAllocation::VisitClinitCheck in lieu of
2890 // the initial art::HClinitCheck instruction (only relevant for
2891 // static calls with explicit clinit check).
2892 void RemoveLoadClassAsLastInput() {
Roland Levillain4c0eb422015-04-24 16:43:49 +01002893 DCHECK(IsStaticWithExplicitClinitCheck());
2894 size_t last_input_index = InputCount() - 1;
2895 HInstruction* last_input = InputAt(last_input_index);
2896 DCHECK(last_input != nullptr);
Roland Levillain3e3d7332015-04-28 11:00:54 +01002897 DCHECK(last_input->IsLoadClass()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01002898 RemoveAsUserOfInput(last_input_index);
2899 inputs_.DeleteAt(last_input_index);
2900 clinit_check_requirement_ = ClinitCheckRequirement::kImplicit;
2901 DCHECK(IsStaticWithImplicitClinitCheck());
2902 }
2903
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01002904 bool IsStringFactoryFor(HFakeString* str) const {
2905 if (!IsStringInit()) return false;
2906 // +1 for the current method.
2907 if (InputCount() == (number_of_arguments_ + 1)) return false;
2908 return InputAt(InputCount() - 1)->AsFakeString() == str;
2909 }
2910
2911 void RemoveFakeStringArgumentAsLastInput() {
2912 DCHECK(IsStringInit());
2913 size_t last_input_index = InputCount() - 1;
2914 HInstruction* last_input = InputAt(last_input_index);
2915 DCHECK(last_input != nullptr);
2916 DCHECK(last_input->IsFakeString()) << last_input->DebugName();
2917 RemoveAsUserOfInput(last_input_index);
2918 inputs_.DeleteAt(last_input_index);
2919 }
2920
Roland Levillain4c0eb422015-04-24 16:43:49 +01002921 // Is this a call to a static method whose declaring class has an
2922 // explicit intialization check in the graph?
2923 bool IsStaticWithExplicitClinitCheck() const {
2924 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kExplicit);
2925 }
2926
2927 // Is this a call to a static method whose declaring class has an
2928 // implicit intialization check requirement?
2929 bool IsStaticWithImplicitClinitCheck() const {
2930 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kImplicit);
2931 }
2932
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002933 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002934
Roland Levillain4c0eb422015-04-24 16:43:49 +01002935 protected:
2936 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
2937 const HUserRecord<HInstruction*> input_record = HInvoke::InputRecordAt(i);
2938 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck() && (i == InputCount() - 1)) {
2939 HInstruction* input = input_record.GetInstruction();
2940 // `input` is the last input of a static invoke marked as having
2941 // an explicit clinit check. It must either be:
2942 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
2943 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
2944 DCHECK(input != nullptr);
2945 DCHECK(input->IsClinitCheck() || input->IsLoadClass()) << input->DebugName();
2946 }
2947 return input_record;
2948 }
2949
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002950 private:
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002951 const InvokeType invoke_type_;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002952 const bool is_recursive_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01002953 ClinitCheckRequirement clinit_check_requirement_;
Jeff Hao848f70a2014-01-15 13:49:50 -08002954 // Thread entrypoint offset for string init method if this is a string init invoke.
2955 // Note that there are multiple string init methods, each having its own offset.
2956 int32_t string_init_offset_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002957
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002958 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002959};
2960
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002961class HInvokeVirtual : public HInvoke {
2962 public:
2963 HInvokeVirtual(ArenaAllocator* arena,
2964 uint32_t number_of_arguments,
2965 Primitive::Type return_type,
2966 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002967 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002968 uint32_t vtable_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002969 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002970 vtable_index_(vtable_index) {}
2971
Calin Juravle641547a2015-04-21 22:08:51 +01002972 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00002973 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01002974 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00002975 }
2976
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002977 uint32_t GetVTableIndex() const { return vtable_index_; }
2978
2979 DECLARE_INSTRUCTION(InvokeVirtual);
2980
2981 private:
2982 const uint32_t vtable_index_;
2983
2984 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
2985};
2986
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002987class HInvokeInterface : public HInvoke {
2988 public:
2989 HInvokeInterface(ArenaAllocator* arena,
2990 uint32_t number_of_arguments,
2991 Primitive::Type return_type,
2992 uint32_t dex_pc,
2993 uint32_t dex_method_index,
2994 uint32_t imt_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002995 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002996 imt_index_(imt_index) {}
2997
Calin Juravle641547a2015-04-21 22:08:51 +01002998 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00002999 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01003000 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00003001 }
3002
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003003 uint32_t GetImtIndex() const { return imt_index_; }
3004 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
3005
3006 DECLARE_INSTRUCTION(InvokeInterface);
3007
3008 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003009 const uint32_t imt_index_;
3010
3011 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
3012};
3013
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003014class HNewInstance : public HExpression<1> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003015 public:
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003016 HNewInstance(HCurrentMethod* current_method,
3017 uint32_t dex_pc,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003018 uint16_t type_index,
3019 const DexFile& dex_file,
3020 QuickEntrypointEnum entrypoint)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003021 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3022 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003023 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003024 dex_file_(dex_file),
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003025 entrypoint_(entrypoint) {
3026 SetRawInputAt(0, current_method);
3027 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003028
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003029 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003030 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003031 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003032
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003033 // Calls runtime so needs an environment.
Calin Juravle92a6ed22014-12-02 18:58:03 +00003034 bool NeedsEnvironment() const OVERRIDE { return true; }
3035 // It may throw when called on:
3036 // - interfaces
3037 // - abstract/innaccessible/unknown classes
3038 // TODO: optimize when possible.
3039 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003040
Calin Juravle10e244f2015-01-26 18:54:32 +00003041 bool CanBeNull() const OVERRIDE { return false; }
3042
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003043 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3044
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003045 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003046
3047 private:
3048 const uint32_t dex_pc_;
3049 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003050 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003051 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003052
3053 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3054};
3055
Roland Levillain88cb1752014-10-20 16:36:47 +01003056class HNeg : public HUnaryOperation {
3057 public:
3058 explicit HNeg(Primitive::Type result_type, HInstruction* input)
3059 : HUnaryOperation(result_type, input) {}
3060
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003061 int32_t Evaluate(int32_t x) const OVERRIDE { return -x; }
3062 int64_t Evaluate(int64_t x) const OVERRIDE { return -x; }
Roland Levillain9240d6a2014-10-20 16:47:04 +01003063
Roland Levillain88cb1752014-10-20 16:36:47 +01003064 DECLARE_INSTRUCTION(Neg);
3065
3066 private:
3067 DISALLOW_COPY_AND_ASSIGN(HNeg);
3068};
3069
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003070class HNewArray : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003071 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003072 HNewArray(HInstruction* length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003073 HCurrentMethod* current_method,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003074 uint32_t dex_pc,
3075 uint16_t type_index,
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003076 const DexFile& dex_file,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003077 QuickEntrypointEnum entrypoint)
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003078 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3079 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003080 type_index_(type_index),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003081 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003082 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003083 SetRawInputAt(0, length);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003084 SetRawInputAt(1, current_method);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003085 }
3086
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003087 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003088 uint16_t GetTypeIndex() const { return type_index_; }
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003089 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003090
3091 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00003092 bool NeedsEnvironment() const OVERRIDE { return true; }
3093
Mingyao Yang0c365e62015-03-31 15:09:29 -07003094 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
3095 bool CanThrow() const OVERRIDE { return true; }
3096
Calin Juravle10e244f2015-01-26 18:54:32 +00003097 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003098
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003099 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3100
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003101 DECLARE_INSTRUCTION(NewArray);
3102
3103 private:
3104 const uint32_t dex_pc_;
3105 const uint16_t type_index_;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003106 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003107 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003108
3109 DISALLOW_COPY_AND_ASSIGN(HNewArray);
3110};
3111
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003112class HAdd : public HBinaryOperation {
3113 public:
3114 HAdd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3115 : HBinaryOperation(result_type, left, right) {}
3116
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003117 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003118
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003119 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01003120 return x + y;
3121 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003122 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01003123 return x + y;
3124 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003125
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003126 DECLARE_INSTRUCTION(Add);
3127
3128 private:
3129 DISALLOW_COPY_AND_ASSIGN(HAdd);
3130};
3131
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003132class HSub : public HBinaryOperation {
3133 public:
3134 HSub(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3135 : HBinaryOperation(result_type, left, right) {}
3136
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003137 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01003138 return x - y;
3139 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003140 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01003141 return x - y;
3142 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003143
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003144 DECLARE_INSTRUCTION(Sub);
3145
3146 private:
3147 DISALLOW_COPY_AND_ASSIGN(HSub);
3148};
3149
Calin Juravle34bacdf2014-10-07 20:23:36 +01003150class HMul : public HBinaryOperation {
3151 public:
3152 HMul(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3153 : HBinaryOperation(result_type, left, right) {}
3154
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003155 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003156
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003157 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x * y; }
3158 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x * y; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003159
3160 DECLARE_INSTRUCTION(Mul);
3161
3162 private:
3163 DISALLOW_COPY_AND_ASSIGN(HMul);
3164};
3165
Calin Juravle7c4954d2014-10-28 16:57:40 +00003166class HDiv : public HBinaryOperation {
3167 public:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003168 HDiv(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
3169 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00003170
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003171 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00003172 // Our graph structure ensures we never have 0 for `y` during constant folding.
3173 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00003174 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00003175 return (y == -1) ? -x : x / y;
3176 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003177
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003178 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00003179 DCHECK_NE(y, 0);
3180 // Special case -1 to avoid getting a SIGFPE on x86(_64).
3181 return (y == -1) ? -x : x / y;
3182 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003183
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003184 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003185
Calin Juravle7c4954d2014-10-28 16:57:40 +00003186 DECLARE_INSTRUCTION(Div);
3187
3188 private:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003189 const uint32_t dex_pc_;
3190
Calin Juravle7c4954d2014-10-28 16:57:40 +00003191 DISALLOW_COPY_AND_ASSIGN(HDiv);
3192};
3193
Calin Juravlebacfec32014-11-14 15:54:36 +00003194class HRem : public HBinaryOperation {
3195 public:
3196 HRem(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
3197 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
3198
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003199 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00003200 DCHECK_NE(y, 0);
3201 // Special case -1 to avoid getting a SIGFPE on x86(_64).
3202 return (y == -1) ? 0 : x % y;
3203 }
3204
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003205 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00003206 DCHECK_NE(y, 0);
3207 // Special case -1 to avoid getting a SIGFPE on x86(_64).
3208 return (y == -1) ? 0 : x % y;
3209 }
3210
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003211 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Calin Juravlebacfec32014-11-14 15:54:36 +00003212
3213 DECLARE_INSTRUCTION(Rem);
3214
3215 private:
3216 const uint32_t dex_pc_;
3217
3218 DISALLOW_COPY_AND_ASSIGN(HRem);
3219};
3220
Calin Juravled0d48522014-11-04 16:40:20 +00003221class HDivZeroCheck : public HExpression<1> {
3222 public:
3223 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
3224 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
3225 SetRawInputAt(0, value);
3226 }
3227
3228 bool CanBeMoved() const OVERRIDE { return true; }
3229
3230 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3231 UNUSED(other);
3232 return true;
3233 }
3234
3235 bool NeedsEnvironment() const OVERRIDE { return true; }
3236 bool CanThrow() const OVERRIDE { return true; }
3237
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003238 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Calin Juravled0d48522014-11-04 16:40:20 +00003239
3240 DECLARE_INSTRUCTION(DivZeroCheck);
3241
3242 private:
3243 const uint32_t dex_pc_;
3244
3245 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
3246};
3247
Calin Juravle9aec02f2014-11-18 23:06:35 +00003248class HShl : public HBinaryOperation {
3249 public:
3250 HShl(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3251 : HBinaryOperation(result_type, left, right) {}
3252
3253 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x << (y & kMaxIntShiftValue); }
3254 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x << (y & kMaxLongShiftValue); }
3255
3256 DECLARE_INSTRUCTION(Shl);
3257
3258 private:
3259 DISALLOW_COPY_AND_ASSIGN(HShl);
3260};
3261
3262class HShr : public HBinaryOperation {
3263 public:
3264 HShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3265 : HBinaryOperation(result_type, left, right) {}
3266
3267 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x >> (y & kMaxIntShiftValue); }
3268 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x >> (y & kMaxLongShiftValue); }
3269
3270 DECLARE_INSTRUCTION(Shr);
3271
3272 private:
3273 DISALLOW_COPY_AND_ASSIGN(HShr);
3274};
3275
3276class HUShr : public HBinaryOperation {
3277 public:
3278 HUShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3279 : HBinaryOperation(result_type, left, right) {}
3280
3281 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
3282 uint32_t ux = static_cast<uint32_t>(x);
3283 uint32_t uy = static_cast<uint32_t>(y) & kMaxIntShiftValue;
3284 return static_cast<int32_t>(ux >> uy);
3285 }
3286
3287 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
3288 uint64_t ux = static_cast<uint64_t>(x);
3289 uint64_t uy = static_cast<uint64_t>(y) & kMaxLongShiftValue;
3290 return static_cast<int64_t>(ux >> uy);
3291 }
3292
3293 DECLARE_INSTRUCTION(UShr);
3294
3295 private:
3296 DISALLOW_COPY_AND_ASSIGN(HUShr);
3297};
3298
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003299class HAnd : public HBinaryOperation {
3300 public:
3301 HAnd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3302 : HBinaryOperation(result_type, left, right) {}
3303
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003304 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003305
3306 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x & y; }
3307 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x & y; }
3308
3309 DECLARE_INSTRUCTION(And);
3310
3311 private:
3312 DISALLOW_COPY_AND_ASSIGN(HAnd);
3313};
3314
3315class HOr : public HBinaryOperation {
3316 public:
3317 HOr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3318 : HBinaryOperation(result_type, left, right) {}
3319
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003320 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003321
3322 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x | y; }
3323 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x | y; }
3324
3325 DECLARE_INSTRUCTION(Or);
3326
3327 private:
3328 DISALLOW_COPY_AND_ASSIGN(HOr);
3329};
3330
3331class HXor : public HBinaryOperation {
3332 public:
3333 HXor(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3334 : HBinaryOperation(result_type, left, right) {}
3335
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003336 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003337
3338 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x ^ y; }
3339 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x ^ y; }
3340
3341 DECLARE_INSTRUCTION(Xor);
3342
3343 private:
3344 DISALLOW_COPY_AND_ASSIGN(HXor);
3345};
3346
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003347// The value of a parameter in this method. Its location depends on
3348// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07003349class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003350 public:
Calin Juravle10e244f2015-01-26 18:54:32 +00003351 HParameterValue(uint8_t index, Primitive::Type parameter_type, bool is_this = false)
3352 : HExpression(parameter_type, SideEffects::None()), index_(index), is_this_(is_this) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003353
3354 uint8_t GetIndex() const { return index_; }
3355
Calin Juravle10e244f2015-01-26 18:54:32 +00003356 bool CanBeNull() const OVERRIDE { return !is_this_; }
3357
Calin Juravle3cd4fc82015-05-14 15:15:42 +01003358 bool IsThis() const { return is_this_; }
3359
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003360 DECLARE_INSTRUCTION(ParameterValue);
3361
3362 private:
3363 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00003364 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003365 const uint8_t index_;
3366
Calin Juravle10e244f2015-01-26 18:54:32 +00003367 // Whether or not the parameter value corresponds to 'this' argument.
3368 const bool is_this_;
3369
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003370 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
3371};
3372
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003373class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003374 public:
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003375 explicit HNot(Primitive::Type result_type, HInstruction* input)
3376 : HUnaryOperation(result_type, input) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003377
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003378 bool CanBeMoved() const OVERRIDE { return true; }
3379 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003380 UNUSED(other);
3381 return true;
3382 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003383
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003384 int32_t Evaluate(int32_t x) const OVERRIDE { return ~x; }
3385 int64_t Evaluate(int64_t x) const OVERRIDE { return ~x; }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003386
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003387 DECLARE_INSTRUCTION(Not);
3388
3389 private:
3390 DISALLOW_COPY_AND_ASSIGN(HNot);
3391};
3392
David Brazdil66d126e2015-04-03 16:02:44 +01003393class HBooleanNot : public HUnaryOperation {
3394 public:
3395 explicit HBooleanNot(HInstruction* input)
3396 : HUnaryOperation(Primitive::Type::kPrimBoolean, input) {}
3397
3398 bool CanBeMoved() const OVERRIDE { return true; }
3399 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3400 UNUSED(other);
3401 return true;
3402 }
3403
3404 int32_t Evaluate(int32_t x) const OVERRIDE {
3405 DCHECK(IsUint<1>(x));
3406 return !x;
3407 }
3408
3409 int64_t Evaluate(int64_t x ATTRIBUTE_UNUSED) const OVERRIDE {
3410 LOG(FATAL) << DebugName() << " cannot be used with 64-bit values";
3411 UNREACHABLE();
3412 }
3413
3414 DECLARE_INSTRUCTION(BooleanNot);
3415
3416 private:
3417 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
3418};
3419
Roland Levillaindff1f282014-11-05 14:15:05 +00003420class HTypeConversion : public HExpression<1> {
3421 public:
3422 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00003423 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
3424 : HExpression(result_type, SideEffects::None()), dex_pc_(dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00003425 SetRawInputAt(0, input);
3426 DCHECK_NE(input->GetType(), result_type);
3427 }
3428
3429 HInstruction* GetInput() const { return InputAt(0); }
3430 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
3431 Primitive::Type GetResultType() const { return GetType(); }
3432
Roland Levillain624279f2014-12-04 11:54:28 +00003433 // Required by the x86 and ARM code generators when producing calls
3434 // to the runtime.
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003435 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Roland Levillain624279f2014-12-04 11:54:28 +00003436
Roland Levillaindff1f282014-11-05 14:15:05 +00003437 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00003438 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00003439
Mark Mendelle82549b2015-05-06 10:55:34 -04003440 // Try to statically evaluate the conversion and return a HConstant
3441 // containing the result. If the input cannot be converted, return nullptr.
3442 HConstant* TryStaticEvaluation() const;
3443
Roland Levillaindff1f282014-11-05 14:15:05 +00003444 DECLARE_INSTRUCTION(TypeConversion);
3445
3446 private:
Roland Levillain624279f2014-12-04 11:54:28 +00003447 const uint32_t dex_pc_;
3448
Roland Levillaindff1f282014-11-05 14:15:05 +00003449 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
3450};
3451
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00003452static constexpr uint32_t kNoRegNumber = -1;
3453
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003454class HPhi : public HInstruction {
3455 public:
3456 HPhi(ArenaAllocator* arena, uint32_t reg_number, size_t number_of_inputs, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003457 : HInstruction(SideEffects::None()),
3458 inputs_(arena, number_of_inputs),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003459 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01003460 type_(type),
Calin Juravle10e244f2015-01-26 18:54:32 +00003461 is_live_(false),
3462 can_be_null_(true) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003463 inputs_.SetSize(number_of_inputs);
3464 }
3465
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00003466 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
3467 static Primitive::Type ToPhiType(Primitive::Type type) {
3468 switch (type) {
3469 case Primitive::kPrimBoolean:
3470 case Primitive::kPrimByte:
3471 case Primitive::kPrimShort:
3472 case Primitive::kPrimChar:
3473 return Primitive::kPrimInt;
3474 default:
3475 return type;
3476 }
3477 }
3478
David Brazdilffee3d32015-07-06 11:48:53 +01003479 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
3480
Calin Juravle10e244f2015-01-26 18:54:32 +00003481 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003482
3483 void AddInput(HInstruction* input);
David Brazdil2d7352b2015-04-20 14:52:42 +01003484 void RemoveInputAt(size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003485
Calin Juravle10e244f2015-01-26 18:54:32 +00003486 Primitive::Type GetType() const OVERRIDE { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003487 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003488
Calin Juravle10e244f2015-01-26 18:54:32 +00003489 bool CanBeNull() const OVERRIDE { return can_be_null_; }
3490 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
3491
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003492 uint32_t GetRegNumber() const { return reg_number_; }
3493
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01003494 void SetDead() { is_live_ = false; }
3495 void SetLive() { is_live_ = true; }
3496 bool IsDead() const { return !is_live_; }
3497 bool IsLive() const { return is_live_; }
3498
Calin Juravlea4f88312015-04-16 12:57:19 +01003499 // Returns the next equivalent phi (starting from the current one) or null if there is none.
3500 // An equivalent phi is a phi having the same dex register and type.
3501 // It assumes that phis with the same dex register are adjacent.
3502 HPhi* GetNextEquivalentPhiWithSameType() {
3503 HInstruction* next = GetNext();
3504 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
3505 if (next->GetType() == GetType()) {
3506 return next->AsPhi();
3507 }
3508 next = next->GetNext();
3509 }
3510 return nullptr;
3511 }
3512
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003513 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003514
David Brazdil1abb4192015-02-17 18:33:36 +00003515 protected:
3516 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
3517
3518 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
3519 inputs_.Put(index, input);
3520 }
3521
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003522 private:
David Brazdil1abb4192015-02-17 18:33:36 +00003523 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003524 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003525 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01003526 bool is_live_;
Calin Juravle10e244f2015-01-26 18:54:32 +00003527 bool can_be_null_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003528
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003529 DISALLOW_COPY_AND_ASSIGN(HPhi);
3530};
3531
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003532class HNullCheck : public HExpression<1> {
3533 public:
3534 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003535 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003536 SetRawInputAt(0, value);
3537 }
3538
Calin Juravle10e244f2015-01-26 18:54:32 +00003539 bool CanBeMoved() const OVERRIDE { return true; }
3540 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003541 UNUSED(other);
3542 return true;
3543 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003544
Calin Juravle10e244f2015-01-26 18:54:32 +00003545 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003546
Calin Juravle10e244f2015-01-26 18:54:32 +00003547 bool CanThrow() const OVERRIDE { return true; }
3548
3549 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01003550
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003551 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003552
3553 DECLARE_INSTRUCTION(NullCheck);
3554
3555 private:
3556 const uint32_t dex_pc_;
3557
3558 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
3559};
3560
3561class FieldInfo : public ValueObject {
3562 public:
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003563 FieldInfo(MemberOffset field_offset,
3564 Primitive::Type field_type,
3565 bool is_volatile,
3566 uint32_t index,
3567 const DexFile& dex_file)
3568 : field_offset_(field_offset),
3569 field_type_(field_type),
3570 is_volatile_(is_volatile),
3571 index_(index),
3572 dex_file_(dex_file) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003573
3574 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003575 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003576 uint32_t GetFieldIndex() const { return index_; }
3577 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00003578 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003579
3580 private:
3581 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01003582 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00003583 const bool is_volatile_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003584 uint32_t index_;
3585 const DexFile& dex_file_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003586};
3587
3588class HInstanceFieldGet : public HExpression<1> {
3589 public:
3590 HInstanceFieldGet(HInstruction* value,
3591 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003592 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003593 bool is_volatile,
3594 uint32_t field_idx,
3595 const DexFile& dex_file)
Aart Bik854a02b2015-07-14 16:07:00 -07003596 : HExpression(field_type, SideEffects::SideEffects::FieldReadOfType(field_type)),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003597 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003598 SetRawInputAt(0, value);
3599 }
3600
Calin Juravle10c9cbe2014-12-19 10:50:19 +00003601 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003602
3603 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3604 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
3605 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003606 }
3607
Calin Juravle641547a2015-04-21 22:08:51 +01003608 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3609 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00003610 }
3611
3612 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01003613 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
3614 }
3615
Calin Juravle52c48962014-12-16 17:02:57 +00003616 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003617 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003618 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003619 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003620
3621 DECLARE_INSTRUCTION(InstanceFieldGet);
3622
3623 private:
3624 const FieldInfo field_info_;
3625
3626 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
3627};
3628
3629class HInstanceFieldSet : public HTemplateInstruction<2> {
3630 public:
3631 HInstanceFieldSet(HInstruction* object,
3632 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01003633 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003634 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003635 bool is_volatile,
3636 uint32_t field_idx,
3637 const DexFile& dex_file)
Aart Bik854a02b2015-07-14 16:07:00 -07003638 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type)),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003639 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003640 value_can_be_null_(true) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003641 SetRawInputAt(0, object);
3642 SetRawInputAt(1, value);
3643 }
3644
Calin Juravle641547a2015-04-21 22:08:51 +01003645 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3646 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00003647 }
3648
Calin Juravle52c48962014-12-16 17:02:57 +00003649 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003650 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003651 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003652 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003653 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003654 bool GetValueCanBeNull() const { return value_can_be_null_; }
3655 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003656
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003657 DECLARE_INSTRUCTION(InstanceFieldSet);
3658
3659 private:
3660 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003661 bool value_can_be_null_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003662
3663 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
3664};
3665
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003666class HArrayGet : public HExpression<2> {
3667 public:
3668 HArrayGet(HInstruction* array, HInstruction* index, Primitive::Type type)
Aart Bik854a02b2015-07-14 16:07:00 -07003669 : HExpression(type, SideEffects::ArrayReadOfType(type)) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003670 SetRawInputAt(0, array);
3671 SetRawInputAt(1, index);
3672 }
3673
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003674 bool CanBeMoved() const OVERRIDE { return true; }
3675 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003676 UNUSED(other);
3677 return true;
3678 }
Calin Juravle641547a2015-04-21 22:08:51 +01003679 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3680 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00003681 // TODO: We can be smarter here.
3682 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
3683 // which generates the implicit null check. There are cases when these can be removed
3684 // to produce better code. If we ever add optimizations to do so we should allow an
3685 // implicit check here (as long as the address falls in the first page).
3686 return false;
3687 }
3688
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003689 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003690
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003691 HInstruction* GetArray() const { return InputAt(0); }
3692 HInstruction* GetIndex() const { return InputAt(1); }
3693
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003694 DECLARE_INSTRUCTION(ArrayGet);
3695
3696 private:
3697 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
3698};
3699
3700class HArraySet : public HTemplateInstruction<3> {
3701 public:
3702 HArraySet(HInstruction* array,
3703 HInstruction* index,
3704 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003705 Primitive::Type expected_component_type,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003706 uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07003707 : HTemplateInstruction(SideEffects::ArrayWriteOfType(expected_component_type)),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003708 dex_pc_(dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003709 expected_component_type_(expected_component_type),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003710 needs_type_check_(value->GetType() == Primitive::kPrimNot),
3711 value_can_be_null_(true) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003712 SetRawInputAt(0, array);
3713 SetRawInputAt(1, index);
3714 SetRawInputAt(2, value);
3715 }
3716
Calin Juravle77520bc2015-01-12 18:45:46 +00003717 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003718 // We currently always call a runtime method to catch array store
3719 // exceptions.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003720 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003721 }
3722
Mingyao Yang81014cb2015-06-02 03:16:27 -07003723 // Can throw ArrayStoreException.
3724 bool CanThrow() const OVERRIDE { return needs_type_check_; }
3725
Calin Juravle641547a2015-04-21 22:08:51 +01003726 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3727 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00003728 // TODO: Same as for ArrayGet.
3729 return false;
3730 }
3731
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003732 void ClearNeedsTypeCheck() {
3733 needs_type_check_ = false;
3734 }
3735
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003736 void ClearValueCanBeNull() {
3737 value_can_be_null_ = false;
3738 }
3739
3740 bool GetValueCanBeNull() const { return value_can_be_null_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003741 bool NeedsTypeCheck() const { return needs_type_check_; }
3742
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003743 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003744
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003745 HInstruction* GetArray() const { return InputAt(0); }
3746 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003747 HInstruction* GetValue() const { return InputAt(2); }
3748
3749 Primitive::Type GetComponentType() const {
3750 // The Dex format does not type floating point index operations. Since the
3751 // `expected_component_type_` is set during building and can therefore not
3752 // be correct, we also check what is the value type. If it is a floating
3753 // point type, we must use that type.
3754 Primitive::Type value_type = GetValue()->GetType();
3755 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
3756 ? value_type
3757 : expected_component_type_;
3758 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003759
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003760 DECLARE_INSTRUCTION(ArraySet);
3761
3762 private:
3763 const uint32_t dex_pc_;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003764 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003765 bool needs_type_check_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003766 bool value_can_be_null_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003767
3768 DISALLOW_COPY_AND_ASSIGN(HArraySet);
3769};
3770
3771class HArrayLength : public HExpression<1> {
3772 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003773 explicit HArrayLength(HInstruction* array)
3774 : HExpression(Primitive::kPrimInt, SideEffects::None()) {
3775 // Note that arrays do not change length, so the instruction does not
3776 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003777 SetRawInputAt(0, array);
3778 }
3779
Calin Juravle77520bc2015-01-12 18:45:46 +00003780 bool CanBeMoved() const OVERRIDE { return true; }
3781 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003782 UNUSED(other);
3783 return true;
3784 }
Calin Juravle641547a2015-04-21 22:08:51 +01003785 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3786 return obj == InputAt(0);
3787 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003788
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003789 DECLARE_INSTRUCTION(ArrayLength);
3790
3791 private:
3792 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
3793};
3794
3795class HBoundsCheck : public HExpression<2> {
3796 public:
3797 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003798 : HExpression(index->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003799 DCHECK(index->GetType() == Primitive::kPrimInt);
3800 SetRawInputAt(0, index);
3801 SetRawInputAt(1, length);
3802 }
3803
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003804 bool CanBeMoved() const OVERRIDE { return true; }
3805 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003806 UNUSED(other);
3807 return true;
3808 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003809
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003810 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003811
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003812 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01003813
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003814 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003815
3816 DECLARE_INSTRUCTION(BoundsCheck);
3817
3818 private:
3819 const uint32_t dex_pc_;
3820
3821 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
3822};
3823
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003824/**
3825 * Some DEX instructions are folded into multiple HInstructions that need
3826 * to stay live until the last HInstruction. This class
3827 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00003828 * HInstruction stays live. `index` represents the stack location index of the
3829 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003830 */
3831class HTemporary : public HTemplateInstruction<0> {
3832 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003833 explicit HTemporary(size_t index) : HTemplateInstruction(SideEffects::None()), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003834
3835 size_t GetIndex() const { return index_; }
3836
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00003837 Primitive::Type GetType() const OVERRIDE {
3838 // The previous instruction is the one that will be stored in the temporary location.
3839 DCHECK(GetPrevious() != nullptr);
3840 return GetPrevious()->GetType();
3841 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00003842
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003843 DECLARE_INSTRUCTION(Temporary);
3844
3845 private:
3846 const size_t index_;
3847
3848 DISALLOW_COPY_AND_ASSIGN(HTemporary);
3849};
3850
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003851class HSuspendCheck : public HTemplateInstruction<0> {
3852 public:
3853 explicit HSuspendCheck(uint32_t dex_pc)
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01003854 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003855
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003856 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003857 return true;
3858 }
3859
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003860 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01003861 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
3862 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003863
3864 DECLARE_INSTRUCTION(SuspendCheck);
3865
3866 private:
3867 const uint32_t dex_pc_;
3868
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01003869 // Only used for code generation, in order to share the same slow path between back edges
3870 // of a same loop.
3871 SlowPathCode* slow_path_;
3872
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003873 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
3874};
3875
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003876/**
3877 * Instruction to load a Class object.
3878 */
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003879class HLoadClass : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003880 public:
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003881 HLoadClass(HCurrentMethod* current_method,
3882 uint16_t type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003883 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003884 bool is_referrers_class,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003885 uint32_t dex_pc)
3886 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3887 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003888 dex_file_(dex_file),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003889 is_referrers_class_(is_referrers_class),
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003890 dex_pc_(dex_pc),
Calin Juravleb1498f62015-02-16 13:13:29 +00003891 generate_clinit_check_(false),
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003892 loaded_class_rti_(ReferenceTypeInfo::CreateTop(/* is_exact */ false)) {
3893 SetRawInputAt(0, current_method);
3894 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003895
3896 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003897
3898 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3899 return other->AsLoadClass()->type_index_ == type_index_;
3900 }
3901
3902 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
3903
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003904 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003905 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003906 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01003907 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003908
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003909 bool NeedsEnvironment() const OVERRIDE {
3910 // Will call runtime and load the class if the class is not loaded yet.
3911 // TODO: finer grain decision.
3912 return !is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003913 }
3914
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003915 bool MustGenerateClinitCheck() const {
3916 return generate_clinit_check_;
3917 }
3918
Calin Juravle0ba218d2015-05-19 18:46:01 +01003919 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
3920 generate_clinit_check_ = generate_clinit_check;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003921 }
3922
3923 bool CanCallRuntime() const {
3924 return MustGenerateClinitCheck() || !is_referrers_class_;
3925 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003926
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003927 bool CanThrow() const OVERRIDE {
3928 // May call runtime and and therefore can throw.
3929 // TODO: finer grain decision.
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01003930 return CanCallRuntime();
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003931 }
3932
Calin Juravleacf735c2015-02-12 15:25:22 +00003933 ReferenceTypeInfo GetLoadedClassRTI() {
3934 return loaded_class_rti_;
3935 }
3936
3937 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
3938 // Make sure we only set exact types (the loaded class should never be merged).
3939 DCHECK(rti.IsExact());
3940 loaded_class_rti_ = rti;
3941 }
3942
3943 bool IsResolved() {
3944 return loaded_class_rti_.IsExact();
3945 }
3946
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003947 const DexFile& GetDexFile() { return dex_file_; }
3948
Nicolas Geoffray9437b782015-03-25 10:08:51 +00003949 bool NeedsDexCache() const OVERRIDE { return !is_referrers_class_; }
3950
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003951 DECLARE_INSTRUCTION(LoadClass);
3952
3953 private:
3954 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003955 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003956 const bool is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003957 const uint32_t dex_pc_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003958 // Whether this instruction must generate the initialization check.
3959 // Used for code generation.
3960 bool generate_clinit_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003961
Calin Juravleacf735c2015-02-12 15:25:22 +00003962 ReferenceTypeInfo loaded_class_rti_;
3963
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003964 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
3965};
3966
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01003967class HLoadString : public HExpression<1> {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003968 public:
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01003969 HLoadString(HCurrentMethod* current_method, uint32_t string_index, uint32_t dex_pc)
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003970 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3971 string_index_(string_index),
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01003972 dex_pc_(dex_pc) {
3973 SetRawInputAt(0, current_method);
3974 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003975
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003976 bool CanBeMoved() const OVERRIDE { return true; }
3977
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003978 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3979 return other->AsLoadString()->string_index_ == string_index_;
3980 }
3981
3982 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
3983
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003984 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003985 uint32_t GetStringIndex() const { return string_index_; }
3986
3987 // TODO: Can we deopt or debug when we resolve a string?
3988 bool NeedsEnvironment() const OVERRIDE { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00003989 bool NeedsDexCache() const OVERRIDE { return true; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003990
3991 DECLARE_INSTRUCTION(LoadString);
3992
3993 private:
3994 const uint32_t string_index_;
3995 const uint32_t dex_pc_;
3996
3997 DISALLOW_COPY_AND_ASSIGN(HLoadString);
3998};
3999
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004000/**
4001 * Performs an initialization check on its Class object input.
4002 */
4003class HClinitCheck : public HExpression<1> {
4004 public:
4005 explicit HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07004006 : HExpression(
4007 Primitive::kPrimNot,
4008 SideEffects::All()), // assume write/read on all fields/arrays
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004009 dex_pc_(dex_pc) {
4010 SetRawInputAt(0, constant);
4011 }
4012
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004013 bool CanBeMoved() const OVERRIDE { return true; }
4014 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4015 UNUSED(other);
4016 return true;
4017 }
4018
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004019 bool NeedsEnvironment() const OVERRIDE {
4020 // May call runtime to initialize the class.
4021 return true;
4022 }
4023
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004024 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004025
4026 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
4027
4028 DECLARE_INSTRUCTION(ClinitCheck);
4029
4030 private:
4031 const uint32_t dex_pc_;
4032
4033 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
4034};
4035
4036class HStaticFieldGet : public HExpression<1> {
4037 public:
4038 HStaticFieldGet(HInstruction* cls,
4039 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004040 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004041 bool is_volatile,
4042 uint32_t field_idx,
4043 const DexFile& dex_file)
Aart Bik854a02b2015-07-14 16:07:00 -07004044 : HExpression(field_type, SideEffects::SideEffects::FieldReadOfType(field_type)),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004045 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004046 SetRawInputAt(0, cls);
4047 }
4048
Calin Juravle52c48962014-12-16 17:02:57 +00004049
Calin Juravle10c9cbe2014-12-19 10:50:19 +00004050 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004051
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004052 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00004053 HStaticFieldGet* other_get = other->AsStaticFieldGet();
4054 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004055 }
4056
4057 size_t ComputeHashCode() const OVERRIDE {
4058 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
4059 }
4060
Calin Juravle52c48962014-12-16 17:02:57 +00004061 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004062 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
4063 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004064 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004065
4066 DECLARE_INSTRUCTION(StaticFieldGet);
4067
4068 private:
4069 const FieldInfo field_info_;
4070
4071 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
4072};
4073
4074class HStaticFieldSet : public HTemplateInstruction<2> {
4075 public:
4076 HStaticFieldSet(HInstruction* cls,
4077 HInstruction* value,
4078 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004079 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004080 bool is_volatile,
4081 uint32_t field_idx,
4082 const DexFile& dex_file)
Aart Bik854a02b2015-07-14 16:07:00 -07004083 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type)),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004084 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004085 value_can_be_null_(true) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004086 SetRawInputAt(0, cls);
4087 SetRawInputAt(1, value);
4088 }
4089
Calin Juravle52c48962014-12-16 17:02:57 +00004090 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004091 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
4092 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004093 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004094
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004095 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004096 bool GetValueCanBeNull() const { return value_can_be_null_; }
4097 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004098
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004099 DECLARE_INSTRUCTION(StaticFieldSet);
4100
4101 private:
4102 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004103 bool value_can_be_null_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004104
4105 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
4106};
4107
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004108// Implement the move-exception DEX instruction.
4109class HLoadException : public HExpression<0> {
4110 public:
4111 HLoadException() : HExpression(Primitive::kPrimNot, SideEffects::None()) {}
4112
4113 DECLARE_INSTRUCTION(LoadException);
4114
4115 private:
4116 DISALLOW_COPY_AND_ASSIGN(HLoadException);
4117};
4118
4119class HThrow : public HTemplateInstruction<1> {
4120 public:
4121 HThrow(HInstruction* exception, uint32_t dex_pc)
4122 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {
4123 SetRawInputAt(0, exception);
4124 }
4125
4126 bool IsControlFlow() const OVERRIDE { return true; }
4127
4128 bool NeedsEnvironment() const OVERRIDE { return true; }
4129
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004130 bool CanThrow() const OVERRIDE { return true; }
4131
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004132 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004133
4134 DECLARE_INSTRUCTION(Throw);
4135
4136 private:
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004137 const uint32_t dex_pc_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004138
4139 DISALLOW_COPY_AND_ASSIGN(HThrow);
4140};
4141
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004142class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004143 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004144 HInstanceOf(HInstruction* object,
4145 HLoadClass* constant,
4146 bool class_is_final,
4147 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004148 : HExpression(Primitive::kPrimBoolean, SideEffects::None()),
4149 class_is_final_(class_is_final),
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004150 must_do_null_check_(true),
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004151 dex_pc_(dex_pc) {
4152 SetRawInputAt(0, object);
4153 SetRawInputAt(1, constant);
4154 }
4155
4156 bool CanBeMoved() const OVERRIDE { return true; }
4157
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004158 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004159 return true;
4160 }
4161
4162 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004163 return false;
4164 }
4165
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004166 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004167
4168 bool IsClassFinal() const { return class_is_final_; }
4169
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004170 // Used only in code generation.
4171 bool MustDoNullCheck() const { return must_do_null_check_; }
4172 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
4173
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004174 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004175
4176 private:
4177 const bool class_is_final_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004178 bool must_do_null_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004179 const uint32_t dex_pc_;
4180
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004181 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
4182};
4183
Calin Juravleb1498f62015-02-16 13:13:29 +00004184class HBoundType : public HExpression<1> {
4185 public:
4186 HBoundType(HInstruction* input, ReferenceTypeInfo bound_type)
4187 : HExpression(Primitive::kPrimNot, SideEffects::None()),
4188 bound_type_(bound_type) {
Calin Juravle61d544b2015-02-23 16:46:57 +00004189 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00004190 SetRawInputAt(0, input);
4191 }
4192
4193 const ReferenceTypeInfo& GetBoundType() const { return bound_type_; }
4194
4195 bool CanBeNull() const OVERRIDE {
4196 // `null instanceof ClassX` always return false so we can't be null.
4197 return false;
4198 }
4199
4200 DECLARE_INSTRUCTION(BoundType);
4201
4202 private:
4203 // Encodes the most upper class that this instruction can have. In other words
4204 // it is always the case that GetBoundType().IsSupertypeOf(GetReferenceType()).
4205 // It is used to bound the type in cases like `if (x instanceof ClassX) {}`
4206 const ReferenceTypeInfo bound_type_;
4207
4208 DISALLOW_COPY_AND_ASSIGN(HBoundType);
4209};
4210
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004211class HCheckCast : public HTemplateInstruction<2> {
4212 public:
4213 HCheckCast(HInstruction* object,
4214 HLoadClass* constant,
4215 bool class_is_final,
4216 uint32_t dex_pc)
4217 : HTemplateInstruction(SideEffects::None()),
4218 class_is_final_(class_is_final),
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004219 must_do_null_check_(true),
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004220 dex_pc_(dex_pc) {
4221 SetRawInputAt(0, object);
4222 SetRawInputAt(1, constant);
4223 }
4224
4225 bool CanBeMoved() const OVERRIDE { return true; }
4226
4227 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
4228 return true;
4229 }
4230
4231 bool NeedsEnvironment() const OVERRIDE {
4232 // Instruction may throw a CheckCastError.
4233 return true;
4234 }
4235
4236 bool CanThrow() const OVERRIDE { return true; }
4237
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004238 bool MustDoNullCheck() const { return must_do_null_check_; }
4239 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
4240
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004241 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004242
4243 bool IsClassFinal() const { return class_is_final_; }
4244
4245 DECLARE_INSTRUCTION(CheckCast);
4246
4247 private:
4248 const bool class_is_final_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004249 bool must_do_null_check_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004250 const uint32_t dex_pc_;
4251
4252 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004253};
4254
Calin Juravle27df7582015-04-17 19:12:31 +01004255class HMemoryBarrier : public HTemplateInstruction<0> {
4256 public:
4257 explicit HMemoryBarrier(MemBarrierKind barrier_kind)
4258 : HTemplateInstruction(SideEffects::None()),
4259 barrier_kind_(barrier_kind) {}
4260
4261 MemBarrierKind GetBarrierKind() { return barrier_kind_; }
4262
4263 DECLARE_INSTRUCTION(MemoryBarrier);
4264
4265 private:
4266 const MemBarrierKind barrier_kind_;
4267
4268 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
4269};
4270
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004271class HMonitorOperation : public HTemplateInstruction<1> {
4272 public:
4273 enum OperationKind {
4274 kEnter,
4275 kExit,
4276 };
4277
4278 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07004279 : HTemplateInstruction(SideEffects::All()), // assume write/read on all fields/arrays
4280 kind_(kind), dex_pc_(dex_pc) {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004281 SetRawInputAt(0, object);
4282 }
4283
4284 // Instruction may throw a Java exception, so we need an environment.
David Brazdilbff75032015-07-08 17:26:51 +00004285 bool NeedsEnvironment() const OVERRIDE { return CanThrow(); }
4286
4287 bool CanThrow() const OVERRIDE {
4288 // Verifier guarantees that monitor-exit cannot throw.
4289 // This is important because it allows the HGraphBuilder to remove
4290 // a dead throw-catch loop generated for `synchronized` blocks/methods.
4291 return IsEnter();
4292 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004293
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004294 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004295
4296 bool IsEnter() const { return kind_ == kEnter; }
4297
4298 DECLARE_INSTRUCTION(MonitorOperation);
4299
Calin Juravle52c48962014-12-16 17:02:57 +00004300 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004301 const OperationKind kind_;
4302 const uint32_t dex_pc_;
4303
4304 private:
4305 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
4306};
4307
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01004308/**
4309 * A HInstruction used as a marker for the replacement of new + <init>
4310 * of a String to a call to a StringFactory. Only baseline will see
4311 * the node at code generation, where it will be be treated as null.
4312 * When compiling non-baseline, `HFakeString` instructions are being removed
4313 * in the instruction simplifier.
4314 */
4315class HFakeString : public HTemplateInstruction<0> {
4316 public:
4317 HFakeString() : HTemplateInstruction(SideEffects::None()) {}
4318
4319 Primitive::Type GetType() const OVERRIDE { return Primitive::kPrimNot; }
4320
4321 DECLARE_INSTRUCTION(FakeString);
4322
4323 private:
4324 DISALLOW_COPY_AND_ASSIGN(HFakeString);
4325};
4326
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004327class MoveOperands : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004328 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01004329 MoveOperands(Location source,
4330 Location destination,
4331 Primitive::Type type,
4332 HInstruction* instruction)
4333 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004334
4335 Location GetSource() const { return source_; }
4336 Location GetDestination() const { return destination_; }
4337
4338 void SetSource(Location value) { source_ = value; }
4339 void SetDestination(Location value) { destination_ = value; }
4340
4341 // The parallel move resolver marks moves as "in-progress" by clearing the
4342 // destination (but not the source).
4343 Location MarkPending() {
4344 DCHECK(!IsPending());
4345 Location dest = destination_;
4346 destination_ = Location::NoLocation();
4347 return dest;
4348 }
4349
4350 void ClearPending(Location dest) {
4351 DCHECK(IsPending());
4352 destination_ = dest;
4353 }
4354
4355 bool IsPending() const {
4356 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
4357 return destination_.IsInvalid() && !source_.IsInvalid();
4358 }
4359
4360 // True if this blocks a move from the given location.
4361 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08004362 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004363 }
4364
4365 // A move is redundant if it's been eliminated, if its source and
4366 // destination are the same, or if its destination is unneeded.
4367 bool IsRedundant() const {
4368 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
4369 }
4370
4371 // We clear both operands to indicate move that's been eliminated.
4372 void Eliminate() {
4373 source_ = destination_ = Location::NoLocation();
4374 }
4375
4376 bool IsEliminated() const {
4377 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
4378 return source_.IsInvalid();
4379 }
4380
Alexey Frunze4dda3372015-06-01 18:31:49 -07004381 Primitive::Type GetType() const { return type_; }
4382
Nicolas Geoffray90218252015-04-15 11:56:51 +01004383 bool Is64BitMove() const {
4384 return Primitive::Is64BitType(type_);
4385 }
4386
Nicolas Geoffray740475d2014-09-29 10:33:25 +01004387 HInstruction* GetInstruction() const { return instruction_; }
4388
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004389 private:
4390 Location source_;
4391 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01004392 // The type this move is for.
4393 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01004394 // The instruction this move is assocatied with. Null when this move is
4395 // for moving an input in the expected locations of user (including a phi user).
4396 // This is only used in debug mode, to ensure we do not connect interval siblings
4397 // in the same parallel move.
4398 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004399};
4400
4401static constexpr size_t kDefaultNumberOfMoves = 4;
4402
4403class HParallelMove : public HTemplateInstruction<0> {
4404 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004405 explicit HParallelMove(ArenaAllocator* arena)
4406 : HTemplateInstruction(SideEffects::None()), moves_(arena, kDefaultNumberOfMoves) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004407
Nicolas Geoffray90218252015-04-15 11:56:51 +01004408 void AddMove(Location source,
4409 Location destination,
4410 Primitive::Type type,
4411 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004412 DCHECK(source.IsValid());
4413 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004414 if (kIsDebugBuild) {
4415 if (instruction != nullptr) {
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00004416 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004417 if (moves_.Get(i).GetInstruction() == instruction) {
4418 // Special case the situation where the move is for the spill slot
4419 // of the instruction.
4420 if ((GetPrevious() == instruction)
4421 || ((GetPrevious() == nullptr)
4422 && instruction->IsPhi()
4423 && instruction->GetBlock() == GetBlock())) {
4424 DCHECK_NE(destination.GetKind(), moves_.Get(i).GetDestination().GetKind())
4425 << "Doing parallel moves for the same instruction.";
4426 } else {
4427 DCHECK(false) << "Doing parallel moves for the same instruction.";
4428 }
4429 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00004430 }
4431 }
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004432 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Zheng Xuad4450e2015-04-17 18:48:56 +08004433 DCHECK(!destination.OverlapsWith(moves_.Get(i).GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01004434 << "Overlapped destination for two moves in a parallel move: "
4435 << moves_.Get(i).GetSource() << " ==> " << moves_.Get(i).GetDestination() << " and "
4436 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004437 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01004438 }
Nicolas Geoffray90218252015-04-15 11:56:51 +01004439 moves_.Add(MoveOperands(source, destination, type, instruction));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004440 }
4441
4442 MoveOperands* MoveOperandsAt(size_t index) const {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004443 return moves_.GetRawStorage() + index;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004444 }
4445
4446 size_t NumMoves() const { return moves_.Size(); }
4447
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004448 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004449
4450 private:
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004451 GrowableArray<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004452
4453 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
4454};
4455
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004456class HGraphVisitor : public ValueObject {
4457 public:
Dave Allison20dfc792014-06-16 20:44:29 -07004458 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
4459 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004460
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004461 virtual void VisitInstruction(HInstruction* instruction) { UNUSED(instruction); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004462 virtual void VisitBasicBlock(HBasicBlock* block);
4463
Roland Levillain633021e2014-10-01 14:12:25 +01004464 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004465 void VisitInsertionOrder();
4466
Roland Levillain633021e2014-10-01 14:12:25 +01004467 // Visit the graph following dominator tree reverse post-order.
4468 void VisitReversePostOrder();
4469
Nicolas Geoffray787c3072014-03-17 10:20:19 +00004470 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00004471
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004472 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004473#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004474 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
4475
4476 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
4477
4478#undef DECLARE_VISIT_INSTRUCTION
4479
4480 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07004481 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004482
4483 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
4484};
4485
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004486class HGraphDelegateVisitor : public HGraphVisitor {
4487 public:
4488 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
4489 virtual ~HGraphDelegateVisitor() {}
4490
4491 // Visit functions that delegate to to super class.
4492#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004493 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004494
4495 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
4496
4497#undef DECLARE_VISIT_INSTRUCTION
4498
4499 private:
4500 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
4501};
4502
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004503class HInsertionOrderIterator : public ValueObject {
4504 public:
4505 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
4506
4507 bool Done() const { return index_ == graph_.GetBlocks().Size(); }
4508 HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); }
4509 void Advance() { ++index_; }
4510
4511 private:
4512 const HGraph& graph_;
4513 size_t index_;
4514
4515 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
4516};
4517
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004518class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004519 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00004520 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
4521 // Check that reverse post order of the graph has been built.
4522 DCHECK(!graph.GetReversePostOrder().IsEmpty());
4523 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004524
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004525 bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); }
4526 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004527 void Advance() { ++index_; }
4528
4529 private:
4530 const HGraph& graph_;
4531 size_t index_;
4532
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004533 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004534};
4535
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004536class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004537 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004538 explicit HPostOrderIterator(const HGraph& graph)
David Brazdil10f56cb2015-03-24 18:49:14 +00004539 : graph_(graph), index_(graph_.GetReversePostOrder().Size()) {
4540 // Check that reverse post order of the graph has been built.
4541 DCHECK(!graph.GetReversePostOrder().IsEmpty());
4542 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004543
4544 bool Done() const { return index_ == 0; }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004545 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004546 void Advance() { --index_; }
4547
4548 private:
4549 const HGraph& graph_;
4550 size_t index_;
4551
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004552 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004553};
4554
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01004555class HLinearPostOrderIterator : public ValueObject {
4556 public:
4557 explicit HLinearPostOrderIterator(const HGraph& graph)
4558 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().Size()) {}
4559
4560 bool Done() const { return index_ == 0; }
4561
4562 HBasicBlock* Current() const { return order_.Get(index_ -1); }
4563
4564 void Advance() {
4565 --index_;
4566 DCHECK_GE(index_, 0U);
4567 }
4568
4569 private:
4570 const GrowableArray<HBasicBlock*>& order_;
4571 size_t index_;
4572
4573 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
4574};
4575
4576class HLinearOrderIterator : public ValueObject {
4577 public:
4578 explicit HLinearOrderIterator(const HGraph& graph)
4579 : order_(graph.GetLinearOrder()), index_(0) {}
4580
4581 bool Done() const { return index_ == order_.Size(); }
4582 HBasicBlock* Current() const { return order_.Get(index_); }
4583 void Advance() { ++index_; }
4584
4585 private:
4586 const GrowableArray<HBasicBlock*>& order_;
4587 size_t index_;
4588
4589 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
4590};
4591
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004592// Iterator over the blocks that art part of the loop. Includes blocks part
4593// of an inner loop. The order in which the blocks are iterated is on their
4594// block id.
4595class HBlocksInLoopIterator : public ValueObject {
4596 public:
4597 explicit HBlocksInLoopIterator(const HLoopInformation& info)
4598 : blocks_in_loop_(info.GetBlocks()),
4599 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
4600 index_(0) {
4601 if (!blocks_in_loop_.IsBitSet(index_)) {
4602 Advance();
4603 }
4604 }
4605
4606 bool Done() const { return index_ == blocks_.Size(); }
4607 HBasicBlock* Current() const { return blocks_.Get(index_); }
4608 void Advance() {
4609 ++index_;
4610 for (size_t e = blocks_.Size(); index_ < e; ++index_) {
4611 if (blocks_in_loop_.IsBitSet(index_)) {
4612 break;
4613 }
4614 }
4615 }
4616
4617 private:
4618 const BitVector& blocks_in_loop_;
4619 const GrowableArray<HBasicBlock*>& blocks_;
4620 size_t index_;
4621
4622 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
4623};
4624
Mingyao Yang3584bce2015-05-19 16:01:59 -07004625// Iterator over the blocks that art part of the loop. Includes blocks part
4626// of an inner loop. The order in which the blocks are iterated is reverse
4627// post order.
4628class HBlocksInLoopReversePostOrderIterator : public ValueObject {
4629 public:
4630 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
4631 : blocks_in_loop_(info.GetBlocks()),
4632 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
4633 index_(0) {
4634 if (!blocks_in_loop_.IsBitSet(blocks_.Get(index_)->GetBlockId())) {
4635 Advance();
4636 }
4637 }
4638
4639 bool Done() const { return index_ == blocks_.Size(); }
4640 HBasicBlock* Current() const { return blocks_.Get(index_); }
4641 void Advance() {
4642 ++index_;
4643 for (size_t e = blocks_.Size(); index_ < e; ++index_) {
4644 if (blocks_in_loop_.IsBitSet(blocks_.Get(index_)->GetBlockId())) {
4645 break;
4646 }
4647 }
4648 }
4649
4650 private:
4651 const BitVector& blocks_in_loop_;
4652 const GrowableArray<HBasicBlock*>& blocks_;
4653 size_t index_;
4654
4655 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
4656};
4657
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00004658inline int64_t Int64FromConstant(HConstant* constant) {
4659 DCHECK(constant->IsIntConstant() || constant->IsLongConstant());
4660 return constant->IsIntConstant() ? constant->AsIntConstant()->GetValue()
4661 : constant->AsLongConstant()->GetValue();
4662}
4663
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004664} // namespace art
4665
4666#endif // ART_COMPILER_OPTIMIZING_NODES_H_