blob: 1190fae91465df57645539c3a9b5a68b4eefc75a [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 Juravle3fabec72015-07-16 16:51:30 +01001463 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact) {
1464 // The constructor will check that the type_handle is valid.
1465 return ReferenceTypeInfo(type_handle, is_exact);
Calin Juravleb1498f62015-02-16 13:13:29 +00001466 }
1467
Calin Juravle3fabec72015-07-16 16:51:30 +01001468 static ReferenceTypeInfo CreateInvalid() { return ReferenceTypeInfo(); }
1469
1470 static bool IsValidHandle(TypeHandle handle) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1471 return handle.GetReference() != nullptr;
Calin Juravleacf735c2015-02-12 15:25:22 +00001472 }
1473
Calin Juravle3fabec72015-07-16 16:51:30 +01001474 bool IsValid() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1475 return IsValidHandle(type_handle_);
1476 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001477 bool IsExact() const { return is_exact_; }
Calin Juravle3fabec72015-07-16 16:51:30 +01001478 bool IsObjectClass() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1479 DCHECK(IsValid());
1480 return GetTypeHandle()->IsObjectClass();
1481 }
Guillaume Sanchez222862c2015-06-09 18:33:02 +01001482 bool IsInterface() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Calin Juravle3fabec72015-07-16 16:51:30 +01001483 DCHECK(IsValid());
1484 return GetTypeHandle()->IsInterface();
Guillaume Sanchez222862c2015-06-09 18:33:02 +01001485 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001486
1487 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
1488
Calin Juravleb1498f62015-02-16 13:13:29 +00001489 bool IsSupertypeOf(ReferenceTypeInfo rti) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Calin Juravle3fabec72015-07-16 16:51:30 +01001490 DCHECK(IsValid());
1491 DCHECK(rti.IsValid());
Calin Juravleacf735c2015-02-12 15:25:22 +00001492 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
1493 }
1494
1495 // Returns true if the type information provide the same amount of details.
1496 // Note that it does not mean that the instructions have the same actual type
Calin Juravle3fabec72015-07-16 16:51:30 +01001497 // (because the type can be the result of a merge).
Calin Juravleacf735c2015-02-12 15:25:22 +00001498 bool IsEqual(ReferenceTypeInfo rti) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Calin Juravle3fabec72015-07-16 16:51:30 +01001499 if (!IsValid() && !rti.IsValid()) {
1500 // Invalid types are equal.
Calin Juravleacf735c2015-02-12 15:25:22 +00001501 return true;
1502 }
Calin Juravle3fabec72015-07-16 16:51:30 +01001503 if (!IsValid() || !rti.IsValid()) {
1504 // One is valid, the other not.
Calin Juravleacf735c2015-02-12 15:25:22 +00001505 return false;
1506 }
Calin Juravle3fabec72015-07-16 16:51:30 +01001507 return IsExact() == rti.IsExact()
1508 && GetTypeHandle().Get() == rti.GetTypeHandle().Get();
Calin Juravleacf735c2015-02-12 15:25:22 +00001509 }
1510
1511 private:
Calin Juravle3fabec72015-07-16 16:51:30 +01001512 ReferenceTypeInfo();
1513 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact);
Calin Juravleb1498f62015-02-16 13:13:29 +00001514
Calin Juravleacf735c2015-02-12 15:25:22 +00001515 // The class of the object.
Calin Juravleb1498f62015-02-16 13:13:29 +00001516 TypeHandle type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001517 // Whether or not the type is exact or a superclass of the actual type.
Calin Juravleb1498f62015-02-16 13:13:29 +00001518 // Whether or not we have any information about this type.
Calin Juravleacf735c2015-02-12 15:25:22 +00001519 bool is_exact_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001520};
1521
1522std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
1523
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001524class HInstruction : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001525 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001526 explicit HInstruction(SideEffects side_effects)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001527 : previous_(nullptr),
1528 next_(nullptr),
1529 block_(nullptr),
1530 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001531 ssa_index_(-1),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001532 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001533 locations_(nullptr),
1534 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001535 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001536 side_effects_(side_effects),
Calin Juravle3fabec72015-07-16 16:51:30 +01001537 reference_type_info_(ReferenceTypeInfo::CreateInvalid()) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001538
Dave Allison20dfc792014-06-16 20:44:29 -07001539 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001540
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001541#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001542 enum InstructionKind {
1543 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1544 };
1545#undef DECLARE_KIND
1546
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001547 HInstruction* GetNext() const { return next_; }
1548 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001549
Calin Juravle77520bc2015-01-12 18:45:46 +00001550 HInstruction* GetNextDisregardingMoves() const;
1551 HInstruction* GetPreviousDisregardingMoves() const;
1552
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001553 HBasicBlock* GetBlock() const { return block_; }
1554 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001555 bool IsInBlock() const { return block_ != nullptr; }
1556 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +01001557 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001558
Roland Levillain6b879dd2014-09-22 17:13:44 +01001559 virtual size_t InputCount() const = 0;
David Brazdil1abb4192015-02-17 18:33:36 +00001560 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001561
1562 virtual void Accept(HGraphVisitor* visitor) = 0;
1563 virtual const char* DebugName() const = 0;
1564
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001565 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
David Brazdil1abb4192015-02-17 18:33:36 +00001566 void SetRawInputAt(size_t index, HInstruction* input) {
1567 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1568 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001569
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001570 virtual bool NeedsEnvironment() const { return false; }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001571 virtual uint32_t GetDexPc() const {
1572 LOG(FATAL) << "GetDexPc() cannot be called on an instruction that"
1573 " does not need an environment";
1574 UNREACHABLE();
1575 }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001576 virtual bool IsControlFlow() const { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01001577 virtual bool CanThrow() const { return false; }
Aart Bik854a02b2015-07-14 16:07:00 -07001578
1579 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001580
Calin Juravle10e244f2015-01-26 18:54:32 +00001581 // Does not apply for all instructions, but having this at top level greatly
1582 // simplifies the null check elimination.
Calin Juravleb0d5fc02015-07-15 14:41:29 +01001583 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001584 virtual bool CanBeNull() const {
1585 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1586 return true;
1587 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001588
Calin Juravle641547a2015-04-21 22:08:51 +01001589 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj) const {
1590 UNUSED(obj);
1591 return false;
1592 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001593
Calin Juravle3fabec72015-07-16 16:51:30 +01001594 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00001595
Calin Juravle61d544b2015-02-23 16:46:57 +00001596 ReferenceTypeInfo GetReferenceTypeInfo() const {
1597 DCHECK_EQ(GetType(), Primitive::kPrimNot);
1598 return reference_type_info_;
1599 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001600
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001601 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001602 DCHECK(user != nullptr);
1603 HUseListNode<HInstruction*>* use =
1604 uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1605 user->SetRawInputRecordAt(index, HUserRecord<HInstruction*>(user->InputRecordAt(index), use));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001606 }
1607
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001608 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001609 DCHECK(user != nullptr);
David Brazdiled596192015-01-23 10:39:45 +00001610 HUseListNode<HEnvironment*>* env_use =
1611 env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1612 user->RecordEnvUse(env_use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001613 }
1614
David Brazdil1abb4192015-02-17 18:33:36 +00001615 void RemoveAsUserOfInput(size_t input) {
1616 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
1617 input_use.GetInstruction()->uses_.Remove(input_use.GetUseNode());
1618 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001619
David Brazdil1abb4192015-02-17 18:33:36 +00001620 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1621 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001622
David Brazdiled596192015-01-23 10:39:45 +00001623 bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); }
1624 bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); }
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001625 bool HasNonEnvironmentUses() const { return !uses_.IsEmpty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001626 bool HasOnlyOneNonEnvironmentUse() const {
1627 return !HasEnvironmentUses() && GetUses().HasOnlyOneUse();
1628 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001629
Roland Levillain6c82d402014-10-13 16:10:27 +01001630 // Does this instruction strictly dominate `other_instruction`?
1631 // Returns false if this instruction and `other_instruction` are the same.
1632 // Aborts if this instruction and `other_instruction` are both phis.
1633 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001634
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001635 int GetId() const { return id_; }
1636 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001637
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001638 int GetSsaIndex() const { return ssa_index_; }
1639 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1640 bool HasSsaIndex() const { return ssa_index_ != -1; }
1641
1642 bool HasEnvironment() const { return environment_ != nullptr; }
1643 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001644 // Set the `environment_` field. Raw because this method does not
1645 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001646 void SetRawEnvironment(HEnvironment* environment) {
1647 DCHECK(environment_ == nullptr);
1648 DCHECK_EQ(environment->GetHolder(), this);
1649 environment_ = environment;
1650 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001651
1652 // Set the environment of this instruction, copying it from `environment`. While
1653 // copying, the uses lists are being updated.
1654 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001655 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001656 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001657 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001658 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001659 if (environment->GetParent() != nullptr) {
1660 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1661 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001662 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001663
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001664 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
1665 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001666 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001667 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001668 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001669 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001670 if (environment->GetParent() != nullptr) {
1671 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1672 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001673 }
1674
Nicolas Geoffray39468442014-09-02 15:17:15 +01001675 // Returns the number of entries in the environment. Typically, that is the
1676 // number of dex registers in a method. It could be more in case of inlining.
1677 size_t EnvironmentSize() const;
1678
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001679 LocationSummary* GetLocations() const { return locations_; }
1680 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001681
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001682 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001683 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001684
Alexandre Rames188d4312015-04-09 18:30:21 +01001685 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
1686 // uses of this instruction by `other` are *not* updated.
1687 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
1688 ReplaceWith(other);
1689 other->ReplaceInput(this, use_index);
1690 }
1691
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001692 // Move `this` instruction before `cursor`.
1693 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001694
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001695#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01001696 bool Is##type() const { return (As##type() != nullptr); } \
1697 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001698 virtual H##type* As##type() { return nullptr; }
1699
1700 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
1701#undef INSTRUCTION_TYPE_CHECK
1702
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001703 // Returns whether the instruction can be moved within the graph.
1704 virtual bool CanBeMoved() const { return false; }
1705
1706 // Returns whether the two instructions are of the same kind.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001707 virtual bool InstructionTypeEquals(HInstruction* other) const {
1708 UNUSED(other);
1709 return false;
1710 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001711
1712 // Returns whether any data encoded in the two instructions is equal.
1713 // This method does not look at the inputs. Both instructions must be
1714 // of the same type, otherwise the method has undefined behavior.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001715 virtual bool InstructionDataEquals(HInstruction* other) const {
1716 UNUSED(other);
1717 return false;
1718 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001719
1720 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00001721 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001722 // 2) Their inputs are identical.
1723 bool Equals(HInstruction* other) const;
1724
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001725 virtual InstructionKind GetKind() const = 0;
1726
1727 virtual size_t ComputeHashCode() const {
1728 size_t result = GetKind();
1729 for (size_t i = 0, e = InputCount(); i < e; ++i) {
1730 result = (result * 31) + InputAt(i)->GetId();
1731 }
1732 return result;
1733 }
1734
1735 SideEffects GetSideEffects() const { return side_effects_; }
1736
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001737 size_t GetLifetimePosition() const { return lifetime_position_; }
1738 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
1739 LiveInterval* GetLiveInterval() const { return live_interval_; }
1740 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
1741 bool HasLiveInterval() const { return live_interval_ != nullptr; }
1742
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001743 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
1744
1745 // Returns whether the code generation of the instruction will require to have access
1746 // to the current method. Such instructions are:
1747 // (1): Instructions that require an environment, as calling the runtime requires
1748 // to walk the stack and have the current method stored at a specific stack address.
1749 // (2): Object literals like classes and strings, that are loaded from the dex cache
1750 // fields of the current method.
1751 bool NeedsCurrentMethod() const {
1752 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
1753 }
1754
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001755 virtual bool NeedsDexCache() const { return false; }
1756
Mark Mendellc4701932015-04-10 13:18:51 -04001757 // Does this instruction have any use in an environment before
1758 // control flow hits 'other'?
1759 bool HasAnyEnvironmentUseBefore(HInstruction* other);
1760
1761 // Remove all references to environment uses of this instruction.
1762 // The caller must ensure that this is safe to do.
1763 void RemoveEnvironmentUsers();
1764
David Brazdil1abb4192015-02-17 18:33:36 +00001765 protected:
1766 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
1767 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
1768
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001769 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001770 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); }
1771
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001772 HInstruction* previous_;
1773 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001774 HBasicBlock* block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001775
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001776 // An instruction gets an id when it is added to the graph.
1777 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01001778 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001779 int id_;
1780
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001781 // When doing liveness analysis, instructions that have uses get an SSA index.
1782 int ssa_index_;
1783
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001784 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00001785 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001786
1787 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00001788 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001789
Nicolas Geoffray39468442014-09-02 15:17:15 +01001790 // The environment associated with this instruction. Not null if the instruction
1791 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001792 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001793
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001794 // Set by the code generator.
1795 LocationSummary* locations_;
1796
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001797 // Set by the liveness analysis.
1798 LiveInterval* live_interval_;
1799
1800 // Set by the liveness analysis, this is the position in a linear
1801 // order of blocks where this instruction's live interval start.
1802 size_t lifetime_position_;
1803
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001804 const SideEffects side_effects_;
1805
Calin Juravleacf735c2015-02-12 15:25:22 +00001806 // TODO: for primitive types this should be marked as invalid.
1807 ReferenceTypeInfo reference_type_info_;
1808
David Brazdil1abb4192015-02-17 18:33:36 +00001809 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001810 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00001811 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001812 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001813 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001814
1815 DISALLOW_COPY_AND_ASSIGN(HInstruction);
1816};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001817std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001818
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001819class HInputIterator : public ValueObject {
1820 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001821 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001822
1823 bool Done() const { return index_ == instruction_->InputCount(); }
1824 HInstruction* Current() const { return instruction_->InputAt(index_); }
1825 void Advance() { index_++; }
1826
1827 private:
1828 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001829 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001830
1831 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
1832};
1833
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001834class HInstructionIterator : public ValueObject {
1835 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001836 explicit HInstructionIterator(const HInstructionList& instructions)
1837 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001838 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001839 }
1840
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001841 bool Done() const { return instruction_ == nullptr; }
1842 HInstruction* Current() const { return instruction_; }
1843 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001844 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001845 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001846 }
1847
1848 private:
1849 HInstruction* instruction_;
1850 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001851
1852 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001853};
1854
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001855class HBackwardInstructionIterator : public ValueObject {
1856 public:
1857 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
1858 : instruction_(instructions.last_instruction_) {
1859 next_ = Done() ? nullptr : instruction_->GetPrevious();
1860 }
1861
1862 bool Done() const { return instruction_ == nullptr; }
1863 HInstruction* Current() const { return instruction_; }
1864 void Advance() {
1865 instruction_ = next_;
1866 next_ = Done() ? nullptr : instruction_->GetPrevious();
1867 }
1868
1869 private:
1870 HInstruction* instruction_;
1871 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001872
1873 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001874};
1875
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001876// An embedded container with N elements of type T. Used (with partial
1877// specialization for N=0) because embedded arrays cannot have size 0.
1878template<typename T, intptr_t N>
1879class EmbeddedArray {
1880 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001881 EmbeddedArray() : elements_() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001882
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001883 intptr_t GetLength() const { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001884
1885 const T& operator[](intptr_t i) const {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001886 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001887 return elements_[i];
1888 }
1889
1890 T& operator[](intptr_t i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001891 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001892 return elements_[i];
1893 }
1894
1895 const T& At(intptr_t i) const {
1896 return (*this)[i];
1897 }
1898
1899 void SetAt(intptr_t i, const T& val) {
1900 (*this)[i] = val;
1901 }
1902
1903 private:
1904 T elements_[N];
1905};
1906
1907template<typename T>
1908class EmbeddedArray<T, 0> {
1909 public:
1910 intptr_t length() const { return 0; }
1911 const T& operator[](intptr_t i) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001912 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001913 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001914 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001915 }
1916 T& operator[](intptr_t i) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001917 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001918 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001919 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001920 }
1921};
1922
1923template<intptr_t N>
1924class HTemplateInstruction: public HInstruction {
1925 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001926 HTemplateInstruction<N>(SideEffects side_effects)
1927 : HInstruction(side_effects), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07001928 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001929
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001930 size_t InputCount() const OVERRIDE { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001931
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001932 protected:
David Brazdil1abb4192015-02-17 18:33:36 +00001933 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_[i]; }
1934
1935 void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE {
1936 inputs_[i] = input;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001937 }
1938
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001939 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001940 EmbeddedArray<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001941
1942 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001943};
1944
Dave Allison20dfc792014-06-16 20:44:29 -07001945template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001946class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07001947 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001948 HExpression<N>(Primitive::Type type, SideEffects side_effects)
1949 : HTemplateInstruction<N>(side_effects), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07001950 virtual ~HExpression() {}
1951
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001952 Primitive::Type GetType() const OVERRIDE { return type_; }
Dave Allison20dfc792014-06-16 20:44:29 -07001953
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001954 protected:
1955 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07001956};
1957
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001958// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
1959// instruction that branches to the exit block.
1960class HReturnVoid : public HTemplateInstruction<0> {
1961 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001962 HReturnVoid() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001963
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001964 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001965
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001966 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001967
1968 private:
1969 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
1970};
1971
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001972// Represents dex's RETURN opcodes. A HReturn is a control flow
1973// instruction that branches to the exit block.
1974class HReturn : public HTemplateInstruction<1> {
1975 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001976 explicit HReturn(HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001977 SetRawInputAt(0, value);
1978 }
1979
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001980 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001981
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001982 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001983
1984 private:
1985 DISALLOW_COPY_AND_ASSIGN(HReturn);
1986};
1987
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001988// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001989// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001990// exit block.
1991class HExit : public HTemplateInstruction<0> {
1992 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001993 HExit() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001994
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001995 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001996
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001997 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001998
1999 private:
2000 DISALLOW_COPY_AND_ASSIGN(HExit);
2001};
2002
2003// Jumps from one block to another.
2004class HGoto : public HTemplateInstruction<0> {
2005 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002006 HGoto() : HTemplateInstruction(SideEffects::None()) {}
2007
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002008 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002009
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002010 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002011 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002012 }
2013
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002014 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002015
2016 private:
2017 DISALLOW_COPY_AND_ASSIGN(HGoto);
2018};
2019
Dave Allison20dfc792014-06-16 20:44:29 -07002020
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002021// Conditional branch. A block ending with an HIf instruction must have
2022// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002023class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002024 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002025 explicit HIf(HInstruction* input) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002026 SetRawInputAt(0, input);
2027 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002028
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002029 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002030
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002031 HBasicBlock* IfTrueSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002032 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002033 }
2034
2035 HBasicBlock* IfFalseSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002036 return GetBlock()->GetSuccessors().Get(1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002037 }
2038
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002039 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002040
2041 private:
2042 DISALLOW_COPY_AND_ASSIGN(HIf);
2043};
2044
David Brazdilfc6a86a2015-06-26 10:33:45 +00002045
2046// Abstract instruction which marks the beginning and/or end of a try block and
2047// links it to the respective exception handlers. Behaves the same as a Goto in
2048// non-exceptional control flow.
2049// Normal-flow successor is stored at index zero, exception handlers under
2050// higher indices in no particular order.
2051class HTryBoundary : public HTemplateInstruction<0> {
2052 public:
David Brazdil56e1acc2015-06-30 15:41:36 +01002053 enum BoundaryKind {
2054 kEntry,
2055 kExit,
2056 };
2057
2058 explicit HTryBoundary(BoundaryKind kind)
2059 : HTemplateInstruction(SideEffects::None()), kind_(kind) {}
David Brazdilfc6a86a2015-06-26 10:33:45 +00002060
2061 bool IsControlFlow() const OVERRIDE { return true; }
2062
2063 // Returns the block's non-exceptional successor (index zero).
2064 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors().Get(0); }
2065
2066 // Returns whether `handler` is among its exception handlers (non-zero index
2067 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002068 bool HasExceptionHandler(const HBasicBlock& handler) const {
2069 DCHECK(handler.IsCatchBlock());
2070 return GetBlock()->GetSuccessors().Contains(
2071 const_cast<HBasicBlock*>(&handler), /* start_from */ 1);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002072 }
2073
2074 // If not present already, adds `handler` to its block's list of exception
2075 // handlers.
2076 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01002077 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002078 GetBlock()->AddSuccessor(handler);
2079 }
2080 }
2081
David Brazdil56e1acc2015-06-30 15:41:36 +01002082 bool IsEntry() const { return kind_ == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002083
David Brazdilffee3d32015-07-06 11:48:53 +01002084 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
2085
David Brazdilfc6a86a2015-06-26 10:33:45 +00002086 DECLARE_INSTRUCTION(TryBoundary);
2087
2088 private:
David Brazdil56e1acc2015-06-30 15:41:36 +01002089 const BoundaryKind kind_;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002090
2091 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2092};
2093
David Brazdilffee3d32015-07-06 11:48:53 +01002094// Iterator over exception handlers of a given HTryBoundary, i.e. over
2095// exceptional successors of its basic block.
2096class HExceptionHandlerIterator : public ValueObject {
2097 public:
2098 explicit HExceptionHandlerIterator(const HTryBoundary& try_boundary)
2099 : block_(*try_boundary.GetBlock()), index_(block_.NumberOfNormalSuccessors()) {}
2100
2101 bool Done() const { return index_ == block_.GetSuccessors().Size(); }
2102 HBasicBlock* Current() const { return block_.GetSuccessors().Get(index_); }
2103 size_t CurrentSuccessorIndex() const { return index_; }
2104 void Advance() { ++index_; }
2105
2106 private:
2107 const HBasicBlock& block_;
2108 size_t index_;
2109
2110 DISALLOW_COPY_AND_ASSIGN(HExceptionHandlerIterator);
2111};
David Brazdilfc6a86a2015-06-26 10:33:45 +00002112
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002113// Deoptimize to interpreter, upon checking a condition.
2114class HDeoptimize : public HTemplateInstruction<1> {
2115 public:
2116 HDeoptimize(HInstruction* cond, uint32_t dex_pc)
2117 : HTemplateInstruction(SideEffects::None()),
2118 dex_pc_(dex_pc) {
2119 SetRawInputAt(0, cond);
2120 }
2121
2122 bool NeedsEnvironment() const OVERRIDE { return true; }
2123 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002124 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002125
2126 DECLARE_INSTRUCTION(Deoptimize);
2127
2128 private:
2129 uint32_t dex_pc_;
2130
2131 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
2132};
2133
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002134// Represents the ArtMethod that was passed as a first argument to
2135// the method. It is used by instructions that depend on it, like
2136// instructions that work with the dex cache.
2137class HCurrentMethod : public HExpression<0> {
2138 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -07002139 explicit HCurrentMethod(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002140
2141 DECLARE_INSTRUCTION(CurrentMethod);
2142
2143 private:
2144 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
2145};
2146
Roland Levillain88cb1752014-10-20 16:36:47 +01002147class HUnaryOperation : public HExpression<1> {
2148 public:
2149 HUnaryOperation(Primitive::Type result_type, HInstruction* input)
2150 : HExpression(result_type, SideEffects::None()) {
2151 SetRawInputAt(0, input);
2152 }
2153
2154 HInstruction* GetInput() const { return InputAt(0); }
2155 Primitive::Type GetResultType() const { return GetType(); }
2156
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002157 bool CanBeMoved() const OVERRIDE { return true; }
2158 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002159 UNUSED(other);
2160 return true;
2161 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002162
Roland Levillain9240d6a2014-10-20 16:47:04 +01002163 // Try to statically evaluate `operation` and return a HConstant
2164 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002165 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002166 HConstant* TryStaticEvaluation() const;
2167
2168 // Apply this operation to `x`.
2169 virtual int32_t Evaluate(int32_t x) const = 0;
2170 virtual int64_t Evaluate(int64_t x) const = 0;
2171
Roland Levillain88cb1752014-10-20 16:36:47 +01002172 DECLARE_INSTRUCTION(UnaryOperation);
2173
2174 private:
2175 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
2176};
2177
Dave Allison20dfc792014-06-16 20:44:29 -07002178class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002179 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002180 HBinaryOperation(Primitive::Type result_type,
2181 HInstruction* left,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002182 HInstruction* right) : HExpression(result_type, SideEffects::None()) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002183 SetRawInputAt(0, left);
2184 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002185 }
2186
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002187 HInstruction* GetLeft() const { return InputAt(0); }
2188 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07002189 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002190
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002191 virtual bool IsCommutative() const { return false; }
2192
2193 // Put constant on the right.
2194 // Returns whether order is changed.
2195 bool OrderInputsWithConstantOnTheRight() {
2196 HInstruction* left = InputAt(0);
2197 HInstruction* right = InputAt(1);
2198 if (left->IsConstant() && !right->IsConstant()) {
2199 ReplaceInput(right, 0);
2200 ReplaceInput(left, 1);
2201 return true;
2202 }
2203 return false;
2204 }
2205
2206 // Order inputs by instruction id, but favor constant on the right side.
2207 // This helps GVN for commutative ops.
2208 void OrderInputs() {
2209 DCHECK(IsCommutative());
2210 HInstruction* left = InputAt(0);
2211 HInstruction* right = InputAt(1);
2212 if (left == right || (!left->IsConstant() && right->IsConstant())) {
2213 return;
2214 }
2215 if (OrderInputsWithConstantOnTheRight()) {
2216 return;
2217 }
2218 // Order according to instruction id.
2219 if (left->GetId() > right->GetId()) {
2220 ReplaceInput(right, 0);
2221 ReplaceInput(left, 1);
2222 }
2223 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002224
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002225 bool CanBeMoved() const OVERRIDE { return true; }
2226 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002227 UNUSED(other);
2228 return true;
2229 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002230
Roland Levillain9240d6a2014-10-20 16:47:04 +01002231 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01002232 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002233 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002234 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01002235
2236 // Apply this operation to `x` and `y`.
2237 virtual int32_t Evaluate(int32_t x, int32_t y) const = 0;
2238 virtual int64_t Evaluate(int64_t x, int64_t y) const = 0;
2239
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002240 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002241 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002242 HConstant* GetConstantRight() const;
2243
2244 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002245 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002246 HInstruction* GetLeastConstantLeft() const;
2247
Roland Levillainccc07a92014-09-16 14:48:16 +01002248 DECLARE_INSTRUCTION(BinaryOperation);
2249
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002250 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002251 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
2252};
2253
Mark Mendellc4701932015-04-10 13:18:51 -04002254// The comparison bias applies for floating point operations and indicates how NaN
2255// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01002256enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04002257 kNoBias, // bias is not applicable (i.e. for long operation)
2258 kGtBias, // return 1 for NaN comparisons
2259 kLtBias, // return -1 for NaN comparisons
2260};
2261
Dave Allison20dfc792014-06-16 20:44:29 -07002262class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002263 public:
Dave Allison20dfc792014-06-16 20:44:29 -07002264 HCondition(HInstruction* first, HInstruction* second)
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002265 : HBinaryOperation(Primitive::kPrimBoolean, first, second),
Mark Mendellc4701932015-04-10 13:18:51 -04002266 needs_materialization_(true),
Roland Levillain4fa13f62015-07-06 18:11:54 +01002267 bias_(ComparisonBias::kNoBias) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002268
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002269 bool NeedsMaterialization() const { return needs_materialization_; }
2270 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002271
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002272 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002273 // `instruction`, and disregard moves in between.
2274 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002275
Dave Allison20dfc792014-06-16 20:44:29 -07002276 DECLARE_INSTRUCTION(Condition);
2277
2278 virtual IfCondition GetCondition() const = 0;
2279
Mark Mendellc4701932015-04-10 13:18:51 -04002280 virtual IfCondition GetOppositeCondition() const = 0;
2281
Roland Levillain4fa13f62015-07-06 18:11:54 +01002282 bool IsGtBias() const { return bias_ == ComparisonBias::kGtBias; }
Mark Mendellc4701932015-04-10 13:18:51 -04002283
2284 void SetBias(ComparisonBias bias) { bias_ = bias; }
2285
2286 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2287 return bias_ == other->AsCondition()->bias_;
2288 }
2289
Roland Levillain4fa13f62015-07-06 18:11:54 +01002290 bool IsFPConditionTrueIfNaN() const {
2291 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2292 IfCondition if_cond = GetCondition();
2293 return IsGtBias() ? ((if_cond == kCondGT) || (if_cond == kCondGE)) : (if_cond == kCondNE);
2294 }
2295
2296 bool IsFPConditionFalseIfNaN() const {
2297 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2298 IfCondition if_cond = GetCondition();
2299 return IsGtBias() ? ((if_cond == kCondLT) || (if_cond == kCondLE)) : (if_cond == kCondEQ);
2300 }
2301
Dave Allison20dfc792014-06-16 20:44:29 -07002302 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002303 // For register allocation purposes, returns whether this instruction needs to be
2304 // materialized (that is, not just be in the processor flags).
2305 bool needs_materialization_;
2306
Mark Mendellc4701932015-04-10 13:18:51 -04002307 // Needed if we merge a HCompare into a HCondition.
2308 ComparisonBias bias_;
2309
Dave Allison20dfc792014-06-16 20:44:29 -07002310 DISALLOW_COPY_AND_ASSIGN(HCondition);
2311};
2312
2313// Instruction to check if two inputs are equal to each other.
2314class HEqual : public HCondition {
2315 public:
2316 HEqual(HInstruction* first, HInstruction* second)
2317 : HCondition(first, second) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002318
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002319 bool IsCommutative() const OVERRIDE { return true; }
2320
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002321 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002322 return x == y ? 1 : 0;
2323 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002324 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002325 return x == y ? 1 : 0;
2326 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002327
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002328 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002329
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002330 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002331 return kCondEQ;
2332 }
2333
Mark Mendellc4701932015-04-10 13:18:51 -04002334 IfCondition GetOppositeCondition() const OVERRIDE {
2335 return kCondNE;
2336 }
2337
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002338 private:
2339 DISALLOW_COPY_AND_ASSIGN(HEqual);
2340};
2341
Dave Allison20dfc792014-06-16 20:44:29 -07002342class HNotEqual : public HCondition {
2343 public:
2344 HNotEqual(HInstruction* first, HInstruction* second)
2345 : HCondition(first, second) {}
2346
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002347 bool IsCommutative() const OVERRIDE { return true; }
2348
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002349 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002350 return x != y ? 1 : 0;
2351 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002352 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002353 return x != y ? 1 : 0;
2354 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002355
Dave Allison20dfc792014-06-16 20:44:29 -07002356 DECLARE_INSTRUCTION(NotEqual);
2357
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002358 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002359 return kCondNE;
2360 }
2361
Mark Mendellc4701932015-04-10 13:18:51 -04002362 IfCondition GetOppositeCondition() const OVERRIDE {
2363 return kCondEQ;
2364 }
2365
Dave Allison20dfc792014-06-16 20:44:29 -07002366 private:
2367 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
2368};
2369
2370class HLessThan : public HCondition {
2371 public:
2372 HLessThan(HInstruction* first, HInstruction* second)
2373 : HCondition(first, second) {}
2374
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002375 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002376 return x < y ? 1 : 0;
2377 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002378 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002379 return x < y ? 1 : 0;
2380 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002381
Dave Allison20dfc792014-06-16 20:44:29 -07002382 DECLARE_INSTRUCTION(LessThan);
2383
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002384 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002385 return kCondLT;
2386 }
2387
Mark Mendellc4701932015-04-10 13:18:51 -04002388 IfCondition GetOppositeCondition() const OVERRIDE {
2389 return kCondGE;
2390 }
2391
Dave Allison20dfc792014-06-16 20:44:29 -07002392 private:
2393 DISALLOW_COPY_AND_ASSIGN(HLessThan);
2394};
2395
2396class HLessThanOrEqual : public HCondition {
2397 public:
2398 HLessThanOrEqual(HInstruction* first, HInstruction* second)
2399 : HCondition(first, second) {}
2400
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002401 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002402 return x <= y ? 1 : 0;
2403 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002404 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002405 return x <= y ? 1 : 0;
2406 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002407
Dave Allison20dfc792014-06-16 20:44:29 -07002408 DECLARE_INSTRUCTION(LessThanOrEqual);
2409
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002410 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002411 return kCondLE;
2412 }
2413
Mark Mendellc4701932015-04-10 13:18:51 -04002414 IfCondition GetOppositeCondition() const OVERRIDE {
2415 return kCondGT;
2416 }
2417
Dave Allison20dfc792014-06-16 20:44:29 -07002418 private:
2419 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
2420};
2421
2422class HGreaterThan : public HCondition {
2423 public:
2424 HGreaterThan(HInstruction* first, HInstruction* second)
2425 : HCondition(first, second) {}
2426
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002427 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002428 return x > y ? 1 : 0;
2429 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002430 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002431 return x > y ? 1 : 0;
2432 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002433
Dave Allison20dfc792014-06-16 20:44:29 -07002434 DECLARE_INSTRUCTION(GreaterThan);
2435
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002436 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002437 return kCondGT;
2438 }
2439
Mark Mendellc4701932015-04-10 13:18:51 -04002440 IfCondition GetOppositeCondition() const OVERRIDE {
2441 return kCondLE;
2442 }
2443
Dave Allison20dfc792014-06-16 20:44:29 -07002444 private:
2445 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
2446};
2447
2448class HGreaterThanOrEqual : public HCondition {
2449 public:
2450 HGreaterThanOrEqual(HInstruction* first, HInstruction* second)
2451 : HCondition(first, second) {}
2452
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002453 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002454 return x >= y ? 1 : 0;
2455 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002456 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002457 return x >= y ? 1 : 0;
2458 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002459
Dave Allison20dfc792014-06-16 20:44:29 -07002460 DECLARE_INSTRUCTION(GreaterThanOrEqual);
2461
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002462 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002463 return kCondGE;
2464 }
2465
Mark Mendellc4701932015-04-10 13:18:51 -04002466 IfCondition GetOppositeCondition() const OVERRIDE {
2467 return kCondLT;
2468 }
2469
Dave Allison20dfc792014-06-16 20:44:29 -07002470 private:
2471 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
2472};
2473
2474
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002475// Instruction to check how two inputs compare to each other.
2476// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
2477class HCompare : public HBinaryOperation {
2478 public:
Alexey Frunze4dda3372015-06-01 18:31:49 -07002479 HCompare(Primitive::Type type,
2480 HInstruction* first,
2481 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04002482 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07002483 uint32_t dex_pc)
2484 : HBinaryOperation(Primitive::kPrimInt, first, second), bias_(bias), dex_pc_(dex_pc) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002485 DCHECK_EQ(type, first->GetType());
2486 DCHECK_EQ(type, second->GetType());
2487 }
2488
Calin Juravleddb7df22014-11-25 20:56:51 +00002489 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01002490 return
2491 x == y ? 0 :
2492 x > y ? 1 :
2493 -1;
2494 }
Calin Juravleddb7df22014-11-25 20:56:51 +00002495
2496 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01002497 return
2498 x == y ? 0 :
2499 x > y ? 1 :
2500 -1;
2501 }
2502
Calin Juravleddb7df22014-11-25 20:56:51 +00002503 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2504 return bias_ == other->AsCompare()->bias_;
2505 }
2506
Mark Mendellc4701932015-04-10 13:18:51 -04002507 ComparisonBias GetBias() const { return bias_; }
2508
Roland Levillain4fa13f62015-07-06 18:11:54 +01002509 bool IsGtBias() { return bias_ == ComparisonBias::kGtBias; }
Calin Juravleddb7df22014-11-25 20:56:51 +00002510
Alexey Frunze4dda3372015-06-01 18:31:49 -07002511 uint32_t GetDexPc() const { return dex_pc_; }
2512
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002513 DECLARE_INSTRUCTION(Compare);
2514
2515 private:
Mark Mendellc4701932015-04-10 13:18:51 -04002516 const ComparisonBias bias_;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002517 const uint32_t dex_pc_;
Calin Juravleddb7df22014-11-25 20:56:51 +00002518
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002519 DISALLOW_COPY_AND_ASSIGN(HCompare);
2520};
2521
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002522// A local in the graph. Corresponds to a Dex register.
2523class HLocal : public HTemplateInstruction<0> {
2524 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002525 explicit HLocal(uint16_t reg_number)
2526 : HTemplateInstruction(SideEffects::None()), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002527
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002528 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002529
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002530 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002531
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002532 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002533 // The Dex register number.
2534 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002535
2536 DISALLOW_COPY_AND_ASSIGN(HLocal);
2537};
2538
2539// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07002540class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002541 public:
Roland Levillain5799fc02014-09-25 12:15:20 +01002542 HLoadLocal(HLocal* local, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002543 : HExpression(type, SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002544 SetRawInputAt(0, local);
2545 }
2546
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002547 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
2548
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002549 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002550
2551 private:
2552 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
2553};
2554
2555// Store a value in a given local. This instruction has two inputs: the value
2556// and the local.
2557class HStoreLocal : public HTemplateInstruction<2> {
2558 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002559 HStoreLocal(HLocal* local, HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002560 SetRawInputAt(0, local);
2561 SetRawInputAt(1, value);
2562 }
2563
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002564 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
2565
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002566 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002567
2568 private:
2569 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
2570};
2571
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002572class HConstant : public HExpression<0> {
2573 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002574 explicit HConstant(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
2575
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002576 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002577
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002578 virtual bool IsMinusOne() const { return false; }
2579 virtual bool IsZero() const { return false; }
2580 virtual bool IsOne() const { return false; }
2581
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002582 DECLARE_INSTRUCTION(Constant);
2583
2584 private:
2585 DISALLOW_COPY_AND_ASSIGN(HConstant);
2586};
2587
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002588class HFloatConstant : public HConstant {
2589 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002590 float GetValue() const { return value_; }
2591
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002592 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002593 return bit_cast<uint32_t, float>(other->AsFloatConstant()->value_) ==
2594 bit_cast<uint32_t, float>(value_);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002595 }
2596
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002597 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002598
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002599 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002600 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002601 }
2602 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002603 return value_ == 0.0f;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002604 }
2605 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002606 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
2607 }
2608 bool IsNaN() const {
2609 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002610 }
2611
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002612 DECLARE_INSTRUCTION(FloatConstant);
2613
2614 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002615 explicit HFloatConstant(float value) : HConstant(Primitive::kPrimFloat), value_(value) {}
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002616 explicit HFloatConstant(int32_t value)
2617 : HConstant(Primitive::kPrimFloat), value_(bit_cast<float, int32_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00002618
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002619 const float value_;
2620
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002621 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00002622 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002623 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002624 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2625};
2626
2627class HDoubleConstant : public HConstant {
2628 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002629 double GetValue() const { return value_; }
2630
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002631 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002632 return bit_cast<uint64_t, double>(other->AsDoubleConstant()->value_) ==
2633 bit_cast<uint64_t, double>(value_);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002634 }
2635
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002636 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002637
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002638 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002639 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002640 }
2641 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002642 return value_ == 0.0;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002643 }
2644 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002645 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
2646 }
2647 bool IsNaN() const {
2648 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002649 }
2650
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002651 DECLARE_INSTRUCTION(DoubleConstant);
2652
2653 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002654 explicit HDoubleConstant(double value) : HConstant(Primitive::kPrimDouble), value_(value) {}
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002655 explicit HDoubleConstant(int64_t value)
2656 : HConstant(Primitive::kPrimDouble), value_(bit_cast<double, int64_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00002657
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002658 const double value_;
2659
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002660 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00002661 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002662 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002663 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
2664};
2665
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002666class HNullConstant : public HConstant {
2667 public:
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002668 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2669 return true;
2670 }
2671
2672 size_t ComputeHashCode() const OVERRIDE { return 0; }
2673
2674 DECLARE_INSTRUCTION(NullConstant);
2675
2676 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002677 HNullConstant() : HConstant(Primitive::kPrimNot) {}
2678
2679 friend class HGraph;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002680 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2681};
2682
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002683// Constants of the type int. Those can be from Dex instructions, or
2684// synthesized (for example with the if-eqz instruction).
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002685class HIntConstant : public HConstant {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002686 public:
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002687 int32_t GetValue() const { return value_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002688
Calin Juravle61d544b2015-02-23 16:46:57 +00002689 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002690 return other->AsIntConstant()->value_ == value_;
2691 }
2692
Calin Juravle61d544b2015-02-23 16:46:57 +00002693 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2694
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002695 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2696 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2697 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2698
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002699 DECLARE_INSTRUCTION(IntConstant);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002700
2701 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002702 explicit HIntConstant(int32_t value) : HConstant(Primitive::kPrimInt), value_(value) {}
2703
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002704 const int32_t value_;
2705
David Brazdil8d5b8b22015-03-24 10:51:52 +00002706 friend class HGraph;
2707 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
Zheng Xuad4450e2015-04-17 18:48:56 +08002708 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002709 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2710};
2711
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002712class HLongConstant : public HConstant {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002713 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002714 int64_t GetValue() const { return value_; }
2715
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002716 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002717 return other->AsLongConstant()->value_ == value_;
2718 }
2719
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002720 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002721
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002722 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2723 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2724 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2725
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002726 DECLARE_INSTRUCTION(LongConstant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002727
2728 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002729 explicit HLongConstant(int64_t value) : HConstant(Primitive::kPrimLong), value_(value) {}
2730
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002731 const int64_t value_;
2732
David Brazdil8d5b8b22015-03-24 10:51:52 +00002733 friend class HGraph;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002734 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2735};
2736
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002737enum class Intrinsics {
2738#define OPTIMIZING_INTRINSICS(Name, IsStatic) k ## Name,
2739#include "intrinsics_list.h"
2740 kNone,
2741 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
2742#undef INTRINSICS_LIST
2743#undef OPTIMIZING_INTRINSICS
2744};
2745std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
2746
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002747class HInvoke : public HInstruction {
2748 public:
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002749 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002750
2751 // Runtime needs to walk the stack, so Dex -> Dex calls need to
2752 // know their environment.
Calin Juravle77520bc2015-01-12 18:45:46 +00002753 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002754
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01002755 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002756 SetRawInputAt(index, argument);
2757 }
2758
Roland Levillain3e3d7332015-04-28 11:00:54 +01002759 // Return the number of arguments. This number can be lower than
2760 // the number of inputs returned by InputCount(), as some invoke
2761 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
2762 // inputs at the end of their list of inputs.
2763 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
2764
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002765 Primitive::Type GetType() const OVERRIDE { return return_type_; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002766
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002767 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002768
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002769 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01002770 const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002771
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002772 InvokeType GetOriginalInvokeType() const { return original_invoke_type_; }
2773
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01002774 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002775 return intrinsic_;
2776 }
2777
2778 void SetIntrinsic(Intrinsics intrinsic) {
2779 intrinsic_ = intrinsic;
2780 }
2781
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01002782 bool IsFromInlinedInvoke() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002783 return GetEnvironment()->GetParent() != nullptr;
2784 }
2785
2786 bool CanThrow() const OVERRIDE { return true; }
2787
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002788 DECLARE_INSTRUCTION(Invoke);
2789
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002790 protected:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002791 HInvoke(ArenaAllocator* arena,
2792 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01002793 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002794 Primitive::Type return_type,
2795 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002796 uint32_t dex_method_index,
2797 InvokeType original_invoke_type)
Aart Bik854a02b2015-07-14 16:07:00 -07002798 : HInstruction(SideEffects::All()), // assume write/read on all fields/arrays
Roland Levillain3e3d7332015-04-28 11:00:54 +01002799 number_of_arguments_(number_of_arguments),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002800 inputs_(arena, number_of_arguments),
2801 return_type_(return_type),
2802 dex_pc_(dex_pc),
2803 dex_method_index_(dex_method_index),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002804 original_invoke_type_(original_invoke_type),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002805 intrinsic_(Intrinsics::kNone) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01002806 uint32_t number_of_inputs = number_of_arguments + number_of_other_inputs;
2807 inputs_.SetSize(number_of_inputs);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002808 }
2809
David Brazdil1abb4192015-02-17 18:33:36 +00002810 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
2811 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
2812 inputs_.Put(index, input);
2813 }
2814
Roland Levillain3e3d7332015-04-28 11:00:54 +01002815 uint32_t number_of_arguments_;
David Brazdil1abb4192015-02-17 18:33:36 +00002816 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002817 const Primitive::Type return_type_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002818 const uint32_t dex_pc_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002819 const uint32_t dex_method_index_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002820 const InvokeType original_invoke_type_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002821 Intrinsics intrinsic_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002822
2823 private:
2824 DISALLOW_COPY_AND_ASSIGN(HInvoke);
2825};
2826
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002827class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002828 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01002829 // Requirements of this method call regarding the class
2830 // initialization (clinit) check of its declaring class.
2831 enum class ClinitCheckRequirement {
2832 kNone, // Class already initialized.
2833 kExplicit, // Static call having explicit clinit check as last input.
2834 kImplicit, // Static call implicitly requiring a clinit check.
2835 };
2836
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002837 HInvokeStaticOrDirect(ArenaAllocator* arena,
2838 uint32_t number_of_arguments,
2839 Primitive::Type return_type,
2840 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002841 uint32_t dex_method_index,
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002842 bool is_recursive,
Jeff Hao848f70a2014-01-15 13:49:50 -08002843 int32_t string_init_offset,
Nicolas Geoffray79041292015-03-26 10:05:54 +00002844 InvokeType original_invoke_type,
Roland Levillain4c0eb422015-04-24 16:43:49 +01002845 InvokeType invoke_type,
2846 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01002847 : HInvoke(arena,
2848 number_of_arguments,
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01002849 // There is one extra argument for the HCurrentMethod node, and
2850 // potentially one other if the clinit check is explicit, and one other
2851 // if the method is a string factory.
2852 1u + (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u)
2853 + (string_init_offset ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01002854 return_type,
2855 dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002856 dex_method_index,
2857 original_invoke_type),
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002858 invoke_type_(invoke_type),
Roland Levillain4c0eb422015-04-24 16:43:49 +01002859 is_recursive_(is_recursive),
Jeff Hao848f70a2014-01-15 13:49:50 -08002860 clinit_check_requirement_(clinit_check_requirement),
2861 string_init_offset_(string_init_offset) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002862
Calin Juravle641547a2015-04-21 22:08:51 +01002863 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
2864 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00002865 // We access the method via the dex cache so we can't do an implicit null check.
2866 // TODO: for intrinsics we can generate implicit null checks.
2867 return false;
2868 }
2869
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002870 InvokeType GetInvokeType() const { return invoke_type_; }
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002871 bool IsRecursive() const { return is_recursive_; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002872 bool NeedsDexCache() const OVERRIDE { return !IsRecursive(); }
Jeff Hao848f70a2014-01-15 13:49:50 -08002873 bool IsStringInit() const { return string_init_offset_ != 0; }
2874 int32_t GetStringInitOffset() const { return string_init_offset_; }
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002875 uint32_t GetCurrentMethodInputIndex() const { return GetNumberOfArguments(); }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002876
Roland Levillain4c0eb422015-04-24 16:43:49 +01002877 // Is this instruction a call to a static method?
2878 bool IsStatic() const {
2879 return GetInvokeType() == kStatic;
2880 }
2881
Roland Levillain3e3d7332015-04-28 11:00:54 +01002882 // Remove the art::HLoadClass instruction set as last input by
2883 // art::PrepareForRegisterAllocation::VisitClinitCheck in lieu of
2884 // the initial art::HClinitCheck instruction (only relevant for
2885 // static calls with explicit clinit check).
2886 void RemoveLoadClassAsLastInput() {
Roland Levillain4c0eb422015-04-24 16:43:49 +01002887 DCHECK(IsStaticWithExplicitClinitCheck());
2888 size_t last_input_index = InputCount() - 1;
2889 HInstruction* last_input = InputAt(last_input_index);
2890 DCHECK(last_input != nullptr);
Roland Levillain3e3d7332015-04-28 11:00:54 +01002891 DCHECK(last_input->IsLoadClass()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01002892 RemoveAsUserOfInput(last_input_index);
2893 inputs_.DeleteAt(last_input_index);
2894 clinit_check_requirement_ = ClinitCheckRequirement::kImplicit;
2895 DCHECK(IsStaticWithImplicitClinitCheck());
2896 }
2897
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01002898 bool IsStringFactoryFor(HFakeString* str) const {
2899 if (!IsStringInit()) return false;
2900 // +1 for the current method.
2901 if (InputCount() == (number_of_arguments_ + 1)) return false;
2902 return InputAt(InputCount() - 1)->AsFakeString() == str;
2903 }
2904
2905 void RemoveFakeStringArgumentAsLastInput() {
2906 DCHECK(IsStringInit());
2907 size_t last_input_index = InputCount() - 1;
2908 HInstruction* last_input = InputAt(last_input_index);
2909 DCHECK(last_input != nullptr);
2910 DCHECK(last_input->IsFakeString()) << last_input->DebugName();
2911 RemoveAsUserOfInput(last_input_index);
2912 inputs_.DeleteAt(last_input_index);
2913 }
2914
Roland Levillain4c0eb422015-04-24 16:43:49 +01002915 // Is this a call to a static method whose declaring class has an
2916 // explicit intialization check in the graph?
2917 bool IsStaticWithExplicitClinitCheck() const {
2918 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kExplicit);
2919 }
2920
2921 // Is this a call to a static method whose declaring class has an
2922 // implicit intialization check requirement?
2923 bool IsStaticWithImplicitClinitCheck() const {
2924 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kImplicit);
2925 }
2926
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002927 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002928
Roland Levillain4c0eb422015-04-24 16:43:49 +01002929 protected:
2930 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
2931 const HUserRecord<HInstruction*> input_record = HInvoke::InputRecordAt(i);
2932 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck() && (i == InputCount() - 1)) {
2933 HInstruction* input = input_record.GetInstruction();
2934 // `input` is the last input of a static invoke marked as having
2935 // an explicit clinit check. It must either be:
2936 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
2937 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
2938 DCHECK(input != nullptr);
2939 DCHECK(input->IsClinitCheck() || input->IsLoadClass()) << input->DebugName();
2940 }
2941 return input_record;
2942 }
2943
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002944 private:
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002945 const InvokeType invoke_type_;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002946 const bool is_recursive_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01002947 ClinitCheckRequirement clinit_check_requirement_;
Jeff Hao848f70a2014-01-15 13:49:50 -08002948 // Thread entrypoint offset for string init method if this is a string init invoke.
2949 // Note that there are multiple string init methods, each having its own offset.
2950 int32_t string_init_offset_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002951
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002952 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002953};
2954
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002955class HInvokeVirtual : public HInvoke {
2956 public:
2957 HInvokeVirtual(ArenaAllocator* arena,
2958 uint32_t number_of_arguments,
2959 Primitive::Type return_type,
2960 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002961 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002962 uint32_t vtable_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002963 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002964 vtable_index_(vtable_index) {}
2965
Calin Juravle641547a2015-04-21 22:08:51 +01002966 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00002967 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01002968 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00002969 }
2970
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002971 uint32_t GetVTableIndex() const { return vtable_index_; }
2972
2973 DECLARE_INSTRUCTION(InvokeVirtual);
2974
2975 private:
2976 const uint32_t vtable_index_;
2977
2978 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
2979};
2980
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002981class HInvokeInterface : public HInvoke {
2982 public:
2983 HInvokeInterface(ArenaAllocator* arena,
2984 uint32_t number_of_arguments,
2985 Primitive::Type return_type,
2986 uint32_t dex_pc,
2987 uint32_t dex_method_index,
2988 uint32_t imt_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002989 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002990 imt_index_(imt_index) {}
2991
Calin Juravle641547a2015-04-21 22:08:51 +01002992 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00002993 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01002994 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00002995 }
2996
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002997 uint32_t GetImtIndex() const { return imt_index_; }
2998 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
2999
3000 DECLARE_INSTRUCTION(InvokeInterface);
3001
3002 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003003 const uint32_t imt_index_;
3004
3005 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
3006};
3007
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003008class HNewInstance : public HExpression<1> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003009 public:
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003010 HNewInstance(HCurrentMethod* current_method,
3011 uint32_t dex_pc,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003012 uint16_t type_index,
3013 const DexFile& dex_file,
3014 QuickEntrypointEnum entrypoint)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003015 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3016 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003017 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003018 dex_file_(dex_file),
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003019 entrypoint_(entrypoint) {
3020 SetRawInputAt(0, current_method);
3021 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003022
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003023 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003024 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003025 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003026
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003027 // Calls runtime so needs an environment.
Calin Juravle92a6ed22014-12-02 18:58:03 +00003028 bool NeedsEnvironment() const OVERRIDE { return true; }
3029 // It may throw when called on:
3030 // - interfaces
3031 // - abstract/innaccessible/unknown classes
3032 // TODO: optimize when possible.
3033 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003034
Calin Juravle10e244f2015-01-26 18:54:32 +00003035 bool CanBeNull() const OVERRIDE { return false; }
3036
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003037 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3038
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003039 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003040
3041 private:
3042 const uint32_t dex_pc_;
3043 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003044 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003045 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003046
3047 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3048};
3049
Roland Levillain88cb1752014-10-20 16:36:47 +01003050class HNeg : public HUnaryOperation {
3051 public:
3052 explicit HNeg(Primitive::Type result_type, HInstruction* input)
3053 : HUnaryOperation(result_type, input) {}
3054
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003055 int32_t Evaluate(int32_t x) const OVERRIDE { return -x; }
3056 int64_t Evaluate(int64_t x) const OVERRIDE { return -x; }
Roland Levillain9240d6a2014-10-20 16:47:04 +01003057
Roland Levillain88cb1752014-10-20 16:36:47 +01003058 DECLARE_INSTRUCTION(Neg);
3059
3060 private:
3061 DISALLOW_COPY_AND_ASSIGN(HNeg);
3062};
3063
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003064class HNewArray : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003065 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003066 HNewArray(HInstruction* length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003067 HCurrentMethod* current_method,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003068 uint32_t dex_pc,
3069 uint16_t type_index,
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003070 const DexFile& dex_file,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003071 QuickEntrypointEnum entrypoint)
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003072 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3073 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003074 type_index_(type_index),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003075 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003076 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003077 SetRawInputAt(0, length);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003078 SetRawInputAt(1, current_method);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003079 }
3080
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003081 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003082 uint16_t GetTypeIndex() const { return type_index_; }
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003083 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003084
3085 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00003086 bool NeedsEnvironment() const OVERRIDE { return true; }
3087
Mingyao Yang0c365e62015-03-31 15:09:29 -07003088 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
3089 bool CanThrow() const OVERRIDE { return true; }
3090
Calin Juravle10e244f2015-01-26 18:54:32 +00003091 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003092
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003093 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3094
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003095 DECLARE_INSTRUCTION(NewArray);
3096
3097 private:
3098 const uint32_t dex_pc_;
3099 const uint16_t type_index_;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003100 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003101 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003102
3103 DISALLOW_COPY_AND_ASSIGN(HNewArray);
3104};
3105
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003106class HAdd : public HBinaryOperation {
3107 public:
3108 HAdd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3109 : HBinaryOperation(result_type, left, right) {}
3110
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003111 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003112
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003113 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01003114 return x + y;
3115 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003116 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01003117 return x + y;
3118 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003119
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003120 DECLARE_INSTRUCTION(Add);
3121
3122 private:
3123 DISALLOW_COPY_AND_ASSIGN(HAdd);
3124};
3125
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003126class HSub : public HBinaryOperation {
3127 public:
3128 HSub(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3129 : HBinaryOperation(result_type, left, right) {}
3130
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003131 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01003132 return x - y;
3133 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003134 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01003135 return x - y;
3136 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003137
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003138 DECLARE_INSTRUCTION(Sub);
3139
3140 private:
3141 DISALLOW_COPY_AND_ASSIGN(HSub);
3142};
3143
Calin Juravle34bacdf2014-10-07 20:23:36 +01003144class HMul : public HBinaryOperation {
3145 public:
3146 HMul(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3147 : HBinaryOperation(result_type, left, right) {}
3148
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003149 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003150
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003151 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x * y; }
3152 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x * y; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003153
3154 DECLARE_INSTRUCTION(Mul);
3155
3156 private:
3157 DISALLOW_COPY_AND_ASSIGN(HMul);
3158};
3159
Calin Juravle7c4954d2014-10-28 16:57:40 +00003160class HDiv : public HBinaryOperation {
3161 public:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003162 HDiv(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
3163 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00003164
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003165 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00003166 // Our graph structure ensures we never have 0 for `y` during constant folding.
3167 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00003168 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00003169 return (y == -1) ? -x : x / y;
3170 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003171
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003172 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00003173 DCHECK_NE(y, 0);
3174 // Special case -1 to avoid getting a SIGFPE on x86(_64).
3175 return (y == -1) ? -x : x / y;
3176 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003177
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003178 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003179
Calin Juravle7c4954d2014-10-28 16:57:40 +00003180 DECLARE_INSTRUCTION(Div);
3181
3182 private:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003183 const uint32_t dex_pc_;
3184
Calin Juravle7c4954d2014-10-28 16:57:40 +00003185 DISALLOW_COPY_AND_ASSIGN(HDiv);
3186};
3187
Calin Juravlebacfec32014-11-14 15:54:36 +00003188class HRem : public HBinaryOperation {
3189 public:
3190 HRem(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
3191 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
3192
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003193 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00003194 DCHECK_NE(y, 0);
3195 // Special case -1 to avoid getting a SIGFPE on x86(_64).
3196 return (y == -1) ? 0 : x % y;
3197 }
3198
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003199 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00003200 DCHECK_NE(y, 0);
3201 // Special case -1 to avoid getting a SIGFPE on x86(_64).
3202 return (y == -1) ? 0 : x % y;
3203 }
3204
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003205 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Calin Juravlebacfec32014-11-14 15:54:36 +00003206
3207 DECLARE_INSTRUCTION(Rem);
3208
3209 private:
3210 const uint32_t dex_pc_;
3211
3212 DISALLOW_COPY_AND_ASSIGN(HRem);
3213};
3214
Calin Juravled0d48522014-11-04 16:40:20 +00003215class HDivZeroCheck : public HExpression<1> {
3216 public:
3217 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
3218 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
3219 SetRawInputAt(0, value);
3220 }
3221
3222 bool CanBeMoved() const OVERRIDE { return true; }
3223
3224 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3225 UNUSED(other);
3226 return true;
3227 }
3228
3229 bool NeedsEnvironment() const OVERRIDE { return true; }
3230 bool CanThrow() const OVERRIDE { return true; }
3231
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003232 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Calin Juravled0d48522014-11-04 16:40:20 +00003233
3234 DECLARE_INSTRUCTION(DivZeroCheck);
3235
3236 private:
3237 const uint32_t dex_pc_;
3238
3239 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
3240};
3241
Calin Juravle9aec02f2014-11-18 23:06:35 +00003242class HShl : public HBinaryOperation {
3243 public:
3244 HShl(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3245 : HBinaryOperation(result_type, left, right) {}
3246
3247 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x << (y & kMaxIntShiftValue); }
3248 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x << (y & kMaxLongShiftValue); }
3249
3250 DECLARE_INSTRUCTION(Shl);
3251
3252 private:
3253 DISALLOW_COPY_AND_ASSIGN(HShl);
3254};
3255
3256class HShr : public HBinaryOperation {
3257 public:
3258 HShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3259 : HBinaryOperation(result_type, left, right) {}
3260
3261 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x >> (y & kMaxIntShiftValue); }
3262 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x >> (y & kMaxLongShiftValue); }
3263
3264 DECLARE_INSTRUCTION(Shr);
3265
3266 private:
3267 DISALLOW_COPY_AND_ASSIGN(HShr);
3268};
3269
3270class HUShr : public HBinaryOperation {
3271 public:
3272 HUShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3273 : HBinaryOperation(result_type, left, right) {}
3274
3275 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
3276 uint32_t ux = static_cast<uint32_t>(x);
3277 uint32_t uy = static_cast<uint32_t>(y) & kMaxIntShiftValue;
3278 return static_cast<int32_t>(ux >> uy);
3279 }
3280
3281 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
3282 uint64_t ux = static_cast<uint64_t>(x);
3283 uint64_t uy = static_cast<uint64_t>(y) & kMaxLongShiftValue;
3284 return static_cast<int64_t>(ux >> uy);
3285 }
3286
3287 DECLARE_INSTRUCTION(UShr);
3288
3289 private:
3290 DISALLOW_COPY_AND_ASSIGN(HUShr);
3291};
3292
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003293class HAnd : public HBinaryOperation {
3294 public:
3295 HAnd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3296 : HBinaryOperation(result_type, left, right) {}
3297
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003298 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003299
3300 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x & y; }
3301 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x & y; }
3302
3303 DECLARE_INSTRUCTION(And);
3304
3305 private:
3306 DISALLOW_COPY_AND_ASSIGN(HAnd);
3307};
3308
3309class HOr : public HBinaryOperation {
3310 public:
3311 HOr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3312 : HBinaryOperation(result_type, left, right) {}
3313
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003314 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003315
3316 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x | y; }
3317 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x | y; }
3318
3319 DECLARE_INSTRUCTION(Or);
3320
3321 private:
3322 DISALLOW_COPY_AND_ASSIGN(HOr);
3323};
3324
3325class HXor : public HBinaryOperation {
3326 public:
3327 HXor(Primitive::Type result_type, HInstruction* left, HInstruction* right)
3328 : HBinaryOperation(result_type, left, right) {}
3329
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003330 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003331
3332 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x ^ y; }
3333 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x ^ y; }
3334
3335 DECLARE_INSTRUCTION(Xor);
3336
3337 private:
3338 DISALLOW_COPY_AND_ASSIGN(HXor);
3339};
3340
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003341// The value of a parameter in this method. Its location depends on
3342// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07003343class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003344 public:
Calin Juravle10e244f2015-01-26 18:54:32 +00003345 HParameterValue(uint8_t index, Primitive::Type parameter_type, bool is_this = false)
3346 : HExpression(parameter_type, SideEffects::None()), index_(index), is_this_(is_this) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003347
3348 uint8_t GetIndex() const { return index_; }
3349
Calin Juravle10e244f2015-01-26 18:54:32 +00003350 bool CanBeNull() const OVERRIDE { return !is_this_; }
3351
Calin Juravle3cd4fc82015-05-14 15:15:42 +01003352 bool IsThis() const { return is_this_; }
3353
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003354 DECLARE_INSTRUCTION(ParameterValue);
3355
3356 private:
3357 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00003358 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003359 const uint8_t index_;
3360
Calin Juravle10e244f2015-01-26 18:54:32 +00003361 // Whether or not the parameter value corresponds to 'this' argument.
3362 const bool is_this_;
3363
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003364 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
3365};
3366
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003367class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003368 public:
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003369 explicit HNot(Primitive::Type result_type, HInstruction* input)
3370 : HUnaryOperation(result_type, input) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003371
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003372 bool CanBeMoved() const OVERRIDE { return true; }
3373 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003374 UNUSED(other);
3375 return true;
3376 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003377
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003378 int32_t Evaluate(int32_t x) const OVERRIDE { return ~x; }
3379 int64_t Evaluate(int64_t x) const OVERRIDE { return ~x; }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003380
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003381 DECLARE_INSTRUCTION(Not);
3382
3383 private:
3384 DISALLOW_COPY_AND_ASSIGN(HNot);
3385};
3386
David Brazdil66d126e2015-04-03 16:02:44 +01003387class HBooleanNot : public HUnaryOperation {
3388 public:
3389 explicit HBooleanNot(HInstruction* input)
3390 : HUnaryOperation(Primitive::Type::kPrimBoolean, input) {}
3391
3392 bool CanBeMoved() const OVERRIDE { return true; }
3393 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3394 UNUSED(other);
3395 return true;
3396 }
3397
3398 int32_t Evaluate(int32_t x) const OVERRIDE {
3399 DCHECK(IsUint<1>(x));
3400 return !x;
3401 }
3402
3403 int64_t Evaluate(int64_t x ATTRIBUTE_UNUSED) const OVERRIDE {
3404 LOG(FATAL) << DebugName() << " cannot be used with 64-bit values";
3405 UNREACHABLE();
3406 }
3407
3408 DECLARE_INSTRUCTION(BooleanNot);
3409
3410 private:
3411 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
3412};
3413
Roland Levillaindff1f282014-11-05 14:15:05 +00003414class HTypeConversion : public HExpression<1> {
3415 public:
3416 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00003417 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
3418 : HExpression(result_type, SideEffects::None()), dex_pc_(dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00003419 SetRawInputAt(0, input);
3420 DCHECK_NE(input->GetType(), result_type);
3421 }
3422
3423 HInstruction* GetInput() const { return InputAt(0); }
3424 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
3425 Primitive::Type GetResultType() const { return GetType(); }
3426
Roland Levillain624279f2014-12-04 11:54:28 +00003427 // Required by the x86 and ARM code generators when producing calls
3428 // to the runtime.
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003429 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Roland Levillain624279f2014-12-04 11:54:28 +00003430
Roland Levillaindff1f282014-11-05 14:15:05 +00003431 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00003432 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00003433
Mark Mendelle82549b2015-05-06 10:55:34 -04003434 // Try to statically evaluate the conversion and return a HConstant
3435 // containing the result. If the input cannot be converted, return nullptr.
3436 HConstant* TryStaticEvaluation() const;
3437
Roland Levillaindff1f282014-11-05 14:15:05 +00003438 DECLARE_INSTRUCTION(TypeConversion);
3439
3440 private:
Roland Levillain624279f2014-12-04 11:54:28 +00003441 const uint32_t dex_pc_;
3442
Roland Levillaindff1f282014-11-05 14:15:05 +00003443 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
3444};
3445
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00003446static constexpr uint32_t kNoRegNumber = -1;
3447
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003448class HPhi : public HInstruction {
3449 public:
3450 HPhi(ArenaAllocator* arena, uint32_t reg_number, size_t number_of_inputs, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003451 : HInstruction(SideEffects::None()),
3452 inputs_(arena, number_of_inputs),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003453 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01003454 type_(type),
Calin Juravle10e244f2015-01-26 18:54:32 +00003455 is_live_(false),
3456 can_be_null_(true) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003457 inputs_.SetSize(number_of_inputs);
3458 }
3459
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00003460 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
3461 static Primitive::Type ToPhiType(Primitive::Type type) {
3462 switch (type) {
3463 case Primitive::kPrimBoolean:
3464 case Primitive::kPrimByte:
3465 case Primitive::kPrimShort:
3466 case Primitive::kPrimChar:
3467 return Primitive::kPrimInt;
3468 default:
3469 return type;
3470 }
3471 }
3472
David Brazdilffee3d32015-07-06 11:48:53 +01003473 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
3474
Calin Juravle10e244f2015-01-26 18:54:32 +00003475 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003476
3477 void AddInput(HInstruction* input);
David Brazdil2d7352b2015-04-20 14:52:42 +01003478 void RemoveInputAt(size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003479
Calin Juravle10e244f2015-01-26 18:54:32 +00003480 Primitive::Type GetType() const OVERRIDE { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003481 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003482
Calin Juravle10e244f2015-01-26 18:54:32 +00003483 bool CanBeNull() const OVERRIDE { return can_be_null_; }
3484 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
3485
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003486 uint32_t GetRegNumber() const { return reg_number_; }
3487
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01003488 void SetDead() { is_live_ = false; }
3489 void SetLive() { is_live_ = true; }
3490 bool IsDead() const { return !is_live_; }
3491 bool IsLive() const { return is_live_; }
3492
Calin Juravlea4f88312015-04-16 12:57:19 +01003493 // Returns the next equivalent phi (starting from the current one) or null if there is none.
3494 // An equivalent phi is a phi having the same dex register and type.
3495 // It assumes that phis with the same dex register are adjacent.
3496 HPhi* GetNextEquivalentPhiWithSameType() {
3497 HInstruction* next = GetNext();
3498 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
3499 if (next->GetType() == GetType()) {
3500 return next->AsPhi();
3501 }
3502 next = next->GetNext();
3503 }
3504 return nullptr;
3505 }
3506
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003507 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003508
David Brazdil1abb4192015-02-17 18:33:36 +00003509 protected:
3510 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
3511
3512 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
3513 inputs_.Put(index, input);
3514 }
3515
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003516 private:
David Brazdil1abb4192015-02-17 18:33:36 +00003517 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003518 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003519 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01003520 bool is_live_;
Calin Juravle10e244f2015-01-26 18:54:32 +00003521 bool can_be_null_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003522
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003523 DISALLOW_COPY_AND_ASSIGN(HPhi);
3524};
3525
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003526class HNullCheck : public HExpression<1> {
3527 public:
3528 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003529 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003530 SetRawInputAt(0, value);
3531 }
3532
Calin Juravle10e244f2015-01-26 18:54:32 +00003533 bool CanBeMoved() const OVERRIDE { return true; }
3534 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003535 UNUSED(other);
3536 return true;
3537 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003538
Calin Juravle10e244f2015-01-26 18:54:32 +00003539 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003540
Calin Juravle10e244f2015-01-26 18:54:32 +00003541 bool CanThrow() const OVERRIDE { return true; }
3542
3543 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01003544
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003545 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003546
3547 DECLARE_INSTRUCTION(NullCheck);
3548
3549 private:
3550 const uint32_t dex_pc_;
3551
3552 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
3553};
3554
3555class FieldInfo : public ValueObject {
3556 public:
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003557 FieldInfo(MemberOffset field_offset,
3558 Primitive::Type field_type,
3559 bool is_volatile,
3560 uint32_t index,
3561 const DexFile& dex_file)
3562 : field_offset_(field_offset),
3563 field_type_(field_type),
3564 is_volatile_(is_volatile),
3565 index_(index),
3566 dex_file_(dex_file) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003567
3568 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003569 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003570 uint32_t GetFieldIndex() const { return index_; }
3571 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00003572 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003573
3574 private:
3575 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01003576 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00003577 const bool is_volatile_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003578 uint32_t index_;
3579 const DexFile& dex_file_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003580};
3581
3582class HInstanceFieldGet : public HExpression<1> {
3583 public:
3584 HInstanceFieldGet(HInstruction* value,
3585 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003586 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003587 bool is_volatile,
3588 uint32_t field_idx,
3589 const DexFile& dex_file)
Aart Bik34c3ba92015-07-20 14:08:59 -07003590 : HExpression(
3591 field_type,
3592 SideEffects::SideEffects::FieldReadOfType(field_type, is_volatile)),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003593 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003594 SetRawInputAt(0, value);
3595 }
3596
Calin Juravle10c9cbe2014-12-19 10:50:19 +00003597 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003598
3599 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3600 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
3601 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003602 }
3603
Calin Juravle641547a2015-04-21 22:08:51 +01003604 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3605 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00003606 }
3607
3608 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01003609 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
3610 }
3611
Calin Juravle52c48962014-12-16 17:02:57 +00003612 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003613 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003614 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003615 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003616
3617 DECLARE_INSTRUCTION(InstanceFieldGet);
3618
3619 private:
3620 const FieldInfo field_info_;
3621
3622 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
3623};
3624
3625class HInstanceFieldSet : public HTemplateInstruction<2> {
3626 public:
3627 HInstanceFieldSet(HInstruction* object,
3628 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01003629 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003630 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003631 bool is_volatile,
3632 uint32_t field_idx,
3633 const DexFile& dex_file)
Aart Bik34c3ba92015-07-20 14:08:59 -07003634 : HTemplateInstruction(
3635 SideEffects::FieldWriteOfType(field_type, is_volatile)),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01003636 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003637 value_can_be_null_(true) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003638 SetRawInputAt(0, object);
3639 SetRawInputAt(1, value);
3640 }
3641
Calin Juravle641547a2015-04-21 22:08:51 +01003642 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3643 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00003644 }
3645
Calin Juravle52c48962014-12-16 17:02:57 +00003646 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003647 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003648 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003649 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003650 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003651 bool GetValueCanBeNull() const { return value_can_be_null_; }
3652 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003653
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003654 DECLARE_INSTRUCTION(InstanceFieldSet);
3655
3656 private:
3657 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003658 bool value_can_be_null_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003659
3660 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
3661};
3662
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003663class HArrayGet : public HExpression<2> {
3664 public:
3665 HArrayGet(HInstruction* array, HInstruction* index, Primitive::Type type)
Aart Bik854a02b2015-07-14 16:07:00 -07003666 : HExpression(type, SideEffects::ArrayReadOfType(type)) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003667 SetRawInputAt(0, array);
3668 SetRawInputAt(1, index);
3669 }
3670
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003671 bool CanBeMoved() const OVERRIDE { return true; }
3672 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003673 UNUSED(other);
3674 return true;
3675 }
Calin Juravle641547a2015-04-21 22:08:51 +01003676 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3677 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00003678 // TODO: We can be smarter here.
3679 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
3680 // which generates the implicit null check. There are cases when these can be removed
3681 // to produce better code. If we ever add optimizations to do so we should allow an
3682 // implicit check here (as long as the address falls in the first page).
3683 return false;
3684 }
3685
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003686 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003687
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003688 HInstruction* GetArray() const { return InputAt(0); }
3689 HInstruction* GetIndex() const { return InputAt(1); }
3690
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003691 DECLARE_INSTRUCTION(ArrayGet);
3692
3693 private:
3694 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
3695};
3696
3697class HArraySet : public HTemplateInstruction<3> {
3698 public:
3699 HArraySet(HInstruction* array,
3700 HInstruction* index,
3701 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003702 Primitive::Type expected_component_type,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003703 uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07003704 : HTemplateInstruction(SideEffects::ArrayWriteOfType(expected_component_type)),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003705 dex_pc_(dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003706 expected_component_type_(expected_component_type),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003707 needs_type_check_(value->GetType() == Primitive::kPrimNot),
3708 value_can_be_null_(true) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003709 SetRawInputAt(0, array);
3710 SetRawInputAt(1, index);
3711 SetRawInputAt(2, value);
3712 }
3713
Calin Juravle77520bc2015-01-12 18:45:46 +00003714 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003715 // We currently always call a runtime method to catch array store
3716 // exceptions.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003717 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003718 }
3719
Mingyao Yang81014cb2015-06-02 03:16:27 -07003720 // Can throw ArrayStoreException.
3721 bool CanThrow() const OVERRIDE { return needs_type_check_; }
3722
Calin Juravle641547a2015-04-21 22:08:51 +01003723 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3724 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00003725 // TODO: Same as for ArrayGet.
3726 return false;
3727 }
3728
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003729 void ClearNeedsTypeCheck() {
3730 needs_type_check_ = false;
3731 }
3732
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003733 void ClearValueCanBeNull() {
3734 value_can_be_null_ = false;
3735 }
3736
3737 bool GetValueCanBeNull() const { return value_can_be_null_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003738 bool NeedsTypeCheck() const { return needs_type_check_; }
3739
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003740 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003741
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003742 HInstruction* GetArray() const { return InputAt(0); }
3743 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003744 HInstruction* GetValue() const { return InputAt(2); }
3745
3746 Primitive::Type GetComponentType() const {
3747 // The Dex format does not type floating point index operations. Since the
3748 // `expected_component_type_` is set during building and can therefore not
3749 // be correct, we also check what is the value type. If it is a floating
3750 // point type, we must use that type.
3751 Primitive::Type value_type = GetValue()->GetType();
3752 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
3753 ? value_type
3754 : expected_component_type_;
3755 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003756
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003757 DECLARE_INSTRUCTION(ArraySet);
3758
3759 private:
3760 const uint32_t dex_pc_;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003761 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003762 bool needs_type_check_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003763 bool value_can_be_null_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003764
3765 DISALLOW_COPY_AND_ASSIGN(HArraySet);
3766};
3767
3768class HArrayLength : public HExpression<1> {
3769 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003770 explicit HArrayLength(HInstruction* array)
3771 : HExpression(Primitive::kPrimInt, SideEffects::None()) {
3772 // Note that arrays do not change length, so the instruction does not
3773 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003774 SetRawInputAt(0, array);
3775 }
3776
Calin Juravle77520bc2015-01-12 18:45:46 +00003777 bool CanBeMoved() const OVERRIDE { return true; }
3778 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003779 UNUSED(other);
3780 return true;
3781 }
Calin Juravle641547a2015-04-21 22:08:51 +01003782 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3783 return obj == InputAt(0);
3784 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003785
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003786 DECLARE_INSTRUCTION(ArrayLength);
3787
3788 private:
3789 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
3790};
3791
3792class HBoundsCheck : public HExpression<2> {
3793 public:
3794 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003795 : HExpression(index->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003796 DCHECK(index->GetType() == Primitive::kPrimInt);
3797 SetRawInputAt(0, index);
3798 SetRawInputAt(1, length);
3799 }
3800
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003801 bool CanBeMoved() const OVERRIDE { return true; }
3802 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003803 UNUSED(other);
3804 return true;
3805 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003806
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003807 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003808
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003809 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01003810
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003811 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003812
3813 DECLARE_INSTRUCTION(BoundsCheck);
3814
3815 private:
3816 const uint32_t dex_pc_;
3817
3818 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
3819};
3820
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003821/**
3822 * Some DEX instructions are folded into multiple HInstructions that need
3823 * to stay live until the last HInstruction. This class
3824 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00003825 * HInstruction stays live. `index` represents the stack location index of the
3826 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003827 */
3828class HTemporary : public HTemplateInstruction<0> {
3829 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003830 explicit HTemporary(size_t index) : HTemplateInstruction(SideEffects::None()), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003831
3832 size_t GetIndex() const { return index_; }
3833
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00003834 Primitive::Type GetType() const OVERRIDE {
3835 // The previous instruction is the one that will be stored in the temporary location.
3836 DCHECK(GetPrevious() != nullptr);
3837 return GetPrevious()->GetType();
3838 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00003839
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003840 DECLARE_INSTRUCTION(Temporary);
3841
3842 private:
3843 const size_t index_;
3844
3845 DISALLOW_COPY_AND_ASSIGN(HTemporary);
3846};
3847
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003848class HSuspendCheck : public HTemplateInstruction<0> {
3849 public:
3850 explicit HSuspendCheck(uint32_t dex_pc)
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01003851 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003852
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003853 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003854 return true;
3855 }
3856
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003857 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01003858 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
3859 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003860
3861 DECLARE_INSTRUCTION(SuspendCheck);
3862
3863 private:
3864 const uint32_t dex_pc_;
3865
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01003866 // Only used for code generation, in order to share the same slow path between back edges
3867 // of a same loop.
3868 SlowPathCode* slow_path_;
3869
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003870 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
3871};
3872
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003873/**
3874 * Instruction to load a Class object.
3875 */
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003876class HLoadClass : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003877 public:
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003878 HLoadClass(HCurrentMethod* current_method,
3879 uint16_t type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003880 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003881 bool is_referrers_class,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003882 uint32_t dex_pc)
3883 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3884 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003885 dex_file_(dex_file),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003886 is_referrers_class_(is_referrers_class),
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003887 dex_pc_(dex_pc),
Calin Juravleb1498f62015-02-16 13:13:29 +00003888 generate_clinit_check_(false),
Calin Juravle3fabec72015-07-16 16:51:30 +01003889 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003890 SetRawInputAt(0, current_method);
3891 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003892
3893 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003894
3895 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3896 return other->AsLoadClass()->type_index_ == type_index_;
3897 }
3898
3899 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
3900
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003901 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003902 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003903 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01003904 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003905
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003906 bool NeedsEnvironment() const OVERRIDE {
3907 // Will call runtime and load the class if the class is not loaded yet.
3908 // TODO: finer grain decision.
3909 return !is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003910 }
3911
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003912 bool MustGenerateClinitCheck() const {
3913 return generate_clinit_check_;
3914 }
3915
Calin Juravle0ba218d2015-05-19 18:46:01 +01003916 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
3917 generate_clinit_check_ = generate_clinit_check;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003918 }
3919
3920 bool CanCallRuntime() const {
3921 return MustGenerateClinitCheck() || !is_referrers_class_;
3922 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003923
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003924 bool CanThrow() const OVERRIDE {
3925 // May call runtime and and therefore can throw.
3926 // TODO: finer grain decision.
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01003927 return CanCallRuntime();
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003928 }
3929
Calin Juravleacf735c2015-02-12 15:25:22 +00003930 ReferenceTypeInfo GetLoadedClassRTI() {
3931 return loaded_class_rti_;
3932 }
3933
3934 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
3935 // Make sure we only set exact types (the loaded class should never be merged).
3936 DCHECK(rti.IsExact());
3937 loaded_class_rti_ = rti;
3938 }
3939
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003940 const DexFile& GetDexFile() { return dex_file_; }
3941
Nicolas Geoffray9437b782015-03-25 10:08:51 +00003942 bool NeedsDexCache() const OVERRIDE { return !is_referrers_class_; }
3943
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003944 DECLARE_INSTRUCTION(LoadClass);
3945
3946 private:
3947 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003948 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003949 const bool is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003950 const uint32_t dex_pc_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003951 // Whether this instruction must generate the initialization check.
3952 // Used for code generation.
3953 bool generate_clinit_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003954
Calin Juravleacf735c2015-02-12 15:25:22 +00003955 ReferenceTypeInfo loaded_class_rti_;
3956
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003957 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
3958};
3959
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01003960class HLoadString : public HExpression<1> {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003961 public:
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01003962 HLoadString(HCurrentMethod* current_method, uint32_t string_index, uint32_t dex_pc)
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003963 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3964 string_index_(string_index),
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01003965 dex_pc_(dex_pc) {
3966 SetRawInputAt(0, current_method);
3967 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003968
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003969 bool CanBeMoved() const OVERRIDE { return true; }
3970
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003971 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3972 return other->AsLoadString()->string_index_ == string_index_;
3973 }
3974
3975 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
3976
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003977 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003978 uint32_t GetStringIndex() const { return string_index_; }
3979
3980 // TODO: Can we deopt or debug when we resolve a string?
3981 bool NeedsEnvironment() const OVERRIDE { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00003982 bool NeedsDexCache() const OVERRIDE { return true; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003983
3984 DECLARE_INSTRUCTION(LoadString);
3985
3986 private:
3987 const uint32_t string_index_;
3988 const uint32_t dex_pc_;
3989
3990 DISALLOW_COPY_AND_ASSIGN(HLoadString);
3991};
3992
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003993/**
3994 * Performs an initialization check on its Class object input.
3995 */
3996class HClinitCheck : public HExpression<1> {
3997 public:
3998 explicit HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07003999 : HExpression(
4000 Primitive::kPrimNot,
Aart Bik34c3ba92015-07-20 14:08:59 -07004001 SideEffects::AllWrites()), // assume write on all fields/arrays
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004002 dex_pc_(dex_pc) {
4003 SetRawInputAt(0, constant);
4004 }
4005
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004006 bool CanBeMoved() const OVERRIDE { return true; }
4007 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4008 UNUSED(other);
4009 return true;
4010 }
4011
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004012 bool NeedsEnvironment() const OVERRIDE {
4013 // May call runtime to initialize the class.
4014 return true;
4015 }
4016
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004017 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004018
4019 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
4020
4021 DECLARE_INSTRUCTION(ClinitCheck);
4022
4023 private:
4024 const uint32_t dex_pc_;
4025
4026 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
4027};
4028
4029class HStaticFieldGet : public HExpression<1> {
4030 public:
4031 HStaticFieldGet(HInstruction* cls,
4032 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004033 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004034 bool is_volatile,
4035 uint32_t field_idx,
4036 const DexFile& dex_file)
Aart Bik34c3ba92015-07-20 14:08:59 -07004037 : HExpression(
4038 field_type,
4039 SideEffects::SideEffects::FieldReadOfType(field_type, is_volatile)),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004040 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004041 SetRawInputAt(0, cls);
4042 }
4043
Calin Juravle52c48962014-12-16 17:02:57 +00004044
Calin Juravle10c9cbe2014-12-19 10:50:19 +00004045 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004046
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004047 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00004048 HStaticFieldGet* other_get = other->AsStaticFieldGet();
4049 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004050 }
4051
4052 size_t ComputeHashCode() const OVERRIDE {
4053 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
4054 }
4055
Calin Juravle52c48962014-12-16 17:02:57 +00004056 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004057 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
4058 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004059 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004060
4061 DECLARE_INSTRUCTION(StaticFieldGet);
4062
4063 private:
4064 const FieldInfo field_info_;
4065
4066 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
4067};
4068
4069class HStaticFieldSet : public HTemplateInstruction<2> {
4070 public:
4071 HStaticFieldSet(HInstruction* cls,
4072 HInstruction* value,
4073 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004074 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004075 bool is_volatile,
4076 uint32_t field_idx,
4077 const DexFile& dex_file)
Aart Bik34c3ba92015-07-20 14:08:59 -07004078 : HTemplateInstruction(
4079 SideEffects::FieldWriteOfType(field_type, is_volatile)),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004080 field_info_(field_offset, field_type, is_volatile, field_idx, dex_file),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004081 value_can_be_null_(true) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004082 SetRawInputAt(0, cls);
4083 SetRawInputAt(1, value);
4084 }
4085
Calin Juravle52c48962014-12-16 17:02:57 +00004086 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004087 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
4088 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004089 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004090
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004091 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004092 bool GetValueCanBeNull() const { return value_can_be_null_; }
4093 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004094
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004095 DECLARE_INSTRUCTION(StaticFieldSet);
4096
4097 private:
4098 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004099 bool value_can_be_null_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004100
4101 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
4102};
4103
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004104// Implement the move-exception DEX instruction.
4105class HLoadException : public HExpression<0> {
4106 public:
4107 HLoadException() : HExpression(Primitive::kPrimNot, SideEffects::None()) {}
4108
4109 DECLARE_INSTRUCTION(LoadException);
4110
4111 private:
4112 DISALLOW_COPY_AND_ASSIGN(HLoadException);
4113};
4114
4115class HThrow : public HTemplateInstruction<1> {
4116 public:
4117 HThrow(HInstruction* exception, uint32_t dex_pc)
4118 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {
4119 SetRawInputAt(0, exception);
4120 }
4121
4122 bool IsControlFlow() const OVERRIDE { return true; }
4123
4124 bool NeedsEnvironment() const OVERRIDE { return true; }
4125
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004126 bool CanThrow() const OVERRIDE { return true; }
4127
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004128 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004129
4130 DECLARE_INSTRUCTION(Throw);
4131
4132 private:
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004133 const uint32_t dex_pc_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004134
4135 DISALLOW_COPY_AND_ASSIGN(HThrow);
4136};
4137
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004138class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004139 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004140 HInstanceOf(HInstruction* object,
4141 HLoadClass* constant,
4142 bool class_is_final,
4143 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004144 : HExpression(Primitive::kPrimBoolean, SideEffects::None()),
4145 class_is_final_(class_is_final),
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004146 must_do_null_check_(true),
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004147 dex_pc_(dex_pc) {
4148 SetRawInputAt(0, object);
4149 SetRawInputAt(1, constant);
4150 }
4151
4152 bool CanBeMoved() const OVERRIDE { return true; }
4153
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004154 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004155 return true;
4156 }
4157
4158 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004159 return false;
4160 }
4161
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004162 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004163
4164 bool IsClassFinal() const { return class_is_final_; }
4165
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004166 // Used only in code generation.
4167 bool MustDoNullCheck() const { return must_do_null_check_; }
4168 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
4169
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004170 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004171
4172 private:
4173 const bool class_is_final_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004174 bool must_do_null_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004175 const uint32_t dex_pc_;
4176
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004177 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
4178};
4179
Calin Juravleb1498f62015-02-16 13:13:29 +00004180class HBoundType : public HExpression<1> {
4181 public:
Calin Juravle3fabec72015-07-16 16:51:30 +01004182 // Constructs an HBoundType with the given upper_bound.
4183 // Ensures that the upper_bound is valid.
Calin Juravleb0d5fc02015-07-15 14:41:29 +01004184 HBoundType(HInstruction* input, ReferenceTypeInfo upper_bound, bool upper_can_be_null)
Calin Juravleb1498f62015-02-16 13:13:29 +00004185 : HExpression(Primitive::kPrimNot, SideEffects::None()),
Calin Juravleb0d5fc02015-07-15 14:41:29 +01004186 upper_bound_(upper_bound),
4187 upper_can_be_null_(upper_can_be_null) {
Calin Juravle61d544b2015-02-23 16:46:57 +00004188 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00004189 SetRawInputAt(0, input);
Calin Juravle3fabec72015-07-16 16:51:30 +01004190 SetReferenceTypeInfo(upper_bound_);
Calin Juravleb1498f62015-02-16 13:13:29 +00004191 }
4192
Calin Juravleb0d5fc02015-07-15 14:41:29 +01004193 // GetUpper* should only be used in reference type propagation.
4194 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
4195 bool GetUpperCanBeNull() const { return upper_can_be_null_; }
Calin Juravleb1498f62015-02-16 13:13:29 +00004196
Calin Juravleb0d5fc02015-07-15 14:41:29 +01004197 void SetCanBeNull(bool can_be_null) {
4198 DCHECK(upper_can_be_null_ || !can_be_null);
4199 can_be_null_ = can_be_null;
Calin Juravleb1498f62015-02-16 13:13:29 +00004200 }
4201
Calin Juravleb0d5fc02015-07-15 14:41:29 +01004202 bool CanBeNull() const OVERRIDE { return can_be_null_; }
4203
Calin Juravleb1498f62015-02-16 13:13:29 +00004204 DECLARE_INSTRUCTION(BoundType);
4205
4206 private:
4207 // Encodes the most upper class that this instruction can have. In other words
Calin Juravleb0d5fc02015-07-15 14:41:29 +01004208 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
4209 // It is used to bound the type in cases like:
4210 // if (x instanceof ClassX) {
4211 // // uper_bound_ will be ClassX
4212 // }
4213 const ReferenceTypeInfo upper_bound_;
4214 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
4215 // is false then can_be_null_ cannot be true).
4216 const bool upper_can_be_null_;
4217 bool can_be_null_;
Calin Juravleb1498f62015-02-16 13:13:29 +00004218
4219 DISALLOW_COPY_AND_ASSIGN(HBoundType);
4220};
4221
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004222class HCheckCast : public HTemplateInstruction<2> {
4223 public:
4224 HCheckCast(HInstruction* object,
4225 HLoadClass* constant,
4226 bool class_is_final,
4227 uint32_t dex_pc)
4228 : HTemplateInstruction(SideEffects::None()),
4229 class_is_final_(class_is_final),
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004230 must_do_null_check_(true),
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004231 dex_pc_(dex_pc) {
4232 SetRawInputAt(0, object);
4233 SetRawInputAt(1, constant);
4234 }
4235
4236 bool CanBeMoved() const OVERRIDE { return true; }
4237
4238 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
4239 return true;
4240 }
4241
4242 bool NeedsEnvironment() const OVERRIDE {
4243 // Instruction may throw a CheckCastError.
4244 return true;
4245 }
4246
4247 bool CanThrow() const OVERRIDE { return true; }
4248
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004249 bool MustDoNullCheck() const { return must_do_null_check_; }
4250 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
4251
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004252 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004253
4254 bool IsClassFinal() const { return class_is_final_; }
4255
4256 DECLARE_INSTRUCTION(CheckCast);
4257
4258 private:
4259 const bool class_is_final_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004260 bool must_do_null_check_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004261 const uint32_t dex_pc_;
4262
4263 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004264};
4265
Calin Juravle27df7582015-04-17 19:12:31 +01004266class HMemoryBarrier : public HTemplateInstruction<0> {
4267 public:
4268 explicit HMemoryBarrier(MemBarrierKind barrier_kind)
Aart Bik34c3ba92015-07-20 14:08:59 -07004269 : HTemplateInstruction(
4270 SideEffects::All()), // assume write/read on all fields/arrays
Calin Juravle27df7582015-04-17 19:12:31 +01004271 barrier_kind_(barrier_kind) {}
4272
4273 MemBarrierKind GetBarrierKind() { return barrier_kind_; }
4274
4275 DECLARE_INSTRUCTION(MemoryBarrier);
4276
4277 private:
4278 const MemBarrierKind barrier_kind_;
4279
4280 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
4281};
4282
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004283class HMonitorOperation : public HTemplateInstruction<1> {
4284 public:
4285 enum OperationKind {
4286 kEnter,
4287 kExit,
4288 };
4289
4290 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07004291 : HTemplateInstruction(SideEffects::All()), // assume write/read on all fields/arrays
4292 kind_(kind), dex_pc_(dex_pc) {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004293 SetRawInputAt(0, object);
4294 }
4295
4296 // Instruction may throw a Java exception, so we need an environment.
David Brazdilbff75032015-07-08 17:26:51 +00004297 bool NeedsEnvironment() const OVERRIDE { return CanThrow(); }
4298
4299 bool CanThrow() const OVERRIDE {
4300 // Verifier guarantees that monitor-exit cannot throw.
4301 // This is important because it allows the HGraphBuilder to remove
4302 // a dead throw-catch loop generated for `synchronized` blocks/methods.
4303 return IsEnter();
4304 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004305
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01004306 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004307
4308 bool IsEnter() const { return kind_ == kEnter; }
4309
4310 DECLARE_INSTRUCTION(MonitorOperation);
4311
Calin Juravle52c48962014-12-16 17:02:57 +00004312 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004313 const OperationKind kind_;
4314 const uint32_t dex_pc_;
4315
4316 private:
4317 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
4318};
4319
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01004320/**
4321 * A HInstruction used as a marker for the replacement of new + <init>
4322 * of a String to a call to a StringFactory. Only baseline will see
4323 * the node at code generation, where it will be be treated as null.
4324 * When compiling non-baseline, `HFakeString` instructions are being removed
4325 * in the instruction simplifier.
4326 */
4327class HFakeString : public HTemplateInstruction<0> {
4328 public:
4329 HFakeString() : HTemplateInstruction(SideEffects::None()) {}
4330
4331 Primitive::Type GetType() const OVERRIDE { return Primitive::kPrimNot; }
4332
4333 DECLARE_INSTRUCTION(FakeString);
4334
4335 private:
4336 DISALLOW_COPY_AND_ASSIGN(HFakeString);
4337};
4338
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004339class MoveOperands : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004340 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01004341 MoveOperands(Location source,
4342 Location destination,
4343 Primitive::Type type,
4344 HInstruction* instruction)
4345 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004346
4347 Location GetSource() const { return source_; }
4348 Location GetDestination() const { return destination_; }
4349
4350 void SetSource(Location value) { source_ = value; }
4351 void SetDestination(Location value) { destination_ = value; }
4352
4353 // The parallel move resolver marks moves as "in-progress" by clearing the
4354 // destination (but not the source).
4355 Location MarkPending() {
4356 DCHECK(!IsPending());
4357 Location dest = destination_;
4358 destination_ = Location::NoLocation();
4359 return dest;
4360 }
4361
4362 void ClearPending(Location dest) {
4363 DCHECK(IsPending());
4364 destination_ = dest;
4365 }
4366
4367 bool IsPending() const {
4368 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
4369 return destination_.IsInvalid() && !source_.IsInvalid();
4370 }
4371
4372 // True if this blocks a move from the given location.
4373 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08004374 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004375 }
4376
4377 // A move is redundant if it's been eliminated, if its source and
4378 // destination are the same, or if its destination is unneeded.
4379 bool IsRedundant() const {
4380 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
4381 }
4382
4383 // We clear both operands to indicate move that's been eliminated.
4384 void Eliminate() {
4385 source_ = destination_ = Location::NoLocation();
4386 }
4387
4388 bool IsEliminated() const {
4389 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
4390 return source_.IsInvalid();
4391 }
4392
Alexey Frunze4dda3372015-06-01 18:31:49 -07004393 Primitive::Type GetType() const { return type_; }
4394
Nicolas Geoffray90218252015-04-15 11:56:51 +01004395 bool Is64BitMove() const {
4396 return Primitive::Is64BitType(type_);
4397 }
4398
Nicolas Geoffray740475d2014-09-29 10:33:25 +01004399 HInstruction* GetInstruction() const { return instruction_; }
4400
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004401 private:
4402 Location source_;
4403 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01004404 // The type this move is for.
4405 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01004406 // The instruction this move is assocatied with. Null when this move is
4407 // for moving an input in the expected locations of user (including a phi user).
4408 // This is only used in debug mode, to ensure we do not connect interval siblings
4409 // in the same parallel move.
4410 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004411};
4412
4413static constexpr size_t kDefaultNumberOfMoves = 4;
4414
4415class HParallelMove : public HTemplateInstruction<0> {
4416 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004417 explicit HParallelMove(ArenaAllocator* arena)
4418 : HTemplateInstruction(SideEffects::None()), moves_(arena, kDefaultNumberOfMoves) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004419
Nicolas Geoffray90218252015-04-15 11:56:51 +01004420 void AddMove(Location source,
4421 Location destination,
4422 Primitive::Type type,
4423 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004424 DCHECK(source.IsValid());
4425 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004426 if (kIsDebugBuild) {
4427 if (instruction != nullptr) {
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00004428 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004429 if (moves_.Get(i).GetInstruction() == instruction) {
4430 // Special case the situation where the move is for the spill slot
4431 // of the instruction.
4432 if ((GetPrevious() == instruction)
4433 || ((GetPrevious() == nullptr)
4434 && instruction->IsPhi()
4435 && instruction->GetBlock() == GetBlock())) {
4436 DCHECK_NE(destination.GetKind(), moves_.Get(i).GetDestination().GetKind())
4437 << "Doing parallel moves for the same instruction.";
4438 } else {
4439 DCHECK(false) << "Doing parallel moves for the same instruction.";
4440 }
4441 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00004442 }
4443 }
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004444 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Zheng Xuad4450e2015-04-17 18:48:56 +08004445 DCHECK(!destination.OverlapsWith(moves_.Get(i).GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01004446 << "Overlapped destination for two moves in a parallel move: "
4447 << moves_.Get(i).GetSource() << " ==> " << moves_.Get(i).GetDestination() << " and "
4448 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004449 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01004450 }
Nicolas Geoffray90218252015-04-15 11:56:51 +01004451 moves_.Add(MoveOperands(source, destination, type, instruction));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004452 }
4453
4454 MoveOperands* MoveOperandsAt(size_t index) const {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004455 return moves_.GetRawStorage() + index;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004456 }
4457
4458 size_t NumMoves() const { return moves_.Size(); }
4459
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004460 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004461
4462 private:
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004463 GrowableArray<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004464
4465 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
4466};
4467
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004468class HGraphVisitor : public ValueObject {
4469 public:
Dave Allison20dfc792014-06-16 20:44:29 -07004470 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
4471 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004472
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004473 virtual void VisitInstruction(HInstruction* instruction) { UNUSED(instruction); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004474 virtual void VisitBasicBlock(HBasicBlock* block);
4475
Roland Levillain633021e2014-10-01 14:12:25 +01004476 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004477 void VisitInsertionOrder();
4478
Roland Levillain633021e2014-10-01 14:12:25 +01004479 // Visit the graph following dominator tree reverse post-order.
4480 void VisitReversePostOrder();
4481
Nicolas Geoffray787c3072014-03-17 10:20:19 +00004482 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00004483
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004484 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004485#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004486 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
4487
4488 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
4489
4490#undef DECLARE_VISIT_INSTRUCTION
4491
4492 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07004493 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004494
4495 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
4496};
4497
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004498class HGraphDelegateVisitor : public HGraphVisitor {
4499 public:
4500 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
4501 virtual ~HGraphDelegateVisitor() {}
4502
4503 // Visit functions that delegate to to super class.
4504#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004505 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004506
4507 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
4508
4509#undef DECLARE_VISIT_INSTRUCTION
4510
4511 private:
4512 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
4513};
4514
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004515class HInsertionOrderIterator : public ValueObject {
4516 public:
4517 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
4518
4519 bool Done() const { return index_ == graph_.GetBlocks().Size(); }
4520 HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); }
4521 void Advance() { ++index_; }
4522
4523 private:
4524 const HGraph& graph_;
4525 size_t index_;
4526
4527 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
4528};
4529
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004530class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004531 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00004532 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
4533 // Check that reverse post order of the graph has been built.
4534 DCHECK(!graph.GetReversePostOrder().IsEmpty());
4535 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004536
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004537 bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); }
4538 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004539 void Advance() { ++index_; }
4540
4541 private:
4542 const HGraph& graph_;
4543 size_t index_;
4544
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004545 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004546};
4547
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004548class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004549 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004550 explicit HPostOrderIterator(const HGraph& graph)
David Brazdil10f56cb2015-03-24 18:49:14 +00004551 : graph_(graph), index_(graph_.GetReversePostOrder().Size()) {
4552 // Check that reverse post order of the graph has been built.
4553 DCHECK(!graph.GetReversePostOrder().IsEmpty());
4554 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004555
4556 bool Done() const { return index_ == 0; }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004557 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004558 void Advance() { --index_; }
4559
4560 private:
4561 const HGraph& graph_;
4562 size_t index_;
4563
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004564 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004565};
4566
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01004567class HLinearPostOrderIterator : public ValueObject {
4568 public:
4569 explicit HLinearPostOrderIterator(const HGraph& graph)
4570 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().Size()) {}
4571
4572 bool Done() const { return index_ == 0; }
4573
4574 HBasicBlock* Current() const { return order_.Get(index_ -1); }
4575
4576 void Advance() {
4577 --index_;
4578 DCHECK_GE(index_, 0U);
4579 }
4580
4581 private:
4582 const GrowableArray<HBasicBlock*>& order_;
4583 size_t index_;
4584
4585 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
4586};
4587
4588class HLinearOrderIterator : public ValueObject {
4589 public:
4590 explicit HLinearOrderIterator(const HGraph& graph)
4591 : order_(graph.GetLinearOrder()), index_(0) {}
4592
4593 bool Done() const { return index_ == order_.Size(); }
4594 HBasicBlock* Current() const { return order_.Get(index_); }
4595 void Advance() { ++index_; }
4596
4597 private:
4598 const GrowableArray<HBasicBlock*>& order_;
4599 size_t index_;
4600
4601 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
4602};
4603
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004604// Iterator over the blocks that art part of the loop. Includes blocks part
4605// of an inner loop. The order in which the blocks are iterated is on their
4606// block id.
4607class HBlocksInLoopIterator : public ValueObject {
4608 public:
4609 explicit HBlocksInLoopIterator(const HLoopInformation& info)
4610 : blocks_in_loop_(info.GetBlocks()),
4611 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
4612 index_(0) {
4613 if (!blocks_in_loop_.IsBitSet(index_)) {
4614 Advance();
4615 }
4616 }
4617
4618 bool Done() const { return index_ == blocks_.Size(); }
4619 HBasicBlock* Current() const { return blocks_.Get(index_); }
4620 void Advance() {
4621 ++index_;
4622 for (size_t e = blocks_.Size(); index_ < e; ++index_) {
4623 if (blocks_in_loop_.IsBitSet(index_)) {
4624 break;
4625 }
4626 }
4627 }
4628
4629 private:
4630 const BitVector& blocks_in_loop_;
4631 const GrowableArray<HBasicBlock*>& blocks_;
4632 size_t index_;
4633
4634 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
4635};
4636
Mingyao Yang3584bce2015-05-19 16:01:59 -07004637// Iterator over the blocks that art part of the loop. Includes blocks part
4638// of an inner loop. The order in which the blocks are iterated is reverse
4639// post order.
4640class HBlocksInLoopReversePostOrderIterator : public ValueObject {
4641 public:
4642 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
4643 : blocks_in_loop_(info.GetBlocks()),
4644 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
4645 index_(0) {
4646 if (!blocks_in_loop_.IsBitSet(blocks_.Get(index_)->GetBlockId())) {
4647 Advance();
4648 }
4649 }
4650
4651 bool Done() const { return index_ == blocks_.Size(); }
4652 HBasicBlock* Current() const { return blocks_.Get(index_); }
4653 void Advance() {
4654 ++index_;
4655 for (size_t e = blocks_.Size(); index_ < e; ++index_) {
4656 if (blocks_in_loop_.IsBitSet(blocks_.Get(index_)->GetBlockId())) {
4657 break;
4658 }
4659 }
4660 }
4661
4662 private:
4663 const BitVector& blocks_in_loop_;
4664 const GrowableArray<HBasicBlock*>& blocks_;
4665 size_t index_;
4666
4667 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
4668};
4669
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00004670inline int64_t Int64FromConstant(HConstant* constant) {
4671 DCHECK(constant->IsIntConstant() || constant->IsLongConstant());
4672 return constant->IsIntConstant() ? constant->AsIntConstant()->GetValue()
4673 : constant->AsLongConstant()->GetValue();
4674}
4675
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004676} // namespace art
4677
4678#endif // ART_COMPILER_OPTIMIZING_NODES_H_