blob: 5c5cecc3d5196dea8a57339f3ad0993b1b3c9f0d [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.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00001599 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001600 virtual bool CanBeNull() const {
1601 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1602 return true;
1603 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001604
Calin Juravle641547a2015-04-21 22:08:51 +01001605 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj) const {
1606 UNUSED(obj);
1607 return false;
1608 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001609
Calin Juravle7733bd62015-07-22 17:14:50 +00001610 void SetReferenceTypeInfo(ReferenceTypeInfo reference_type_info) {
1611 DCHECK_EQ(GetType(), Primitive::kPrimNot);
1612 reference_type_info_ = reference_type_info;
1613 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001614
Calin Juravle61d544b2015-02-23 16:46:57 +00001615 ReferenceTypeInfo GetReferenceTypeInfo() const {
1616 DCHECK_EQ(GetType(), Primitive::kPrimNot);
1617 return reference_type_info_;
1618 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001619
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001620 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001621 DCHECK(user != nullptr);
1622 HUseListNode<HInstruction*>* use =
1623 uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1624 user->SetRawInputRecordAt(index, HUserRecord<HInstruction*>(user->InputRecordAt(index), use));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001625 }
1626
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001627 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001628 DCHECK(user != nullptr);
David Brazdiled596192015-01-23 10:39:45 +00001629 HUseListNode<HEnvironment*>* env_use =
1630 env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1631 user->RecordEnvUse(env_use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001632 }
1633
David Brazdil1abb4192015-02-17 18:33:36 +00001634 void RemoveAsUserOfInput(size_t input) {
1635 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
1636 input_use.GetInstruction()->uses_.Remove(input_use.GetUseNode());
1637 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001638
David Brazdil1abb4192015-02-17 18:33:36 +00001639 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1640 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001641
David Brazdiled596192015-01-23 10:39:45 +00001642 bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); }
1643 bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); }
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001644 bool HasNonEnvironmentUses() const { return !uses_.IsEmpty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001645 bool HasOnlyOneNonEnvironmentUse() const {
1646 return !HasEnvironmentUses() && GetUses().HasOnlyOneUse();
1647 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001648
Roland Levillain6c82d402014-10-13 16:10:27 +01001649 // Does this instruction strictly dominate `other_instruction`?
1650 // Returns false if this instruction and `other_instruction` are the same.
1651 // Aborts if this instruction and `other_instruction` are both phis.
1652 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001653
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001654 int GetId() const { return id_; }
1655 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001656
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001657 int GetSsaIndex() const { return ssa_index_; }
1658 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1659 bool HasSsaIndex() const { return ssa_index_ != -1; }
1660
1661 bool HasEnvironment() const { return environment_ != nullptr; }
1662 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001663 // Set the `environment_` field. Raw because this method does not
1664 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001665 void SetRawEnvironment(HEnvironment* environment) {
1666 DCHECK(environment_ == nullptr);
1667 DCHECK_EQ(environment->GetHolder(), this);
1668 environment_ = environment;
1669 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001670
1671 // Set the environment of this instruction, copying it from `environment`. While
1672 // copying, the uses lists are being updated.
1673 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001674 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001675 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001676 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001677 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001678 if (environment->GetParent() != nullptr) {
1679 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1680 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001681 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001682
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001683 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
1684 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001685 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001686 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001687 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001688 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001689 if (environment->GetParent() != nullptr) {
1690 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1691 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001692 }
1693
Nicolas Geoffray39468442014-09-02 15:17:15 +01001694 // Returns the number of entries in the environment. Typically, that is the
1695 // number of dex registers in a method. It could be more in case of inlining.
1696 size_t EnvironmentSize() const;
1697
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001698 LocationSummary* GetLocations() const { return locations_; }
1699 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001700
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001701 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001702 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001703
Alexandre Rames188d4312015-04-09 18:30:21 +01001704 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
1705 // uses of this instruction by `other` are *not* updated.
1706 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
1707 ReplaceWith(other);
1708 other->ReplaceInput(this, use_index);
1709 }
1710
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001711 // Move `this` instruction before `cursor`.
1712 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001713
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001714#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01001715 bool Is##type() const { return (As##type() != nullptr); } \
1716 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001717 virtual H##type* As##type() { return nullptr; }
1718
1719 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
1720#undef INSTRUCTION_TYPE_CHECK
1721
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001722 // Returns whether the instruction can be moved within the graph.
1723 virtual bool CanBeMoved() const { return false; }
1724
1725 // Returns whether the two instructions are of the same kind.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001726 virtual bool InstructionTypeEquals(HInstruction* other) const {
1727 UNUSED(other);
1728 return false;
1729 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001730
1731 // Returns whether any data encoded in the two instructions is equal.
1732 // This method does not look at the inputs. Both instructions must be
1733 // of the same type, otherwise the method has undefined behavior.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001734 virtual bool InstructionDataEquals(HInstruction* other) const {
1735 UNUSED(other);
1736 return false;
1737 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001738
1739 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00001740 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001741 // 2) Their inputs are identical.
1742 bool Equals(HInstruction* other) const;
1743
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001744 virtual InstructionKind GetKind() const = 0;
1745
1746 virtual size_t ComputeHashCode() const {
1747 size_t result = GetKind();
1748 for (size_t i = 0, e = InputCount(); i < e; ++i) {
1749 result = (result * 31) + InputAt(i)->GetId();
1750 }
1751 return result;
1752 }
1753
1754 SideEffects GetSideEffects() const { return side_effects_; }
1755
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001756 size_t GetLifetimePosition() const { return lifetime_position_; }
1757 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
1758 LiveInterval* GetLiveInterval() const { return live_interval_; }
1759 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
1760 bool HasLiveInterval() const { return live_interval_ != nullptr; }
1761
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001762 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
1763
1764 // Returns whether the code generation of the instruction will require to have access
1765 // to the current method. Such instructions are:
1766 // (1): Instructions that require an environment, as calling the runtime requires
1767 // to walk the stack and have the current method stored at a specific stack address.
1768 // (2): Object literals like classes and strings, that are loaded from the dex cache
1769 // fields of the current method.
1770 bool NeedsCurrentMethod() const {
1771 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
1772 }
1773
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001774 virtual bool NeedsDexCache() const { return false; }
1775
Mark Mendellc4701932015-04-10 13:18:51 -04001776 // Does this instruction have any use in an environment before
1777 // control flow hits 'other'?
1778 bool HasAnyEnvironmentUseBefore(HInstruction* other);
1779
1780 // Remove all references to environment uses of this instruction.
1781 // The caller must ensure that this is safe to do.
1782 void RemoveEnvironmentUsers();
1783
David Brazdil1abb4192015-02-17 18:33:36 +00001784 protected:
1785 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
1786 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
1787
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001788 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001789 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); }
1790
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001791 HInstruction* previous_;
1792 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001793 HBasicBlock* block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001794
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001795 // An instruction gets an id when it is added to the graph.
1796 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01001797 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001798 int id_;
1799
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001800 // When doing liveness analysis, instructions that have uses get an SSA index.
1801 int ssa_index_;
1802
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001803 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00001804 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001805
1806 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00001807 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001808
Nicolas Geoffray39468442014-09-02 15:17:15 +01001809 // The environment associated with this instruction. Not null if the instruction
1810 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001811 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001812
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001813 // Set by the code generator.
1814 LocationSummary* locations_;
1815
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001816 // Set by the liveness analysis.
1817 LiveInterval* live_interval_;
1818
1819 // Set by the liveness analysis, this is the position in a linear
1820 // order of blocks where this instruction's live interval start.
1821 size_t lifetime_position_;
1822
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001823 const SideEffects side_effects_;
1824
Calin Juravleacf735c2015-02-12 15:25:22 +00001825 // TODO: for primitive types this should be marked as invalid.
1826 ReferenceTypeInfo reference_type_info_;
1827
David Brazdil1abb4192015-02-17 18:33:36 +00001828 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001829 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00001830 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001831 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001832 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001833
1834 DISALLOW_COPY_AND_ASSIGN(HInstruction);
1835};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001836std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001837
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001838class HInputIterator : public ValueObject {
1839 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001840 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001841
1842 bool Done() const { return index_ == instruction_->InputCount(); }
1843 HInstruction* Current() const { return instruction_->InputAt(index_); }
1844 void Advance() { index_++; }
1845
1846 private:
1847 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001848 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001849
1850 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
1851};
1852
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001853class HInstructionIterator : public ValueObject {
1854 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001855 explicit HInstructionIterator(const HInstructionList& instructions)
1856 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001857 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001858 }
1859
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001860 bool Done() const { return instruction_ == nullptr; }
1861 HInstruction* Current() const { return instruction_; }
1862 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001863 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001864 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001865 }
1866
1867 private:
1868 HInstruction* instruction_;
1869 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001870
1871 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001872};
1873
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001874class HBackwardInstructionIterator : public ValueObject {
1875 public:
1876 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
1877 : instruction_(instructions.last_instruction_) {
1878 next_ = Done() ? nullptr : instruction_->GetPrevious();
1879 }
1880
1881 bool Done() const { return instruction_ == nullptr; }
1882 HInstruction* Current() const { return instruction_; }
1883 void Advance() {
1884 instruction_ = next_;
1885 next_ = Done() ? nullptr : instruction_->GetPrevious();
1886 }
1887
1888 private:
1889 HInstruction* instruction_;
1890 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001891
1892 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001893};
1894
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001895// An embedded container with N elements of type T. Used (with partial
1896// specialization for N=0) because embedded arrays cannot have size 0.
1897template<typename T, intptr_t N>
1898class EmbeddedArray {
1899 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001900 EmbeddedArray() : elements_() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001901
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001902 intptr_t GetLength() const { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001903
1904 const T& operator[](intptr_t i) const {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001905 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001906 return elements_[i];
1907 }
1908
1909 T& operator[](intptr_t i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001910 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001911 return elements_[i];
1912 }
1913
1914 const T& At(intptr_t i) const {
1915 return (*this)[i];
1916 }
1917
1918 void SetAt(intptr_t i, const T& val) {
1919 (*this)[i] = val;
1920 }
1921
1922 private:
1923 T elements_[N];
1924};
1925
1926template<typename T>
1927class EmbeddedArray<T, 0> {
1928 public:
1929 intptr_t length() const { return 0; }
1930 const T& operator[](intptr_t i) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001931 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001932 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001933 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001934 }
1935 T& operator[](intptr_t i) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001936 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001937 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001938 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001939 }
1940};
1941
1942template<intptr_t N>
1943class HTemplateInstruction: public HInstruction {
1944 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001945 HTemplateInstruction<N>(SideEffects side_effects)
1946 : HInstruction(side_effects), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07001947 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001948
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001949 size_t InputCount() const OVERRIDE { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001950
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001951 protected:
David Brazdil1abb4192015-02-17 18:33:36 +00001952 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_[i]; }
1953
1954 void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE {
1955 inputs_[i] = input;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001956 }
1957
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001958 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001959 EmbeddedArray<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001960
1961 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001962};
1963
Dave Allison20dfc792014-06-16 20:44:29 -07001964template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001965class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07001966 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001967 HExpression<N>(Primitive::Type type, SideEffects side_effects)
1968 : HTemplateInstruction<N>(side_effects), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07001969 virtual ~HExpression() {}
1970
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001971 Primitive::Type GetType() const OVERRIDE { return type_; }
Dave Allison20dfc792014-06-16 20:44:29 -07001972
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001973 protected:
1974 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07001975};
1976
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001977// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
1978// instruction that branches to the exit block.
1979class HReturnVoid : public HTemplateInstruction<0> {
1980 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001981 HReturnVoid() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001982
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001983 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001984
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001985 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001986
1987 private:
1988 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
1989};
1990
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001991// Represents dex's RETURN opcodes. A HReturn is a control flow
1992// instruction that branches to the exit block.
1993class HReturn : public HTemplateInstruction<1> {
1994 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001995 explicit HReturn(HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001996 SetRawInputAt(0, value);
1997 }
1998
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001999 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002000
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002001 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002002
2003 private:
2004 DISALLOW_COPY_AND_ASSIGN(HReturn);
2005};
2006
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002007// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002008// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002009// exit block.
2010class HExit : public HTemplateInstruction<0> {
2011 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002012 HExit() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002013
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002014 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002015
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002016 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002017
2018 private:
2019 DISALLOW_COPY_AND_ASSIGN(HExit);
2020};
2021
2022// Jumps from one block to another.
2023class HGoto : public HTemplateInstruction<0> {
2024 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002025 HGoto() : HTemplateInstruction(SideEffects::None()) {}
2026
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002027 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002028
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002029 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002030 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002031 }
2032
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002033 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002034
2035 private:
2036 DISALLOW_COPY_AND_ASSIGN(HGoto);
2037};
2038
Dave Allison20dfc792014-06-16 20:44:29 -07002039
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002040// Conditional branch. A block ending with an HIf instruction must have
2041// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002042class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002043 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002044 explicit HIf(HInstruction* input) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002045 SetRawInputAt(0, input);
2046 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002047
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002048 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002049
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002050 HBasicBlock* IfTrueSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002051 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002052 }
2053
2054 HBasicBlock* IfFalseSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002055 return GetBlock()->GetSuccessors().Get(1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002056 }
2057
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002058 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002059
2060 private:
2061 DISALLOW_COPY_AND_ASSIGN(HIf);
2062};
2063
David Brazdilfc6a86a2015-06-26 10:33:45 +00002064
2065// Abstract instruction which marks the beginning and/or end of a try block and
2066// links it to the respective exception handlers. Behaves the same as a Goto in
2067// non-exceptional control flow.
2068// Normal-flow successor is stored at index zero, exception handlers under
2069// higher indices in no particular order.
2070class HTryBoundary : public HTemplateInstruction<0> {
2071 public:
David Brazdil56e1acc2015-06-30 15:41:36 +01002072 enum BoundaryKind {
2073 kEntry,
2074 kExit,
2075 };
2076
2077 explicit HTryBoundary(BoundaryKind kind)
2078 : HTemplateInstruction(SideEffects::None()), kind_(kind) {}
David Brazdilfc6a86a2015-06-26 10:33:45 +00002079
2080 bool IsControlFlow() const OVERRIDE { return true; }
2081
2082 // Returns the block's non-exceptional successor (index zero).
2083 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors().Get(0); }
2084
2085 // Returns whether `handler` is among its exception handlers (non-zero index
2086 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002087 bool HasExceptionHandler(const HBasicBlock& handler) const {
2088 DCHECK(handler.IsCatchBlock());
2089 return GetBlock()->GetSuccessors().Contains(
2090 const_cast<HBasicBlock*>(&handler), /* start_from */ 1);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002091 }
2092
2093 // If not present already, adds `handler` to its block's list of exception
2094 // handlers.
2095 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01002096 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002097 GetBlock()->AddSuccessor(handler);
2098 }
2099 }
2100
David Brazdil56e1acc2015-06-30 15:41:36 +01002101 bool IsEntry() const { return kind_ == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002102
David Brazdilffee3d32015-07-06 11:48:53 +01002103 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
2104
David Brazdilfc6a86a2015-06-26 10:33:45 +00002105 DECLARE_INSTRUCTION(TryBoundary);
2106
2107 private:
David Brazdil56e1acc2015-06-30 15:41:36 +01002108 const BoundaryKind kind_;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002109
2110 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2111};
2112
David Brazdilffee3d32015-07-06 11:48:53 +01002113// Iterator over exception handlers of a given HTryBoundary, i.e. over
2114// exceptional successors of its basic block.
2115class HExceptionHandlerIterator : public ValueObject {
2116 public:
2117 explicit HExceptionHandlerIterator(const HTryBoundary& try_boundary)
2118 : block_(*try_boundary.GetBlock()), index_(block_.NumberOfNormalSuccessors()) {}
2119
2120 bool Done() const { return index_ == block_.GetSuccessors().Size(); }
2121 HBasicBlock* Current() const { return block_.GetSuccessors().Get(index_); }
2122 size_t CurrentSuccessorIndex() const { return index_; }
2123 void Advance() { ++index_; }
2124
2125 private:
2126 const HBasicBlock& block_;
2127 size_t index_;
2128
2129 DISALLOW_COPY_AND_ASSIGN(HExceptionHandlerIterator);
2130};
David Brazdilfc6a86a2015-06-26 10:33:45 +00002131
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002132// Deoptimize to interpreter, upon checking a condition.
2133class HDeoptimize : public HTemplateInstruction<1> {
2134 public:
2135 HDeoptimize(HInstruction* cond, uint32_t dex_pc)
2136 : HTemplateInstruction(SideEffects::None()),
2137 dex_pc_(dex_pc) {
2138 SetRawInputAt(0, cond);
2139 }
2140
2141 bool NeedsEnvironment() const OVERRIDE { return true; }
2142 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002143 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002144
2145 DECLARE_INSTRUCTION(Deoptimize);
2146
2147 private:
2148 uint32_t dex_pc_;
2149
2150 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
2151};
2152
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002153// Represents the ArtMethod that was passed as a first argument to
2154// the method. It is used by instructions that depend on it, like
2155// instructions that work with the dex cache.
2156class HCurrentMethod : public HExpression<0> {
2157 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -07002158 explicit HCurrentMethod(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002159
2160 DECLARE_INSTRUCTION(CurrentMethod);
2161
2162 private:
2163 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
2164};
2165
Roland Levillain88cb1752014-10-20 16:36:47 +01002166class HUnaryOperation : public HExpression<1> {
2167 public:
2168 HUnaryOperation(Primitive::Type result_type, HInstruction* input)
2169 : HExpression(result_type, SideEffects::None()) {
2170 SetRawInputAt(0, input);
2171 }
2172
2173 HInstruction* GetInput() const { return InputAt(0); }
2174 Primitive::Type GetResultType() const { return GetType(); }
2175
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002176 bool CanBeMoved() const OVERRIDE { return true; }
2177 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002178 UNUSED(other);
2179 return true;
2180 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002181
Roland Levillain9240d6a2014-10-20 16:47:04 +01002182 // Try to statically evaluate `operation` and return a HConstant
2183 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002184 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002185 HConstant* TryStaticEvaluation() const;
2186
2187 // Apply this operation to `x`.
2188 virtual int32_t Evaluate(int32_t x) const = 0;
2189 virtual int64_t Evaluate(int64_t x) const = 0;
2190
Roland Levillain88cb1752014-10-20 16:36:47 +01002191 DECLARE_INSTRUCTION(UnaryOperation);
2192
2193 private:
2194 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
2195};
2196
Dave Allison20dfc792014-06-16 20:44:29 -07002197class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002198 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002199 HBinaryOperation(Primitive::Type result_type,
2200 HInstruction* left,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002201 HInstruction* right) : HExpression(result_type, SideEffects::None()) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002202 SetRawInputAt(0, left);
2203 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002204 }
2205
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002206 HInstruction* GetLeft() const { return InputAt(0); }
2207 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07002208 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002209
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002210 virtual bool IsCommutative() const { return false; }
2211
2212 // Put constant on the right.
2213 // Returns whether order is changed.
2214 bool OrderInputsWithConstantOnTheRight() {
2215 HInstruction* left = InputAt(0);
2216 HInstruction* right = InputAt(1);
2217 if (left->IsConstant() && !right->IsConstant()) {
2218 ReplaceInput(right, 0);
2219 ReplaceInput(left, 1);
2220 return true;
2221 }
2222 return false;
2223 }
2224
2225 // Order inputs by instruction id, but favor constant on the right side.
2226 // This helps GVN for commutative ops.
2227 void OrderInputs() {
2228 DCHECK(IsCommutative());
2229 HInstruction* left = InputAt(0);
2230 HInstruction* right = InputAt(1);
2231 if (left == right || (!left->IsConstant() && right->IsConstant())) {
2232 return;
2233 }
2234 if (OrderInputsWithConstantOnTheRight()) {
2235 return;
2236 }
2237 // Order according to instruction id.
2238 if (left->GetId() > right->GetId()) {
2239 ReplaceInput(right, 0);
2240 ReplaceInput(left, 1);
2241 }
2242 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002243
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002244 bool CanBeMoved() const OVERRIDE { return true; }
2245 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002246 UNUSED(other);
2247 return true;
2248 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002249
Roland Levillain9240d6a2014-10-20 16:47:04 +01002250 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01002251 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002252 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002253 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01002254
2255 // Apply this operation to `x` and `y`.
2256 virtual int32_t Evaluate(int32_t x, int32_t y) const = 0;
2257 virtual int64_t Evaluate(int64_t x, int64_t y) const = 0;
2258
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002259 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002260 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002261 HConstant* GetConstantRight() const;
2262
2263 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002264 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002265 HInstruction* GetLeastConstantLeft() const;
2266
Roland Levillainccc07a92014-09-16 14:48:16 +01002267 DECLARE_INSTRUCTION(BinaryOperation);
2268
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002269 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002270 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
2271};
2272
Mark Mendellc4701932015-04-10 13:18:51 -04002273// The comparison bias applies for floating point operations and indicates how NaN
2274// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01002275enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04002276 kNoBias, // bias is not applicable (i.e. for long operation)
2277 kGtBias, // return 1 for NaN comparisons
2278 kLtBias, // return -1 for NaN comparisons
2279};
2280
Dave Allison20dfc792014-06-16 20:44:29 -07002281class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002282 public:
Dave Allison20dfc792014-06-16 20:44:29 -07002283 HCondition(HInstruction* first, HInstruction* second)
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002284 : HBinaryOperation(Primitive::kPrimBoolean, first, second),
Mark Mendellc4701932015-04-10 13:18:51 -04002285 needs_materialization_(true),
Roland Levillain4fa13f62015-07-06 18:11:54 +01002286 bias_(ComparisonBias::kNoBias) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002287
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002288 bool NeedsMaterialization() const { return needs_materialization_; }
2289 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002290
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002291 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002292 // `instruction`, and disregard moves in between.
2293 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002294
Dave Allison20dfc792014-06-16 20:44:29 -07002295 DECLARE_INSTRUCTION(Condition);
2296
2297 virtual IfCondition GetCondition() const = 0;
2298
Mark Mendellc4701932015-04-10 13:18:51 -04002299 virtual IfCondition GetOppositeCondition() const = 0;
2300
Roland Levillain4fa13f62015-07-06 18:11:54 +01002301 bool IsGtBias() const { return bias_ == ComparisonBias::kGtBias; }
Mark Mendellc4701932015-04-10 13:18:51 -04002302
2303 void SetBias(ComparisonBias bias) { bias_ = bias; }
2304
2305 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2306 return bias_ == other->AsCondition()->bias_;
2307 }
2308
Roland Levillain4fa13f62015-07-06 18:11:54 +01002309 bool IsFPConditionTrueIfNaN() const {
2310 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2311 IfCondition if_cond = GetCondition();
2312 return IsGtBias() ? ((if_cond == kCondGT) || (if_cond == kCondGE)) : (if_cond == kCondNE);
2313 }
2314
2315 bool IsFPConditionFalseIfNaN() const {
2316 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2317 IfCondition if_cond = GetCondition();
2318 return IsGtBias() ? ((if_cond == kCondLT) || (if_cond == kCondLE)) : (if_cond == kCondEQ);
2319 }
2320
Dave Allison20dfc792014-06-16 20:44:29 -07002321 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002322 // For register allocation purposes, returns whether this instruction needs to be
2323 // materialized (that is, not just be in the processor flags).
2324 bool needs_materialization_;
2325
Mark Mendellc4701932015-04-10 13:18:51 -04002326 // Needed if we merge a HCompare into a HCondition.
2327 ComparisonBias bias_;
2328
Dave Allison20dfc792014-06-16 20:44:29 -07002329 DISALLOW_COPY_AND_ASSIGN(HCondition);
2330};
2331
2332// Instruction to check if two inputs are equal to each other.
2333class HEqual : public HCondition {
2334 public:
2335 HEqual(HInstruction* first, HInstruction* second)
2336 : HCondition(first, second) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002337
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002338 bool IsCommutative() const OVERRIDE { return true; }
2339
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002340 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002341 return x == y ? 1 : 0;
2342 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002343 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002344 return x == y ? 1 : 0;
2345 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002346
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002347 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002348
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002349 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002350 return kCondEQ;
2351 }
2352
Mark Mendellc4701932015-04-10 13:18:51 -04002353 IfCondition GetOppositeCondition() const OVERRIDE {
2354 return kCondNE;
2355 }
2356
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002357 private:
2358 DISALLOW_COPY_AND_ASSIGN(HEqual);
2359};
2360
Dave Allison20dfc792014-06-16 20:44:29 -07002361class HNotEqual : public HCondition {
2362 public:
2363 HNotEqual(HInstruction* first, HInstruction* second)
2364 : HCondition(first, second) {}
2365
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002366 bool IsCommutative() const OVERRIDE { return true; }
2367
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002368 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002369 return x != y ? 1 : 0;
2370 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002371 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002372 return x != y ? 1 : 0;
2373 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002374
Dave Allison20dfc792014-06-16 20:44:29 -07002375 DECLARE_INSTRUCTION(NotEqual);
2376
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002377 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002378 return kCondNE;
2379 }
2380
Mark Mendellc4701932015-04-10 13:18:51 -04002381 IfCondition GetOppositeCondition() const OVERRIDE {
2382 return kCondEQ;
2383 }
2384
Dave Allison20dfc792014-06-16 20:44:29 -07002385 private:
2386 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
2387};
2388
2389class HLessThan : public HCondition {
2390 public:
2391 HLessThan(HInstruction* first, HInstruction* second)
2392 : HCondition(first, second) {}
2393
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002394 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002395 return x < y ? 1 : 0;
2396 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002397 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002398 return x < y ? 1 : 0;
2399 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002400
Dave Allison20dfc792014-06-16 20:44:29 -07002401 DECLARE_INSTRUCTION(LessThan);
2402
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002403 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002404 return kCondLT;
2405 }
2406
Mark Mendellc4701932015-04-10 13:18:51 -04002407 IfCondition GetOppositeCondition() const OVERRIDE {
2408 return kCondGE;
2409 }
2410
Dave Allison20dfc792014-06-16 20:44:29 -07002411 private:
2412 DISALLOW_COPY_AND_ASSIGN(HLessThan);
2413};
2414
2415class HLessThanOrEqual : public HCondition {
2416 public:
2417 HLessThanOrEqual(HInstruction* first, HInstruction* second)
2418 : HCondition(first, second) {}
2419
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002420 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002421 return x <= y ? 1 : 0;
2422 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002423 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002424 return x <= y ? 1 : 0;
2425 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002426
Dave Allison20dfc792014-06-16 20:44:29 -07002427 DECLARE_INSTRUCTION(LessThanOrEqual);
2428
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002429 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002430 return kCondLE;
2431 }
2432
Mark Mendellc4701932015-04-10 13:18:51 -04002433 IfCondition GetOppositeCondition() const OVERRIDE {
2434 return kCondGT;
2435 }
2436
Dave Allison20dfc792014-06-16 20:44:29 -07002437 private:
2438 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
2439};
2440
2441class HGreaterThan : public HCondition {
2442 public:
2443 HGreaterThan(HInstruction* first, HInstruction* second)
2444 : HCondition(first, second) {}
2445
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002446 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002447 return x > y ? 1 : 0;
2448 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002449 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002450 return x > y ? 1 : 0;
2451 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002452
Dave Allison20dfc792014-06-16 20:44:29 -07002453 DECLARE_INSTRUCTION(GreaterThan);
2454
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002455 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002456 return kCondGT;
2457 }
2458
Mark Mendellc4701932015-04-10 13:18:51 -04002459 IfCondition GetOppositeCondition() const OVERRIDE {
2460 return kCondLE;
2461 }
2462
Dave Allison20dfc792014-06-16 20:44:29 -07002463 private:
2464 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
2465};
2466
2467class HGreaterThanOrEqual : public HCondition {
2468 public:
2469 HGreaterThanOrEqual(HInstruction* first, HInstruction* second)
2470 : HCondition(first, second) {}
2471
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002472 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002473 return x >= y ? 1 : 0;
2474 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002475 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002476 return x >= y ? 1 : 0;
2477 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002478
Dave Allison20dfc792014-06-16 20:44:29 -07002479 DECLARE_INSTRUCTION(GreaterThanOrEqual);
2480
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002481 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002482 return kCondGE;
2483 }
2484
Mark Mendellc4701932015-04-10 13:18:51 -04002485 IfCondition GetOppositeCondition() const OVERRIDE {
2486 return kCondLT;
2487 }
2488
Dave Allison20dfc792014-06-16 20:44:29 -07002489 private:
2490 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
2491};
2492
2493
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002494// Instruction to check how two inputs compare to each other.
2495// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
2496class HCompare : public HBinaryOperation {
2497 public:
Alexey Frunze4dda3372015-06-01 18:31:49 -07002498 HCompare(Primitive::Type type,
2499 HInstruction* first,
2500 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04002501 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07002502 uint32_t dex_pc)
2503 : HBinaryOperation(Primitive::kPrimInt, first, second), bias_(bias), dex_pc_(dex_pc) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002504 DCHECK_EQ(type, first->GetType());
2505 DCHECK_EQ(type, second->GetType());
2506 }
2507
Calin Juravleddb7df22014-11-25 20:56:51 +00002508 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01002509 return
2510 x == y ? 0 :
2511 x > y ? 1 :
2512 -1;
2513 }
Calin Juravleddb7df22014-11-25 20:56:51 +00002514
2515 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01002516 return
2517 x == y ? 0 :
2518 x > y ? 1 :
2519 -1;
2520 }
2521
Calin Juravleddb7df22014-11-25 20:56:51 +00002522 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2523 return bias_ == other->AsCompare()->bias_;
2524 }
2525
Mark Mendellc4701932015-04-10 13:18:51 -04002526 ComparisonBias GetBias() const { return bias_; }
2527
Roland Levillain4fa13f62015-07-06 18:11:54 +01002528 bool IsGtBias() { return bias_ == ComparisonBias::kGtBias; }
Calin Juravleddb7df22014-11-25 20:56:51 +00002529
Alexey Frunze4dda3372015-06-01 18:31:49 -07002530 uint32_t GetDexPc() const { return dex_pc_; }
2531
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002532 DECLARE_INSTRUCTION(Compare);
2533
2534 private:
Mark Mendellc4701932015-04-10 13:18:51 -04002535 const ComparisonBias bias_;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002536 const uint32_t dex_pc_;
Calin Juravleddb7df22014-11-25 20:56:51 +00002537
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002538 DISALLOW_COPY_AND_ASSIGN(HCompare);
2539};
2540
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002541// A local in the graph. Corresponds to a Dex register.
2542class HLocal : public HTemplateInstruction<0> {
2543 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002544 explicit HLocal(uint16_t reg_number)
2545 : HTemplateInstruction(SideEffects::None()), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002546
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002547 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002548
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002549 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002550
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002551 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002552 // The Dex register number.
2553 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002554
2555 DISALLOW_COPY_AND_ASSIGN(HLocal);
2556};
2557
2558// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07002559class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002560 public:
Roland Levillain5799fc02014-09-25 12:15:20 +01002561 HLoadLocal(HLocal* local, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002562 : HExpression(type, SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002563 SetRawInputAt(0, local);
2564 }
2565
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002566 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
2567
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002568 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002569
2570 private:
2571 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
2572};
2573
2574// Store a value in a given local. This instruction has two inputs: the value
2575// and the local.
2576class HStoreLocal : public HTemplateInstruction<2> {
2577 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002578 HStoreLocal(HLocal* local, HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002579 SetRawInputAt(0, local);
2580 SetRawInputAt(1, value);
2581 }
2582
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002583 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
2584
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002585 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002586
2587 private:
2588 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
2589};
2590
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002591class HConstant : public HExpression<0> {
2592 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002593 explicit HConstant(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
2594
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002595 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002596
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002597 virtual bool IsMinusOne() const { return false; }
2598 virtual bool IsZero() const { return false; }
2599 virtual bool IsOne() const { return false; }
2600
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002601 DECLARE_INSTRUCTION(Constant);
2602
2603 private:
2604 DISALLOW_COPY_AND_ASSIGN(HConstant);
2605};
2606
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002607class HFloatConstant : public HConstant {
2608 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002609 float GetValue() const { return value_; }
2610
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002611 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002612 return bit_cast<uint32_t, float>(other->AsFloatConstant()->value_) ==
2613 bit_cast<uint32_t, float>(value_);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002614 }
2615
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002616 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002617
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002618 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002619 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002620 }
2621 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002622 return value_ == 0.0f;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002623 }
2624 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002625 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
2626 }
2627 bool IsNaN() const {
2628 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002629 }
2630
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002631 DECLARE_INSTRUCTION(FloatConstant);
2632
2633 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002634 explicit HFloatConstant(float value) : HConstant(Primitive::kPrimFloat), value_(value) {}
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002635 explicit HFloatConstant(int32_t value)
2636 : HConstant(Primitive::kPrimFloat), value_(bit_cast<float, int32_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00002637
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002638 const float value_;
2639
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002640 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00002641 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002642 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002643 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2644};
2645
2646class HDoubleConstant : public HConstant {
2647 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002648 double GetValue() const { return value_; }
2649
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002650 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002651 return bit_cast<uint64_t, double>(other->AsDoubleConstant()->value_) ==
2652 bit_cast<uint64_t, double>(value_);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002653 }
2654
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002655 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002656
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002657 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002658 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002659 }
2660 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002661 return value_ == 0.0;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002662 }
2663 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002664 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
2665 }
2666 bool IsNaN() const {
2667 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002668 }
2669
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002670 DECLARE_INSTRUCTION(DoubleConstant);
2671
2672 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002673 explicit HDoubleConstant(double value) : HConstant(Primitive::kPrimDouble), value_(value) {}
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002674 explicit HDoubleConstant(int64_t value)
2675 : HConstant(Primitive::kPrimDouble), value_(bit_cast<double, int64_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00002676
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002677 const double value_;
2678
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002679 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00002680 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002681 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002682 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
2683};
2684
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002685class HNullConstant : public HConstant {
2686 public:
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002687 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2688 return true;
2689 }
2690
2691 size_t ComputeHashCode() const OVERRIDE { return 0; }
2692
2693 DECLARE_INSTRUCTION(NullConstant);
2694
2695 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002696 HNullConstant() : HConstant(Primitive::kPrimNot) {}
2697
2698 friend class HGraph;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002699 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2700};
2701
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002702// Constants of the type int. Those can be from Dex instructions, or
2703// synthesized (for example with the if-eqz instruction).
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002704class HIntConstant : public HConstant {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002705 public:
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002706 int32_t GetValue() const { return value_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002707
Calin Juravle61d544b2015-02-23 16:46:57 +00002708 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002709 return other->AsIntConstant()->value_ == value_;
2710 }
2711
Calin Juravle61d544b2015-02-23 16:46:57 +00002712 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2713
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002714 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2715 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2716 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2717
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002718 DECLARE_INSTRUCTION(IntConstant);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002719
2720 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002721 explicit HIntConstant(int32_t value) : HConstant(Primitive::kPrimInt), value_(value) {}
2722
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002723 const int32_t value_;
2724
David Brazdil8d5b8b22015-03-24 10:51:52 +00002725 friend class HGraph;
2726 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
Zheng Xuad4450e2015-04-17 18:48:56 +08002727 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002728 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2729};
2730
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002731class HLongConstant : public HConstant {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002732 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002733 int64_t GetValue() const { return value_; }
2734
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002735 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002736 return other->AsLongConstant()->value_ == value_;
2737 }
2738
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002739 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002740
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002741 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2742 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2743 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2744
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002745 DECLARE_INSTRUCTION(LongConstant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002746
2747 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002748 explicit HLongConstant(int64_t value) : HConstant(Primitive::kPrimLong), value_(value) {}
2749
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002750 const int64_t value_;
2751
David Brazdil8d5b8b22015-03-24 10:51:52 +00002752 friend class HGraph;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002753 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2754};
2755
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002756enum class Intrinsics {
2757#define OPTIMIZING_INTRINSICS(Name, IsStatic) k ## Name,
2758#include "intrinsics_list.h"
2759 kNone,
2760 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
2761#undef INTRINSICS_LIST
2762#undef OPTIMIZING_INTRINSICS
2763};
2764std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
2765
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002766class HInvoke : public HInstruction {
2767 public:
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002768 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002769
2770 // Runtime needs to walk the stack, so Dex -> Dex calls need to
2771 // know their environment.
Calin Juravle77520bc2015-01-12 18:45:46 +00002772 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002773
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01002774 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002775 SetRawInputAt(index, argument);
2776 }
2777
Roland Levillain3e3d7332015-04-28 11:00:54 +01002778 // Return the number of arguments. This number can be lower than
2779 // the number of inputs returned by InputCount(), as some invoke
2780 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
2781 // inputs at the end of their list of inputs.
2782 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
2783
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002784 Primitive::Type GetType() const OVERRIDE { return return_type_; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002785
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002786 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002787
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002788 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002789 const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002790
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002791 InvokeType GetOriginalInvokeType() const { return original_invoke_type_; }
2792
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01002793 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002794 return intrinsic_;
2795 }
2796
2797 void SetIntrinsic(Intrinsics intrinsic) {
2798 intrinsic_ = intrinsic;
2799 }
2800
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01002801 bool IsFromInlinedInvoke() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002802 return GetEnvironment()->GetParent() != nullptr;
2803 }
2804
2805 bool CanThrow() const OVERRIDE { return true; }
2806
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002807 DECLARE_INSTRUCTION(Invoke);
2808
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002809 protected:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002810 HInvoke(ArenaAllocator* arena,
2811 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01002812 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002813 Primitive::Type return_type,
2814 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002815 uint32_t dex_method_index,
2816 InvokeType original_invoke_type)
Aart Bik854a02b2015-07-14 16:07:00 -07002817 : HInstruction(SideEffects::All()), // assume write/read on all fields/arrays
Roland Levillain3e3d7332015-04-28 11:00:54 +01002818 number_of_arguments_(number_of_arguments),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002819 inputs_(arena, number_of_arguments),
2820 return_type_(return_type),
2821 dex_pc_(dex_pc),
2822 dex_method_index_(dex_method_index),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002823 original_invoke_type_(original_invoke_type),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002824 intrinsic_(Intrinsics::kNone) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01002825 uint32_t number_of_inputs = number_of_arguments + number_of_other_inputs;
2826 inputs_.SetSize(number_of_inputs);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002827 }
2828
David Brazdil1abb4192015-02-17 18:33:36 +00002829 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
2830 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
2831 inputs_.Put(index, input);
2832 }
2833
Roland Levillain3e3d7332015-04-28 11:00:54 +01002834 uint32_t number_of_arguments_;
David Brazdil1abb4192015-02-17 18:33:36 +00002835 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002836 const Primitive::Type return_type_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002837 const uint32_t dex_pc_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002838 const uint32_t dex_method_index_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002839 const InvokeType original_invoke_type_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002840 Intrinsics intrinsic_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002841
2842 private:
2843 DISALLOW_COPY_AND_ASSIGN(HInvoke);
2844};
2845
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002846class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002847 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01002848 // Requirements of this method call regarding the class
2849 // initialization (clinit) check of its declaring class.
2850 enum class ClinitCheckRequirement {
2851 kNone, // Class already initialized.
2852 kExplicit, // Static call having explicit clinit check as last input.
2853 kImplicit, // Static call implicitly requiring a clinit check.
2854 };
2855
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002856 HInvokeStaticOrDirect(ArenaAllocator* arena,
2857 uint32_t number_of_arguments,
2858 Primitive::Type return_type,
2859 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002860 uint32_t dex_method_index,
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002861 bool is_recursive,
Jeff Hao848f70a2014-01-15 13:49:50 -08002862 int32_t string_init_offset,
Nicolas Geoffray79041292015-03-26 10:05:54 +00002863 InvokeType original_invoke_type,
Roland Levillain4c0eb422015-04-24 16:43:49 +01002864 InvokeType invoke_type,
2865 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01002866 : HInvoke(arena,
2867 number_of_arguments,
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01002868 // There is one extra argument for the HCurrentMethod node, and
2869 // potentially one other if the clinit check is explicit, and one other
2870 // if the method is a string factory.
2871 1u + (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u)
2872 + (string_init_offset ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01002873 return_type,
2874 dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002875 dex_method_index,
2876 original_invoke_type),
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002877 invoke_type_(invoke_type),
Roland Levillain4c0eb422015-04-24 16:43:49 +01002878 is_recursive_(is_recursive),
Jeff Hao848f70a2014-01-15 13:49:50 -08002879 clinit_check_requirement_(clinit_check_requirement),
2880 string_init_offset_(string_init_offset) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002881
Calin Juravle641547a2015-04-21 22:08:51 +01002882 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
2883 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00002884 // We access the method via the dex cache so we can't do an implicit null check.
2885 // TODO: for intrinsics we can generate implicit null checks.
2886 return false;
2887 }
2888
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002889 InvokeType GetInvokeType() const { return invoke_type_; }
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002890 bool IsRecursive() const { return is_recursive_; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002891 bool NeedsDexCache() const OVERRIDE { return !IsRecursive(); }
Jeff Hao848f70a2014-01-15 13:49:50 -08002892 bool IsStringInit() const { return string_init_offset_ != 0; }
2893 int32_t GetStringInitOffset() const { return string_init_offset_; }
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002894 uint32_t GetCurrentMethodInputIndex() const { return GetNumberOfArguments(); }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002895
Roland Levillain4c0eb422015-04-24 16:43:49 +01002896 // Is this instruction a call to a static method?
2897 bool IsStatic() const {
2898 return GetInvokeType() == kStatic;
2899 }
2900
Roland Levillain3e3d7332015-04-28 11:00:54 +01002901 // Remove the art::HLoadClass instruction set as last input by
2902 // art::PrepareForRegisterAllocation::VisitClinitCheck in lieu of
2903 // the initial art::HClinitCheck instruction (only relevant for
2904 // static calls with explicit clinit check).
2905 void RemoveLoadClassAsLastInput() {
Roland Levillain4c0eb422015-04-24 16:43:49 +01002906 DCHECK(IsStaticWithExplicitClinitCheck());
2907 size_t last_input_index = InputCount() - 1;
2908 HInstruction* last_input = InputAt(last_input_index);
2909 DCHECK(last_input != nullptr);
Roland Levillain3e3d7332015-04-28 11:00:54 +01002910 DCHECK(last_input->IsLoadClass()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01002911 RemoveAsUserOfInput(last_input_index);
2912 inputs_.DeleteAt(last_input_index);
2913 clinit_check_requirement_ = ClinitCheckRequirement::kImplicit;
2914 DCHECK(IsStaticWithImplicitClinitCheck());
2915 }
2916
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01002917 bool IsStringFactoryFor(HFakeString* str) const {
2918 if (!IsStringInit()) return false;
2919 // +1 for the current method.
2920 if (InputCount() == (number_of_arguments_ + 1)) return false;
2921 return InputAt(InputCount() - 1)->AsFakeString() == str;
2922 }
2923
2924 void RemoveFakeStringArgumentAsLastInput() {
2925 DCHECK(IsStringInit());
2926 size_t last_input_index = InputCount() - 1;
2927 HInstruction* last_input = InputAt(last_input_index);
2928 DCHECK(last_input != nullptr);
2929 DCHECK(last_input->IsFakeString()) << last_input->DebugName();
2930 RemoveAsUserOfInput(last_input_index);
2931 inputs_.DeleteAt(last_input_index);
2932 }
2933
Roland Levillain4c0eb422015-04-24 16:43:49 +01002934 // Is this a call to a static method whose declaring class has an
2935 // explicit intialization check in the graph?
2936 bool IsStaticWithExplicitClinitCheck() const {
2937 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kExplicit);
2938 }
2939
2940 // Is this a call to a static method whose declaring class has an
2941 // implicit intialization check requirement?
2942 bool IsStaticWithImplicitClinitCheck() const {
2943 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kImplicit);
2944 }
2945
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002946 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002947
Roland Levillain4c0eb422015-04-24 16:43:49 +01002948 protected:
2949 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
2950 const HUserRecord<HInstruction*> input_record = HInvoke::InputRecordAt(i);
2951 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck() && (i == InputCount() - 1)) {
2952 HInstruction* input = input_record.GetInstruction();
2953 // `input` is the last input of a static invoke marked as having
2954 // an explicit clinit check. It must either be:
2955 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
2956 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
2957 DCHECK(input != nullptr);
2958 DCHECK(input->IsClinitCheck() || input->IsLoadClass()) << input->DebugName();
2959 }
2960 return input_record;
2961 }
2962
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002963 private:
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002964 const InvokeType invoke_type_;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002965 const bool is_recursive_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01002966 ClinitCheckRequirement clinit_check_requirement_;
Jeff Hao848f70a2014-01-15 13:49:50 -08002967 // Thread entrypoint offset for string init method if this is a string init invoke.
2968 // Note that there are multiple string init methods, each having its own offset.
2969 int32_t string_init_offset_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002970
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002971 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002972};
2973
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002974class HInvokeVirtual : public HInvoke {
2975 public:
2976 HInvokeVirtual(ArenaAllocator* arena,
2977 uint32_t number_of_arguments,
2978 Primitive::Type return_type,
2979 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002980 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002981 uint32_t vtable_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002982 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002983 vtable_index_(vtable_index) {}
2984
Calin Juravle641547a2015-04-21 22:08:51 +01002985 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00002986 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01002987 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00002988 }
2989
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002990 uint32_t GetVTableIndex() const { return vtable_index_; }
2991
2992 DECLARE_INSTRUCTION(InvokeVirtual);
2993
2994 private:
2995 const uint32_t vtable_index_;
2996
2997 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
2998};
2999
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003000class HInvokeInterface : public HInvoke {
3001 public:
3002 HInvokeInterface(ArenaAllocator* arena,
3003 uint32_t number_of_arguments,
3004 Primitive::Type return_type,
3005 uint32_t dex_pc,
3006 uint32_t dex_method_index,
3007 uint32_t imt_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003008 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003009 imt_index_(imt_index) {}
3010
Calin Juravle641547a2015-04-21 22:08:51 +01003011 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003012 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01003013 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00003014 }
3015
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003016 uint32_t GetImtIndex() const { return imt_index_; }
3017 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
3018
3019 DECLARE_INSTRUCTION(InvokeInterface);
3020
3021 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003022 const uint32_t imt_index_;
3023
3024 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
3025};
3026
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003027class HNewInstance : public HExpression<1> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003028 public:
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003029 HNewInstance(HCurrentMethod* current_method,
3030 uint32_t dex_pc,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003031 uint16_t type_index,
3032 const DexFile& dex_file,
3033 QuickEntrypointEnum entrypoint)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003034 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3035 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003036 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003037 dex_file_(dex_file),
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003038 entrypoint_(entrypoint) {
3039 SetRawInputAt(0, current_method);
3040 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003041
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003042 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003043 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003044 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003045
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003046 // Calls runtime so needs an environment.
Calin Juravle92a6ed22014-12-02 18:58:03 +00003047 bool NeedsEnvironment() const OVERRIDE { return true; }
3048 // It may throw when called on:
3049 // - interfaces
3050 // - abstract/innaccessible/unknown classes
3051 // TODO: optimize when possible.
3052 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003053
Calin Juravle10e244f2015-01-26 18:54:32 +00003054 bool CanBeNull() const OVERRIDE { return false; }
3055
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003056 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3057
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003058 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003059
3060 private:
3061 const uint32_t dex_pc_;
3062 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003063 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003064 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003065
3066 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3067};
3068
Roland Levillain88cb1752014-10-20 16:36:47 +01003069class HNeg : public HUnaryOperation {
3070 public:
3071 explicit HNeg(Primitive::Type result_type, HInstruction* input)
3072 : HUnaryOperation(result_type, input) {}
3073
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003074 int32_t Evaluate(int32_t x) const OVERRIDE { return -x; }
3075 int64_t Evaluate(int64_t x) const OVERRIDE { return -x; }
Roland Levillain9240d6a2014-10-20 16:47:04 +01003076
Roland Levillain88cb1752014-10-20 16:36:47 +01003077 DECLARE_INSTRUCTION(Neg);
3078
3079 private:
3080 DISALLOW_COPY_AND_ASSIGN(HNeg);
3081};
3082
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003083class HNewArray : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003084 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003085 HNewArray(HInstruction* length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003086 HCurrentMethod* current_method,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003087 uint32_t dex_pc,
3088 uint16_t type_index,
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003089 const DexFile& dex_file,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003090 QuickEntrypointEnum entrypoint)
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003091 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3092 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003093 type_index_(type_index),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003094 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003095 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003096 SetRawInputAt(0, length);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003097 SetRawInputAt(1, current_method);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003098 }
3099
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003100 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003101 uint16_t GetTypeIndex() const { return type_index_; }
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003102 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003103
3104 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00003105 bool NeedsEnvironment() const OVERRIDE { return true; }
3106
Mingyao Yang0c365e62015-03-31 15:09:29 -07003107 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
3108 bool CanThrow() const OVERRIDE { return true; }
3109
Calin Juravle10e244f2015-01-26 18:54:32 +00003110 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003111
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003112 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3113
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003114 DECLARE_INSTRUCTION(NewArray);
3115
3116 private:
3117 const uint32_t dex_pc_;
3118 const uint16_t type_index_;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003119 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003120 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003121
3122 DISALLOW_COPY_AND_ASSIGN(HNewArray);
3123};
3124
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003125class HAdd : public HBinaryOperation {
3126 public:
3127 HAdd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3128 : HBinaryOperation(result_type, left, right) {}
3129
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003130 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003131
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003132 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01003133 return x + y;
3134 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003135 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01003136 return x + y;
3137 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003138
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003139 DECLARE_INSTRUCTION(Add);
3140
3141 private:
3142 DISALLOW_COPY_AND_ASSIGN(HAdd);
3143};
3144
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003145class HSub : public HBinaryOperation {
3146 public:
3147 HSub(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3148 : HBinaryOperation(result_type, left, right) {}
3149
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003150 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01003151 return x - y;
3152 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003153 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01003154 return x - y;
3155 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003156
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003157 DECLARE_INSTRUCTION(Sub);
3158
3159 private:
3160 DISALLOW_COPY_AND_ASSIGN(HSub);
3161};
3162
Calin Juravle34bacdf2014-10-07 20:23:36 +01003163class HMul : public HBinaryOperation {
3164 public:
3165 HMul(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3166 : HBinaryOperation(result_type, left, right) {}
3167
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003168 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003169
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003170 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x * y; }
3171 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x * y; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003172
3173 DECLARE_INSTRUCTION(Mul);
3174
3175 private:
3176 DISALLOW_COPY_AND_ASSIGN(HMul);
3177};
3178
Calin Juravle7c4954d2014-10-28 16:57:40 +00003179class HDiv : public HBinaryOperation {
3180 public:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003181 HDiv(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
3182 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00003183
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003184 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00003185 // Our graph structure ensures we never have 0 for `y` during constant folding.
3186 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00003187 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00003188 return (y == -1) ? -x : x / y;
3189 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003190
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003191 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00003192 DCHECK_NE(y, 0);
3193 // Special case -1 to avoid getting a SIGFPE on x86(_64).
3194 return (y == -1) ? -x : x / y;
3195 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003196
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003197 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003198
Calin Juravle7c4954d2014-10-28 16:57:40 +00003199 DECLARE_INSTRUCTION(Div);
3200
3201 private:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003202 const uint32_t dex_pc_;
3203
Calin Juravle7c4954d2014-10-28 16:57:40 +00003204 DISALLOW_COPY_AND_ASSIGN(HDiv);
3205};
3206
Calin Juravlebacfec32014-11-14 15:54:36 +00003207class HRem : public HBinaryOperation {
3208 public:
3209 HRem(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
3210 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
3211
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003212 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00003213 DCHECK_NE(y, 0);
3214 // Special case -1 to avoid getting a SIGFPE on x86(_64).
3215 return (y == -1) ? 0 : x % y;
3216 }
3217
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003218 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00003219 DCHECK_NE(y, 0);
3220 // Special case -1 to avoid getting a SIGFPE on x86(_64).
3221 return (y == -1) ? 0 : x % y;
3222 }
3223
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003224 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Calin Juravlebacfec32014-11-14 15:54:36 +00003225
3226 DECLARE_INSTRUCTION(Rem);
3227
3228 private:
3229 const uint32_t dex_pc_;
3230
3231 DISALLOW_COPY_AND_ASSIGN(HRem);
3232};
3233
Calin Juravled0d48522014-11-04 16:40:20 +00003234class HDivZeroCheck : public HExpression<1> {
3235 public:
3236 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
3237 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
3238 SetRawInputAt(0, value);
3239 }
3240
3241 bool CanBeMoved() const OVERRIDE { return true; }
3242
3243 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3244 UNUSED(other);
3245 return true;
3246 }
3247
3248 bool NeedsEnvironment() const OVERRIDE { return true; }
3249 bool CanThrow() const OVERRIDE { return true; }
3250
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003251 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Calin Juravled0d48522014-11-04 16:40:20 +00003252
3253 DECLARE_INSTRUCTION(DivZeroCheck);
3254
3255 private:
3256 const uint32_t dex_pc_;
3257
3258 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
3259};
3260
Calin Juravle9aec02f2014-11-18 23:06:35 +00003261class HShl : public HBinaryOperation {
3262 public:
3263 HShl(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3264 : HBinaryOperation(result_type, left, right) {}
3265
3266 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x << (y & kMaxIntShiftValue); }
3267 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x << (y & kMaxLongShiftValue); }
3268
3269 DECLARE_INSTRUCTION(Shl);
3270
3271 private:
3272 DISALLOW_COPY_AND_ASSIGN(HShl);
3273};
3274
3275class HShr : public HBinaryOperation {
3276 public:
3277 HShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3278 : HBinaryOperation(result_type, left, right) {}
3279
3280 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x >> (y & kMaxIntShiftValue); }
3281 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x >> (y & kMaxLongShiftValue); }
3282
3283 DECLARE_INSTRUCTION(Shr);
3284
3285 private:
3286 DISALLOW_COPY_AND_ASSIGN(HShr);
3287};
3288
3289class HUShr : public HBinaryOperation {
3290 public:
3291 HUShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3292 : HBinaryOperation(result_type, left, right) {}
3293
3294 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
3295 uint32_t ux = static_cast<uint32_t>(x);
3296 uint32_t uy = static_cast<uint32_t>(y) & kMaxIntShiftValue;
3297 return static_cast<int32_t>(ux >> uy);
3298 }
3299
3300 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
3301 uint64_t ux = static_cast<uint64_t>(x);
3302 uint64_t uy = static_cast<uint64_t>(y) & kMaxLongShiftValue;
3303 return static_cast<int64_t>(ux >> uy);
3304 }
3305
3306 DECLARE_INSTRUCTION(UShr);
3307
3308 private:
3309 DISALLOW_COPY_AND_ASSIGN(HUShr);
3310};
3311
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003312class HAnd : public HBinaryOperation {
3313 public:
3314 HAnd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3315 : HBinaryOperation(result_type, left, right) {}
3316
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003317 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003318
3319 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x & y; }
3320 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x & y; }
3321
3322 DECLARE_INSTRUCTION(And);
3323
3324 private:
3325 DISALLOW_COPY_AND_ASSIGN(HAnd);
3326};
3327
3328class HOr : public HBinaryOperation {
3329 public:
3330 HOr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3331 : HBinaryOperation(result_type, left, right) {}
3332
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003333 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003334
3335 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x | y; }
3336 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x | y; }
3337
3338 DECLARE_INSTRUCTION(Or);
3339
3340 private:
3341 DISALLOW_COPY_AND_ASSIGN(HOr);
3342};
3343
3344class HXor : public HBinaryOperation {
3345 public:
3346 HXor(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3347 : HBinaryOperation(result_type, left, right) {}
3348
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003349 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003350
3351 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x ^ y; }
3352 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x ^ y; }
3353
3354 DECLARE_INSTRUCTION(Xor);
3355
3356 private:
3357 DISALLOW_COPY_AND_ASSIGN(HXor);
3358};
3359
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003360// The value of a parameter in this method. Its location depends on
3361// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07003362class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003363 public:
Calin Juravle10e244f2015-01-26 18:54:32 +00003364 HParameterValue(uint8_t index, Primitive::Type parameter_type, bool is_this = false)
3365 : HExpression(parameter_type, SideEffects::None()), index_(index), is_this_(is_this) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003366
3367 uint8_t GetIndex() const { return index_; }
3368
Calin Juravle10e244f2015-01-26 18:54:32 +00003369 bool CanBeNull() const OVERRIDE { return !is_this_; }
3370
Calin Juravle3cd4fc82015-05-14 15:15:42 +01003371 bool IsThis() const { return is_this_; }
3372
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003373 DECLARE_INSTRUCTION(ParameterValue);
3374
3375 private:
3376 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00003377 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003378 const uint8_t index_;
3379
Calin Juravle10e244f2015-01-26 18:54:32 +00003380 // Whether or not the parameter value corresponds to 'this' argument.
3381 const bool is_this_;
3382
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003383 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
3384};
3385
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003386class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003387 public:
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003388 explicit HNot(Primitive::Type result_type, HInstruction* input)
3389 : HUnaryOperation(result_type, input) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003390
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003391 bool CanBeMoved() const OVERRIDE { return true; }
3392 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003393 UNUSED(other);
3394 return true;
3395 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003396
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003397 int32_t Evaluate(int32_t x) const OVERRIDE { return ~x; }
3398 int64_t Evaluate(int64_t x) const OVERRIDE { return ~x; }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003399
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003400 DECLARE_INSTRUCTION(Not);
3401
3402 private:
3403 DISALLOW_COPY_AND_ASSIGN(HNot);
3404};
3405
David Brazdil66d126e2015-04-03 16:02:44 +01003406class HBooleanNot : public HUnaryOperation {
3407 public:
3408 explicit HBooleanNot(HInstruction* input)
3409 : HUnaryOperation(Primitive::Type::kPrimBoolean, input) {}
3410
3411 bool CanBeMoved() const OVERRIDE { return true; }
3412 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3413 UNUSED(other);
3414 return true;
3415 }
3416
3417 int32_t Evaluate(int32_t x) const OVERRIDE {
3418 DCHECK(IsUint<1>(x));
3419 return !x;
3420 }
3421
3422 int64_t Evaluate(int64_t x ATTRIBUTE_UNUSED) const OVERRIDE {
3423 LOG(FATAL) << DebugName() << " cannot be used with 64-bit values";
3424 UNREACHABLE();
3425 }
3426
3427 DECLARE_INSTRUCTION(BooleanNot);
3428
3429 private:
3430 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
3431};
3432
Roland Levillaindff1f282014-11-05 14:15:05 +00003433class HTypeConversion : public HExpression<1> {
3434 public:
3435 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00003436 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
3437 : HExpression(result_type, SideEffects::None()), dex_pc_(dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00003438 SetRawInputAt(0, input);
3439 DCHECK_NE(input->GetType(), result_type);
3440 }
3441
3442 HInstruction* GetInput() const { return InputAt(0); }
3443 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
3444 Primitive::Type GetResultType() const { return GetType(); }
3445
Roland Levillain624279f2014-12-04 11:54:28 +00003446 // Required by the x86 and ARM code generators when producing calls
3447 // to the runtime.
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003448 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Roland Levillain624279f2014-12-04 11:54:28 +00003449
Roland Levillaindff1f282014-11-05 14:15:05 +00003450 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00003451 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00003452
Mark Mendelle82549b2015-05-06 10:55:34 -04003453 // Try to statically evaluate the conversion and return a HConstant
3454 // containing the result. If the input cannot be converted, return nullptr.
3455 HConstant* TryStaticEvaluation() const;
3456
Roland Levillaindff1f282014-11-05 14:15:05 +00003457 DECLARE_INSTRUCTION(TypeConversion);
3458
3459 private:
Roland Levillain624279f2014-12-04 11:54:28 +00003460 const uint32_t dex_pc_;
3461
Roland Levillaindff1f282014-11-05 14:15:05 +00003462 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
3463};
3464
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00003465static constexpr uint32_t kNoRegNumber = -1;
3466
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003467class HPhi : public HInstruction {
3468 public:
3469 HPhi(ArenaAllocator* arena, uint32_t reg_number, size_t number_of_inputs, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003470 : HInstruction(SideEffects::None()),
3471 inputs_(arena, number_of_inputs),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003472 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01003473 type_(type),
Calin Juravle10e244f2015-01-26 18:54:32 +00003474 is_live_(false),
3475 can_be_null_(true) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003476 inputs_.SetSize(number_of_inputs);
3477 }
3478
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00003479 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
3480 static Primitive::Type ToPhiType(Primitive::Type type) {
3481 switch (type) {
3482 case Primitive::kPrimBoolean:
3483 case Primitive::kPrimByte:
3484 case Primitive::kPrimShort:
3485 case Primitive::kPrimChar:
3486 return Primitive::kPrimInt;
3487 default:
3488 return type;
3489 }
3490 }
3491
David Brazdilffee3d32015-07-06 11:48:53 +01003492 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
3493
Calin Juravle10e244f2015-01-26 18:54:32 +00003494 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003495
3496 void AddInput(HInstruction* input);
David Brazdil2d7352b2015-04-20 14:52:42 +01003497 void RemoveInputAt(size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003498
Calin Juravle10e244f2015-01-26 18:54:32 +00003499 Primitive::Type GetType() const OVERRIDE { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003500 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003501
Calin Juravle10e244f2015-01-26 18:54:32 +00003502 bool CanBeNull() const OVERRIDE { return can_be_null_; }
3503 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
3504
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003505 uint32_t GetRegNumber() const { return reg_number_; }
3506
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01003507 void SetDead() { is_live_ = false; }
3508 void SetLive() { is_live_ = true; }
3509 bool IsDead() const { return !is_live_; }
3510 bool IsLive() const { return is_live_; }
3511
Calin Juravlea4f88312015-04-16 12:57:19 +01003512 // Returns the next equivalent phi (starting from the current one) or null if there is none.
3513 // An equivalent phi is a phi having the same dex register and type.
3514 // It assumes that phis with the same dex register are adjacent.
3515 HPhi* GetNextEquivalentPhiWithSameType() {
3516 HInstruction* next = GetNext();
3517 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
3518 if (next->GetType() == GetType()) {
3519 return next->AsPhi();
3520 }
3521 next = next->GetNext();
3522 }
3523 return nullptr;
3524 }
3525
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003526 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003527
David Brazdil1abb4192015-02-17 18:33:36 +00003528 protected:
3529 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
3530
3531 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
3532 inputs_.Put(index, input);
3533 }
3534
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003535 private:
David Brazdil1abb4192015-02-17 18:33:36 +00003536 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003537 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003538 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01003539 bool is_live_;
Calin Juravle10e244f2015-01-26 18:54:32 +00003540 bool can_be_null_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003541
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003542 DISALLOW_COPY_AND_ASSIGN(HPhi);
3543};
3544
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003545class HNullCheck : public HExpression<1> {
3546 public:
3547 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003548 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003549 SetRawInputAt(0, value);
3550 }
3551
Calin Juravle10e244f2015-01-26 18:54:32 +00003552 bool CanBeMoved() const OVERRIDE { return true; }
3553 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003554 UNUSED(other);
3555 return true;
3556 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003557
Calin Juravle10e244f2015-01-26 18:54:32 +00003558 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003559
Calin Juravle10e244f2015-01-26 18:54:32 +00003560 bool CanThrow() const OVERRIDE { return true; }
3561
3562 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01003563
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003564 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003565
3566 DECLARE_INSTRUCTION(NullCheck);
3567
3568 private:
3569 const uint32_t dex_pc_;
3570
3571 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
3572};
3573
3574class FieldInfo : public ValueObject {
3575 public:
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003576 FieldInfo(MemberOffset field_offset,
3577 Primitive::Type field_type,
3578 bool is_volatile,
3579 uint32_t index,
3580 const DexFile& dex_file)
3581 : field_offset_(field_offset),
3582 field_type_(field_type),
3583 is_volatile_(is_volatile),
3584 index_(index),
3585 dex_file_(dex_file) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003586
3587 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003588 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003589 uint32_t GetFieldIndex() const { return index_; }
3590 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00003591 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003592
3593 private:
3594 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01003595 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00003596 const bool is_volatile_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003597 uint32_t index_;
3598 const DexFile& dex_file_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003599};
3600
3601class HInstanceFieldGet : public HExpression<1> {
3602 public:
3603 HInstanceFieldGet(HInstruction* value,
3604 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003605 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003606 bool is_volatile,
3607 uint32_t field_idx,
3608 const DexFile& dex_file)
Aart Bik34c3ba92015-07-20 14:08:59 -07003609 : HExpression(
3610 field_type,
Alexandre Rames1c4ccea2015-07-22 11:32:58 +01003611 SideEffects::FieldReadOfType(field_type, is_volatile)),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003612 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003613 SetRawInputAt(0, value);
3614 }
3615
Calin Juravle10c9cbe2014-12-19 10:50:19 +00003616 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003617
3618 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3619 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
3620 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003621 }
3622
Calin Juravle641547a2015-04-21 22:08:51 +01003623 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3624 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00003625 }
3626
3627 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01003628 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
3629 }
3630
Calin Juravle52c48962014-12-16 17:02:57 +00003631 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003632 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003633 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003634 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003635
3636 DECLARE_INSTRUCTION(InstanceFieldGet);
3637
3638 private:
3639 const FieldInfo field_info_;
3640
3641 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
3642};
3643
3644class HInstanceFieldSet : public HTemplateInstruction<2> {
3645 public:
3646 HInstanceFieldSet(HInstruction* object,
3647 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01003648 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003649 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003650 bool is_volatile,
3651 uint32_t field_idx,
3652 const DexFile& dex_file)
Aart Bik34c3ba92015-07-20 14:08:59 -07003653 : HTemplateInstruction(
3654 SideEffects::FieldWriteOfType(field_type, is_volatile)),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003655 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003656 value_can_be_null_(true) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003657 SetRawInputAt(0, object);
3658 SetRawInputAt(1, value);
3659 }
3660
Calin Juravle641547a2015-04-21 22:08:51 +01003661 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3662 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00003663 }
3664
Calin Juravle52c48962014-12-16 17:02:57 +00003665 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003666 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003667 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003668 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003669 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003670 bool GetValueCanBeNull() const { return value_can_be_null_; }
3671 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003672
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003673 DECLARE_INSTRUCTION(InstanceFieldSet);
3674
3675 private:
3676 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003677 bool value_can_be_null_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003678
3679 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
3680};
3681
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003682class HArrayGet : public HExpression<2> {
3683 public:
3684 HArrayGet(HInstruction* array, HInstruction* index, Primitive::Type type)
Aart Bik854a02b2015-07-14 16:07:00 -07003685 : HExpression(type, SideEffects::ArrayReadOfType(type)) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003686 SetRawInputAt(0, array);
3687 SetRawInputAt(1, index);
3688 }
3689
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003690 bool CanBeMoved() const OVERRIDE { return true; }
3691 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003692 UNUSED(other);
3693 return true;
3694 }
Calin Juravle641547a2015-04-21 22:08:51 +01003695 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3696 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00003697 // TODO: We can be smarter here.
3698 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
3699 // which generates the implicit null check. There are cases when these can be removed
3700 // to produce better code. If we ever add optimizations to do so we should allow an
3701 // implicit check here (as long as the address falls in the first page).
3702 return false;
3703 }
3704
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003705 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003706
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003707 HInstruction* GetArray() const { return InputAt(0); }
3708 HInstruction* GetIndex() const { return InputAt(1); }
3709
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003710 DECLARE_INSTRUCTION(ArrayGet);
3711
3712 private:
3713 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
3714};
3715
3716class HArraySet : public HTemplateInstruction<3> {
3717 public:
3718 HArraySet(HInstruction* array,
3719 HInstruction* index,
3720 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003721 Primitive::Type expected_component_type,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003722 uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07003723 : HTemplateInstruction(SideEffects::ArrayWriteOfType(expected_component_type)),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003724 dex_pc_(dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003725 expected_component_type_(expected_component_type),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003726 needs_type_check_(value->GetType() == Primitive::kPrimNot),
3727 value_can_be_null_(true) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003728 SetRawInputAt(0, array);
3729 SetRawInputAt(1, index);
3730 SetRawInputAt(2, value);
3731 }
3732
Calin Juravle77520bc2015-01-12 18:45:46 +00003733 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003734 // We currently always call a runtime method to catch array store
3735 // exceptions.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003736 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003737 }
3738
Mingyao Yang81014cb2015-06-02 03:16:27 -07003739 // Can throw ArrayStoreException.
3740 bool CanThrow() const OVERRIDE { return needs_type_check_; }
3741
Calin Juravle641547a2015-04-21 22:08:51 +01003742 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3743 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00003744 // TODO: Same as for ArrayGet.
3745 return false;
3746 }
3747
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003748 void ClearNeedsTypeCheck() {
3749 needs_type_check_ = false;
3750 }
3751
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003752 void ClearValueCanBeNull() {
3753 value_can_be_null_ = false;
3754 }
3755
3756 bool GetValueCanBeNull() const { return value_can_be_null_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003757 bool NeedsTypeCheck() const { return needs_type_check_; }
3758
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003759 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003760
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003761 HInstruction* GetArray() const { return InputAt(0); }
3762 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003763 HInstruction* GetValue() const { return InputAt(2); }
3764
3765 Primitive::Type GetComponentType() const {
3766 // The Dex format does not type floating point index operations. Since the
3767 // `expected_component_type_` is set during building and can therefore not
3768 // be correct, we also check what is the value type. If it is a floating
3769 // point type, we must use that type.
3770 Primitive::Type value_type = GetValue()->GetType();
3771 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
3772 ? value_type
3773 : expected_component_type_;
3774 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003775
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003776 DECLARE_INSTRUCTION(ArraySet);
3777
3778 private:
3779 const uint32_t dex_pc_;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003780 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003781 bool needs_type_check_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003782 bool value_can_be_null_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003783
3784 DISALLOW_COPY_AND_ASSIGN(HArraySet);
3785};
3786
3787class HArrayLength : public HExpression<1> {
3788 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003789 explicit HArrayLength(HInstruction* array)
3790 : HExpression(Primitive::kPrimInt, SideEffects::None()) {
3791 // Note that arrays do not change length, so the instruction does not
3792 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003793 SetRawInputAt(0, array);
3794 }
3795
Calin Juravle77520bc2015-01-12 18:45:46 +00003796 bool CanBeMoved() const OVERRIDE { return true; }
3797 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003798 UNUSED(other);
3799 return true;
3800 }
Calin Juravle641547a2015-04-21 22:08:51 +01003801 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3802 return obj == InputAt(0);
3803 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003804
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003805 DECLARE_INSTRUCTION(ArrayLength);
3806
3807 private:
3808 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
3809};
3810
3811class HBoundsCheck : public HExpression<2> {
3812 public:
3813 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003814 : HExpression(index->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003815 DCHECK(index->GetType() == Primitive::kPrimInt);
3816 SetRawInputAt(0, index);
3817 SetRawInputAt(1, length);
3818 }
3819
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003820 bool CanBeMoved() const OVERRIDE { return true; }
3821 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003822 UNUSED(other);
3823 return true;
3824 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003825
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003826 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003827
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003828 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01003829
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003830 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003831
3832 DECLARE_INSTRUCTION(BoundsCheck);
3833
3834 private:
3835 const uint32_t dex_pc_;
3836
3837 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
3838};
3839
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003840/**
3841 * Some DEX instructions are folded into multiple HInstructions that need
3842 * to stay live until the last HInstruction. This class
3843 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00003844 * HInstruction stays live. `index` represents the stack location index of the
3845 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003846 */
3847class HTemporary : public HTemplateInstruction<0> {
3848 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003849 explicit HTemporary(size_t index) : HTemplateInstruction(SideEffects::None()), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003850
3851 size_t GetIndex() const { return index_; }
3852
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00003853 Primitive::Type GetType() const OVERRIDE {
3854 // The previous instruction is the one that will be stored in the temporary location.
3855 DCHECK(GetPrevious() != nullptr);
3856 return GetPrevious()->GetType();
3857 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00003858
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003859 DECLARE_INSTRUCTION(Temporary);
3860
3861 private:
3862 const size_t index_;
3863
3864 DISALLOW_COPY_AND_ASSIGN(HTemporary);
3865};
3866
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003867class HSuspendCheck : public HTemplateInstruction<0> {
3868 public:
3869 explicit HSuspendCheck(uint32_t dex_pc)
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01003870 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003871
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003872 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003873 return true;
3874 }
3875
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003876 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01003877 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
3878 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003879
3880 DECLARE_INSTRUCTION(SuspendCheck);
3881
3882 private:
3883 const uint32_t dex_pc_;
3884
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01003885 // Only used for code generation, in order to share the same slow path between back edges
3886 // of a same loop.
3887 SlowPathCode* slow_path_;
3888
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003889 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
3890};
3891
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003892/**
3893 * Instruction to load a Class object.
3894 */
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003895class HLoadClass : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003896 public:
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003897 HLoadClass(HCurrentMethod* current_method,
3898 uint16_t type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003899 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003900 bool is_referrers_class,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003901 uint32_t dex_pc)
3902 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3903 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003904 dex_file_(dex_file),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003905 is_referrers_class_(is_referrers_class),
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003906 dex_pc_(dex_pc),
Calin Juravleb1498f62015-02-16 13:13:29 +00003907 generate_clinit_check_(false),
Calin Juravle7733bd62015-07-22 17:14:50 +00003908 loaded_class_rti_(ReferenceTypeInfo::CreateTop(/* is_exact */ false)) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003909 SetRawInputAt(0, current_method);
3910 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003911
3912 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003913
3914 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3915 return other->AsLoadClass()->type_index_ == type_index_;
3916 }
3917
3918 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
3919
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003920 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003921 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003922 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01003923 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003924
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003925 bool NeedsEnvironment() const OVERRIDE {
3926 // Will call runtime and load the class if the class is not loaded yet.
3927 // TODO: finer grain decision.
3928 return !is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003929 }
3930
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003931 bool MustGenerateClinitCheck() const {
3932 return generate_clinit_check_;
3933 }
3934
Calin Juravle0ba218d2015-05-19 18:46:01 +01003935 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
3936 generate_clinit_check_ = generate_clinit_check;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003937 }
3938
3939 bool CanCallRuntime() const {
3940 return MustGenerateClinitCheck() || !is_referrers_class_;
3941 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003942
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003943 bool CanThrow() const OVERRIDE {
3944 // May call runtime and and therefore can throw.
3945 // TODO: finer grain decision.
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01003946 return CanCallRuntime();
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003947 }
3948
Calin Juravleacf735c2015-02-12 15:25:22 +00003949 ReferenceTypeInfo GetLoadedClassRTI() {
3950 return loaded_class_rti_;
3951 }
3952
3953 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
3954 // Make sure we only set exact types (the loaded class should never be merged).
3955 DCHECK(rti.IsExact());
3956 loaded_class_rti_ = rti;
3957 }
3958
Calin Juravle7733bd62015-07-22 17:14:50 +00003959 bool IsResolved() {
3960 return loaded_class_rti_.IsExact();
3961 }
3962
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003963 const DexFile& GetDexFile() { return dex_file_; }
3964
Nicolas Geoffray9437b782015-03-25 10:08:51 +00003965 bool NeedsDexCache() const OVERRIDE { return !is_referrers_class_; }
3966
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003967 DECLARE_INSTRUCTION(LoadClass);
3968
3969 private:
3970 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003971 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003972 const bool is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003973 const uint32_t dex_pc_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003974 // Whether this instruction must generate the initialization check.
3975 // Used for code generation.
3976 bool generate_clinit_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003977
Calin Juravleacf735c2015-02-12 15:25:22 +00003978 ReferenceTypeInfo loaded_class_rti_;
3979
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003980 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
3981};
3982
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01003983class HLoadString : public HExpression<1> {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003984 public:
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01003985 HLoadString(HCurrentMethod* current_method, uint32_t string_index, uint32_t dex_pc)
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003986 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3987 string_index_(string_index),
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01003988 dex_pc_(dex_pc) {
3989 SetRawInputAt(0, current_method);
3990 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003991
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003992 bool CanBeMoved() const OVERRIDE { return true; }
3993
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003994 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3995 return other->AsLoadString()->string_index_ == string_index_;
3996 }
3997
3998 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
3999
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004000 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004001 uint32_t GetStringIndex() const { return string_index_; }
4002
4003 // TODO: Can we deopt or debug when we resolve a string?
4004 bool NeedsEnvironment() const OVERRIDE { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00004005 bool NeedsDexCache() const OVERRIDE { return true; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004006
4007 DECLARE_INSTRUCTION(LoadString);
4008
4009 private:
4010 const uint32_t string_index_;
4011 const uint32_t dex_pc_;
4012
4013 DISALLOW_COPY_AND_ASSIGN(HLoadString);
4014};
4015
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004016/**
4017 * Performs an initialization check on its Class object input.
4018 */
4019class HClinitCheck : public HExpression<1> {
4020 public:
4021 explicit HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07004022 : HExpression(
4023 Primitive::kPrimNot,
Aart Bik34c3ba92015-07-20 14:08:59 -07004024 SideEffects::AllWrites()), // assume write on all fields/arrays
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004025 dex_pc_(dex_pc) {
4026 SetRawInputAt(0, constant);
4027 }
4028
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004029 bool CanBeMoved() const OVERRIDE { return true; }
4030 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4031 UNUSED(other);
4032 return true;
4033 }
4034
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004035 bool NeedsEnvironment() const OVERRIDE {
4036 // May call runtime to initialize the class.
4037 return true;
4038 }
4039
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004040 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004041
4042 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
4043
4044 DECLARE_INSTRUCTION(ClinitCheck);
4045
4046 private:
4047 const uint32_t dex_pc_;
4048
4049 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
4050};
4051
4052class HStaticFieldGet : public HExpression<1> {
4053 public:
4054 HStaticFieldGet(HInstruction* cls,
4055 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004056 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004057 bool is_volatile,
4058 uint32_t field_idx,
4059 const DexFile& dex_file)
Aart Bik34c3ba92015-07-20 14:08:59 -07004060 : HExpression(
4061 field_type,
Alexandre Rames1c4ccea2015-07-22 11:32:58 +01004062 SideEffects::FieldReadOfType(field_type, is_volatile)),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004063 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004064 SetRawInputAt(0, cls);
4065 }
4066
Calin Juravle52c48962014-12-16 17:02:57 +00004067
Calin Juravle10c9cbe2014-12-19 10:50:19 +00004068 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004069
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004070 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00004071 HStaticFieldGet* other_get = other->AsStaticFieldGet();
4072 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004073 }
4074
4075 size_t ComputeHashCode() const OVERRIDE {
4076 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
4077 }
4078
Calin Juravle52c48962014-12-16 17:02:57 +00004079 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004080 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
4081 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004082 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004083
4084 DECLARE_INSTRUCTION(StaticFieldGet);
4085
4086 private:
4087 const FieldInfo field_info_;
4088
4089 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
4090};
4091
4092class HStaticFieldSet : public HTemplateInstruction<2> {
4093 public:
4094 HStaticFieldSet(HInstruction* cls,
4095 HInstruction* value,
4096 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004097 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004098 bool is_volatile,
4099 uint32_t field_idx,
4100 const DexFile& dex_file)
Aart Bik34c3ba92015-07-20 14:08:59 -07004101 : HTemplateInstruction(
4102 SideEffects::FieldWriteOfType(field_type, is_volatile)),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004103 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004104 value_can_be_null_(true) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004105 SetRawInputAt(0, cls);
4106 SetRawInputAt(1, value);
4107 }
4108
Calin Juravle52c48962014-12-16 17:02:57 +00004109 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004110 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
4111 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004112 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004113
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004114 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004115 bool GetValueCanBeNull() const { return value_can_be_null_; }
4116 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004117
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004118 DECLARE_INSTRUCTION(StaticFieldSet);
4119
4120 private:
4121 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004122 bool value_can_be_null_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004123
4124 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
4125};
4126
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004127// Implement the move-exception DEX instruction.
4128class HLoadException : public HExpression<0> {
4129 public:
4130 HLoadException() : HExpression(Primitive::kPrimNot, SideEffects::None()) {}
4131
4132 DECLARE_INSTRUCTION(LoadException);
4133
4134 private:
4135 DISALLOW_COPY_AND_ASSIGN(HLoadException);
4136};
4137
4138class HThrow : public HTemplateInstruction<1> {
4139 public:
4140 HThrow(HInstruction* exception, uint32_t dex_pc)
4141 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {
4142 SetRawInputAt(0, exception);
4143 }
4144
4145 bool IsControlFlow() const OVERRIDE { return true; }
4146
4147 bool NeedsEnvironment() const OVERRIDE { return true; }
4148
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004149 bool CanThrow() const OVERRIDE { return true; }
4150
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004151 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004152
4153 DECLARE_INSTRUCTION(Throw);
4154
4155 private:
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004156 const uint32_t dex_pc_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004157
4158 DISALLOW_COPY_AND_ASSIGN(HThrow);
4159};
4160
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004161class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004162 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004163 HInstanceOf(HInstruction* object,
4164 HLoadClass* constant,
4165 bool class_is_final,
4166 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004167 : HExpression(Primitive::kPrimBoolean, SideEffects::None()),
4168 class_is_final_(class_is_final),
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004169 must_do_null_check_(true),
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004170 dex_pc_(dex_pc) {
4171 SetRawInputAt(0, object);
4172 SetRawInputAt(1, constant);
4173 }
4174
4175 bool CanBeMoved() const OVERRIDE { return true; }
4176
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004177 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004178 return true;
4179 }
4180
4181 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004182 return false;
4183 }
4184
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004185 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004186
4187 bool IsClassFinal() const { return class_is_final_; }
4188
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004189 // Used only in code generation.
4190 bool MustDoNullCheck() const { return must_do_null_check_; }
4191 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
4192
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004193 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004194
4195 private:
4196 const bool class_is_final_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004197 bool must_do_null_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004198 const uint32_t dex_pc_;
4199
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004200 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
4201};
4202
Calin Juravleb1498f62015-02-16 13:13:29 +00004203class HBoundType : public HExpression<1> {
4204 public:
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004205 HBoundType(HInstruction* input, ReferenceTypeInfo upper_bound, bool upper_can_be_null)
Calin Juravleb1498f62015-02-16 13:13:29 +00004206 : HExpression(Primitive::kPrimNot, SideEffects::None()),
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004207 upper_bound_(upper_bound),
4208 upper_can_be_null_(upper_can_be_null),
4209 can_be_null_(upper_can_be_null) {
Calin Juravle61d544b2015-02-23 16:46:57 +00004210 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00004211 SetRawInputAt(0, input);
4212 }
4213
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004214 // GetUpper* should only be used in reference type propagation.
4215 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
4216 bool GetUpperCanBeNull() const { return upper_can_be_null_; }
Calin Juravleb1498f62015-02-16 13:13:29 +00004217
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004218 void SetCanBeNull(bool can_be_null) {
4219 DCHECK(upper_can_be_null_ || !can_be_null);
4220 can_be_null_ = can_be_null;
Calin Juravleb1498f62015-02-16 13:13:29 +00004221 }
4222
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004223 bool CanBeNull() const OVERRIDE { return can_be_null_; }
4224
Calin Juravleb1498f62015-02-16 13:13:29 +00004225 DECLARE_INSTRUCTION(BoundType);
4226
4227 private:
4228 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00004229 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
4230 // It is used to bound the type in cases like:
4231 // if (x instanceof ClassX) {
4232 // // uper_bound_ will be ClassX
4233 // }
4234 const ReferenceTypeInfo upper_bound_;
4235 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
4236 // is false then can_be_null_ cannot be true).
4237 const bool upper_can_be_null_;
4238 bool can_be_null_;
Calin Juravleb1498f62015-02-16 13:13:29 +00004239
4240 DISALLOW_COPY_AND_ASSIGN(HBoundType);
4241};
4242
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004243class HCheckCast : public HTemplateInstruction<2> {
4244 public:
4245 HCheckCast(HInstruction* object,
4246 HLoadClass* constant,
4247 bool class_is_final,
4248 uint32_t dex_pc)
4249 : HTemplateInstruction(SideEffects::None()),
4250 class_is_final_(class_is_final),
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004251 must_do_null_check_(true),
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004252 dex_pc_(dex_pc) {
4253 SetRawInputAt(0, object);
4254 SetRawInputAt(1, constant);
4255 }
4256
4257 bool CanBeMoved() const OVERRIDE { return true; }
4258
4259 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
4260 return true;
4261 }
4262
4263 bool NeedsEnvironment() const OVERRIDE {
4264 // Instruction may throw a CheckCastError.
4265 return true;
4266 }
4267
4268 bool CanThrow() const OVERRIDE { return true; }
4269
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004270 bool MustDoNullCheck() const { return must_do_null_check_; }
4271 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
4272
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004273 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004274
4275 bool IsClassFinal() const { return class_is_final_; }
4276
4277 DECLARE_INSTRUCTION(CheckCast);
4278
4279 private:
4280 const bool class_is_final_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004281 bool must_do_null_check_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004282 const uint32_t dex_pc_;
4283
4284 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004285};
4286
Calin Juravle27df7582015-04-17 19:12:31 +01004287class HMemoryBarrier : public HTemplateInstruction<0> {
4288 public:
4289 explicit HMemoryBarrier(MemBarrierKind barrier_kind)
Aart Bik34c3ba92015-07-20 14:08:59 -07004290 : HTemplateInstruction(
4291 SideEffects::All()), // assume write/read on all fields/arrays
Calin Juravle27df7582015-04-17 19:12:31 +01004292 barrier_kind_(barrier_kind) {}
4293
4294 MemBarrierKind GetBarrierKind() { return barrier_kind_; }
4295
4296 DECLARE_INSTRUCTION(MemoryBarrier);
4297
4298 private:
4299 const MemBarrierKind barrier_kind_;
4300
4301 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
4302};
4303
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004304class HMonitorOperation : public HTemplateInstruction<1> {
4305 public:
4306 enum OperationKind {
4307 kEnter,
4308 kExit,
4309 };
4310
4311 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07004312 : HTemplateInstruction(SideEffects::All()), // assume write/read on all fields/arrays
4313 kind_(kind), dex_pc_(dex_pc) {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004314 SetRawInputAt(0, object);
4315 }
4316
4317 // Instruction may throw a Java exception, so we need an environment.
David Brazdilbff75032015-07-08 17:26:51 +00004318 bool NeedsEnvironment() const OVERRIDE { return CanThrow(); }
4319
4320 bool CanThrow() const OVERRIDE {
4321 // Verifier guarantees that monitor-exit cannot throw.
4322 // This is important because it allows the HGraphBuilder to remove
4323 // a dead throw-catch loop generated for `synchronized` blocks/methods.
4324 return IsEnter();
4325 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004326
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004327 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004328
4329 bool IsEnter() const { return kind_ == kEnter; }
4330
4331 DECLARE_INSTRUCTION(MonitorOperation);
4332
Calin Juravle52c48962014-12-16 17:02:57 +00004333 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004334 const OperationKind kind_;
4335 const uint32_t dex_pc_;
4336
4337 private:
4338 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
4339};
4340
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01004341/**
4342 * A HInstruction used as a marker for the replacement of new + <init>
4343 * of a String to a call to a StringFactory. Only baseline will see
4344 * the node at code generation, where it will be be treated as null.
4345 * When compiling non-baseline, `HFakeString` instructions are being removed
4346 * in the instruction simplifier.
4347 */
4348class HFakeString : public HTemplateInstruction<0> {
4349 public:
4350 HFakeString() : HTemplateInstruction(SideEffects::None()) {}
4351
4352 Primitive::Type GetType() const OVERRIDE { return Primitive::kPrimNot; }
4353
4354 DECLARE_INSTRUCTION(FakeString);
4355
4356 private:
4357 DISALLOW_COPY_AND_ASSIGN(HFakeString);
4358};
4359
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004360class MoveOperands : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004361 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01004362 MoveOperands(Location source,
4363 Location destination,
4364 Primitive::Type type,
4365 HInstruction* instruction)
4366 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004367
4368 Location GetSource() const { return source_; }
4369 Location GetDestination() const { return destination_; }
4370
4371 void SetSource(Location value) { source_ = value; }
4372 void SetDestination(Location value) { destination_ = value; }
4373
4374 // The parallel move resolver marks moves as "in-progress" by clearing the
4375 // destination (but not the source).
4376 Location MarkPending() {
4377 DCHECK(!IsPending());
4378 Location dest = destination_;
4379 destination_ = Location::NoLocation();
4380 return dest;
4381 }
4382
4383 void ClearPending(Location dest) {
4384 DCHECK(IsPending());
4385 destination_ = dest;
4386 }
4387
4388 bool IsPending() const {
4389 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
4390 return destination_.IsInvalid() && !source_.IsInvalid();
4391 }
4392
4393 // True if this blocks a move from the given location.
4394 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08004395 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004396 }
4397
4398 // A move is redundant if it's been eliminated, if its source and
4399 // destination are the same, or if its destination is unneeded.
4400 bool IsRedundant() const {
4401 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
4402 }
4403
4404 // We clear both operands to indicate move that's been eliminated.
4405 void Eliminate() {
4406 source_ = destination_ = Location::NoLocation();
4407 }
4408
4409 bool IsEliminated() const {
4410 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
4411 return source_.IsInvalid();
4412 }
4413
Alexey Frunze4dda3372015-06-01 18:31:49 -07004414 Primitive::Type GetType() const { return type_; }
4415
Nicolas Geoffray90218252015-04-15 11:56:51 +01004416 bool Is64BitMove() const {
4417 return Primitive::Is64BitType(type_);
4418 }
4419
Nicolas Geoffray740475d2014-09-29 10:33:25 +01004420 HInstruction* GetInstruction() const { return instruction_; }
4421
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004422 private:
4423 Location source_;
4424 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01004425 // The type this move is for.
4426 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01004427 // The instruction this move is assocatied with. Null when this move is
4428 // for moving an input in the expected locations of user (including a phi user).
4429 // This is only used in debug mode, to ensure we do not connect interval siblings
4430 // in the same parallel move.
4431 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004432};
4433
4434static constexpr size_t kDefaultNumberOfMoves = 4;
4435
4436class HParallelMove : public HTemplateInstruction<0> {
4437 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004438 explicit HParallelMove(ArenaAllocator* arena)
4439 : HTemplateInstruction(SideEffects::None()), moves_(arena, kDefaultNumberOfMoves) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004440
Nicolas Geoffray90218252015-04-15 11:56:51 +01004441 void AddMove(Location source,
4442 Location destination,
4443 Primitive::Type type,
4444 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004445 DCHECK(source.IsValid());
4446 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004447 if (kIsDebugBuild) {
4448 if (instruction != nullptr) {
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00004449 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004450 if (moves_.Get(i).GetInstruction() == instruction) {
4451 // Special case the situation where the move is for the spill slot
4452 // of the instruction.
4453 if ((GetPrevious() == instruction)
4454 || ((GetPrevious() == nullptr)
4455 && instruction->IsPhi()
4456 && instruction->GetBlock() == GetBlock())) {
4457 DCHECK_NE(destination.GetKind(), moves_.Get(i).GetDestination().GetKind())
4458 << "Doing parallel moves for the same instruction.";
4459 } else {
4460 DCHECK(false) << "Doing parallel moves for the same instruction.";
4461 }
4462 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00004463 }
4464 }
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004465 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Zheng Xuad4450e2015-04-17 18:48:56 +08004466 DCHECK(!destination.OverlapsWith(moves_.Get(i).GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01004467 << "Overlapped destination for two moves in a parallel move: "
4468 << moves_.Get(i).GetSource() << " ==> " << moves_.Get(i).GetDestination() << " and "
4469 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004470 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01004471 }
Nicolas Geoffray90218252015-04-15 11:56:51 +01004472 moves_.Add(MoveOperands(source, destination, type, instruction));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004473 }
4474
4475 MoveOperands* MoveOperandsAt(size_t index) const {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004476 return moves_.GetRawStorage() + index;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004477 }
4478
4479 size_t NumMoves() const { return moves_.Size(); }
4480
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004481 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004482
4483 private:
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004484 GrowableArray<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004485
4486 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
4487};
4488
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004489class HGraphVisitor : public ValueObject {
4490 public:
Dave Allison20dfc792014-06-16 20:44:29 -07004491 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
4492 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004493
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004494 virtual void VisitInstruction(HInstruction* instruction) { UNUSED(instruction); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004495 virtual void VisitBasicBlock(HBasicBlock* block);
4496
Roland Levillain633021e2014-10-01 14:12:25 +01004497 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004498 void VisitInsertionOrder();
4499
Roland Levillain633021e2014-10-01 14:12:25 +01004500 // Visit the graph following dominator tree reverse post-order.
4501 void VisitReversePostOrder();
4502
Nicolas Geoffray787c3072014-03-17 10:20:19 +00004503 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00004504
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004505 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004506#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004507 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
4508
4509 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
4510
4511#undef DECLARE_VISIT_INSTRUCTION
4512
4513 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07004514 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004515
4516 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
4517};
4518
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004519class HGraphDelegateVisitor : public HGraphVisitor {
4520 public:
4521 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
4522 virtual ~HGraphDelegateVisitor() {}
4523
4524 // Visit functions that delegate to to super class.
4525#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004526 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004527
4528 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
4529
4530#undef DECLARE_VISIT_INSTRUCTION
4531
4532 private:
4533 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
4534};
4535
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004536class HInsertionOrderIterator : public ValueObject {
4537 public:
4538 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
4539
4540 bool Done() const { return index_ == graph_.GetBlocks().Size(); }
4541 HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); }
4542 void Advance() { ++index_; }
4543
4544 private:
4545 const HGraph& graph_;
4546 size_t index_;
4547
4548 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
4549};
4550
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004551class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004552 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00004553 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
4554 // Check that reverse post order of the graph has been built.
4555 DCHECK(!graph.GetReversePostOrder().IsEmpty());
4556 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004557
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004558 bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); }
4559 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004560 void Advance() { ++index_; }
4561
4562 private:
4563 const HGraph& graph_;
4564 size_t index_;
4565
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004566 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004567};
4568
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004569class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004570 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004571 explicit HPostOrderIterator(const HGraph& graph)
David Brazdil10f56cb2015-03-24 18:49:14 +00004572 : graph_(graph), index_(graph_.GetReversePostOrder().Size()) {
4573 // Check that reverse post order of the graph has been built.
4574 DCHECK(!graph.GetReversePostOrder().IsEmpty());
4575 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004576
4577 bool Done() const { return index_ == 0; }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004578 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004579 void Advance() { --index_; }
4580
4581 private:
4582 const HGraph& graph_;
4583 size_t index_;
4584
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004585 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004586};
4587
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01004588class HLinearPostOrderIterator : public ValueObject {
4589 public:
4590 explicit HLinearPostOrderIterator(const HGraph& graph)
4591 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().Size()) {}
4592
4593 bool Done() const { return index_ == 0; }
4594
4595 HBasicBlock* Current() const { return order_.Get(index_ -1); }
4596
4597 void Advance() {
4598 --index_;
4599 DCHECK_GE(index_, 0U);
4600 }
4601
4602 private:
4603 const GrowableArray<HBasicBlock*>& order_;
4604 size_t index_;
4605
4606 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
4607};
4608
4609class HLinearOrderIterator : public ValueObject {
4610 public:
4611 explicit HLinearOrderIterator(const HGraph& graph)
4612 : order_(graph.GetLinearOrder()), index_(0) {}
4613
4614 bool Done() const { return index_ == order_.Size(); }
4615 HBasicBlock* Current() const { return order_.Get(index_); }
4616 void Advance() { ++index_; }
4617
4618 private:
4619 const GrowableArray<HBasicBlock*>& order_;
4620 size_t index_;
4621
4622 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
4623};
4624
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004625// Iterator over the blocks that art part of the loop. Includes blocks part
4626// of an inner loop. The order in which the blocks are iterated is on their
4627// block id.
4628class HBlocksInLoopIterator : public ValueObject {
4629 public:
4630 explicit HBlocksInLoopIterator(const HLoopInformation& info)
4631 : blocks_in_loop_(info.GetBlocks()),
4632 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
4633 index_(0) {
4634 if (!blocks_in_loop_.IsBitSet(index_)) {
4635 Advance();
4636 }
4637 }
4638
4639 bool Done() const { return index_ == blocks_.Size(); }
4640 HBasicBlock* Current() const { return blocks_.Get(index_); }
4641 void Advance() {
4642 ++index_;
4643 for (size_t e = blocks_.Size(); index_ < e; ++index_) {
4644 if (blocks_in_loop_.IsBitSet(index_)) {
4645 break;
4646 }
4647 }
4648 }
4649
4650 private:
4651 const BitVector& blocks_in_loop_;
4652 const GrowableArray<HBasicBlock*>& blocks_;
4653 size_t index_;
4654
4655 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
4656};
4657
Mingyao Yang3584bce2015-05-19 16:01:59 -07004658// Iterator over the blocks that art part of the loop. Includes blocks part
4659// of an inner loop. The order in which the blocks are iterated is reverse
4660// post order.
4661class HBlocksInLoopReversePostOrderIterator : public ValueObject {
4662 public:
4663 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
4664 : blocks_in_loop_(info.GetBlocks()),
4665 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
4666 index_(0) {
4667 if (!blocks_in_loop_.IsBitSet(blocks_.Get(index_)->GetBlockId())) {
4668 Advance();
4669 }
4670 }
4671
4672 bool Done() const { return index_ == blocks_.Size(); }
4673 HBasicBlock* Current() const { return blocks_.Get(index_); }
4674 void Advance() {
4675 ++index_;
4676 for (size_t e = blocks_.Size(); index_ < e; ++index_) {
4677 if (blocks_in_loop_.IsBitSet(blocks_.Get(index_)->GetBlockId())) {
4678 break;
4679 }
4680 }
4681 }
4682
4683 private:
4684 const BitVector& blocks_in_loop_;
4685 const GrowableArray<HBasicBlock*>& blocks_;
4686 size_t index_;
4687
4688 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
4689};
4690
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00004691inline int64_t Int64FromConstant(HConstant* constant) {
4692 DCHECK(constant->IsIntConstant() || constant->IsLongConstant());
4693 return constant->IsIntConstant() ? constant->AsIntConstant()->GetValue()
4694 : constant->AsLongConstant()->GetValue();
4695}
4696
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004697} // namespace art
4698
4699#endif // ART_COMPILER_OPTIMIZING_NODES_H_