blob: 7f446d4cf6dfbcf177d8c4257687aec147f49cab [file] [log] [blame]
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ART_COMPILER_OPTIMIZING_NODES_H_
18#define ART_COMPILER_OPTIMIZING_NODES_H_
19
David Brazdil8d5b8b22015-03-24 10:51:52 +000020#include "base/arena_containers.h"
Mathieu Chartierb666f482015-02-18 14:33:14 -080021#include "base/arena_object.h"
Calin Juravle27df7582015-04-17 19:12:31 +010022#include "dex/compiler_enums.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000023#include "entrypoints/quick/quick_entrypoints_enum.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000024#include "handle.h"
25#include "handle_scope.h"
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000026#include "invoke_type.h"
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +010027#include "locations.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000028#include "mirror/class.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010029#include "offsets.h"
Ian Rogerse63db272014-07-15 15:36:11 -070030#include "primitive.h"
Nicolas Geoffray0e336432014-02-26 18:24:38 +000031#include "utils/arena_bit_vector.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000032#include "utils/growable_array.h"
33
34namespace art {
35
David Brazdil1abb4192015-02-17 18:33:36 +000036class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000037class HBasicBlock;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010038class HCurrentMethod;
David Brazdil8d5b8b22015-03-24 10:51:52 +000039class HDoubleConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010040class HEnvironment;
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +010041class HFakeString;
David Brazdil8d5b8b22015-03-24 10:51:52 +000042class HFloatConstant;
David Brazdilfc6a86a2015-06-26 10:33:45 +000043class HGraphBuilder;
David Brazdil8d5b8b22015-03-24 10:51:52 +000044class HGraphVisitor;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000045class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000046class HIntConstant;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000047class HInvoke;
David Brazdil8d5b8b22015-03-24 10:51:52 +000048class HLongConstant;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +000049class HNullConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010050class HPhi;
Nicolas Geoffray3c049742014-09-24 18:10:46 +010051class HSuspendCheck;
David Brazdilffee3d32015-07-06 11:48:53 +010052class HTryBoundary;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010053class LiveInterval;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000054class LocationSummary;
Nicolas Geoffraydb216f42015-05-05 17:02:20 +010055class SlowPathCode;
David Brazdil8d5b8b22015-03-24 10:51:52 +000056class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000057
58static const int kDefaultNumberOfBlocks = 8;
59static const int kDefaultNumberOfSuccessors = 2;
60static const int kDefaultNumberOfPredecessors = 2;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010061static const int kDefaultNumberOfDominatedBlocks = 1;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000062static const int kDefaultNumberOfBackEdges = 1;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000063
Calin Juravle9aec02f2014-11-18 23:06:35 +000064static constexpr uint32_t kMaxIntShiftValue = 0x1f;
65static constexpr uint64_t kMaxLongShiftValue = 0x3f;
66
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010067static constexpr uint32_t kUnknownFieldIndex = static_cast<uint32_t>(-1);
68
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +010069static constexpr InvokeType kInvalidInvokeType = static_cast<InvokeType>(-1);
70
Dave Allison20dfc792014-06-16 20:44:29 -070071enum IfCondition {
72 kCondEQ,
73 kCondNE,
74 kCondLT,
75 kCondLE,
76 kCondGT,
77 kCondGE,
78};
79
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010080class HInstructionList {
81 public:
82 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
83
84 void AddInstruction(HInstruction* instruction);
85 void RemoveInstruction(HInstruction* instruction);
86
David Brazdilc3d743f2015-04-22 13:40:50 +010087 // Insert `instruction` before/after an existing instruction `cursor`.
88 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
89 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
90
Roland Levillain6b469232014-09-25 10:10:38 +010091 // Return true if this list contains `instruction`.
92 bool Contains(HInstruction* instruction) const;
93
Roland Levillainccc07a92014-09-16 14:48:16 +010094 // Return true if `instruction1` is found before `instruction2` in
95 // this instruction list and false otherwise. Abort if none
96 // of these instructions is found.
97 bool FoundBefore(const HInstruction* instruction1,
98 const HInstruction* instruction2) const;
99
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000100 bool IsEmpty() const { return first_instruction_ == nullptr; }
101 void Clear() { first_instruction_ = last_instruction_ = nullptr; }
102
103 // Update the block of all instructions to be `block`.
104 void SetBlockOfInstructions(HBasicBlock* block) const;
105
106 void AddAfter(HInstruction* cursor, const HInstructionList& instruction_list);
107 void Add(const HInstructionList& instruction_list);
108
David Brazdil2d7352b2015-04-20 14:52:42 +0100109 // Return the number of instructions in the list. This is an expensive operation.
110 size_t CountSize() const;
111
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100112 private:
113 HInstruction* first_instruction_;
114 HInstruction* last_instruction_;
115
116 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000117 friend class HGraph;
118 friend class HInstruction;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100119 friend class HInstructionIterator;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100120 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100121
122 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
123};
124
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000125// Control-flow graph of a method. Contains a list of basic blocks.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700126class HGraph : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000127 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100128 HGraph(ArenaAllocator* arena,
129 const DexFile& dex_file,
130 uint32_t method_idx,
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100131 bool should_generate_constructor_barrier,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700132 InstructionSet instruction_set,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100133 InvokeType invoke_type = kInvalidInvokeType,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100134 bool debuggable = false,
135 int start_instruction_id = 0)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000136 : arena_(arena),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000137 blocks_(arena, kDefaultNumberOfBlocks),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100138 reverse_post_order_(arena, kDefaultNumberOfBlocks),
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100139 linear_order_(arena, kDefaultNumberOfBlocks),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700140 entry_block_(nullptr),
141 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100142 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100143 number_of_vregs_(0),
144 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000145 temporaries_vreg_slots_(0),
Mark Mendell1152c922015-04-24 17:06:35 -0400146 has_bounds_checks_(false),
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000147 debuggable_(debuggable),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000148 current_instruction_id_(start_instruction_id),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100149 dex_file_(dex_file),
150 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100151 invoke_type_(invoke_type),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100152 in_ssa_form_(false),
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100153 should_generate_constructor_barrier_(should_generate_constructor_barrier),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700154 instruction_set_(instruction_set),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000155 cached_null_constant_(nullptr),
156 cached_int_constants_(std::less<int32_t>(), arena->Adapter()),
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000157 cached_float_constants_(std::less<int32_t>(), arena->Adapter()),
158 cached_long_constants_(std::less<int64_t>(), arena->Adapter()),
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100159 cached_double_constants_(std::less<int64_t>(), arena->Adapter()),
160 cached_current_method_(nullptr) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000161
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000162 ArenaAllocator* GetArena() const { return arena_; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100163 const GrowableArray<HBasicBlock*>& GetBlocks() const { return blocks_; }
Roland Levillain93445682014-10-06 19:24:02 +0100164 HBasicBlock* GetBlock(size_t id) const { return blocks_.Get(id); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000165
David Brazdil69ba7b72015-06-23 18:27:30 +0100166 bool IsInSsaForm() const { return in_ssa_form_; }
167
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000168 HBasicBlock* GetEntryBlock() const { return entry_block_; }
169 HBasicBlock* GetExitBlock() const { return exit_block_; }
David Brazdilc7af85d2015-05-26 12:05:55 +0100170 bool HasExitBlock() const { return exit_block_ != nullptr; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000171
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000172 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
173 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000174
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000175 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100176
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000177 // Try building the SSA form of this graph, with dominance computation and loop
178 // recognition. Returns whether it was successful in doing all these steps.
179 bool TryBuildingSsa() {
180 BuildDominatorTree();
Nicolas Geoffrayd3350832015-03-12 11:16:23 +0000181 // The SSA builder requires loops to all be natural. Specifically, the dead phi
182 // elimination phase checks the consistency of the graph when doing a post-order
183 // visit for eliminating dead phis: a dead phi can only have loop header phi
184 // users remaining when being visited.
185 if (!AnalyzeNaturalLoops()) return false;
David Brazdilffee3d32015-07-06 11:48:53 +0100186 // Precompute per-block try membership before entering the SSA builder,
187 // which needs the information to build catch block phis from values of
188 // locals at throwing instructions inside try blocks.
189 ComputeTryBlockInformation();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000190 TransformToSsa();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100191 in_ssa_form_ = true;
Nicolas Geoffrayd3350832015-03-12 11:16:23 +0000192 return true;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000193 }
194
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100195 void ComputeDominanceInformation();
196 void ClearDominanceInformation();
197
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000198 void BuildDominatorTree();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000199 void TransformToSsa();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100200 void SimplifyCFG();
David Brazdilffee3d32015-07-06 11:48:53 +0100201 void SimplifyCatchBlocks();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000202
Nicolas Geoffrayf5370122014-12-02 11:51:19 +0000203 // Analyze all natural loops in this graph. Returns false if one
204 // loop is not natural, that is the header does not dominate the
205 // back edge.
206 bool AnalyzeNaturalLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100207
David Brazdilffee3d32015-07-06 11:48:53 +0100208 // Iterate over blocks to compute try block membership. Needs reverse post
209 // order and loop information.
210 void ComputeTryBlockInformation();
211
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000212 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
213 void InlineInto(HGraph* outer_graph, HInvoke* invoke);
214
Mingyao Yang3584bce2015-05-19 16:01:59 -0700215 // Need to add a couple of blocks to test if the loop body is entered and
216 // put deoptimization instructions, etc.
217 void TransformLoopHeaderForBCE(HBasicBlock* header);
218
David Brazdil2d7352b2015-04-20 14:52:42 +0100219 // Removes `block` from the graph.
220 void DeleteDeadBlock(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000221
David Brazdilfc6a86a2015-06-26 10:33:45 +0000222 // Splits the edge between `block` and `successor` while preserving the
223 // indices in the predecessor/successor lists. If there are multiple edges
224 // between the blocks, the lowest indices are used.
225 // Returns the new block which is empty and has the same dex pc as `successor`.
226 HBasicBlock* SplitEdge(HBasicBlock* block, HBasicBlock* successor);
227
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100228 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
229 void SimplifyLoop(HBasicBlock* header);
230
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000231 int32_t GetNextInstructionId() {
232 DCHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000233 return current_instruction_id_++;
234 }
235
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000236 int32_t GetCurrentInstructionId() const {
237 return current_instruction_id_;
238 }
239
240 void SetCurrentInstructionId(int32_t id) {
241 current_instruction_id_ = id;
242 }
243
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100244 uint16_t GetMaximumNumberOfOutVRegs() const {
245 return maximum_number_of_out_vregs_;
246 }
247
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000248 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
249 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100250 }
251
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100252 void UpdateMaximumNumberOfOutVRegs(uint16_t other_value) {
253 maximum_number_of_out_vregs_ = std::max(maximum_number_of_out_vregs_, other_value);
254 }
255
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000256 void UpdateTemporariesVRegSlots(size_t slots) {
257 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100258 }
259
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000260 size_t GetTemporariesVRegSlots() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100261 DCHECK(!in_ssa_form_);
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000262 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100263 }
264
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100265 void SetNumberOfVRegs(uint16_t number_of_vregs) {
266 number_of_vregs_ = number_of_vregs;
267 }
268
269 uint16_t GetNumberOfVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100270 DCHECK(!in_ssa_form_);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100271 return number_of_vregs_;
272 }
273
274 void SetNumberOfInVRegs(uint16_t value) {
275 number_of_in_vregs_ = value;
276 }
277
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100278 uint16_t GetNumberOfLocalVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100279 DCHECK(!in_ssa_form_);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100280 return number_of_vregs_ - number_of_in_vregs_;
281 }
282
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100283 const GrowableArray<HBasicBlock*>& GetReversePostOrder() const {
284 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100285 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100286
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100287 const GrowableArray<HBasicBlock*>& GetLinearOrder() const {
288 return linear_order_;
289 }
290
Mark Mendell1152c922015-04-24 17:06:35 -0400291 bool HasBoundsChecks() const {
292 return has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800293 }
294
Mark Mendell1152c922015-04-24 17:06:35 -0400295 void SetHasBoundsChecks(bool value) {
296 has_bounds_checks_ = value;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800297 }
298
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100299 bool ShouldGenerateConstructorBarrier() const {
300 return should_generate_constructor_barrier_;
301 }
302
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000303 bool IsDebuggable() const { return debuggable_; }
304
David Brazdil8d5b8b22015-03-24 10:51:52 +0000305 // Returns a constant of the given type and value. If it does not exist
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000306 // already, it is created and inserted into the graph. This method is only for
307 // integral types.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000308 HConstant* GetConstant(Primitive::Type type, int64_t value);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000309 HNullConstant* GetNullConstant();
David Brazdil8d5b8b22015-03-24 10:51:52 +0000310 HIntConstant* GetIntConstant(int32_t value) {
311 return CreateConstant(value, &cached_int_constants_);
312 }
313 HLongConstant* GetLongConstant(int64_t value) {
314 return CreateConstant(value, &cached_long_constants_);
315 }
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000316 HFloatConstant* GetFloatConstant(float value) {
317 return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_);
318 }
319 HDoubleConstant* GetDoubleConstant(double value) {
320 return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_);
321 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000322
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100323 HCurrentMethod* GetCurrentMethod();
324
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000325 HBasicBlock* FindCommonDominator(HBasicBlock* first, HBasicBlock* second) const;
David Brazdil2d7352b2015-04-20 14:52:42 +0100326
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100327 const DexFile& GetDexFile() const {
328 return dex_file_;
329 }
330
331 uint32_t GetMethodIdx() const {
332 return method_idx_;
333 }
334
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100335 InvokeType GetInvokeType() const {
336 return invoke_type_;
337 }
338
Mark Mendellc4701932015-04-10 13:18:51 -0400339 InstructionSet GetInstructionSet() const {
340 return instruction_set_;
341 }
342
David Brazdil2d7352b2015-04-20 14:52:42 +0100343 private:
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000344 void VisitBlockForDominatorTree(HBasicBlock* block,
345 HBasicBlock* predecessor,
346 GrowableArray<size_t>* visits);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100347 void FindBackEdges(ArenaBitVector* visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000348 void VisitBlockForBackEdges(HBasicBlock* block,
349 ArenaBitVector* visited,
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100350 ArenaBitVector* visiting);
Roland Levillainfc600dc2014-12-02 17:16:31 +0000351 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100352 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000353
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000354 template <class InstructionType, typename ValueType>
355 InstructionType* CreateConstant(ValueType value,
356 ArenaSafeMap<ValueType, InstructionType*>* cache) {
357 // Try to find an existing constant of the given value.
358 InstructionType* constant = nullptr;
359 auto cached_constant = cache->find(value);
360 if (cached_constant != cache->end()) {
361 constant = cached_constant->second;
362 }
363
364 // If not found or previously deleted, create and cache a new instruction.
Nicolas Geoffrayf78848f2015-06-17 11:57:56 +0100365 // Don't bother reviving a previously deleted instruction, for simplicity.
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000366 if (constant == nullptr || constant->GetBlock() == nullptr) {
367 constant = new (arena_) InstructionType(value);
368 cache->Overwrite(value, constant);
369 InsertConstant(constant);
370 }
371 return constant;
372 }
373
David Brazdil8d5b8b22015-03-24 10:51:52 +0000374 void InsertConstant(HConstant* instruction);
375
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000376 // Cache a float constant into the graph. This method should only be
377 // called by the SsaBuilder when creating "equivalent" instructions.
378 void CacheFloatConstant(HFloatConstant* constant);
379
380 // See CacheFloatConstant comment.
381 void CacheDoubleConstant(HDoubleConstant* constant);
382
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000383 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000384
385 // List of blocks in insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000386 GrowableArray<HBasicBlock*> blocks_;
387
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100388 // List of blocks to perform a reverse post order tree traversal.
389 GrowableArray<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000390
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100391 // List of blocks to perform a linear order tree traversal.
392 GrowableArray<HBasicBlock*> linear_order_;
393
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000394 HBasicBlock* entry_block_;
395 HBasicBlock* exit_block_;
396
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100397 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100398 uint16_t maximum_number_of_out_vregs_;
399
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100400 // The number of virtual registers in this method. Contains the parameters.
401 uint16_t number_of_vregs_;
402
403 // The number of virtual registers used by parameters of this method.
404 uint16_t number_of_in_vregs_;
405
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000406 // Number of vreg size slots that the temporaries use (used in baseline compiler).
407 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100408
Mark Mendell1152c922015-04-24 17:06:35 -0400409 // Has bounds checks. We can totally skip BCE if it's false.
410 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800411
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000412 // Indicates whether the graph should be compiled in a way that
413 // ensures full debuggability. If false, we can apply more
414 // aggressive optimizations that may limit the level of debugging.
415 const bool debuggable_;
416
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000417 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000418 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000419
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100420 // The dex file from which the method is from.
421 const DexFile& dex_file_;
422
423 // The method index in the dex file.
424 const uint32_t method_idx_;
425
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100426 // If inlined, this encodes how the callee is being invoked.
427 const InvokeType invoke_type_;
428
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100429 // Whether the graph has been transformed to SSA form. Only used
430 // in debug mode to ensure we are not using properties only valid
431 // for non-SSA form (like the number of temporaries).
432 bool in_ssa_form_;
433
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100434 const bool should_generate_constructor_barrier_;
435
Mathieu Chartiere401d142015-04-22 13:56:20 -0700436 const InstructionSet instruction_set_;
437
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000438 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000439 HNullConstant* cached_null_constant_;
440 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000441 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000442 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000443 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000444
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100445 HCurrentMethod* cached_current_method_;
446
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000447 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100448 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000449 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000450 DISALLOW_COPY_AND_ASSIGN(HGraph);
451};
452
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700453class HLoopInformation : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000454 public:
455 HLoopInformation(HBasicBlock* header, HGraph* graph)
456 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100457 suspend_check_(nullptr),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100458 back_edges_(graph->GetArena(), kDefaultNumberOfBackEdges),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100459 // Make bit vector growable, as the number of blocks may change.
460 blocks_(graph->GetArena(), graph->GetBlocks().Size(), true) {}
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100461
462 HBasicBlock* GetHeader() const {
463 return header_;
464 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000465
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000466 void SetHeader(HBasicBlock* block) {
467 header_ = block;
468 }
469
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100470 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
471 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
472 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
473
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000474 void AddBackEdge(HBasicBlock* back_edge) {
475 back_edges_.Add(back_edge);
476 }
477
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100478 void RemoveBackEdge(HBasicBlock* back_edge) {
479 back_edges_.Delete(back_edge);
480 }
481
David Brazdil46e2a392015-03-16 17:31:52 +0000482 bool IsBackEdge(const HBasicBlock& block) const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100483 for (size_t i = 0, e = back_edges_.Size(); i < e; ++i) {
David Brazdil46e2a392015-03-16 17:31:52 +0000484 if (back_edges_.Get(i) == &block) return true;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100485 }
486 return false;
487 }
488
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000489 size_t NumberOfBackEdges() const {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000490 return back_edges_.Size();
491 }
492
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100493 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100494
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100495 const GrowableArray<HBasicBlock*>& GetBackEdges() const {
496 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100497 }
498
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100499 // Returns the lifetime position of the back edge that has the
500 // greatest lifetime position.
501 size_t GetLifetimeEnd() const;
502
503 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
504 for (size_t i = 0, e = back_edges_.Size(); i < e; ++i) {
505 if (back_edges_.Get(i) == existing) {
506 back_edges_.Put(i, new_back_edge);
507 return;
508 }
509 }
510 UNREACHABLE();
Nicolas Geoffray57902602015-04-21 14:28:41 +0100511 }
512
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100513 // Finds blocks that are part of this loop. Returns whether the loop is a natural loop,
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100514 // that is the header dominates the back edge.
515 bool Populate();
516
David Brazdila4b8c212015-05-07 09:59:30 +0100517 // Reanalyzes the loop by removing loop info from its blocks and re-running
518 // Populate(). If there are no back edges left, the loop info is completely
519 // removed as well as its SuspendCheck instruction. It must be run on nested
520 // inner loops first.
521 void Update();
522
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100523 // Returns whether this loop information contains `block`.
524 // Note that this loop information *must* be populated before entering this function.
525 bool Contains(const HBasicBlock& block) const;
526
527 // Returns whether this loop information is an inner loop of `other`.
528 // Note that `other` *must* be populated before entering this function.
529 bool IsIn(const HLoopInformation& other) const;
530
531 const ArenaBitVector& GetBlocks() const { return blocks_; }
532
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000533 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000534 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000535
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000536 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100537 // Internal recursive implementation of `Populate`.
538 void PopulateRecursive(HBasicBlock* block);
539
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000540 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100541 HSuspendCheck* suspend_check_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000542 GrowableArray<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100543 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000544
545 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
546};
547
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100548static constexpr size_t kNoLifetime = -1;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100549static constexpr uint32_t kNoDexPc = -1;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100550
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000551// A block in a method. Contains the list of instructions represented
552// as a double linked list. Each block knows its predecessors and
553// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100554
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700555class HBasicBlock : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000556 public:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100557 explicit HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000558 : graph_(graph),
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000559 predecessors_(graph->GetArena(), kDefaultNumberOfPredecessors),
560 successors_(graph->GetArena(), kDefaultNumberOfSuccessors),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000561 loop_information_(nullptr),
562 dominator_(nullptr),
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100563 dominated_blocks_(graph->GetArena(), kDefaultNumberOfDominatedBlocks),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100564 block_id_(-1),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100565 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100566 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000567 lifetime_end_(kNoLifetime),
568 is_catch_block_(false) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000569
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100570 const GrowableArray<HBasicBlock*>& GetPredecessors() const {
571 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000572 }
573
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100574 const GrowableArray<HBasicBlock*>& GetSuccessors() const {
575 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000576 }
577
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100578 const GrowableArray<HBasicBlock*>& GetDominatedBlocks() const {
579 return dominated_blocks_;
580 }
581
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100582 bool IsEntryBlock() const {
583 return graph_->GetEntryBlock() == this;
584 }
585
586 bool IsExitBlock() const {
587 return graph_->GetExitBlock() == this;
588 }
589
David Brazdil46e2a392015-03-16 17:31:52 +0000590 bool IsSingleGoto() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000591 bool IsSingleTryBoundary() const;
592
593 // Returns true if this block emits nothing but a jump.
594 bool IsSingleJump() const {
595 HLoopInformation* loop_info = GetLoopInformation();
596 return (IsSingleGoto() || IsSingleTryBoundary())
597 // Back edges generate a suspend check.
598 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
599 }
David Brazdil46e2a392015-03-16 17:31:52 +0000600
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000601 void AddBackEdge(HBasicBlock* back_edge) {
602 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000603 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000604 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100605 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000606 loop_information_->AddBackEdge(back_edge);
607 }
608
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000609 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000610 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000611
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000612 int GetBlockId() const { return block_id_; }
613 void SetBlockId(int id) { block_id_ = id; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000614
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000615 HBasicBlock* GetDominator() const { return dominator_; }
616 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100617 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.Add(block); }
David Brazdil2d7352b2015-04-20 14:52:42 +0100618 void RemoveDominatedBlock(HBasicBlock* block) { dominated_blocks_.Delete(block); }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000619 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
620 for (size_t i = 0, e = dominated_blocks_.Size(); i < e; ++i) {
621 if (dominated_blocks_.Get(i) == existing) {
622 dominated_blocks_.Put(i, new_block);
623 return;
624 }
625 }
626 LOG(FATAL) << "Unreachable";
627 UNREACHABLE();
628 }
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100629 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000630
631 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100632 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000633 }
634
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100635 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
636 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100637 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100638 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +0100639 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
640 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000641
642 void AddSuccessor(HBasicBlock* block) {
643 successors_.Add(block);
644 block->predecessors_.Add(this);
645 }
646
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100647 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
648 size_t successor_index = GetSuccessorIndexOf(existing);
649 DCHECK_NE(successor_index, static_cast<size_t>(-1));
650 existing->RemovePredecessor(this);
651 new_block->predecessors_.Add(this);
652 successors_.Put(successor_index, new_block);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000653 }
654
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000655 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
656 size_t predecessor_index = GetPredecessorIndexOf(existing);
657 DCHECK_NE(predecessor_index, static_cast<size_t>(-1));
658 existing->RemoveSuccessor(this);
659 new_block->successors_.Add(this);
660 predecessors_.Put(predecessor_index, new_block);
661 }
662
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100663 // Insert `this` between `predecessor` and `successor. This method
664 // preserves the indicies, and will update the first edge found between
665 // `predecessor` and `successor`.
666 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
667 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
668 DCHECK_NE(predecessor_index, static_cast<size_t>(-1));
669 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
670 DCHECK_NE(successor_index, static_cast<size_t>(-1));
671 successor->predecessors_.Put(predecessor_index, this);
672 predecessor->successors_.Put(successor_index, this);
673 successors_.Add(successor);
674 predecessors_.Add(predecessor);
675 }
676
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100677 void RemovePredecessor(HBasicBlock* block) {
678 predecessors_.Delete(block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100679 }
680
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000681 void RemoveSuccessor(HBasicBlock* block) {
682 successors_.Delete(block);
683 }
684
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100685 void ClearAllPredecessors() {
686 predecessors_.Reset();
687 }
688
689 void AddPredecessor(HBasicBlock* block) {
690 predecessors_.Add(block);
691 block->successors_.Add(this);
692 }
693
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100694 void SwapPredecessors() {
Nicolas Geoffrayc83d4412014-09-18 16:46:20 +0100695 DCHECK_EQ(predecessors_.Size(), 2u);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100696 HBasicBlock* temp = predecessors_.Get(0);
697 predecessors_.Put(0, predecessors_.Get(1));
698 predecessors_.Put(1, temp);
699 }
700
David Brazdil769c9e52015-04-27 13:54:09 +0100701 void SwapSuccessors() {
702 DCHECK_EQ(successors_.Size(), 2u);
703 HBasicBlock* temp = successors_.Get(0);
704 successors_.Put(0, successors_.Get(1));
705 successors_.Put(1, temp);
706 }
707
David Brazdilfc6a86a2015-06-26 10:33:45 +0000708 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100709 for (size_t i = 0, e = predecessors_.Size(); i < e; ++i) {
710 if (predecessors_.Get(i) == predecessor) {
711 return i;
712 }
713 }
714 return -1;
715 }
716
David Brazdilfc6a86a2015-06-26 10:33:45 +0000717 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100718 for (size_t i = 0, e = successors_.Size(); i < e; ++i) {
719 if (successors_.Get(i) == successor) {
720 return i;
721 }
722 }
723 return -1;
724 }
725
David Brazdilfc6a86a2015-06-26 10:33:45 +0000726 HBasicBlock* GetSinglePredecessor() const {
727 DCHECK_EQ(GetPredecessors().Size(), 1u);
728 return GetPredecessors().Get(0);
729 }
730
731 HBasicBlock* GetSingleSuccessor() const {
732 DCHECK_EQ(GetSuccessors().Size(), 1u);
733 return GetSuccessors().Get(0);
734 }
735
736 // Returns whether the first occurrence of `predecessor` in the list of
737 // predecessors is at index `idx`.
738 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
739 DCHECK_EQ(GetPredecessors().Get(idx), predecessor);
740 return GetPredecessorIndexOf(predecessor) == idx;
741 }
742
David Brazdilffee3d32015-07-06 11:48:53 +0100743 // Returns the number of non-exceptional successors. SsaChecker ensures that
744 // these are stored at the beginning of the successor list.
745 size_t NumberOfNormalSuccessors() const {
746 return EndsWithTryBoundary() ? 1 : GetSuccessors().Size();
747 }
David Brazdilfc6a86a2015-06-26 10:33:45 +0000748
749 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +0100750 // created, latter block. Note that this method will add the block to the
751 // graph, create a Goto at the end of the former block and will create an edge
752 // between the blocks. It will not, however, update the reverse post order or
753 // loop information.
David Brazdilfc6a86a2015-06-26 10:33:45 +0000754 HBasicBlock* SplitBefore(HInstruction* cursor);
755
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000756 // Split the block into two blocks just after `cursor`. Returns the newly
757 // created block. Note that this method just updates raw block information,
758 // like predecessors, successors, dominators, and instruction list. It does not
759 // update the graph, reverse post order, loop information, nor make sure the
760 // blocks are consistent (for example ending with a control flow instruction).
761 HBasicBlock* SplitAfter(HInstruction* cursor);
762
763 // Merge `other` at the end of `this`. Successors and dominated blocks of
764 // `other` are changed to be successors and dominated blocks of `this`. Note
765 // that this method does not update the graph, reverse post order, loop
766 // information, nor make sure the blocks are consistent (for example ending
767 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +0100768 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000769
770 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
771 // of `this` are moved to `other`.
772 // Note that this method does not update the graph, reverse post order, loop
773 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +0000774 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000775 void ReplaceWith(HBasicBlock* other);
776
David Brazdil2d7352b2015-04-20 14:52:42 +0100777 // Merge `other` at the end of `this`. This method updates loops, reverse post
778 // order, links to predecessors, successors, dominators and deletes the block
779 // from the graph. The two blocks must be successive, i.e. `this` the only
780 // predecessor of `other` and vice versa.
781 void MergeWith(HBasicBlock* other);
782
783 // Disconnects `this` from all its predecessors, successors and dominator,
784 // removes it from all loops it is included in and eventually from the graph.
785 // The block must not dominate any other block. Predecessors and successors
786 // are safely updated.
787 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +0000788
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000789 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +0100790 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100791 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +0100792 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +0100793 // Replace instruction `initial` with `replacement` within this block.
794 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
795 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100796 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100797 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +0000798 // RemoveInstruction and RemovePhi delete a given instruction from the respective
799 // instruction list. With 'ensure_safety' set to true, it verifies that the
800 // instruction is not in use and removes it from the use lists of its inputs.
801 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
802 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +0100803 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100804
805 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +0100806 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100807 }
808
Roland Levillain6b879dd2014-09-22 17:13:44 +0100809 bool IsLoopPreHeaderFirstPredecessor() const {
810 DCHECK(IsLoopHeader());
811 DCHECK(!GetPredecessors().IsEmpty());
812 return GetPredecessors().Get(0) == GetLoopInformation()->GetPreHeader();
813 }
814
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100815 HLoopInformation* GetLoopInformation() const {
816 return loop_information_;
817 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000818
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000819 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100820 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000821 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100822 void SetInLoop(HLoopInformation* info) {
823 if (IsLoopHeader()) {
824 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +0100825 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100826 loop_information_ = info;
827 } else if (loop_information_->Contains(*info->GetHeader())) {
828 // Block is currently part of an outer loop. Make it part of this inner loop.
829 // Note that a non loop header having a loop information means this loop information
830 // has already been populated
831 loop_information_ = info;
832 } else {
833 // Block is part of an inner loop. Do not update the loop information.
834 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
835 // at this point, because this method is being called while populating `info`.
836 }
837 }
838
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000839 // Raw update of the loop information.
840 void SetLoopInformation(HLoopInformation* info) {
841 loop_information_ = info;
842 }
843
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100844 bool IsInLoop() const { return loop_information_ != nullptr; }
845
David Brazdilffee3d32015-07-06 11:48:53 +0100846 HTryBoundary* GetTryEntry() const { return try_entry_; }
847 void SetTryEntry(HTryBoundary* try_entry) { try_entry_ = try_entry; }
848 bool IsInTry() const { return try_entry_ != nullptr; }
849
850 // Returns the try entry that this block's successors should have. They will
851 // be in the same try, unless the block ends in a try boundary. In that case,
852 // the appropriate try entry will be returned.
853 HTryBoundary* ComputeTryEntryOfSuccessors() const;
854
David Brazdila4b8c212015-05-07 09:59:30 +0100855 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100856 bool Dominates(HBasicBlock* block) const;
857
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100858 size_t GetLifetimeStart() const { return lifetime_start_; }
859 size_t GetLifetimeEnd() const { return lifetime_end_; }
860
861 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
862 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
863
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100864 uint32_t GetDexPc() const { return dex_pc_; }
865
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000866 bool IsCatchBlock() const { return is_catch_block_; }
867 void SetIsCatchBlock() { is_catch_block_ = true; }
868
David Brazdil8d5b8b22015-03-24 10:51:52 +0000869 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +0000870 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +0100871 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +0000872 bool HasSinglePhi() const;
873
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000874 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000875 HGraph* graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000876 GrowableArray<HBasicBlock*> predecessors_;
877 GrowableArray<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100878 HInstructionList instructions_;
879 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000880 HLoopInformation* loop_information_;
881 HBasicBlock* dominator_;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100882 GrowableArray<HBasicBlock*> dominated_blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000883 int block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100884 // The dex program counter of the first instruction of this block.
885 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100886 size_t lifetime_start_;
887 size_t lifetime_end_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000888 bool is_catch_block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000889
David Brazdilffee3d32015-07-06 11:48:53 +0100890 // If this block is in a try block, `try_entry_` stores one of, possibly
891 // several, TryBoundary instructions entering it.
892 HTryBoundary* try_entry_;
893
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000894 friend class HGraph;
895 friend class HInstruction;
896
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000897 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
898};
899
David Brazdilb2bd1c52015-03-25 11:17:37 +0000900// Iterates over the LoopInformation of all loops which contain 'block'
901// from the innermost to the outermost.
902class HLoopInformationOutwardIterator : public ValueObject {
903 public:
904 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
905 : current_(block.GetLoopInformation()) {}
906
907 bool Done() const { return current_ == nullptr; }
908
909 void Advance() {
910 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +0100911 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +0000912 }
913
914 HLoopInformation* Current() const {
915 DCHECK(!Done());
916 return current_;
917 }
918
919 private:
920 HLoopInformation* current_;
921
922 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
923};
924
Alexandre Ramesef20f712015-06-09 10:29:30 +0100925#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100926 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000927 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000928 M(ArrayGet, Instruction) \
929 M(ArrayLength, Instruction) \
930 M(ArraySet, Instruction) \
David Brazdil66d126e2015-04-03 16:02:44 +0100931 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000932 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +0000933 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000934 M(CheckCast, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100935 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000936 M(Compare, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100937 M(Condition, BinaryOperation) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100938 M(CurrentMethod, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700939 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000940 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +0000941 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000942 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100943 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000944 M(Exit, Instruction) \
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +0100945 M(FakeString, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000946 M(FloatConstant, Constant) \
947 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100948 M(GreaterThan, Condition) \
949 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100950 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000951 M(InstanceFieldGet, Instruction) \
952 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000953 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100954 M(IntConstant, Constant) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +0000955 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000956 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100957 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000958 M(LessThan, Condition) \
959 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000960 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000961 M(LoadException, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100962 M(LoadLocal, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000963 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100964 M(Local, Instruction) \
965 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +0100966 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +0000967 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000968 M(Mul, BinaryOperation) \
969 M(Neg, UnaryOperation) \
970 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100971 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +0100972 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000973 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000974 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000975 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000976 M(Or, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100977 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000978 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100979 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000980 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100981 M(Return, Instruction) \
982 M(ReturnVoid, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000983 M(Shl, BinaryOperation) \
984 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100985 M(StaticFieldGet, Instruction) \
986 M(StaticFieldSet, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100987 M(StoreLocal, Instruction) \
988 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100989 M(SuspendCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000990 M(Temporary, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000991 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +0000992 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +0000993 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000994 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000995 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000996
Alexandre Ramesef20f712015-06-09 10:29:30 +0100997#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
998
999#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
1000
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001001#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1002
Alexandre Ramesef20f712015-06-09 10:29:30 +01001003#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1004
1005#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1006
1007#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1008 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
1009 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1010 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001011 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001012 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1013 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1014
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001015#define FOR_EACH_INSTRUCTION(M) \
1016 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1017 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001018 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001019 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001020 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -07001021
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001022#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001023FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1024#undef FORWARD_DECLARATION
1025
Roland Levillainccc07a92014-09-16 14:48:16 +01001026#define DECLARE_INSTRUCTION(type) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001027 InstructionKind GetKind() const OVERRIDE { return k##type; } \
1028 const char* DebugName() const OVERRIDE { return #type; } \
1029 const H##type* As##type() const OVERRIDE { return this; } \
1030 H##type* As##type() OVERRIDE { return this; } \
1031 bool InstructionTypeEquals(HInstruction* other) const OVERRIDE { \
Roland Levillainccc07a92014-09-16 14:48:16 +01001032 return other->Is##type(); \
1033 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001034 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001035
David Brazdiled596192015-01-23 10:39:45 +00001036template <typename T> class HUseList;
1037
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001038template <typename T>
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001039class HUseListNode : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001040 public:
David Brazdiled596192015-01-23 10:39:45 +00001041 HUseListNode* GetPrevious() const { return prev_; }
1042 HUseListNode* GetNext() const { return next_; }
1043 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001044 size_t GetIndex() const { return index_; }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001045 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001046
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001047 private:
David Brazdiled596192015-01-23 10:39:45 +00001048 HUseListNode(T user, size_t index)
1049 : user_(user), index_(index), prev_(nullptr), next_(nullptr) {}
1050
1051 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001052 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001053 HUseListNode<T>* prev_;
1054 HUseListNode<T>* next_;
1055
1056 friend class HUseList<T>;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001057
1058 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1059};
1060
David Brazdiled596192015-01-23 10:39:45 +00001061template <typename T>
1062class HUseList : public ValueObject {
1063 public:
1064 HUseList() : first_(nullptr) {}
1065
1066 void Clear() {
1067 first_ = nullptr;
1068 }
1069
1070 // Adds a new entry at the beginning of the use list and returns
1071 // the newly created node.
1072 HUseListNode<T>* AddUse(T user, size_t index, ArenaAllocator* arena) {
David Brazdilea55b932015-01-27 17:12:29 +00001073 HUseListNode<T>* new_node = new (arena) HUseListNode<T>(user, index);
David Brazdiled596192015-01-23 10:39:45 +00001074 if (IsEmpty()) {
1075 first_ = new_node;
1076 } else {
1077 first_->prev_ = new_node;
1078 new_node->next_ = first_;
1079 first_ = new_node;
1080 }
1081 return new_node;
1082 }
1083
1084 HUseListNode<T>* GetFirst() const {
1085 return first_;
1086 }
1087
1088 void Remove(HUseListNode<T>* node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001089 DCHECK(node != nullptr);
1090 DCHECK(Contains(node));
1091
David Brazdiled596192015-01-23 10:39:45 +00001092 if (node->prev_ != nullptr) {
1093 node->prev_->next_ = node->next_;
1094 }
1095 if (node->next_ != nullptr) {
1096 node->next_->prev_ = node->prev_;
1097 }
1098 if (node == first_) {
1099 first_ = node->next_;
1100 }
1101 }
1102
David Brazdil1abb4192015-02-17 18:33:36 +00001103 bool Contains(const HUseListNode<T>* node) const {
1104 if (node == nullptr) {
1105 return false;
1106 }
1107 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1108 if (current == node) {
1109 return true;
1110 }
1111 }
1112 return false;
1113 }
1114
David Brazdiled596192015-01-23 10:39:45 +00001115 bool IsEmpty() const {
1116 return first_ == nullptr;
1117 }
1118
1119 bool HasOnlyOneUse() const {
1120 return first_ != nullptr && first_->next_ == nullptr;
1121 }
1122
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001123 size_t SizeSlow() const {
1124 size_t count = 0;
1125 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1126 ++count;
1127 }
1128 return count;
1129 }
1130
David Brazdiled596192015-01-23 10:39:45 +00001131 private:
1132 HUseListNode<T>* first_;
1133};
1134
1135template<typename T>
1136class HUseIterator : public ValueObject {
1137 public:
1138 explicit HUseIterator(const HUseList<T>& uses) : current_(uses.GetFirst()) {}
1139
1140 bool Done() const { return current_ == nullptr; }
1141
1142 void Advance() {
1143 DCHECK(!Done());
1144 current_ = current_->GetNext();
1145 }
1146
1147 HUseListNode<T>* Current() const {
1148 DCHECK(!Done());
1149 return current_;
1150 }
1151
1152 private:
1153 HUseListNode<T>* current_;
1154
1155 friend class HValue;
1156};
1157
David Brazdil1abb4192015-02-17 18:33:36 +00001158// This class is used by HEnvironment and HInstruction classes to record the
1159// instructions they use and pointers to the corresponding HUseListNodes kept
1160// by the used instructions.
1161template <typename T>
1162class HUserRecord : public ValueObject {
1163 public:
1164 HUserRecord() : instruction_(nullptr), use_node_(nullptr) {}
1165 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), use_node_(nullptr) {}
1166
1167 HUserRecord(const HUserRecord<T>& old_record, HUseListNode<T>* use_node)
1168 : instruction_(old_record.instruction_), use_node_(use_node) {
1169 DCHECK(instruction_ != nullptr);
1170 DCHECK(use_node_ != nullptr);
1171 DCHECK(old_record.use_node_ == nullptr);
1172 }
1173
1174 HInstruction* GetInstruction() const { return instruction_; }
1175 HUseListNode<T>* GetUseNode() const { return use_node_; }
1176
1177 private:
1178 // Instruction used by the user.
1179 HInstruction* instruction_;
1180
1181 // Corresponding entry in the use list kept by 'instruction_'.
1182 HUseListNode<T>* use_node_;
1183};
1184
Aart Bik854a02b2015-07-14 16:07:00 -07001185/**
1186 * Side-effects representation for write/read dependences on fields/arrays.
1187 *
1188 * The dependence analysis uses type disambiguation (e.g. a float field write
1189 * cannot modify the value of an integer field read) and the access type (e.g.
1190 * a reference array write cannot modify the value of a reference field read
1191 * [although it may modify the reference fetch prior to reading the field,
1192 * which is represented by its own write/read dependence]). The analysis
1193 * makes conservative points-to assumptions on reference types (e.g. two same
1194 * typed arrays are assumed to be the same, and any reference read depends
1195 * on any reference read without further regard of its type).
1196 *
1197 * The internal representation uses the following 36-bit flags assignments:
1198 *
1199 * |ARRAY-R |FIELD-R |ARRAY-W |FIELD-W |
1200 * +---------+---------+---------+---------+
1201 * |543210987|654321098|765432109|876543210|
1202 * |DFJISCBZL|DFJISCBZL|DFJISCBZL|DFJISCBZL|
1203 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001204class SideEffects : public ValueObject {
1205 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001206 SideEffects() : flags_(0) {}
1207
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001208 static SideEffects None() {
1209 return SideEffects(0);
1210 }
1211
1212 static SideEffects All() {
Aart Bik854a02b2015-07-14 16:07:00 -07001213 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001214 }
1215
Aart Bik34c3ba92015-07-20 14:08:59 -07001216 static SideEffects AllWrites() {
1217 return SideEffects(kAllWrites);
1218 }
1219
1220 static SideEffects AllReads() {
1221 return SideEffects(kAllReads);
1222 }
1223
1224 static SideEffects FieldWriteOfType(Primitive::Type type, bool is_volatile) {
1225 return is_volatile
1226 ? All()
1227 : SideEffects(TypeFlagWithAlias(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001228 }
1229
Aart Bik854a02b2015-07-14 16:07:00 -07001230 static SideEffects ArrayWriteOfType(Primitive::Type type) {
1231 return SideEffects(TypeFlagWithAlias(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001232 }
1233
Aart Bik34c3ba92015-07-20 14:08:59 -07001234 static SideEffects FieldReadOfType(Primitive::Type type, bool is_volatile) {
1235 return is_volatile
1236 ? All()
1237 : SideEffects(TypeFlagWithAlias(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001238 }
1239
1240 static SideEffects ArrayReadOfType(Primitive::Type type) {
1241 return SideEffects(TypeFlagWithAlias(type, kArrayReadOffset));
1242 }
1243
1244 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001245 SideEffects Union(SideEffects other) const {
1246 return SideEffects(flags_ | other.flags_);
1247 }
1248
Aart Bik854a02b2015-07-14 16:07:00 -07001249 // Returns true if something is written.
1250 bool DoesAnyWrite() const {
1251 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001252 }
1253
Aart Bik854a02b2015-07-14 16:07:00 -07001254 // Returns true if something is read.
1255 bool DoesAnyRead() const {
1256 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001257 }
1258
Aart Bik854a02b2015-07-14 16:07:00 -07001259 // Returns true if nothing is written or read.
1260 bool DoesNothing() const {
1261 return flags_ == 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001262 }
1263
Aart Bik854a02b2015-07-14 16:07:00 -07001264 // Returns true if potentially everything is written and read
1265 // (every type and every kind of access).
1266 bool DoesAll() const {
1267 return flags_ == (kAllWrites | kAllReads);
1268 }
1269
1270 // Returns true if this may read something written by other.
1271 bool MayDependOn(SideEffects other) const {
1272 const uint64_t reads = (flags_ & kAllReads) >> kFieldReadOffset;
1273 return (other.flags_ & reads);
1274 }
1275
1276 // Returns string representation of flags (for debugging only).
1277 // Format: |DFJISCBZL|DFJISCBZL|DFJISCBZL|DFJISCBZL|
1278 std::string ToString() const {
1279 static const char *kDebug = "LZBCSIJFD";
1280 std::string flags = "|";
1281 for (int s = 35; s >= 0; s--) {
1282 const int t = s % kBits;
1283 if ((flags_ >> s) & 1)
1284 flags += kDebug[t];
1285 if (t == 0)
1286 flags += "|";
1287 }
1288 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001289 }
1290
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001291 private:
Aart Bik854a02b2015-07-14 16:07:00 -07001292 static constexpr int kBits = 9;
1293 static constexpr int kFieldWriteOffset = 0 * kBits;
1294 static constexpr int kArrayWriteOffset = 1 * kBits;
1295 static constexpr int kFieldReadOffset = 2 * kBits;
1296 static constexpr int kArrayReadOffset = 3 * kBits;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001297
Aart Bik854a02b2015-07-14 16:07:00 -07001298 static constexpr uint64_t kAllWrites = 0x0003ffff;
1299 static constexpr uint64_t kAllReads = kAllWrites << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001300
Aart Bik854a02b2015-07-14 16:07:00 -07001301 // Work around the fact that HIR aliases I/F and J/D.
1302 // TODO: remove this interceptor once HIR types are clean
1303 static uint64_t TypeFlagWithAlias(Primitive::Type type, int offset) {
1304 switch (type) {
1305 case Primitive::kPrimInt:
1306 case Primitive::kPrimFloat:
1307 return TypeFlag(Primitive::kPrimInt, offset) |
1308 TypeFlag(Primitive::kPrimFloat, offset);
1309 case Primitive::kPrimLong:
1310 case Primitive::kPrimDouble:
1311 return TypeFlag(Primitive::kPrimLong, offset) |
1312 TypeFlag(Primitive::kPrimDouble, offset);
1313 default:
1314 return TypeFlag(type, offset);
1315 }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001316 }
1317
Aart Bik854a02b2015-07-14 16:07:00 -07001318 // Translates type to bit flag.
1319 static uint64_t TypeFlag(Primitive::Type type, int offset) {
1320 CHECK_NE(type, Primitive::kPrimVoid);
1321 const uint64_t one = 1;
1322 const int shift = type; // 0-based consecutive enum
1323 DCHECK_LE(kFieldWriteOffset, shift);
1324 DCHECK_LT(shift, kArrayWriteOffset);
1325 return one << (type + offset);
1326 }
1327
1328 // Private constructor on direct flags value.
1329 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1330
1331 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001332};
1333
David Brazdiled596192015-01-23 10:39:45 +00001334// A HEnvironment object contains the values of virtual registers at a given location.
1335class HEnvironment : public ArenaObject<kArenaAllocMisc> {
1336 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001337 HEnvironment(ArenaAllocator* arena,
1338 size_t number_of_vregs,
1339 const DexFile& dex_file,
1340 uint32_t method_idx,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001341 uint32_t dex_pc,
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001342 InvokeType invoke_type,
1343 HInstruction* holder)
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001344 : vregs_(arena, number_of_vregs),
1345 locations_(arena, number_of_vregs),
1346 parent_(nullptr),
1347 dex_file_(dex_file),
1348 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001349 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001350 invoke_type_(invoke_type),
1351 holder_(holder) {
David Brazdiled596192015-01-23 10:39:45 +00001352 vregs_.SetSize(number_of_vregs);
1353 for (size_t i = 0; i < number_of_vregs; i++) {
David Brazdil1abb4192015-02-17 18:33:36 +00001354 vregs_.Put(i, HUserRecord<HEnvironment*>());
David Brazdiled596192015-01-23 10:39:45 +00001355 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001356
1357 locations_.SetSize(number_of_vregs);
1358 for (size_t i = 0; i < number_of_vregs; ++i) {
1359 locations_.Put(i, Location());
1360 }
1361 }
1362
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001363 HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001364 : HEnvironment(arena,
1365 to_copy.Size(),
1366 to_copy.GetDexFile(),
1367 to_copy.GetMethodIdx(),
1368 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001369 to_copy.GetInvokeType(),
1370 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001371
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001372 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001373 if (parent_ != nullptr) {
1374 parent_->SetAndCopyParentChain(allocator, parent);
1375 } else {
1376 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1377 parent_->CopyFrom(parent);
1378 if (parent->GetParent() != nullptr) {
1379 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1380 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001381 }
David Brazdiled596192015-01-23 10:39:45 +00001382 }
1383
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001384 void CopyFrom(const GrowableArray<HInstruction*>& locals);
1385 void CopyFrom(HEnvironment* environment);
1386
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001387 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1388 // input to the loop phi instead. This is for inserting instructions that
1389 // require an environment (like HDeoptimization) in the loop pre-header.
1390 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001391
1392 void SetRawEnvAt(size_t index, HInstruction* instruction) {
David Brazdil1abb4192015-02-17 18:33:36 +00001393 vregs_.Put(index, HUserRecord<HEnvironment*>(instruction));
David Brazdiled596192015-01-23 10:39:45 +00001394 }
1395
1396 HInstruction* GetInstructionAt(size_t index) const {
David Brazdil1abb4192015-02-17 18:33:36 +00001397 return vregs_.Get(index).GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001398 }
1399
David Brazdil1abb4192015-02-17 18:33:36 +00001400 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001401
1402 size_t Size() const { return vregs_.Size(); }
1403
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001404 HEnvironment* GetParent() const { return parent_; }
1405
1406 void SetLocationAt(size_t index, Location location) {
1407 locations_.Put(index, location);
1408 }
1409
1410 Location GetLocationAt(size_t index) const {
1411 return locations_.Get(index);
1412 }
1413
1414 uint32_t GetDexPc() const {
1415 return dex_pc_;
1416 }
1417
1418 uint32_t GetMethodIdx() const {
1419 return method_idx_;
1420 }
1421
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001422 InvokeType GetInvokeType() const {
1423 return invoke_type_;
1424 }
1425
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001426 const DexFile& GetDexFile() const {
1427 return dex_file_;
1428 }
1429
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001430 HInstruction* GetHolder() const {
1431 return holder_;
1432 }
1433
David Brazdiled596192015-01-23 10:39:45 +00001434 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001435 // Record instructions' use entries of this environment for constant-time removal.
1436 // It should only be called by HInstruction when a new environment use is added.
1437 void RecordEnvUse(HUseListNode<HEnvironment*>* env_use) {
1438 DCHECK(env_use->GetUser() == this);
1439 size_t index = env_use->GetIndex();
1440 vregs_.Put(index, HUserRecord<HEnvironment*>(vregs_.Get(index), env_use));
1441 }
David Brazdiled596192015-01-23 10:39:45 +00001442
David Brazdil1abb4192015-02-17 18:33:36 +00001443 GrowableArray<HUserRecord<HEnvironment*> > vregs_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001444 GrowableArray<Location> locations_;
1445 HEnvironment* parent_;
1446 const DexFile& dex_file_;
1447 const uint32_t method_idx_;
1448 const uint32_t dex_pc_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001449 const InvokeType invoke_type_;
David Brazdiled596192015-01-23 10:39:45 +00001450
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001451 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001452 HInstruction* const holder_;
1453
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001454 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001455
1456 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1457};
1458
Calin Juravleacf735c2015-02-12 15:25:22 +00001459class ReferenceTypeInfo : ValueObject {
1460 public:
Calin Juravleb1498f62015-02-16 13:13:29 +00001461 typedef Handle<mirror::Class> TypeHandle;
1462
Calin Juravle7733bd62015-07-22 17:14:50 +00001463 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact)
Mathieu Chartier90443472015-07-16 20:32:27 -07001464 SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle7733bd62015-07-22 17:14:50 +00001465 if (type_handle->IsObjectClass()) {
1466 // Override the type handle to be consistent with the case when we get to
1467 // Top but don't have the Object class available. It avoids having to guess
1468 // what value the type_handle has when it's Top.
1469 return ReferenceTypeInfo(TypeHandle(), is_exact, true);
1470 } else {
1471 return ReferenceTypeInfo(type_handle, is_exact, false);
1472 }
Calin Juravleb1498f62015-02-16 13:13:29 +00001473 }
1474
Calin Juravle7733bd62015-07-22 17:14:50 +00001475 static ReferenceTypeInfo CreateTop(bool is_exact) {
1476 return ReferenceTypeInfo(TypeHandle(), is_exact, true);
Calin Juravleacf735c2015-02-12 15:25:22 +00001477 }
1478
1479 bool IsExact() const { return is_exact_; }
Calin Juravle7733bd62015-07-22 17:14:50 +00001480 bool IsTop() const { return is_top_; }
Mathieu Chartier90443472015-07-16 20:32:27 -07001481 bool IsInterface() const SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle7733bd62015-07-22 17:14:50 +00001482 return !IsTop() && GetTypeHandle()->IsInterface();
Guillaume Sanchez222862c2015-06-09 18:33:02 +01001483 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001484
1485 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
1486
Mathieu Chartier90443472015-07-16 20:32:27 -07001487 bool IsSupertypeOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle7733bd62015-07-22 17:14:50 +00001488 if (IsTop()) {
1489 // Top (equivalent for java.lang.Object) is supertype of anything.
1490 return true;
1491 }
1492 if (rti.IsTop()) {
1493 // If we get here `this` is not Top() so it can't be a supertype.
1494 return false;
1495 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001496 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
1497 }
1498
1499 // Returns true if the type information provide the same amount of details.
1500 // Note that it does not mean that the instructions have the same actual type
Calin Juravle7733bd62015-07-22 17:14:50 +00001501 // (e.g. tops are equal but they can be the result of a merge).
Mathieu Chartier90443472015-07-16 20:32:27 -07001502 bool IsEqual(ReferenceTypeInfo rti) SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle7733bd62015-07-22 17:14:50 +00001503 if (IsExact() != rti.IsExact()) {
Calin Juravleacf735c2015-02-12 15:25:22 +00001504 return false;
1505 }
Calin Juravle7733bd62015-07-22 17:14:50 +00001506 if (IsTop() && rti.IsTop()) {
1507 // `Top` means java.lang.Object, so the types are equivalent.
1508 return true;
1509 }
1510 if (IsTop() || rti.IsTop()) {
1511 // If only one is top or object than they are not equivalent.
1512 // NB: We need this extra check because the type_handle of `Top` is invalid
1513 // and we cannot inspect its reference.
1514 return false;
1515 }
1516
1517 // Finally check the types.
1518 return GetTypeHandle().Get() == rti.GetTypeHandle().Get();
Calin Juravleacf735c2015-02-12 15:25:22 +00001519 }
1520
1521 private:
Calin Juravle7733bd62015-07-22 17:14:50 +00001522 ReferenceTypeInfo() : ReferenceTypeInfo(TypeHandle(), false, true) {}
1523 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact, bool is_top)
1524 : type_handle_(type_handle), is_exact_(is_exact), is_top_(is_top) {}
Calin Juravleb1498f62015-02-16 13:13:29 +00001525
Calin Juravleacf735c2015-02-12 15:25:22 +00001526 // The class of the object.
Calin Juravleb1498f62015-02-16 13:13:29 +00001527 TypeHandle type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001528 // Whether or not the type is exact or a superclass of the actual type.
Calin Juravleb1498f62015-02-16 13:13:29 +00001529 // Whether or not we have any information about this type.
Calin Juravleacf735c2015-02-12 15:25:22 +00001530 bool is_exact_;
Calin Juravle7733bd62015-07-22 17:14:50 +00001531 // A true value here means that the object type should be java.lang.Object.
1532 // We don't have access to the corresponding mirror object every time so this
1533 // flag acts as a substitute. When true, the TypeHandle refers to a null
1534 // pointer and should not be used.
1535 bool is_top_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001536};
1537
1538std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
1539
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001540class HInstruction : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001541 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001542 explicit HInstruction(SideEffects side_effects)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001543 : previous_(nullptr),
1544 next_(nullptr),
1545 block_(nullptr),
1546 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001547 ssa_index_(-1),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001548 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001549 locations_(nullptr),
1550 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001551 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001552 side_effects_(side_effects),
Calin Juravle7733bd62015-07-22 17:14:50 +00001553 reference_type_info_(ReferenceTypeInfo::CreateTop(/* is_exact */ false)) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001554
Dave Allison20dfc792014-06-16 20:44:29 -07001555 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001556
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001557#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001558 enum InstructionKind {
1559 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1560 };
1561#undef DECLARE_KIND
1562
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001563 HInstruction* GetNext() const { return next_; }
1564 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001565
Calin Juravle77520bc2015-01-12 18:45:46 +00001566 HInstruction* GetNextDisregardingMoves() const;
1567 HInstruction* GetPreviousDisregardingMoves() const;
1568
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001569 HBasicBlock* GetBlock() const { return block_; }
1570 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001571 bool IsInBlock() const { return block_ != nullptr; }
1572 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +01001573 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001574
Roland Levillain6b879dd2014-09-22 17:13:44 +01001575 virtual size_t InputCount() const = 0;
David Brazdil1abb4192015-02-17 18:33:36 +00001576 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001577
1578 virtual void Accept(HGraphVisitor* visitor) = 0;
1579 virtual const char* DebugName() const = 0;
1580
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001581 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
David Brazdil1abb4192015-02-17 18:33:36 +00001582 void SetRawInputAt(size_t index, HInstruction* input) {
1583 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1584 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001585
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001586 virtual bool NeedsEnvironment() const { return false; }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001587 virtual uint32_t GetDexPc() const {
1588 LOG(FATAL) << "GetDexPc() cannot be called on an instruction that"
1589 " does not need an environment";
1590 UNREACHABLE();
1591 }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001592 virtual bool IsControlFlow() const { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01001593 virtual bool CanThrow() const { return false; }
Aart Bik854a02b2015-07-14 16:07:00 -07001594
1595 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001596
Calin Juravle10e244f2015-01-26 18:54:32 +00001597 // Does not apply for all instructions, but having this at top level greatly
1598 // simplifies the null check elimination.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001599 virtual bool CanBeNull() const {
1600 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1601 return true;
1602 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001603
Calin Juravle641547a2015-04-21 22:08:51 +01001604 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj) const {
1605 UNUSED(obj);
1606 return false;
1607 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001608
Calin Juravle7733bd62015-07-22 17:14:50 +00001609 void SetReferenceTypeInfo(ReferenceTypeInfo reference_type_info) {
1610 DCHECK_EQ(GetType(), Primitive::kPrimNot);
1611 reference_type_info_ = reference_type_info;
1612 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001613
Calin Juravle61d544b2015-02-23 16:46:57 +00001614 ReferenceTypeInfo GetReferenceTypeInfo() const {
1615 DCHECK_EQ(GetType(), Primitive::kPrimNot);
1616 return reference_type_info_;
1617 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001618
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001619 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001620 DCHECK(user != nullptr);
1621 HUseListNode<HInstruction*>* use =
1622 uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1623 user->SetRawInputRecordAt(index, HUserRecord<HInstruction*>(user->InputRecordAt(index), use));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001624 }
1625
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001626 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001627 DCHECK(user != nullptr);
David Brazdiled596192015-01-23 10:39:45 +00001628 HUseListNode<HEnvironment*>* env_use =
1629 env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1630 user->RecordEnvUse(env_use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001631 }
1632
David Brazdil1abb4192015-02-17 18:33:36 +00001633 void RemoveAsUserOfInput(size_t input) {
1634 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
1635 input_use.GetInstruction()->uses_.Remove(input_use.GetUseNode());
1636 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001637
David Brazdil1abb4192015-02-17 18:33:36 +00001638 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1639 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001640
David Brazdiled596192015-01-23 10:39:45 +00001641 bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); }
1642 bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); }
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001643 bool HasNonEnvironmentUses() const { return !uses_.IsEmpty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001644 bool HasOnlyOneNonEnvironmentUse() const {
1645 return !HasEnvironmentUses() && GetUses().HasOnlyOneUse();
1646 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001647
Roland Levillain6c82d402014-10-13 16:10:27 +01001648 // Does this instruction strictly dominate `other_instruction`?
1649 // Returns false if this instruction and `other_instruction` are the same.
1650 // Aborts if this instruction and `other_instruction` are both phis.
1651 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001652
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001653 int GetId() const { return id_; }
1654 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001655
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001656 int GetSsaIndex() const { return ssa_index_; }
1657 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1658 bool HasSsaIndex() const { return ssa_index_ != -1; }
1659
1660 bool HasEnvironment() const { return environment_ != nullptr; }
1661 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001662 // Set the `environment_` field. Raw because this method does not
1663 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001664 void SetRawEnvironment(HEnvironment* environment) {
1665 DCHECK(environment_ == nullptr);
1666 DCHECK_EQ(environment->GetHolder(), this);
1667 environment_ = environment;
1668 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001669
1670 // Set the environment of this instruction, copying it from `environment`. While
1671 // copying, the uses lists are being updated.
1672 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001673 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001674 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001675 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001676 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001677 if (environment->GetParent() != nullptr) {
1678 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1679 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001680 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001681
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001682 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
1683 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001684 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001685 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001686 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001687 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001688 if (environment->GetParent() != nullptr) {
1689 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1690 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001691 }
1692
Nicolas Geoffray39468442014-09-02 15:17:15 +01001693 // Returns the number of entries in the environment. Typically, that is the
1694 // number of dex registers in a method. It could be more in case of inlining.
1695 size_t EnvironmentSize() const;
1696
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001697 LocationSummary* GetLocations() const { return locations_; }
1698 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001699
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001700 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001701 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001702
Alexandre Rames188d4312015-04-09 18:30:21 +01001703 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
1704 // uses of this instruction by `other` are *not* updated.
1705 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
1706 ReplaceWith(other);
1707 other->ReplaceInput(this, use_index);
1708 }
1709
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001710 // Move `this` instruction before `cursor`.
1711 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001712
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001713#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01001714 bool Is##type() const { return (As##type() != nullptr); } \
1715 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001716 virtual H##type* As##type() { return nullptr; }
1717
1718 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
1719#undef INSTRUCTION_TYPE_CHECK
1720
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001721 // Returns whether the instruction can be moved within the graph.
1722 virtual bool CanBeMoved() const { return false; }
1723
1724 // Returns whether the two instructions are of the same kind.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001725 virtual bool InstructionTypeEquals(HInstruction* other) const {
1726 UNUSED(other);
1727 return false;
1728 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001729
1730 // Returns whether any data encoded in the two instructions is equal.
1731 // This method does not look at the inputs. Both instructions must be
1732 // of the same type, otherwise the method has undefined behavior.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001733 virtual bool InstructionDataEquals(HInstruction* other) const {
1734 UNUSED(other);
1735 return false;
1736 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001737
1738 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00001739 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001740 // 2) Their inputs are identical.
1741 bool Equals(HInstruction* other) const;
1742
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001743 virtual InstructionKind GetKind() const = 0;
1744
1745 virtual size_t ComputeHashCode() const {
1746 size_t result = GetKind();
1747 for (size_t i = 0, e = InputCount(); i < e; ++i) {
1748 result = (result * 31) + InputAt(i)->GetId();
1749 }
1750 return result;
1751 }
1752
1753 SideEffects GetSideEffects() const { return side_effects_; }
1754
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001755 size_t GetLifetimePosition() const { return lifetime_position_; }
1756 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
1757 LiveInterval* GetLiveInterval() const { return live_interval_; }
1758 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
1759 bool HasLiveInterval() const { return live_interval_ != nullptr; }
1760
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001761 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
1762
1763 // Returns whether the code generation of the instruction will require to have access
1764 // to the current method. Such instructions are:
1765 // (1): Instructions that require an environment, as calling the runtime requires
1766 // to walk the stack and have the current method stored at a specific stack address.
1767 // (2): Object literals like classes and strings, that are loaded from the dex cache
1768 // fields of the current method.
1769 bool NeedsCurrentMethod() const {
1770 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
1771 }
1772
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001773 virtual bool NeedsDexCache() const { return false; }
1774
Mark Mendellc4701932015-04-10 13:18:51 -04001775 // Does this instruction have any use in an environment before
1776 // control flow hits 'other'?
1777 bool HasAnyEnvironmentUseBefore(HInstruction* other);
1778
1779 // Remove all references to environment uses of this instruction.
1780 // The caller must ensure that this is safe to do.
1781 void RemoveEnvironmentUsers();
1782
David Brazdil1abb4192015-02-17 18:33:36 +00001783 protected:
1784 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
1785 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
1786
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001787 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001788 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); }
1789
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001790 HInstruction* previous_;
1791 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001792 HBasicBlock* block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001793
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001794 // An instruction gets an id when it is added to the graph.
1795 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01001796 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001797 int id_;
1798
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001799 // When doing liveness analysis, instructions that have uses get an SSA index.
1800 int ssa_index_;
1801
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001802 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00001803 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001804
1805 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00001806 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001807
Nicolas Geoffray39468442014-09-02 15:17:15 +01001808 // The environment associated with this instruction. Not null if the instruction
1809 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001810 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001811
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001812 // Set by the code generator.
1813 LocationSummary* locations_;
1814
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001815 // Set by the liveness analysis.
1816 LiveInterval* live_interval_;
1817
1818 // Set by the liveness analysis, this is the position in a linear
1819 // order of blocks where this instruction's live interval start.
1820 size_t lifetime_position_;
1821
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001822 const SideEffects side_effects_;
1823
Calin Juravleacf735c2015-02-12 15:25:22 +00001824 // TODO: for primitive types this should be marked as invalid.
1825 ReferenceTypeInfo reference_type_info_;
1826
David Brazdil1abb4192015-02-17 18:33:36 +00001827 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001828 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00001829 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001830 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001831 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001832
1833 DISALLOW_COPY_AND_ASSIGN(HInstruction);
1834};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001835std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001836
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001837class HInputIterator : public ValueObject {
1838 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001839 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001840
1841 bool Done() const { return index_ == instruction_->InputCount(); }
1842 HInstruction* Current() const { return instruction_->InputAt(index_); }
1843 void Advance() { index_++; }
1844
1845 private:
1846 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001847 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001848
1849 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
1850};
1851
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001852class HInstructionIterator : public ValueObject {
1853 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001854 explicit HInstructionIterator(const HInstructionList& instructions)
1855 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001856 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001857 }
1858
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001859 bool Done() const { return instruction_ == nullptr; }
1860 HInstruction* Current() const { return instruction_; }
1861 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001862 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001863 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001864 }
1865
1866 private:
1867 HInstruction* instruction_;
1868 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001869
1870 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001871};
1872
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001873class HBackwardInstructionIterator : public ValueObject {
1874 public:
1875 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
1876 : instruction_(instructions.last_instruction_) {
1877 next_ = Done() ? nullptr : instruction_->GetPrevious();
1878 }
1879
1880 bool Done() const { return instruction_ == nullptr; }
1881 HInstruction* Current() const { return instruction_; }
1882 void Advance() {
1883 instruction_ = next_;
1884 next_ = Done() ? nullptr : instruction_->GetPrevious();
1885 }
1886
1887 private:
1888 HInstruction* instruction_;
1889 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001890
1891 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001892};
1893
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001894// An embedded container with N elements of type T. Used (with partial
1895// specialization for N=0) because embedded arrays cannot have size 0.
1896template<typename T, intptr_t N>
1897class EmbeddedArray {
1898 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001899 EmbeddedArray() : elements_() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001900
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001901 intptr_t GetLength() const { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001902
1903 const T& operator[](intptr_t i) const {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001904 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001905 return elements_[i];
1906 }
1907
1908 T& operator[](intptr_t i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001909 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001910 return elements_[i];
1911 }
1912
1913 const T& At(intptr_t i) const {
1914 return (*this)[i];
1915 }
1916
1917 void SetAt(intptr_t i, const T& val) {
1918 (*this)[i] = val;
1919 }
1920
1921 private:
1922 T elements_[N];
1923};
1924
1925template<typename T>
1926class EmbeddedArray<T, 0> {
1927 public:
1928 intptr_t length() const { return 0; }
1929 const T& operator[](intptr_t i) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001930 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001931 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001932 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001933 }
1934 T& operator[](intptr_t i) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001935 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001936 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001937 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001938 }
1939};
1940
1941template<intptr_t N>
1942class HTemplateInstruction: public HInstruction {
1943 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001944 HTemplateInstruction<N>(SideEffects side_effects)
1945 : HInstruction(side_effects), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07001946 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001947
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001948 size_t InputCount() const OVERRIDE { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001949
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001950 protected:
David Brazdil1abb4192015-02-17 18:33:36 +00001951 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_[i]; }
1952
1953 void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE {
1954 inputs_[i] = input;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001955 }
1956
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001957 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001958 EmbeddedArray<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001959
1960 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001961};
1962
Dave Allison20dfc792014-06-16 20:44:29 -07001963template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001964class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07001965 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001966 HExpression<N>(Primitive::Type type, SideEffects side_effects)
1967 : HTemplateInstruction<N>(side_effects), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07001968 virtual ~HExpression() {}
1969
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001970 Primitive::Type GetType() const OVERRIDE { return type_; }
Dave Allison20dfc792014-06-16 20:44:29 -07001971
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001972 protected:
1973 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07001974};
1975
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001976// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
1977// instruction that branches to the exit block.
1978class HReturnVoid : public HTemplateInstruction<0> {
1979 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001980 HReturnVoid() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001981
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001982 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001983
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001984 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001985
1986 private:
1987 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
1988};
1989
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001990// Represents dex's RETURN opcodes. A HReturn is a control flow
1991// instruction that branches to the exit block.
1992class HReturn : public HTemplateInstruction<1> {
1993 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001994 explicit HReturn(HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001995 SetRawInputAt(0, value);
1996 }
1997
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001998 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001999
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002000 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002001
2002 private:
2003 DISALLOW_COPY_AND_ASSIGN(HReturn);
2004};
2005
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002006// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002007// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002008// exit block.
2009class HExit : public HTemplateInstruction<0> {
2010 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002011 HExit() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002012
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002013 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002014
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002015 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002016
2017 private:
2018 DISALLOW_COPY_AND_ASSIGN(HExit);
2019};
2020
2021// Jumps from one block to another.
2022class HGoto : public HTemplateInstruction<0> {
2023 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002024 HGoto() : HTemplateInstruction(SideEffects::None()) {}
2025
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002026 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002027
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002028 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002029 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002030 }
2031
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002032 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002033
2034 private:
2035 DISALLOW_COPY_AND_ASSIGN(HGoto);
2036};
2037
Dave Allison20dfc792014-06-16 20:44:29 -07002038
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002039// Conditional branch. A block ending with an HIf instruction must have
2040// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002041class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002042 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002043 explicit HIf(HInstruction* input) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002044 SetRawInputAt(0, input);
2045 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002046
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002047 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002048
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002049 HBasicBlock* IfTrueSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002050 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002051 }
2052
2053 HBasicBlock* IfFalseSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002054 return GetBlock()->GetSuccessors().Get(1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002055 }
2056
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002057 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002058
2059 private:
2060 DISALLOW_COPY_AND_ASSIGN(HIf);
2061};
2062
David Brazdilfc6a86a2015-06-26 10:33:45 +00002063
2064// Abstract instruction which marks the beginning and/or end of a try block and
2065// links it to the respective exception handlers. Behaves the same as a Goto in
2066// non-exceptional control flow.
2067// Normal-flow successor is stored at index zero, exception handlers under
2068// higher indices in no particular order.
2069class HTryBoundary : public HTemplateInstruction<0> {
2070 public:
David Brazdil56e1acc2015-06-30 15:41:36 +01002071 enum BoundaryKind {
2072 kEntry,
2073 kExit,
2074 };
2075
2076 explicit HTryBoundary(BoundaryKind kind)
2077 : HTemplateInstruction(SideEffects::None()), kind_(kind) {}
David Brazdilfc6a86a2015-06-26 10:33:45 +00002078
2079 bool IsControlFlow() const OVERRIDE { return true; }
2080
2081 // Returns the block's non-exceptional successor (index zero).
2082 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors().Get(0); }
2083
2084 // Returns whether `handler` is among its exception handlers (non-zero index
2085 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002086 bool HasExceptionHandler(const HBasicBlock& handler) const {
2087 DCHECK(handler.IsCatchBlock());
2088 return GetBlock()->GetSuccessors().Contains(
2089 const_cast<HBasicBlock*>(&handler), /* start_from */ 1);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002090 }
2091
2092 // If not present already, adds `handler` to its block's list of exception
2093 // handlers.
2094 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01002095 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002096 GetBlock()->AddSuccessor(handler);
2097 }
2098 }
2099
David Brazdil56e1acc2015-06-30 15:41:36 +01002100 bool IsEntry() const { return kind_ == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002101
David Brazdilffee3d32015-07-06 11:48:53 +01002102 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
2103
David Brazdilfc6a86a2015-06-26 10:33:45 +00002104 DECLARE_INSTRUCTION(TryBoundary);
2105
2106 private:
David Brazdil56e1acc2015-06-30 15:41:36 +01002107 const BoundaryKind kind_;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002108
2109 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2110};
2111
David Brazdilffee3d32015-07-06 11:48:53 +01002112// Iterator over exception handlers of a given HTryBoundary, i.e. over
2113// exceptional successors of its basic block.
2114class HExceptionHandlerIterator : public ValueObject {
2115 public:
2116 explicit HExceptionHandlerIterator(const HTryBoundary& try_boundary)
2117 : block_(*try_boundary.GetBlock()), index_(block_.NumberOfNormalSuccessors()) {}
2118
2119 bool Done() const { return index_ == block_.GetSuccessors().Size(); }
2120 HBasicBlock* Current() const { return block_.GetSuccessors().Get(index_); }
2121 size_t CurrentSuccessorIndex() const { return index_; }
2122 void Advance() { ++index_; }
2123
2124 private:
2125 const HBasicBlock& block_;
2126 size_t index_;
2127
2128 DISALLOW_COPY_AND_ASSIGN(HExceptionHandlerIterator);
2129};
David Brazdilfc6a86a2015-06-26 10:33:45 +00002130
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002131// Deoptimize to interpreter, upon checking a condition.
2132class HDeoptimize : public HTemplateInstruction<1> {
2133 public:
2134 HDeoptimize(HInstruction* cond, uint32_t dex_pc)
2135 : HTemplateInstruction(SideEffects::None()),
2136 dex_pc_(dex_pc) {
2137 SetRawInputAt(0, cond);
2138 }
2139
2140 bool NeedsEnvironment() const OVERRIDE { return true; }
2141 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002142 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002143
2144 DECLARE_INSTRUCTION(Deoptimize);
2145
2146 private:
2147 uint32_t dex_pc_;
2148
2149 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
2150};
2151
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002152// Represents the ArtMethod that was passed as a first argument to
2153// the method. It is used by instructions that depend on it, like
2154// instructions that work with the dex cache.
2155class HCurrentMethod : public HExpression<0> {
2156 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -07002157 explicit HCurrentMethod(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002158
2159 DECLARE_INSTRUCTION(CurrentMethod);
2160
2161 private:
2162 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
2163};
2164
Roland Levillain88cb1752014-10-20 16:36:47 +01002165class HUnaryOperation : public HExpression<1> {
2166 public:
2167 HUnaryOperation(Primitive::Type result_type, HInstruction* input)
2168 : HExpression(result_type, SideEffects::None()) {
2169 SetRawInputAt(0, input);
2170 }
2171
2172 HInstruction* GetInput() const { return InputAt(0); }
2173 Primitive::Type GetResultType() const { return GetType(); }
2174
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002175 bool CanBeMoved() const OVERRIDE { return true; }
2176 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002177 UNUSED(other);
2178 return true;
2179 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002180
Roland Levillain9240d6a2014-10-20 16:47:04 +01002181 // Try to statically evaluate `operation` and return a HConstant
2182 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002183 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002184 HConstant* TryStaticEvaluation() const;
2185
2186 // Apply this operation to `x`.
2187 virtual int32_t Evaluate(int32_t x) const = 0;
2188 virtual int64_t Evaluate(int64_t x) const = 0;
2189
Roland Levillain88cb1752014-10-20 16:36:47 +01002190 DECLARE_INSTRUCTION(UnaryOperation);
2191
2192 private:
2193 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
2194};
2195
Dave Allison20dfc792014-06-16 20:44:29 -07002196class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002197 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002198 HBinaryOperation(Primitive::Type result_type,
2199 HInstruction* left,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002200 HInstruction* right) : HExpression(result_type, SideEffects::None()) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002201 SetRawInputAt(0, left);
2202 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002203 }
2204
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002205 HInstruction* GetLeft() const { return InputAt(0); }
2206 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07002207 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002208
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002209 virtual bool IsCommutative() const { return false; }
2210
2211 // Put constant on the right.
2212 // Returns whether order is changed.
2213 bool OrderInputsWithConstantOnTheRight() {
2214 HInstruction* left = InputAt(0);
2215 HInstruction* right = InputAt(1);
2216 if (left->IsConstant() && !right->IsConstant()) {
2217 ReplaceInput(right, 0);
2218 ReplaceInput(left, 1);
2219 return true;
2220 }
2221 return false;
2222 }
2223
2224 // Order inputs by instruction id, but favor constant on the right side.
2225 // This helps GVN for commutative ops.
2226 void OrderInputs() {
2227 DCHECK(IsCommutative());
2228 HInstruction* left = InputAt(0);
2229 HInstruction* right = InputAt(1);
2230 if (left == right || (!left->IsConstant() && right->IsConstant())) {
2231 return;
2232 }
2233 if (OrderInputsWithConstantOnTheRight()) {
2234 return;
2235 }
2236 // Order according to instruction id.
2237 if (left->GetId() > right->GetId()) {
2238 ReplaceInput(right, 0);
2239 ReplaceInput(left, 1);
2240 }
2241 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002242
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002243 bool CanBeMoved() const OVERRIDE { return true; }
2244 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002245 UNUSED(other);
2246 return true;
2247 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002248
Roland Levillain9240d6a2014-10-20 16:47:04 +01002249 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01002250 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002251 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002252 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01002253
2254 // Apply this operation to `x` and `y`.
2255 virtual int32_t Evaluate(int32_t x, int32_t y) const = 0;
2256 virtual int64_t Evaluate(int64_t x, int64_t y) const = 0;
2257
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002258 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002259 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002260 HConstant* GetConstantRight() const;
2261
2262 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002263 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002264 HInstruction* GetLeastConstantLeft() const;
2265
Roland Levillainccc07a92014-09-16 14:48:16 +01002266 DECLARE_INSTRUCTION(BinaryOperation);
2267
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002268 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002269 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
2270};
2271
Mark Mendellc4701932015-04-10 13:18:51 -04002272// The comparison bias applies for floating point operations and indicates how NaN
2273// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01002274enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04002275 kNoBias, // bias is not applicable (i.e. for long operation)
2276 kGtBias, // return 1 for NaN comparisons
2277 kLtBias, // return -1 for NaN comparisons
2278};
2279
Dave Allison20dfc792014-06-16 20:44:29 -07002280class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002281 public:
Dave Allison20dfc792014-06-16 20:44:29 -07002282 HCondition(HInstruction* first, HInstruction* second)
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002283 : HBinaryOperation(Primitive::kPrimBoolean, first, second),
Mark Mendellc4701932015-04-10 13:18:51 -04002284 needs_materialization_(true),
Roland Levillain4fa13f62015-07-06 18:11:54 +01002285 bias_(ComparisonBias::kNoBias) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002286
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002287 bool NeedsMaterialization() const { return needs_materialization_; }
2288 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002289
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002290 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002291 // `instruction`, and disregard moves in between.
2292 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002293
Dave Allison20dfc792014-06-16 20:44:29 -07002294 DECLARE_INSTRUCTION(Condition);
2295
2296 virtual IfCondition GetCondition() const = 0;
2297
Mark Mendellc4701932015-04-10 13:18:51 -04002298 virtual IfCondition GetOppositeCondition() const = 0;
2299
Roland Levillain4fa13f62015-07-06 18:11:54 +01002300 bool IsGtBias() const { return bias_ == ComparisonBias::kGtBias; }
Mark Mendellc4701932015-04-10 13:18:51 -04002301
2302 void SetBias(ComparisonBias bias) { bias_ = bias; }
2303
2304 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2305 return bias_ == other->AsCondition()->bias_;
2306 }
2307
Roland Levillain4fa13f62015-07-06 18:11:54 +01002308 bool IsFPConditionTrueIfNaN() const {
2309 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2310 IfCondition if_cond = GetCondition();
2311 return IsGtBias() ? ((if_cond == kCondGT) || (if_cond == kCondGE)) : (if_cond == kCondNE);
2312 }
2313
2314 bool IsFPConditionFalseIfNaN() const {
2315 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2316 IfCondition if_cond = GetCondition();
2317 return IsGtBias() ? ((if_cond == kCondLT) || (if_cond == kCondLE)) : (if_cond == kCondEQ);
2318 }
2319
Dave Allison20dfc792014-06-16 20:44:29 -07002320 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002321 // For register allocation purposes, returns whether this instruction needs to be
2322 // materialized (that is, not just be in the processor flags).
2323 bool needs_materialization_;
2324
Mark Mendellc4701932015-04-10 13:18:51 -04002325 // Needed if we merge a HCompare into a HCondition.
2326 ComparisonBias bias_;
2327
Dave Allison20dfc792014-06-16 20:44:29 -07002328 DISALLOW_COPY_AND_ASSIGN(HCondition);
2329};
2330
2331// Instruction to check if two inputs are equal to each other.
2332class HEqual : public HCondition {
2333 public:
2334 HEqual(HInstruction* first, HInstruction* second)
2335 : HCondition(first, second) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002336
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002337 bool IsCommutative() const OVERRIDE { return true; }
2338
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002339 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002340 return x == y ? 1 : 0;
2341 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002342 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002343 return x == y ? 1 : 0;
2344 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002345
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002346 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002347
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002348 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002349 return kCondEQ;
2350 }
2351
Mark Mendellc4701932015-04-10 13:18:51 -04002352 IfCondition GetOppositeCondition() const OVERRIDE {
2353 return kCondNE;
2354 }
2355
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002356 private:
2357 DISALLOW_COPY_AND_ASSIGN(HEqual);
2358};
2359
Dave Allison20dfc792014-06-16 20:44:29 -07002360class HNotEqual : public HCondition {
2361 public:
2362 HNotEqual(HInstruction* first, HInstruction* second)
2363 : HCondition(first, second) {}
2364
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002365 bool IsCommutative() const OVERRIDE { return true; }
2366
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002367 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002368 return x != y ? 1 : 0;
2369 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002370 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002371 return x != y ? 1 : 0;
2372 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002373
Dave Allison20dfc792014-06-16 20:44:29 -07002374 DECLARE_INSTRUCTION(NotEqual);
2375
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002376 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002377 return kCondNE;
2378 }
2379
Mark Mendellc4701932015-04-10 13:18:51 -04002380 IfCondition GetOppositeCondition() const OVERRIDE {
2381 return kCondEQ;
2382 }
2383
Dave Allison20dfc792014-06-16 20:44:29 -07002384 private:
2385 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
2386};
2387
2388class HLessThan : public HCondition {
2389 public:
2390 HLessThan(HInstruction* first, HInstruction* second)
2391 : HCondition(first, second) {}
2392
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002393 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002394 return x < y ? 1 : 0;
2395 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002396 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002397 return x < y ? 1 : 0;
2398 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002399
Dave Allison20dfc792014-06-16 20:44:29 -07002400 DECLARE_INSTRUCTION(LessThan);
2401
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002402 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002403 return kCondLT;
2404 }
2405
Mark Mendellc4701932015-04-10 13:18:51 -04002406 IfCondition GetOppositeCondition() const OVERRIDE {
2407 return kCondGE;
2408 }
2409
Dave Allison20dfc792014-06-16 20:44:29 -07002410 private:
2411 DISALLOW_COPY_AND_ASSIGN(HLessThan);
2412};
2413
2414class HLessThanOrEqual : public HCondition {
2415 public:
2416 HLessThanOrEqual(HInstruction* first, HInstruction* second)
2417 : HCondition(first, second) {}
2418
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002419 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002420 return x <= y ? 1 : 0;
2421 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002422 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002423 return x <= y ? 1 : 0;
2424 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002425
Dave Allison20dfc792014-06-16 20:44:29 -07002426 DECLARE_INSTRUCTION(LessThanOrEqual);
2427
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002428 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002429 return kCondLE;
2430 }
2431
Mark Mendellc4701932015-04-10 13:18:51 -04002432 IfCondition GetOppositeCondition() const OVERRIDE {
2433 return kCondGT;
2434 }
2435
Dave Allison20dfc792014-06-16 20:44:29 -07002436 private:
2437 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
2438};
2439
2440class HGreaterThan : public HCondition {
2441 public:
2442 HGreaterThan(HInstruction* first, HInstruction* second)
2443 : HCondition(first, second) {}
2444
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002445 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002446 return x > y ? 1 : 0;
2447 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002448 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002449 return x > y ? 1 : 0;
2450 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002451
Dave Allison20dfc792014-06-16 20:44:29 -07002452 DECLARE_INSTRUCTION(GreaterThan);
2453
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002454 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002455 return kCondGT;
2456 }
2457
Mark Mendellc4701932015-04-10 13:18:51 -04002458 IfCondition GetOppositeCondition() const OVERRIDE {
2459 return kCondLE;
2460 }
2461
Dave Allison20dfc792014-06-16 20:44:29 -07002462 private:
2463 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
2464};
2465
2466class HGreaterThanOrEqual : public HCondition {
2467 public:
2468 HGreaterThanOrEqual(HInstruction* first, HInstruction* second)
2469 : HCondition(first, second) {}
2470
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002471 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002472 return x >= y ? 1 : 0;
2473 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002474 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002475 return x >= y ? 1 : 0;
2476 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002477
Dave Allison20dfc792014-06-16 20:44:29 -07002478 DECLARE_INSTRUCTION(GreaterThanOrEqual);
2479
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002480 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002481 return kCondGE;
2482 }
2483
Mark Mendellc4701932015-04-10 13:18:51 -04002484 IfCondition GetOppositeCondition() const OVERRIDE {
2485 return kCondLT;
2486 }
2487
Dave Allison20dfc792014-06-16 20:44:29 -07002488 private:
2489 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
2490};
2491
2492
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002493// Instruction to check how two inputs compare to each other.
2494// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
2495class HCompare : public HBinaryOperation {
2496 public:
Alexey Frunze4dda3372015-06-01 18:31:49 -07002497 HCompare(Primitive::Type type,
2498 HInstruction* first,
2499 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04002500 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07002501 uint32_t dex_pc)
2502 : HBinaryOperation(Primitive::kPrimInt, first, second), bias_(bias), dex_pc_(dex_pc) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002503 DCHECK_EQ(type, first->GetType());
2504 DCHECK_EQ(type, second->GetType());
2505 }
2506
Calin Juravleddb7df22014-11-25 20:56:51 +00002507 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01002508 return
2509 x == y ? 0 :
2510 x > y ? 1 :
2511 -1;
2512 }
Calin Juravleddb7df22014-11-25 20:56:51 +00002513
2514 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01002515 return
2516 x == y ? 0 :
2517 x > y ? 1 :
2518 -1;
2519 }
2520
Calin Juravleddb7df22014-11-25 20:56:51 +00002521 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2522 return bias_ == other->AsCompare()->bias_;
2523 }
2524
Mark Mendellc4701932015-04-10 13:18:51 -04002525 ComparisonBias GetBias() const { return bias_; }
2526
Roland Levillain4fa13f62015-07-06 18:11:54 +01002527 bool IsGtBias() { return bias_ == ComparisonBias::kGtBias; }
Calin Juravleddb7df22014-11-25 20:56:51 +00002528
Alexey Frunze4dda3372015-06-01 18:31:49 -07002529 uint32_t GetDexPc() const { return dex_pc_; }
2530
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002531 DECLARE_INSTRUCTION(Compare);
2532
2533 private:
Mark Mendellc4701932015-04-10 13:18:51 -04002534 const ComparisonBias bias_;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002535 const uint32_t dex_pc_;
Calin Juravleddb7df22014-11-25 20:56:51 +00002536
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002537 DISALLOW_COPY_AND_ASSIGN(HCompare);
2538};
2539
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002540// A local in the graph. Corresponds to a Dex register.
2541class HLocal : public HTemplateInstruction<0> {
2542 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002543 explicit HLocal(uint16_t reg_number)
2544 : HTemplateInstruction(SideEffects::None()), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002545
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002546 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002547
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002548 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002549
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002550 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002551 // The Dex register number.
2552 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002553
2554 DISALLOW_COPY_AND_ASSIGN(HLocal);
2555};
2556
2557// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07002558class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002559 public:
Roland Levillain5799fc02014-09-25 12:15:20 +01002560 HLoadLocal(HLocal* local, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002561 : HExpression(type, SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002562 SetRawInputAt(0, local);
2563 }
2564
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002565 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
2566
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002567 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002568
2569 private:
2570 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
2571};
2572
2573// Store a value in a given local. This instruction has two inputs: the value
2574// and the local.
2575class HStoreLocal : public HTemplateInstruction<2> {
2576 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002577 HStoreLocal(HLocal* local, HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002578 SetRawInputAt(0, local);
2579 SetRawInputAt(1, value);
2580 }
2581
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002582 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
2583
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002584 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002585
2586 private:
2587 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
2588};
2589
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002590class HConstant : public HExpression<0> {
2591 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002592 explicit HConstant(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
2593
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002594 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002595
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002596 virtual bool IsMinusOne() const { return false; }
2597 virtual bool IsZero() const { return false; }
2598 virtual bool IsOne() const { return false; }
2599
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002600 DECLARE_INSTRUCTION(Constant);
2601
2602 private:
2603 DISALLOW_COPY_AND_ASSIGN(HConstant);
2604};
2605
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002606class HFloatConstant : public HConstant {
2607 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002608 float GetValue() const { return value_; }
2609
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002610 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002611 return bit_cast<uint32_t, float>(other->AsFloatConstant()->value_) ==
2612 bit_cast<uint32_t, float>(value_);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002613 }
2614
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002615 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002616
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002617 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002618 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002619 }
2620 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002621 return value_ == 0.0f;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002622 }
2623 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002624 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
2625 }
2626 bool IsNaN() const {
2627 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002628 }
2629
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002630 DECLARE_INSTRUCTION(FloatConstant);
2631
2632 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002633 explicit HFloatConstant(float value) : HConstant(Primitive::kPrimFloat), value_(value) {}
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002634 explicit HFloatConstant(int32_t value)
2635 : HConstant(Primitive::kPrimFloat), value_(bit_cast<float, int32_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00002636
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002637 const float value_;
2638
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002639 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00002640 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002641 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002642 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2643};
2644
2645class HDoubleConstant : public HConstant {
2646 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002647 double GetValue() const { return value_; }
2648
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002649 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002650 return bit_cast<uint64_t, double>(other->AsDoubleConstant()->value_) ==
2651 bit_cast<uint64_t, double>(value_);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002652 }
2653
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002654 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002655
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002656 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002657 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002658 }
2659 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002660 return value_ == 0.0;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002661 }
2662 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002663 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
2664 }
2665 bool IsNaN() const {
2666 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002667 }
2668
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002669 DECLARE_INSTRUCTION(DoubleConstant);
2670
2671 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002672 explicit HDoubleConstant(double value) : HConstant(Primitive::kPrimDouble), value_(value) {}
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002673 explicit HDoubleConstant(int64_t value)
2674 : HConstant(Primitive::kPrimDouble), value_(bit_cast<double, int64_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00002675
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002676 const double value_;
2677
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002678 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00002679 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002680 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002681 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
2682};
2683
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002684class HNullConstant : public HConstant {
2685 public:
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002686 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2687 return true;
2688 }
2689
2690 size_t ComputeHashCode() const OVERRIDE { return 0; }
2691
2692 DECLARE_INSTRUCTION(NullConstant);
2693
2694 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002695 HNullConstant() : HConstant(Primitive::kPrimNot) {}
2696
2697 friend class HGraph;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002698 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2699};
2700
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002701// Constants of the type int. Those can be from Dex instructions, or
2702// synthesized (for example with the if-eqz instruction).
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002703class HIntConstant : public HConstant {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002704 public:
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002705 int32_t GetValue() const { return value_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002706
Calin Juravle61d544b2015-02-23 16:46:57 +00002707 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002708 return other->AsIntConstant()->value_ == value_;
2709 }
2710
Calin Juravle61d544b2015-02-23 16:46:57 +00002711 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2712
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002713 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2714 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2715 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2716
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002717 DECLARE_INSTRUCTION(IntConstant);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002718
2719 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002720 explicit HIntConstant(int32_t value) : HConstant(Primitive::kPrimInt), value_(value) {}
2721
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002722 const int32_t value_;
2723
David Brazdil8d5b8b22015-03-24 10:51:52 +00002724 friend class HGraph;
2725 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
Zheng Xuad4450e2015-04-17 18:48:56 +08002726 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002727 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2728};
2729
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002730class HLongConstant : public HConstant {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002731 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002732 int64_t GetValue() const { return value_; }
2733
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002734 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002735 return other->AsLongConstant()->value_ == value_;
2736 }
2737
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002738 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002739
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002740 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2741 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2742 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2743
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002744 DECLARE_INSTRUCTION(LongConstant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002745
2746 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002747 explicit HLongConstant(int64_t value) : HConstant(Primitive::kPrimLong), value_(value) {}
2748
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002749 const int64_t value_;
2750
David Brazdil8d5b8b22015-03-24 10:51:52 +00002751 friend class HGraph;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002752 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2753};
2754
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002755enum class Intrinsics {
2756#define OPTIMIZING_INTRINSICS(Name, IsStatic) k ## Name,
2757#include "intrinsics_list.h"
2758 kNone,
2759 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
2760#undef INTRINSICS_LIST
2761#undef OPTIMIZING_INTRINSICS
2762};
2763std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
2764
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002765class HInvoke : public HInstruction {
2766 public:
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002767 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002768
2769 // Runtime needs to walk the stack, so Dex -> Dex calls need to
2770 // know their environment.
Calin Juravle77520bc2015-01-12 18:45:46 +00002771 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002772
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01002773 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002774 SetRawInputAt(index, argument);
2775 }
2776
Roland Levillain3e3d7332015-04-28 11:00:54 +01002777 // Return the number of arguments. This number can be lower than
2778 // the number of inputs returned by InputCount(), as some invoke
2779 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
2780 // inputs at the end of their list of inputs.
2781 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
2782
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002783 Primitive::Type GetType() const OVERRIDE { return return_type_; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002784
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002785 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002786
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002787 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002788 const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002789
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002790 InvokeType GetOriginalInvokeType() const { return original_invoke_type_; }
2791
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01002792 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002793 return intrinsic_;
2794 }
2795
2796 void SetIntrinsic(Intrinsics intrinsic) {
2797 intrinsic_ = intrinsic;
2798 }
2799
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01002800 bool IsFromInlinedInvoke() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002801 return GetEnvironment()->GetParent() != nullptr;
2802 }
2803
2804 bool CanThrow() const OVERRIDE { return true; }
2805
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002806 DECLARE_INSTRUCTION(Invoke);
2807
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002808 protected:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002809 HInvoke(ArenaAllocator* arena,
2810 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01002811 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002812 Primitive::Type return_type,
2813 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002814 uint32_t dex_method_index,
2815 InvokeType original_invoke_type)
Aart Bik854a02b2015-07-14 16:07:00 -07002816 : HInstruction(SideEffects::All()), // assume write/read on all fields/arrays
Roland Levillain3e3d7332015-04-28 11:00:54 +01002817 number_of_arguments_(number_of_arguments),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002818 inputs_(arena, number_of_arguments),
2819 return_type_(return_type),
2820 dex_pc_(dex_pc),
2821 dex_method_index_(dex_method_index),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002822 original_invoke_type_(original_invoke_type),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002823 intrinsic_(Intrinsics::kNone) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01002824 uint32_t number_of_inputs = number_of_arguments + number_of_other_inputs;
2825 inputs_.SetSize(number_of_inputs);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002826 }
2827
David Brazdil1abb4192015-02-17 18:33:36 +00002828 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
2829 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
2830 inputs_.Put(index, input);
2831 }
2832
Roland Levillain3e3d7332015-04-28 11:00:54 +01002833 uint32_t number_of_arguments_;
David Brazdil1abb4192015-02-17 18:33:36 +00002834 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002835 const Primitive::Type return_type_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002836 const uint32_t dex_pc_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002837 const uint32_t dex_method_index_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002838 const InvokeType original_invoke_type_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002839 Intrinsics intrinsic_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002840
2841 private:
2842 DISALLOW_COPY_AND_ASSIGN(HInvoke);
2843};
2844
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002845class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002846 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01002847 // Requirements of this method call regarding the class
2848 // initialization (clinit) check of its declaring class.
2849 enum class ClinitCheckRequirement {
2850 kNone, // Class already initialized.
2851 kExplicit, // Static call having explicit clinit check as last input.
2852 kImplicit, // Static call implicitly requiring a clinit check.
2853 };
2854
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002855 HInvokeStaticOrDirect(ArenaAllocator* arena,
2856 uint32_t number_of_arguments,
2857 Primitive::Type return_type,
2858 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002859 uint32_t dex_method_index,
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002860 bool is_recursive,
Jeff Hao848f70a2014-01-15 13:49:50 -08002861 int32_t string_init_offset,
Nicolas Geoffray79041292015-03-26 10:05:54 +00002862 InvokeType original_invoke_type,
Roland Levillain4c0eb422015-04-24 16:43:49 +01002863 InvokeType invoke_type,
2864 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01002865 : HInvoke(arena,
2866 number_of_arguments,
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01002867 // There is one extra argument for the HCurrentMethod node, and
2868 // potentially one other if the clinit check is explicit, and one other
2869 // if the method is a string factory.
2870 1u + (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u)
2871 + (string_init_offset ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01002872 return_type,
2873 dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002874 dex_method_index,
2875 original_invoke_type),
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002876 invoke_type_(invoke_type),
Roland Levillain4c0eb422015-04-24 16:43:49 +01002877 is_recursive_(is_recursive),
Jeff Hao848f70a2014-01-15 13:49:50 -08002878 clinit_check_requirement_(clinit_check_requirement),
2879 string_init_offset_(string_init_offset) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002880
Calin Juravle641547a2015-04-21 22:08:51 +01002881 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
2882 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00002883 // We access the method via the dex cache so we can't do an implicit null check.
2884 // TODO: for intrinsics we can generate implicit null checks.
2885 return false;
2886 }
2887
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002888 InvokeType GetInvokeType() const { return invoke_type_; }
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002889 bool IsRecursive() const { return is_recursive_; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002890 bool NeedsDexCache() const OVERRIDE { return !IsRecursive(); }
Jeff Hao848f70a2014-01-15 13:49:50 -08002891 bool IsStringInit() const { return string_init_offset_ != 0; }
2892 int32_t GetStringInitOffset() const { return string_init_offset_; }
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002893 uint32_t GetCurrentMethodInputIndex() const { return GetNumberOfArguments(); }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002894
Roland Levillain4c0eb422015-04-24 16:43:49 +01002895 // Is this instruction a call to a static method?
2896 bool IsStatic() const {
2897 return GetInvokeType() == kStatic;
2898 }
2899
Roland Levillain3e3d7332015-04-28 11:00:54 +01002900 // Remove the art::HLoadClass instruction set as last input by
2901 // art::PrepareForRegisterAllocation::VisitClinitCheck in lieu of
2902 // the initial art::HClinitCheck instruction (only relevant for
2903 // static calls with explicit clinit check).
2904 void RemoveLoadClassAsLastInput() {
Roland Levillain4c0eb422015-04-24 16:43:49 +01002905 DCHECK(IsStaticWithExplicitClinitCheck());
2906 size_t last_input_index = InputCount() - 1;
2907 HInstruction* last_input = InputAt(last_input_index);
2908 DCHECK(last_input != nullptr);
Roland Levillain3e3d7332015-04-28 11:00:54 +01002909 DCHECK(last_input->IsLoadClass()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01002910 RemoveAsUserOfInput(last_input_index);
2911 inputs_.DeleteAt(last_input_index);
2912 clinit_check_requirement_ = ClinitCheckRequirement::kImplicit;
2913 DCHECK(IsStaticWithImplicitClinitCheck());
2914 }
2915
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01002916 bool IsStringFactoryFor(HFakeString* str) const {
2917 if (!IsStringInit()) return false;
2918 // +1 for the current method.
2919 if (InputCount() == (number_of_arguments_ + 1)) return false;
2920 return InputAt(InputCount() - 1)->AsFakeString() == str;
2921 }
2922
2923 void RemoveFakeStringArgumentAsLastInput() {
2924 DCHECK(IsStringInit());
2925 size_t last_input_index = InputCount() - 1;
2926 HInstruction* last_input = InputAt(last_input_index);
2927 DCHECK(last_input != nullptr);
2928 DCHECK(last_input->IsFakeString()) << last_input->DebugName();
2929 RemoveAsUserOfInput(last_input_index);
2930 inputs_.DeleteAt(last_input_index);
2931 }
2932
Roland Levillain4c0eb422015-04-24 16:43:49 +01002933 // Is this a call to a static method whose declaring class has an
2934 // explicit intialization check in the graph?
2935 bool IsStaticWithExplicitClinitCheck() const {
2936 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kExplicit);
2937 }
2938
2939 // Is this a call to a static method whose declaring class has an
2940 // implicit intialization check requirement?
2941 bool IsStaticWithImplicitClinitCheck() const {
2942 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kImplicit);
2943 }
2944
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002945 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002946
Roland Levillain4c0eb422015-04-24 16:43:49 +01002947 protected:
2948 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
2949 const HUserRecord<HInstruction*> input_record = HInvoke::InputRecordAt(i);
2950 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck() && (i == InputCount() - 1)) {
2951 HInstruction* input = input_record.GetInstruction();
2952 // `input` is the last input of a static invoke marked as having
2953 // an explicit clinit check. It must either be:
2954 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
2955 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
2956 DCHECK(input != nullptr);
2957 DCHECK(input->IsClinitCheck() || input->IsLoadClass()) << input->DebugName();
2958 }
2959 return input_record;
2960 }
2961
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002962 private:
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002963 const InvokeType invoke_type_;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002964 const bool is_recursive_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01002965 ClinitCheckRequirement clinit_check_requirement_;
Jeff Hao848f70a2014-01-15 13:49:50 -08002966 // Thread entrypoint offset for string init method if this is a string init invoke.
2967 // Note that there are multiple string init methods, each having its own offset.
2968 int32_t string_init_offset_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002969
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002970 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002971};
2972
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002973class HInvokeVirtual : public HInvoke {
2974 public:
2975 HInvokeVirtual(ArenaAllocator* arena,
2976 uint32_t number_of_arguments,
2977 Primitive::Type return_type,
2978 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002979 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002980 uint32_t vtable_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002981 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002982 vtable_index_(vtable_index) {}
2983
Calin Juravle641547a2015-04-21 22:08:51 +01002984 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00002985 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01002986 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00002987 }
2988
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002989 uint32_t GetVTableIndex() const { return vtable_index_; }
2990
2991 DECLARE_INSTRUCTION(InvokeVirtual);
2992
2993 private:
2994 const uint32_t vtable_index_;
2995
2996 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
2997};
2998
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002999class HInvokeInterface : public HInvoke {
3000 public:
3001 HInvokeInterface(ArenaAllocator* arena,
3002 uint32_t number_of_arguments,
3003 Primitive::Type return_type,
3004 uint32_t dex_pc,
3005 uint32_t dex_method_index,
3006 uint32_t imt_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003007 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003008 imt_index_(imt_index) {}
3009
Calin Juravle641547a2015-04-21 22:08:51 +01003010 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003011 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01003012 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00003013 }
3014
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003015 uint32_t GetImtIndex() const { return imt_index_; }
3016 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
3017
3018 DECLARE_INSTRUCTION(InvokeInterface);
3019
3020 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003021 const uint32_t imt_index_;
3022
3023 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
3024};
3025
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003026class HNewInstance : public HExpression<1> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003027 public:
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003028 HNewInstance(HCurrentMethod* current_method,
3029 uint32_t dex_pc,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003030 uint16_t type_index,
3031 const DexFile& dex_file,
3032 QuickEntrypointEnum entrypoint)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003033 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3034 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003035 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003036 dex_file_(dex_file),
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003037 entrypoint_(entrypoint) {
3038 SetRawInputAt(0, current_method);
3039 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003040
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003041 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003042 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003043 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003044
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003045 // Calls runtime so needs an environment.
Calin Juravle92a6ed22014-12-02 18:58:03 +00003046 bool NeedsEnvironment() const OVERRIDE { return true; }
3047 // It may throw when called on:
3048 // - interfaces
3049 // - abstract/innaccessible/unknown classes
3050 // TODO: optimize when possible.
3051 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003052
Calin Juravle10e244f2015-01-26 18:54:32 +00003053 bool CanBeNull() const OVERRIDE { return false; }
3054
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003055 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3056
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003057 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003058
3059 private:
3060 const uint32_t dex_pc_;
3061 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003062 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003063 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003064
3065 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3066};
3067
Roland Levillain88cb1752014-10-20 16:36:47 +01003068class HNeg : public HUnaryOperation {
3069 public:
3070 explicit HNeg(Primitive::Type result_type, HInstruction* input)
3071 : HUnaryOperation(result_type, input) {}
3072
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003073 int32_t Evaluate(int32_t x) const OVERRIDE { return -x; }
3074 int64_t Evaluate(int64_t x) const OVERRIDE { return -x; }
Roland Levillain9240d6a2014-10-20 16:47:04 +01003075
Roland Levillain88cb1752014-10-20 16:36:47 +01003076 DECLARE_INSTRUCTION(Neg);
3077
3078 private:
3079 DISALLOW_COPY_AND_ASSIGN(HNeg);
3080};
3081
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003082class HNewArray : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003083 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003084 HNewArray(HInstruction* length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003085 HCurrentMethod* current_method,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003086 uint32_t dex_pc,
3087 uint16_t type_index,
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003088 const DexFile& dex_file,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003089 QuickEntrypointEnum entrypoint)
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003090 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3091 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003092 type_index_(type_index),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003093 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003094 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003095 SetRawInputAt(0, length);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003096 SetRawInputAt(1, current_method);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003097 }
3098
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003099 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003100 uint16_t GetTypeIndex() const { return type_index_; }
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003101 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003102
3103 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00003104 bool NeedsEnvironment() const OVERRIDE { return true; }
3105
Mingyao Yang0c365e62015-03-31 15:09:29 -07003106 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
3107 bool CanThrow() const OVERRIDE { return true; }
3108
Calin Juravle10e244f2015-01-26 18:54:32 +00003109 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003110
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003111 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3112
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003113 DECLARE_INSTRUCTION(NewArray);
3114
3115 private:
3116 const uint32_t dex_pc_;
3117 const uint16_t type_index_;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003118 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003119 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003120
3121 DISALLOW_COPY_AND_ASSIGN(HNewArray);
3122};
3123
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003124class HAdd : public HBinaryOperation {
3125 public:
3126 HAdd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3127 : HBinaryOperation(result_type, left, right) {}
3128
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003129 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003130
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003131 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01003132 return x + y;
3133 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003134 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01003135 return x + y;
3136 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003137
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003138 DECLARE_INSTRUCTION(Add);
3139
3140 private:
3141 DISALLOW_COPY_AND_ASSIGN(HAdd);
3142};
3143
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003144class HSub : public HBinaryOperation {
3145 public:
3146 HSub(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3147 : HBinaryOperation(result_type, left, right) {}
3148
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003149 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01003150 return x - y;
3151 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003152 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01003153 return x - y;
3154 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003155
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003156 DECLARE_INSTRUCTION(Sub);
3157
3158 private:
3159 DISALLOW_COPY_AND_ASSIGN(HSub);
3160};
3161
Calin Juravle34bacdf2014-10-07 20:23:36 +01003162class HMul : public HBinaryOperation {
3163 public:
3164 HMul(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3165 : HBinaryOperation(result_type, left, right) {}
3166
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003167 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003168
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003169 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x * y; }
3170 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x * y; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003171
3172 DECLARE_INSTRUCTION(Mul);
3173
3174 private:
3175 DISALLOW_COPY_AND_ASSIGN(HMul);
3176};
3177
Calin Juravle7c4954d2014-10-28 16:57:40 +00003178class HDiv : public HBinaryOperation {
3179 public:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003180 HDiv(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
3181 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00003182
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003183 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00003184 // Our graph structure ensures we never have 0 for `y` during constant folding.
3185 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00003186 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00003187 return (y == -1) ? -x : x / y;
3188 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003189
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003190 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00003191 DCHECK_NE(y, 0);
3192 // Special case -1 to avoid getting a SIGFPE on x86(_64).
3193 return (y == -1) ? -x : x / y;
3194 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003195
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003196 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003197
Calin Juravle7c4954d2014-10-28 16:57:40 +00003198 DECLARE_INSTRUCTION(Div);
3199
3200 private:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003201 const uint32_t dex_pc_;
3202
Calin Juravle7c4954d2014-10-28 16:57:40 +00003203 DISALLOW_COPY_AND_ASSIGN(HDiv);
3204};
3205
Calin Juravlebacfec32014-11-14 15:54:36 +00003206class HRem : public HBinaryOperation {
3207 public:
3208 HRem(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
3209 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
3210
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003211 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00003212 DCHECK_NE(y, 0);
3213 // Special case -1 to avoid getting a SIGFPE on x86(_64).
3214 return (y == -1) ? 0 : x % y;
3215 }
3216
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003217 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00003218 DCHECK_NE(y, 0);
3219 // Special case -1 to avoid getting a SIGFPE on x86(_64).
3220 return (y == -1) ? 0 : x % y;
3221 }
3222
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003223 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Calin Juravlebacfec32014-11-14 15:54:36 +00003224
3225 DECLARE_INSTRUCTION(Rem);
3226
3227 private:
3228 const uint32_t dex_pc_;
3229
3230 DISALLOW_COPY_AND_ASSIGN(HRem);
3231};
3232
Calin Juravled0d48522014-11-04 16:40:20 +00003233class HDivZeroCheck : public HExpression<1> {
3234 public:
3235 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
3236 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
3237 SetRawInputAt(0, value);
3238 }
3239
3240 bool CanBeMoved() const OVERRIDE { return true; }
3241
3242 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3243 UNUSED(other);
3244 return true;
3245 }
3246
3247 bool NeedsEnvironment() const OVERRIDE { return true; }
3248 bool CanThrow() const OVERRIDE { return true; }
3249
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003250 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Calin Juravled0d48522014-11-04 16:40:20 +00003251
3252 DECLARE_INSTRUCTION(DivZeroCheck);
3253
3254 private:
3255 const uint32_t dex_pc_;
3256
3257 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
3258};
3259
Calin Juravle9aec02f2014-11-18 23:06:35 +00003260class HShl : public HBinaryOperation {
3261 public:
3262 HShl(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3263 : HBinaryOperation(result_type, left, right) {}
3264
3265 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x << (y & kMaxIntShiftValue); }
3266 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x << (y & kMaxLongShiftValue); }
3267
3268 DECLARE_INSTRUCTION(Shl);
3269
3270 private:
3271 DISALLOW_COPY_AND_ASSIGN(HShl);
3272};
3273
3274class HShr : public HBinaryOperation {
3275 public:
3276 HShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3277 : HBinaryOperation(result_type, left, right) {}
3278
3279 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x >> (y & kMaxIntShiftValue); }
3280 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x >> (y & kMaxLongShiftValue); }
3281
3282 DECLARE_INSTRUCTION(Shr);
3283
3284 private:
3285 DISALLOW_COPY_AND_ASSIGN(HShr);
3286};
3287
3288class HUShr : public HBinaryOperation {
3289 public:
3290 HUShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3291 : HBinaryOperation(result_type, left, right) {}
3292
3293 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
3294 uint32_t ux = static_cast<uint32_t>(x);
3295 uint32_t uy = static_cast<uint32_t>(y) & kMaxIntShiftValue;
3296 return static_cast<int32_t>(ux >> uy);
3297 }
3298
3299 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
3300 uint64_t ux = static_cast<uint64_t>(x);
3301 uint64_t uy = static_cast<uint64_t>(y) & kMaxLongShiftValue;
3302 return static_cast<int64_t>(ux >> uy);
3303 }
3304
3305 DECLARE_INSTRUCTION(UShr);
3306
3307 private:
3308 DISALLOW_COPY_AND_ASSIGN(HUShr);
3309};
3310
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003311class HAnd : public HBinaryOperation {
3312 public:
3313 HAnd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3314 : HBinaryOperation(result_type, left, right) {}
3315
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003316 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003317
3318 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x & y; }
3319 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x & y; }
3320
3321 DECLARE_INSTRUCTION(And);
3322
3323 private:
3324 DISALLOW_COPY_AND_ASSIGN(HAnd);
3325};
3326
3327class HOr : public HBinaryOperation {
3328 public:
3329 HOr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3330 : HBinaryOperation(result_type, left, right) {}
3331
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003332 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003333
3334 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x | y; }
3335 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x | y; }
3336
3337 DECLARE_INSTRUCTION(Or);
3338
3339 private:
3340 DISALLOW_COPY_AND_ASSIGN(HOr);
3341};
3342
3343class HXor : public HBinaryOperation {
3344 public:
3345 HXor(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3346 : HBinaryOperation(result_type, left, right) {}
3347
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003348 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003349
3350 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x ^ y; }
3351 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x ^ y; }
3352
3353 DECLARE_INSTRUCTION(Xor);
3354
3355 private:
3356 DISALLOW_COPY_AND_ASSIGN(HXor);
3357};
3358
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003359// The value of a parameter in this method. Its location depends on
3360// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07003361class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003362 public:
Calin Juravle10e244f2015-01-26 18:54:32 +00003363 HParameterValue(uint8_t index, Primitive::Type parameter_type, bool is_this = false)
3364 : HExpression(parameter_type, SideEffects::None()), index_(index), is_this_(is_this) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003365
3366 uint8_t GetIndex() const { return index_; }
3367
Calin Juravle10e244f2015-01-26 18:54:32 +00003368 bool CanBeNull() const OVERRIDE { return !is_this_; }
3369
Calin Juravle3cd4fc82015-05-14 15:15:42 +01003370 bool IsThis() const { return is_this_; }
3371
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003372 DECLARE_INSTRUCTION(ParameterValue);
3373
3374 private:
3375 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00003376 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003377 const uint8_t index_;
3378
Calin Juravle10e244f2015-01-26 18:54:32 +00003379 // Whether or not the parameter value corresponds to 'this' argument.
3380 const bool is_this_;
3381
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003382 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
3383};
3384
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003385class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003386 public:
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003387 explicit HNot(Primitive::Type result_type, HInstruction* input)
3388 : HUnaryOperation(result_type, input) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003389
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003390 bool CanBeMoved() const OVERRIDE { return true; }
3391 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003392 UNUSED(other);
3393 return true;
3394 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003395
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003396 int32_t Evaluate(int32_t x) const OVERRIDE { return ~x; }
3397 int64_t Evaluate(int64_t x) const OVERRIDE { return ~x; }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003398
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003399 DECLARE_INSTRUCTION(Not);
3400
3401 private:
3402 DISALLOW_COPY_AND_ASSIGN(HNot);
3403};
3404
David Brazdil66d126e2015-04-03 16:02:44 +01003405class HBooleanNot : public HUnaryOperation {
3406 public:
3407 explicit HBooleanNot(HInstruction* input)
3408 : HUnaryOperation(Primitive::Type::kPrimBoolean, input) {}
3409
3410 bool CanBeMoved() const OVERRIDE { return true; }
3411 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3412 UNUSED(other);
3413 return true;
3414 }
3415
3416 int32_t Evaluate(int32_t x) const OVERRIDE {
3417 DCHECK(IsUint<1>(x));
3418 return !x;
3419 }
3420
3421 int64_t Evaluate(int64_t x ATTRIBUTE_UNUSED) const OVERRIDE {
3422 LOG(FATAL) << DebugName() << " cannot be used with 64-bit values";
3423 UNREACHABLE();
3424 }
3425
3426 DECLARE_INSTRUCTION(BooleanNot);
3427
3428 private:
3429 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
3430};
3431
Roland Levillaindff1f282014-11-05 14:15:05 +00003432class HTypeConversion : public HExpression<1> {
3433 public:
3434 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00003435 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
3436 : HExpression(result_type, SideEffects::None()), dex_pc_(dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00003437 SetRawInputAt(0, input);
3438 DCHECK_NE(input->GetType(), result_type);
3439 }
3440
3441 HInstruction* GetInput() const { return InputAt(0); }
3442 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
3443 Primitive::Type GetResultType() const { return GetType(); }
3444
Roland Levillain624279f2014-12-04 11:54:28 +00003445 // Required by the x86 and ARM code generators when producing calls
3446 // to the runtime.
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003447 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Roland Levillain624279f2014-12-04 11:54:28 +00003448
Roland Levillaindff1f282014-11-05 14:15:05 +00003449 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00003450 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00003451
Mark Mendelle82549b2015-05-06 10:55:34 -04003452 // Try to statically evaluate the conversion and return a HConstant
3453 // containing the result. If the input cannot be converted, return nullptr.
3454 HConstant* TryStaticEvaluation() const;
3455
Roland Levillaindff1f282014-11-05 14:15:05 +00003456 DECLARE_INSTRUCTION(TypeConversion);
3457
3458 private:
Roland Levillain624279f2014-12-04 11:54:28 +00003459 const uint32_t dex_pc_;
3460
Roland Levillaindff1f282014-11-05 14:15:05 +00003461 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
3462};
3463
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00003464static constexpr uint32_t kNoRegNumber = -1;
3465
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003466class HPhi : public HInstruction {
3467 public:
3468 HPhi(ArenaAllocator* arena, uint32_t reg_number, size_t number_of_inputs, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003469 : HInstruction(SideEffects::None()),
3470 inputs_(arena, number_of_inputs),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003471 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01003472 type_(type),
Calin Juravle10e244f2015-01-26 18:54:32 +00003473 is_live_(false),
3474 can_be_null_(true) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003475 inputs_.SetSize(number_of_inputs);
3476 }
3477
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00003478 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
3479 static Primitive::Type ToPhiType(Primitive::Type type) {
3480 switch (type) {
3481 case Primitive::kPrimBoolean:
3482 case Primitive::kPrimByte:
3483 case Primitive::kPrimShort:
3484 case Primitive::kPrimChar:
3485 return Primitive::kPrimInt;
3486 default:
3487 return type;
3488 }
3489 }
3490
David Brazdilffee3d32015-07-06 11:48:53 +01003491 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
3492
Calin Juravle10e244f2015-01-26 18:54:32 +00003493 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003494
3495 void AddInput(HInstruction* input);
David Brazdil2d7352b2015-04-20 14:52:42 +01003496 void RemoveInputAt(size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003497
Calin Juravle10e244f2015-01-26 18:54:32 +00003498 Primitive::Type GetType() const OVERRIDE { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003499 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003500
Calin Juravle10e244f2015-01-26 18:54:32 +00003501 bool CanBeNull() const OVERRIDE { return can_be_null_; }
3502 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
3503
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003504 uint32_t GetRegNumber() const { return reg_number_; }
3505
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01003506 void SetDead() { is_live_ = false; }
3507 void SetLive() { is_live_ = true; }
3508 bool IsDead() const { return !is_live_; }
3509 bool IsLive() const { return is_live_; }
3510
Calin Juravlea4f88312015-04-16 12:57:19 +01003511 // Returns the next equivalent phi (starting from the current one) or null if there is none.
3512 // An equivalent phi is a phi having the same dex register and type.
3513 // It assumes that phis with the same dex register are adjacent.
3514 HPhi* GetNextEquivalentPhiWithSameType() {
3515 HInstruction* next = GetNext();
3516 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
3517 if (next->GetType() == GetType()) {
3518 return next->AsPhi();
3519 }
3520 next = next->GetNext();
3521 }
3522 return nullptr;
3523 }
3524
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003525 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003526
David Brazdil1abb4192015-02-17 18:33:36 +00003527 protected:
3528 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
3529
3530 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
3531 inputs_.Put(index, input);
3532 }
3533
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003534 private:
David Brazdil1abb4192015-02-17 18:33:36 +00003535 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003536 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003537 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01003538 bool is_live_;
Calin Juravle10e244f2015-01-26 18:54:32 +00003539 bool can_be_null_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003540
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003541 DISALLOW_COPY_AND_ASSIGN(HPhi);
3542};
3543
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003544class HNullCheck : public HExpression<1> {
3545 public:
3546 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003547 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003548 SetRawInputAt(0, value);
3549 }
3550
Calin Juravle10e244f2015-01-26 18:54:32 +00003551 bool CanBeMoved() const OVERRIDE { return true; }
3552 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003553 UNUSED(other);
3554 return true;
3555 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003556
Calin Juravle10e244f2015-01-26 18:54:32 +00003557 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003558
Calin Juravle10e244f2015-01-26 18:54:32 +00003559 bool CanThrow() const OVERRIDE { return true; }
3560
3561 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01003562
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003563 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003564
3565 DECLARE_INSTRUCTION(NullCheck);
3566
3567 private:
3568 const uint32_t dex_pc_;
3569
3570 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
3571};
3572
3573class FieldInfo : public ValueObject {
3574 public:
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003575 FieldInfo(MemberOffset field_offset,
3576 Primitive::Type field_type,
3577 bool is_volatile,
3578 uint32_t index,
3579 const DexFile& dex_file)
3580 : field_offset_(field_offset),
3581 field_type_(field_type),
3582 is_volatile_(is_volatile),
3583 index_(index),
3584 dex_file_(dex_file) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003585
3586 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003587 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003588 uint32_t GetFieldIndex() const { return index_; }
3589 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00003590 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003591
3592 private:
3593 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01003594 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00003595 const bool is_volatile_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003596 uint32_t index_;
3597 const DexFile& dex_file_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003598};
3599
3600class HInstanceFieldGet : public HExpression<1> {
3601 public:
3602 HInstanceFieldGet(HInstruction* value,
3603 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003604 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003605 bool is_volatile,
3606 uint32_t field_idx,
3607 const DexFile& dex_file)
Aart Bik34c3ba92015-07-20 14:08:59 -07003608 : HExpression(
3609 field_type,
Alexandre Rames1c4ccea2015-07-22 11:32:58 +01003610 SideEffects::FieldReadOfType(field_type, is_volatile)),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003611 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003612 SetRawInputAt(0, value);
3613 }
3614
Calin Juravle10c9cbe2014-12-19 10:50:19 +00003615 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003616
3617 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3618 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
3619 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003620 }
3621
Calin Juravle641547a2015-04-21 22:08:51 +01003622 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3623 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00003624 }
3625
3626 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01003627 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
3628 }
3629
Calin Juravle52c48962014-12-16 17:02:57 +00003630 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003631 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003632 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003633 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003634
3635 DECLARE_INSTRUCTION(InstanceFieldGet);
3636
3637 private:
3638 const FieldInfo field_info_;
3639
3640 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
3641};
3642
3643class HInstanceFieldSet : public HTemplateInstruction<2> {
3644 public:
3645 HInstanceFieldSet(HInstruction* object,
3646 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01003647 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003648 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003649 bool is_volatile,
3650 uint32_t field_idx,
3651 const DexFile& dex_file)
Aart Bik34c3ba92015-07-20 14:08:59 -07003652 : HTemplateInstruction(
3653 SideEffects::FieldWriteOfType(field_type, is_volatile)),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003654 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003655 value_can_be_null_(true) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003656 SetRawInputAt(0, object);
3657 SetRawInputAt(1, value);
3658 }
3659
Calin Juravle641547a2015-04-21 22:08:51 +01003660 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3661 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00003662 }
3663
Calin Juravle52c48962014-12-16 17:02:57 +00003664 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003665 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003666 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003667 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003668 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003669 bool GetValueCanBeNull() const { return value_can_be_null_; }
3670 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003671
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003672 DECLARE_INSTRUCTION(InstanceFieldSet);
3673
3674 private:
3675 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003676 bool value_can_be_null_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003677
3678 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
3679};
3680
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003681class HArrayGet : public HExpression<2> {
3682 public:
3683 HArrayGet(HInstruction* array, HInstruction* index, Primitive::Type type)
Aart Bik854a02b2015-07-14 16:07:00 -07003684 : HExpression(type, SideEffects::ArrayReadOfType(type)) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003685 SetRawInputAt(0, array);
3686 SetRawInputAt(1, index);
3687 }
3688
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003689 bool CanBeMoved() const OVERRIDE { return true; }
3690 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003691 UNUSED(other);
3692 return true;
3693 }
Calin Juravle641547a2015-04-21 22:08:51 +01003694 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3695 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00003696 // TODO: We can be smarter here.
3697 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
3698 // which generates the implicit null check. There are cases when these can be removed
3699 // to produce better code. If we ever add optimizations to do so we should allow an
3700 // implicit check here (as long as the address falls in the first page).
3701 return false;
3702 }
3703
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003704 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003705
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003706 HInstruction* GetArray() const { return InputAt(0); }
3707 HInstruction* GetIndex() const { return InputAt(1); }
3708
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003709 DECLARE_INSTRUCTION(ArrayGet);
3710
3711 private:
3712 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
3713};
3714
3715class HArraySet : public HTemplateInstruction<3> {
3716 public:
3717 HArraySet(HInstruction* array,
3718 HInstruction* index,
3719 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003720 Primitive::Type expected_component_type,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003721 uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07003722 : HTemplateInstruction(SideEffects::ArrayWriteOfType(expected_component_type)),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003723 dex_pc_(dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003724 expected_component_type_(expected_component_type),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003725 needs_type_check_(value->GetType() == Primitive::kPrimNot),
3726 value_can_be_null_(true) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003727 SetRawInputAt(0, array);
3728 SetRawInputAt(1, index);
3729 SetRawInputAt(2, value);
3730 }
3731
Calin Juravle77520bc2015-01-12 18:45:46 +00003732 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003733 // We currently always call a runtime method to catch array store
3734 // exceptions.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003735 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003736 }
3737
Mingyao Yang81014cb2015-06-02 03:16:27 -07003738 // Can throw ArrayStoreException.
3739 bool CanThrow() const OVERRIDE { return needs_type_check_; }
3740
Calin Juravle641547a2015-04-21 22:08:51 +01003741 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3742 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00003743 // TODO: Same as for ArrayGet.
3744 return false;
3745 }
3746
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003747 void ClearNeedsTypeCheck() {
3748 needs_type_check_ = false;
3749 }
3750
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003751 void ClearValueCanBeNull() {
3752 value_can_be_null_ = false;
3753 }
3754
3755 bool GetValueCanBeNull() const { return value_can_be_null_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003756 bool NeedsTypeCheck() const { return needs_type_check_; }
3757
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003758 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003759
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003760 HInstruction* GetArray() const { return InputAt(0); }
3761 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003762 HInstruction* GetValue() const { return InputAt(2); }
3763
3764 Primitive::Type GetComponentType() const {
3765 // The Dex format does not type floating point index operations. Since the
3766 // `expected_component_type_` is set during building and can therefore not
3767 // be correct, we also check what is the value type. If it is a floating
3768 // point type, we must use that type.
3769 Primitive::Type value_type = GetValue()->GetType();
3770 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
3771 ? value_type
3772 : expected_component_type_;
3773 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003774
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003775 DECLARE_INSTRUCTION(ArraySet);
3776
3777 private:
3778 const uint32_t dex_pc_;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003779 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003780 bool needs_type_check_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003781 bool value_can_be_null_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003782
3783 DISALLOW_COPY_AND_ASSIGN(HArraySet);
3784};
3785
3786class HArrayLength : public HExpression<1> {
3787 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003788 explicit HArrayLength(HInstruction* array)
3789 : HExpression(Primitive::kPrimInt, SideEffects::None()) {
3790 // Note that arrays do not change length, so the instruction does not
3791 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003792 SetRawInputAt(0, array);
3793 }
3794
Calin Juravle77520bc2015-01-12 18:45:46 +00003795 bool CanBeMoved() const OVERRIDE { return true; }
3796 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003797 UNUSED(other);
3798 return true;
3799 }
Calin Juravle641547a2015-04-21 22:08:51 +01003800 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3801 return obj == InputAt(0);
3802 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003803
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003804 DECLARE_INSTRUCTION(ArrayLength);
3805
3806 private:
3807 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
3808};
3809
3810class HBoundsCheck : public HExpression<2> {
3811 public:
3812 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003813 : HExpression(index->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003814 DCHECK(index->GetType() == Primitive::kPrimInt);
3815 SetRawInputAt(0, index);
3816 SetRawInputAt(1, length);
3817 }
3818
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003819 bool CanBeMoved() const OVERRIDE { return true; }
3820 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003821 UNUSED(other);
3822 return true;
3823 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003824
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003825 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003826
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003827 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01003828
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003829 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003830
3831 DECLARE_INSTRUCTION(BoundsCheck);
3832
3833 private:
3834 const uint32_t dex_pc_;
3835
3836 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
3837};
3838
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003839/**
3840 * Some DEX instructions are folded into multiple HInstructions that need
3841 * to stay live until the last HInstruction. This class
3842 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00003843 * HInstruction stays live. `index` represents the stack location index of the
3844 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003845 */
3846class HTemporary : public HTemplateInstruction<0> {
3847 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003848 explicit HTemporary(size_t index) : HTemplateInstruction(SideEffects::None()), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003849
3850 size_t GetIndex() const { return index_; }
3851
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00003852 Primitive::Type GetType() const OVERRIDE {
3853 // The previous instruction is the one that will be stored in the temporary location.
3854 DCHECK(GetPrevious() != nullptr);
3855 return GetPrevious()->GetType();
3856 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00003857
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003858 DECLARE_INSTRUCTION(Temporary);
3859
3860 private:
3861 const size_t index_;
3862
3863 DISALLOW_COPY_AND_ASSIGN(HTemporary);
3864};
3865
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003866class HSuspendCheck : public HTemplateInstruction<0> {
3867 public:
3868 explicit HSuspendCheck(uint32_t dex_pc)
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01003869 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003870
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003871 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003872 return true;
3873 }
3874
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003875 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01003876 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
3877 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003878
3879 DECLARE_INSTRUCTION(SuspendCheck);
3880
3881 private:
3882 const uint32_t dex_pc_;
3883
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01003884 // Only used for code generation, in order to share the same slow path between back edges
3885 // of a same loop.
3886 SlowPathCode* slow_path_;
3887
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003888 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
3889};
3890
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003891/**
3892 * Instruction to load a Class object.
3893 */
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003894class HLoadClass : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003895 public:
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003896 HLoadClass(HCurrentMethod* current_method,
3897 uint16_t type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003898 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003899 bool is_referrers_class,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003900 uint32_t dex_pc)
3901 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3902 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003903 dex_file_(dex_file),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003904 is_referrers_class_(is_referrers_class),
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003905 dex_pc_(dex_pc),
Calin Juravleb1498f62015-02-16 13:13:29 +00003906 generate_clinit_check_(false),
Calin Juravle7733bd62015-07-22 17:14:50 +00003907 loaded_class_rti_(ReferenceTypeInfo::CreateTop(/* is_exact */ false)) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003908 SetRawInputAt(0, current_method);
3909 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003910
3911 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003912
3913 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3914 return other->AsLoadClass()->type_index_ == type_index_;
3915 }
3916
3917 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
3918
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003919 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003920 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003921 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01003922 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003923
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003924 bool NeedsEnvironment() const OVERRIDE {
3925 // Will call runtime and load the class if the class is not loaded yet.
3926 // TODO: finer grain decision.
3927 return !is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003928 }
3929
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003930 bool MustGenerateClinitCheck() const {
3931 return generate_clinit_check_;
3932 }
3933
Calin Juravle0ba218d2015-05-19 18:46:01 +01003934 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
3935 generate_clinit_check_ = generate_clinit_check;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003936 }
3937
3938 bool CanCallRuntime() const {
3939 return MustGenerateClinitCheck() || !is_referrers_class_;
3940 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003941
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003942 bool CanThrow() const OVERRIDE {
3943 // May call runtime and and therefore can throw.
3944 // TODO: finer grain decision.
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01003945 return CanCallRuntime();
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003946 }
3947
Calin Juravleacf735c2015-02-12 15:25:22 +00003948 ReferenceTypeInfo GetLoadedClassRTI() {
3949 return loaded_class_rti_;
3950 }
3951
3952 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
3953 // Make sure we only set exact types (the loaded class should never be merged).
3954 DCHECK(rti.IsExact());
3955 loaded_class_rti_ = rti;
3956 }
3957
Calin Juravle7733bd62015-07-22 17:14:50 +00003958 bool IsResolved() {
3959 return loaded_class_rti_.IsExact();
3960 }
3961
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003962 const DexFile& GetDexFile() { return dex_file_; }
3963
Nicolas Geoffray9437b782015-03-25 10:08:51 +00003964 bool NeedsDexCache() const OVERRIDE { return !is_referrers_class_; }
3965
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003966 DECLARE_INSTRUCTION(LoadClass);
3967
3968 private:
3969 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003970 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003971 const bool is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003972 const uint32_t dex_pc_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003973 // Whether this instruction must generate the initialization check.
3974 // Used for code generation.
3975 bool generate_clinit_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003976
Calin Juravleacf735c2015-02-12 15:25:22 +00003977 ReferenceTypeInfo loaded_class_rti_;
3978
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003979 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
3980};
3981
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01003982class HLoadString : public HExpression<1> {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003983 public:
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01003984 HLoadString(HCurrentMethod* current_method, uint32_t string_index, uint32_t dex_pc)
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003985 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3986 string_index_(string_index),
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01003987 dex_pc_(dex_pc) {
3988 SetRawInputAt(0, current_method);
3989 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003990
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003991 bool CanBeMoved() const OVERRIDE { return true; }
3992
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003993 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3994 return other->AsLoadString()->string_index_ == string_index_;
3995 }
3996
3997 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
3998
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003999 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004000 uint32_t GetStringIndex() const { return string_index_; }
4001
4002 // TODO: Can we deopt or debug when we resolve a string?
4003 bool NeedsEnvironment() const OVERRIDE { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00004004 bool NeedsDexCache() const OVERRIDE { return true; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004005
4006 DECLARE_INSTRUCTION(LoadString);
4007
4008 private:
4009 const uint32_t string_index_;
4010 const uint32_t dex_pc_;
4011
4012 DISALLOW_COPY_AND_ASSIGN(HLoadString);
4013};
4014
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004015/**
4016 * Performs an initialization check on its Class object input.
4017 */
4018class HClinitCheck : public HExpression<1> {
4019 public:
4020 explicit HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07004021 : HExpression(
4022 Primitive::kPrimNot,
Aart Bik34c3ba92015-07-20 14:08:59 -07004023 SideEffects::AllWrites()), // assume write on all fields/arrays
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004024 dex_pc_(dex_pc) {
4025 SetRawInputAt(0, constant);
4026 }
4027
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004028 bool CanBeMoved() const OVERRIDE { return true; }
4029 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4030 UNUSED(other);
4031 return true;
4032 }
4033
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004034 bool NeedsEnvironment() const OVERRIDE {
4035 // May call runtime to initialize the class.
4036 return true;
4037 }
4038
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004039 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004040
4041 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
4042
4043 DECLARE_INSTRUCTION(ClinitCheck);
4044
4045 private:
4046 const uint32_t dex_pc_;
4047
4048 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
4049};
4050
4051class HStaticFieldGet : public HExpression<1> {
4052 public:
4053 HStaticFieldGet(HInstruction* cls,
4054 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004055 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004056 bool is_volatile,
4057 uint32_t field_idx,
4058 const DexFile& dex_file)
Aart Bik34c3ba92015-07-20 14:08:59 -07004059 : HExpression(
4060 field_type,
Alexandre Rames1c4ccea2015-07-22 11:32:58 +01004061 SideEffects::FieldReadOfType(field_type, is_volatile)),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004062 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004063 SetRawInputAt(0, cls);
4064 }
4065
Calin Juravle52c48962014-12-16 17:02:57 +00004066
Calin Juravle10c9cbe2014-12-19 10:50:19 +00004067 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004068
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004069 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00004070 HStaticFieldGet* other_get = other->AsStaticFieldGet();
4071 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004072 }
4073
4074 size_t ComputeHashCode() const OVERRIDE {
4075 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
4076 }
4077
Calin Juravle52c48962014-12-16 17:02:57 +00004078 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004079 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
4080 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004081 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004082
4083 DECLARE_INSTRUCTION(StaticFieldGet);
4084
4085 private:
4086 const FieldInfo field_info_;
4087
4088 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
4089};
4090
4091class HStaticFieldSet : public HTemplateInstruction<2> {
4092 public:
4093 HStaticFieldSet(HInstruction* cls,
4094 HInstruction* value,
4095 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004096 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004097 bool is_volatile,
4098 uint32_t field_idx,
4099 const DexFile& dex_file)
Aart Bik34c3ba92015-07-20 14:08:59 -07004100 : HTemplateInstruction(
4101 SideEffects::FieldWriteOfType(field_type, is_volatile)),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004102 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004103 value_can_be_null_(true) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004104 SetRawInputAt(0, cls);
4105 SetRawInputAt(1, value);
4106 }
4107
Calin Juravle52c48962014-12-16 17:02:57 +00004108 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004109 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
4110 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004111 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004112
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004113 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004114 bool GetValueCanBeNull() const { return value_can_be_null_; }
4115 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004116
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004117 DECLARE_INSTRUCTION(StaticFieldSet);
4118
4119 private:
4120 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004121 bool value_can_be_null_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004122
4123 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
4124};
4125
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004126// Implement the move-exception DEX instruction.
4127class HLoadException : public HExpression<0> {
4128 public:
4129 HLoadException() : HExpression(Primitive::kPrimNot, SideEffects::None()) {}
4130
4131 DECLARE_INSTRUCTION(LoadException);
4132
4133 private:
4134 DISALLOW_COPY_AND_ASSIGN(HLoadException);
4135};
4136
4137class HThrow : public HTemplateInstruction<1> {
4138 public:
4139 HThrow(HInstruction* exception, uint32_t dex_pc)
4140 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {
4141 SetRawInputAt(0, exception);
4142 }
4143
4144 bool IsControlFlow() const OVERRIDE { return true; }
4145
4146 bool NeedsEnvironment() const OVERRIDE { return true; }
4147
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004148 bool CanThrow() const OVERRIDE { return true; }
4149
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004150 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004151
4152 DECLARE_INSTRUCTION(Throw);
4153
4154 private:
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004155 const uint32_t dex_pc_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004156
4157 DISALLOW_COPY_AND_ASSIGN(HThrow);
4158};
4159
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004160class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004161 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004162 HInstanceOf(HInstruction* object,
4163 HLoadClass* constant,
4164 bool class_is_final,
4165 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004166 : HExpression(Primitive::kPrimBoolean, SideEffects::None()),
4167 class_is_final_(class_is_final),
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004168 must_do_null_check_(true),
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004169 dex_pc_(dex_pc) {
4170 SetRawInputAt(0, object);
4171 SetRawInputAt(1, constant);
4172 }
4173
4174 bool CanBeMoved() const OVERRIDE { return true; }
4175
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004176 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004177 return true;
4178 }
4179
4180 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004181 return false;
4182 }
4183
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004184 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004185
4186 bool IsClassFinal() const { return class_is_final_; }
4187
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004188 // Used only in code generation.
4189 bool MustDoNullCheck() const { return must_do_null_check_; }
4190 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
4191
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004192 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004193
4194 private:
4195 const bool class_is_final_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004196 bool must_do_null_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004197 const uint32_t dex_pc_;
4198
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004199 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
4200};
4201
Calin Juravleb1498f62015-02-16 13:13:29 +00004202class HBoundType : public HExpression<1> {
4203 public:
Calin Juravle9b0096b2015-07-22 17:14:32 +00004204 HBoundType(HInstruction* input, ReferenceTypeInfo bound_type)
Calin Juravleb1498f62015-02-16 13:13:29 +00004205 : HExpression(Primitive::kPrimNot, SideEffects::None()),
Calin Juravle9b0096b2015-07-22 17:14:32 +00004206 bound_type_(bound_type) {
Calin Juravle61d544b2015-02-23 16:46:57 +00004207 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00004208 SetRawInputAt(0, input);
4209 }
4210
Calin Juravle9b0096b2015-07-22 17:14:32 +00004211 const ReferenceTypeInfo& GetBoundType() const { return bound_type_; }
Calin Juravleb1498f62015-02-16 13:13:29 +00004212
Calin Juravle9b0096b2015-07-22 17:14:32 +00004213 bool CanBeNull() const OVERRIDE {
4214 // `null instanceof ClassX` always return false so we can't be null.
4215 return false;
Calin Juravleb1498f62015-02-16 13:13:29 +00004216 }
4217
4218 DECLARE_INSTRUCTION(BoundType);
4219
4220 private:
4221 // Encodes the most upper class that this instruction can have. In other words
Calin Juravle9b0096b2015-07-22 17:14:32 +00004222 // it is always the case that GetBoundType().IsSupertypeOf(GetReferenceType()).
4223 // It is used to bound the type in cases like `if (x instanceof ClassX) {}`
4224 const ReferenceTypeInfo bound_type_;
Calin Juravleb1498f62015-02-16 13:13:29 +00004225
4226 DISALLOW_COPY_AND_ASSIGN(HBoundType);
4227};
4228
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004229class HCheckCast : public HTemplateInstruction<2> {
4230 public:
4231 HCheckCast(HInstruction* object,
4232 HLoadClass* constant,
4233 bool class_is_final,
4234 uint32_t dex_pc)
4235 : HTemplateInstruction(SideEffects::None()),
4236 class_is_final_(class_is_final),
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004237 must_do_null_check_(true),
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004238 dex_pc_(dex_pc) {
4239 SetRawInputAt(0, object);
4240 SetRawInputAt(1, constant);
4241 }
4242
4243 bool CanBeMoved() const OVERRIDE { return true; }
4244
4245 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
4246 return true;
4247 }
4248
4249 bool NeedsEnvironment() const OVERRIDE {
4250 // Instruction may throw a CheckCastError.
4251 return true;
4252 }
4253
4254 bool CanThrow() const OVERRIDE { return true; }
4255
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004256 bool MustDoNullCheck() const { return must_do_null_check_; }
4257 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
4258
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004259 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004260
4261 bool IsClassFinal() const { return class_is_final_; }
4262
4263 DECLARE_INSTRUCTION(CheckCast);
4264
4265 private:
4266 const bool class_is_final_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004267 bool must_do_null_check_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004268 const uint32_t dex_pc_;
4269
4270 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004271};
4272
Calin Juravle27df7582015-04-17 19:12:31 +01004273class HMemoryBarrier : public HTemplateInstruction<0> {
4274 public:
4275 explicit HMemoryBarrier(MemBarrierKind barrier_kind)
Aart Bik34c3ba92015-07-20 14:08:59 -07004276 : HTemplateInstruction(
4277 SideEffects::All()), // assume write/read on all fields/arrays
Calin Juravle27df7582015-04-17 19:12:31 +01004278 barrier_kind_(barrier_kind) {}
4279
4280 MemBarrierKind GetBarrierKind() { return barrier_kind_; }
4281
4282 DECLARE_INSTRUCTION(MemoryBarrier);
4283
4284 private:
4285 const MemBarrierKind barrier_kind_;
4286
4287 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
4288};
4289
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004290class HMonitorOperation : public HTemplateInstruction<1> {
4291 public:
4292 enum OperationKind {
4293 kEnter,
4294 kExit,
4295 };
4296
4297 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07004298 : HTemplateInstruction(SideEffects::All()), // assume write/read on all fields/arrays
4299 kind_(kind), dex_pc_(dex_pc) {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004300 SetRawInputAt(0, object);
4301 }
4302
4303 // Instruction may throw a Java exception, so we need an environment.
David Brazdilbff75032015-07-08 17:26:51 +00004304 bool NeedsEnvironment() const OVERRIDE { return CanThrow(); }
4305
4306 bool CanThrow() const OVERRIDE {
4307 // Verifier guarantees that monitor-exit cannot throw.
4308 // This is important because it allows the HGraphBuilder to remove
4309 // a dead throw-catch loop generated for `synchronized` blocks/methods.
4310 return IsEnter();
4311 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004312
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004313 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004314
4315 bool IsEnter() const { return kind_ == kEnter; }
4316
4317 DECLARE_INSTRUCTION(MonitorOperation);
4318
Calin Juravle52c48962014-12-16 17:02:57 +00004319 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004320 const OperationKind kind_;
4321 const uint32_t dex_pc_;
4322
4323 private:
4324 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
4325};
4326
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01004327/**
4328 * A HInstruction used as a marker for the replacement of new + <init>
4329 * of a String to a call to a StringFactory. Only baseline will see
4330 * the node at code generation, where it will be be treated as null.
4331 * When compiling non-baseline, `HFakeString` instructions are being removed
4332 * in the instruction simplifier.
4333 */
4334class HFakeString : public HTemplateInstruction<0> {
4335 public:
4336 HFakeString() : HTemplateInstruction(SideEffects::None()) {}
4337
4338 Primitive::Type GetType() const OVERRIDE { return Primitive::kPrimNot; }
4339
4340 DECLARE_INSTRUCTION(FakeString);
4341
4342 private:
4343 DISALLOW_COPY_AND_ASSIGN(HFakeString);
4344};
4345
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004346class MoveOperands : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004347 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01004348 MoveOperands(Location source,
4349 Location destination,
4350 Primitive::Type type,
4351 HInstruction* instruction)
4352 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004353
4354 Location GetSource() const { return source_; }
4355 Location GetDestination() const { return destination_; }
4356
4357 void SetSource(Location value) { source_ = value; }
4358 void SetDestination(Location value) { destination_ = value; }
4359
4360 // The parallel move resolver marks moves as "in-progress" by clearing the
4361 // destination (but not the source).
4362 Location MarkPending() {
4363 DCHECK(!IsPending());
4364 Location dest = destination_;
4365 destination_ = Location::NoLocation();
4366 return dest;
4367 }
4368
4369 void ClearPending(Location dest) {
4370 DCHECK(IsPending());
4371 destination_ = dest;
4372 }
4373
4374 bool IsPending() const {
4375 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
4376 return destination_.IsInvalid() && !source_.IsInvalid();
4377 }
4378
4379 // True if this blocks a move from the given location.
4380 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08004381 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004382 }
4383
4384 // A move is redundant if it's been eliminated, if its source and
4385 // destination are the same, or if its destination is unneeded.
4386 bool IsRedundant() const {
4387 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
4388 }
4389
4390 // We clear both operands to indicate move that's been eliminated.
4391 void Eliminate() {
4392 source_ = destination_ = Location::NoLocation();
4393 }
4394
4395 bool IsEliminated() const {
4396 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
4397 return source_.IsInvalid();
4398 }
4399
Alexey Frunze4dda3372015-06-01 18:31:49 -07004400 Primitive::Type GetType() const { return type_; }
4401
Nicolas Geoffray90218252015-04-15 11:56:51 +01004402 bool Is64BitMove() const {
4403 return Primitive::Is64BitType(type_);
4404 }
4405
Nicolas Geoffray740475d2014-09-29 10:33:25 +01004406 HInstruction* GetInstruction() const { return instruction_; }
4407
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004408 private:
4409 Location source_;
4410 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01004411 // The type this move is for.
4412 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01004413 // The instruction this move is assocatied with. Null when this move is
4414 // for moving an input in the expected locations of user (including a phi user).
4415 // This is only used in debug mode, to ensure we do not connect interval siblings
4416 // in the same parallel move.
4417 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004418};
4419
4420static constexpr size_t kDefaultNumberOfMoves = 4;
4421
4422class HParallelMove : public HTemplateInstruction<0> {
4423 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004424 explicit HParallelMove(ArenaAllocator* arena)
4425 : HTemplateInstruction(SideEffects::None()), moves_(arena, kDefaultNumberOfMoves) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004426
Nicolas Geoffray90218252015-04-15 11:56:51 +01004427 void AddMove(Location source,
4428 Location destination,
4429 Primitive::Type type,
4430 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004431 DCHECK(source.IsValid());
4432 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004433 if (kIsDebugBuild) {
4434 if (instruction != nullptr) {
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00004435 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004436 if (moves_.Get(i).GetInstruction() == instruction) {
4437 // Special case the situation where the move is for the spill slot
4438 // of the instruction.
4439 if ((GetPrevious() == instruction)
4440 || ((GetPrevious() == nullptr)
4441 && instruction->IsPhi()
4442 && instruction->GetBlock() == GetBlock())) {
4443 DCHECK_NE(destination.GetKind(), moves_.Get(i).GetDestination().GetKind())
4444 << "Doing parallel moves for the same instruction.";
4445 } else {
4446 DCHECK(false) << "Doing parallel moves for the same instruction.";
4447 }
4448 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00004449 }
4450 }
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004451 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Zheng Xuad4450e2015-04-17 18:48:56 +08004452 DCHECK(!destination.OverlapsWith(moves_.Get(i).GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01004453 << "Overlapped destination for two moves in a parallel move: "
4454 << moves_.Get(i).GetSource() << " ==> " << moves_.Get(i).GetDestination() << " and "
4455 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004456 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01004457 }
Nicolas Geoffray90218252015-04-15 11:56:51 +01004458 moves_.Add(MoveOperands(source, destination, type, instruction));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004459 }
4460
4461 MoveOperands* MoveOperandsAt(size_t index) const {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004462 return moves_.GetRawStorage() + index;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004463 }
4464
4465 size_t NumMoves() const { return moves_.Size(); }
4466
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004467 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004468
4469 private:
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004470 GrowableArray<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004471
4472 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
4473};
4474
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004475class HGraphVisitor : public ValueObject {
4476 public:
Dave Allison20dfc792014-06-16 20:44:29 -07004477 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
4478 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004479
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004480 virtual void VisitInstruction(HInstruction* instruction) { UNUSED(instruction); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004481 virtual void VisitBasicBlock(HBasicBlock* block);
4482
Roland Levillain633021e2014-10-01 14:12:25 +01004483 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004484 void VisitInsertionOrder();
4485
Roland Levillain633021e2014-10-01 14:12:25 +01004486 // Visit the graph following dominator tree reverse post-order.
4487 void VisitReversePostOrder();
4488
Nicolas Geoffray787c3072014-03-17 10:20:19 +00004489 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00004490
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004491 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004492#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004493 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
4494
4495 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
4496
4497#undef DECLARE_VISIT_INSTRUCTION
4498
4499 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07004500 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004501
4502 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
4503};
4504
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004505class HGraphDelegateVisitor : public HGraphVisitor {
4506 public:
4507 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
4508 virtual ~HGraphDelegateVisitor() {}
4509
4510 // Visit functions that delegate to to super class.
4511#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004512 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004513
4514 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
4515
4516#undef DECLARE_VISIT_INSTRUCTION
4517
4518 private:
4519 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
4520};
4521
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004522class HInsertionOrderIterator : public ValueObject {
4523 public:
4524 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
4525
4526 bool Done() const { return index_ == graph_.GetBlocks().Size(); }
4527 HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); }
4528 void Advance() { ++index_; }
4529
4530 private:
4531 const HGraph& graph_;
4532 size_t index_;
4533
4534 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
4535};
4536
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004537class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004538 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00004539 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
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
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004544 bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); }
4545 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); }
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(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004553};
4554
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004555class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004556 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004557 explicit HPostOrderIterator(const HGraph& graph)
David Brazdil10f56cb2015-03-24 18:49:14 +00004558 : graph_(graph), index_(graph_.GetReversePostOrder().Size()) {
4559 // Check that reverse post order of the graph has been built.
4560 DCHECK(!graph.GetReversePostOrder().IsEmpty());
4561 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004562
4563 bool Done() const { return index_ == 0; }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004564 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004565 void Advance() { --index_; }
4566
4567 private:
4568 const HGraph& graph_;
4569 size_t index_;
4570
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004571 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004572};
4573
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01004574class HLinearPostOrderIterator : public ValueObject {
4575 public:
4576 explicit HLinearPostOrderIterator(const HGraph& graph)
4577 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().Size()) {}
4578
4579 bool Done() const { return index_ == 0; }
4580
4581 HBasicBlock* Current() const { return order_.Get(index_ -1); }
4582
4583 void Advance() {
4584 --index_;
4585 DCHECK_GE(index_, 0U);
4586 }
4587
4588 private:
4589 const GrowableArray<HBasicBlock*>& order_;
4590 size_t index_;
4591
4592 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
4593};
4594
4595class HLinearOrderIterator : public ValueObject {
4596 public:
4597 explicit HLinearOrderIterator(const HGraph& graph)
4598 : order_(graph.GetLinearOrder()), index_(0) {}
4599
4600 bool Done() const { return index_ == order_.Size(); }
4601 HBasicBlock* Current() const { return order_.Get(index_); }
4602 void Advance() { ++index_; }
4603
4604 private:
4605 const GrowableArray<HBasicBlock*>& order_;
4606 size_t index_;
4607
4608 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
4609};
4610
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004611// Iterator over the blocks that art part of the loop. Includes blocks part
4612// of an inner loop. The order in which the blocks are iterated is on their
4613// block id.
4614class HBlocksInLoopIterator : public ValueObject {
4615 public:
4616 explicit HBlocksInLoopIterator(const HLoopInformation& info)
4617 : blocks_in_loop_(info.GetBlocks()),
4618 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
4619 index_(0) {
4620 if (!blocks_in_loop_.IsBitSet(index_)) {
4621 Advance();
4622 }
4623 }
4624
4625 bool Done() const { return index_ == blocks_.Size(); }
4626 HBasicBlock* Current() const { return blocks_.Get(index_); }
4627 void Advance() {
4628 ++index_;
4629 for (size_t e = blocks_.Size(); index_ < e; ++index_) {
4630 if (blocks_in_loop_.IsBitSet(index_)) {
4631 break;
4632 }
4633 }
4634 }
4635
4636 private:
4637 const BitVector& blocks_in_loop_;
4638 const GrowableArray<HBasicBlock*>& blocks_;
4639 size_t index_;
4640
4641 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
4642};
4643
Mingyao Yang3584bce2015-05-19 16:01:59 -07004644// Iterator over the blocks that art part of the loop. Includes blocks part
4645// of an inner loop. The order in which the blocks are iterated is reverse
4646// post order.
4647class HBlocksInLoopReversePostOrderIterator : public ValueObject {
4648 public:
4649 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
4650 : blocks_in_loop_(info.GetBlocks()),
4651 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
4652 index_(0) {
4653 if (!blocks_in_loop_.IsBitSet(blocks_.Get(index_)->GetBlockId())) {
4654 Advance();
4655 }
4656 }
4657
4658 bool Done() const { return index_ == blocks_.Size(); }
4659 HBasicBlock* Current() const { return blocks_.Get(index_); }
4660 void Advance() {
4661 ++index_;
4662 for (size_t e = blocks_.Size(); index_ < e; ++index_) {
4663 if (blocks_in_loop_.IsBitSet(blocks_.Get(index_)->GetBlockId())) {
4664 break;
4665 }
4666 }
4667 }
4668
4669 private:
4670 const BitVector& blocks_in_loop_;
4671 const GrowableArray<HBasicBlock*>& blocks_;
4672 size_t index_;
4673
4674 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
4675};
4676
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00004677inline int64_t Int64FromConstant(HConstant* constant) {
4678 DCHECK(constant->IsIntConstant() || constant->IsLongConstant());
4679 return constant->IsIntConstant() ? constant->AsIntConstant()->GetValue()
4680 : constant->AsLongConstant()->GetValue();
4681}
4682
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004683} // namespace art
4684
4685#endif // ART_COMPILER_OPTIMIZING_NODES_H_