blob: 7e075644efb27904261bd1b6e9edeb4228d03ad1 [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
Mathieu Chartierb666f482015-02-18 14:33:14 -080020#include "base/arena_object.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000021#include "entrypoints/quick/quick_entrypoints_enum.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000022#include "handle.h"
23#include "handle_scope.h"
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000024#include "invoke_type.h"
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +010025#include "locations.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000026#include "mirror/class.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010027#include "offsets.h"
Ian Rogerse63db272014-07-15 15:36:11 -070028#include "primitive.h"
Nicolas Geoffray0e336432014-02-26 18:24:38 +000029#include "utils/arena_bit_vector.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000030#include "utils/growable_array.h"
31
32namespace art {
33
David Brazdil1abb4192015-02-17 18:33:36 +000034class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000035class HBasicBlock;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010036class HEnvironment;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000037class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000038class HIntConstant;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000039class HInvoke;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000040class HGraphVisitor;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +000041class HNullConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010042class HPhi;
Nicolas Geoffray3c049742014-09-24 18:10:46 +010043class HSuspendCheck;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010044class LiveInterval;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000045class LocationSummary;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000046
47static const int kDefaultNumberOfBlocks = 8;
48static const int kDefaultNumberOfSuccessors = 2;
49static const int kDefaultNumberOfPredecessors = 2;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010050static const int kDefaultNumberOfDominatedBlocks = 1;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000051static const int kDefaultNumberOfBackEdges = 1;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000052
Calin Juravle9aec02f2014-11-18 23:06:35 +000053static constexpr uint32_t kMaxIntShiftValue = 0x1f;
54static constexpr uint64_t kMaxLongShiftValue = 0x3f;
55
Dave Allison20dfc792014-06-16 20:44:29 -070056enum IfCondition {
57 kCondEQ,
58 kCondNE,
59 kCondLT,
60 kCondLE,
61 kCondGT,
62 kCondGE,
63};
64
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010065class HInstructionList {
66 public:
67 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
68
69 void AddInstruction(HInstruction* instruction);
70 void RemoveInstruction(HInstruction* instruction);
71
Roland Levillain6b469232014-09-25 10:10:38 +010072 // Return true if this list contains `instruction`.
73 bool Contains(HInstruction* instruction) const;
74
Roland Levillainccc07a92014-09-16 14:48:16 +010075 // Return true if `instruction1` is found before `instruction2` in
76 // this instruction list and false otherwise. Abort if none
77 // of these instructions is found.
78 bool FoundBefore(const HInstruction* instruction1,
79 const HInstruction* instruction2) const;
80
Nicolas Geoffray276d9da2015-02-02 18:24:11 +000081 bool IsEmpty() const { return first_instruction_ == nullptr; }
82 void Clear() { first_instruction_ = last_instruction_ = nullptr; }
83
84 // Update the block of all instructions to be `block`.
85 void SetBlockOfInstructions(HBasicBlock* block) const;
86
87 void AddAfter(HInstruction* cursor, const HInstructionList& instruction_list);
88 void Add(const HInstructionList& instruction_list);
89
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010090 private:
91 HInstruction* first_instruction_;
92 HInstruction* last_instruction_;
93
94 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000095 friend class HGraph;
96 friend class HInstruction;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010097 friend class HInstructionIterator;
Nicolas Geoffray804d0932014-05-02 08:46:00 +010098 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010099
100 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
101};
102
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000103// Control-flow graph of a method. Contains a list of basic blocks.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700104class HGraph : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000105 public:
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000106 HGraph(ArenaAllocator* arena, int start_instruction_id = 0)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000107 : arena_(arena),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000108 blocks_(arena, kDefaultNumberOfBlocks),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100109 reverse_post_order_(arena, kDefaultNumberOfBlocks),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700110 entry_block_(nullptr),
111 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100112 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100113 number_of_vregs_(0),
114 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000115 temporaries_vreg_slots_(0),
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000116 current_instruction_id_(start_instruction_id) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000117
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000118 ArenaAllocator* GetArena() const { return arena_; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100119 const GrowableArray<HBasicBlock*>& GetBlocks() const { return blocks_; }
Roland Levillain93445682014-10-06 19:24:02 +0100120 HBasicBlock* GetBlock(size_t id) const { return blocks_.Get(id); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000121
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000122 HBasicBlock* GetEntryBlock() const { return entry_block_; }
123 HBasicBlock* GetExitBlock() const { return exit_block_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000124
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000125 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
126 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000127
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000128 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100129
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000130 // Try building the SSA form of this graph, with dominance computation and loop
131 // recognition. Returns whether it was successful in doing all these steps.
132 bool TryBuildingSsa() {
133 BuildDominatorTree();
134 TransformToSsa();
135 return AnalyzeNaturalLoops();
136 }
137
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000138 void BuildDominatorTree();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000139 void TransformToSsa();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100140 void SimplifyCFG();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000141
Nicolas Geoffrayf5370122014-12-02 11:51:19 +0000142 // Analyze all natural loops in this graph. Returns false if one
143 // loop is not natural, that is the header does not dominate the
144 // back edge.
145 bool AnalyzeNaturalLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100146
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000147 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
148 void InlineInto(HGraph* outer_graph, HInvoke* invoke);
149
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100150 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
151 void SimplifyLoop(HBasicBlock* header);
152
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000153 int32_t GetNextInstructionId() {
154 DCHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000155 return current_instruction_id_++;
156 }
157
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000158 int32_t GetCurrentInstructionId() const {
159 return current_instruction_id_;
160 }
161
162 void SetCurrentInstructionId(int32_t id) {
163 current_instruction_id_ = id;
164 }
165
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100166 uint16_t GetMaximumNumberOfOutVRegs() const {
167 return maximum_number_of_out_vregs_;
168 }
169
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000170 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
171 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100172 }
173
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000174 void UpdateTemporariesVRegSlots(size_t slots) {
175 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100176 }
177
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000178 size_t GetTemporariesVRegSlots() const {
179 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100180 }
181
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100182 void SetNumberOfVRegs(uint16_t number_of_vregs) {
183 number_of_vregs_ = number_of_vregs;
184 }
185
186 uint16_t GetNumberOfVRegs() const {
187 return number_of_vregs_;
188 }
189
190 void SetNumberOfInVRegs(uint16_t value) {
191 number_of_in_vregs_ = value;
192 }
193
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100194 uint16_t GetNumberOfLocalVRegs() const {
195 return number_of_vregs_ - number_of_in_vregs_;
196 }
197
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100198 const GrowableArray<HBasicBlock*>& GetReversePostOrder() const {
199 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100200 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100201
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000202 HNullConstant* GetNullConstant();
203
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000204 private:
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000205 HBasicBlock* FindCommonDominator(HBasicBlock* first, HBasicBlock* second) const;
206 void VisitBlockForDominatorTree(HBasicBlock* block,
207 HBasicBlock* predecessor,
208 GrowableArray<size_t>* visits);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100209 void FindBackEdges(ArenaBitVector* visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000210 void VisitBlockForBackEdges(HBasicBlock* block,
211 ArenaBitVector* visited,
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100212 ArenaBitVector* visiting);
Roland Levillainfc600dc2014-12-02 17:16:31 +0000213 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000214 void RemoveDeadBlocks(const ArenaBitVector& visited) const;
215
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000216 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000217
218 // List of blocks in insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000219 GrowableArray<HBasicBlock*> blocks_;
220
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100221 // List of blocks to perform a reverse post order tree traversal.
222 GrowableArray<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000223
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000224 HBasicBlock* entry_block_;
225 HBasicBlock* exit_block_;
226
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100227 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100228 uint16_t maximum_number_of_out_vregs_;
229
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100230 // The number of virtual registers in this method. Contains the parameters.
231 uint16_t number_of_vregs_;
232
233 // The number of virtual registers used by parameters of this method.
234 uint16_t number_of_in_vregs_;
235
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000236 // Number of vreg size slots that the temporaries use (used in baseline compiler).
237 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100238
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000239 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000240 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000241
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000242 // Cached null constant that might be created when building SSA form.
243 HNullConstant* cached_null_constant_;
244
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000245 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000246 DISALLOW_COPY_AND_ASSIGN(HGraph);
247};
248
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700249class HLoopInformation : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000250 public:
251 HLoopInformation(HBasicBlock* header, HGraph* graph)
252 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100253 suspend_check_(nullptr),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100254 back_edges_(graph->GetArena(), kDefaultNumberOfBackEdges),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100255 // Make bit vector growable, as the number of blocks may change.
256 blocks_(graph->GetArena(), graph->GetBlocks().Size(), true) {}
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100257
258 HBasicBlock* GetHeader() const {
259 return header_;
260 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000261
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000262 void SetHeader(HBasicBlock* block) {
263 header_ = block;
264 }
265
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100266 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
267 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
268 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
269
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000270 void AddBackEdge(HBasicBlock* back_edge) {
271 back_edges_.Add(back_edge);
272 }
273
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100274 void RemoveBackEdge(HBasicBlock* back_edge) {
275 back_edges_.Delete(back_edge);
276 }
277
278 bool IsBackEdge(HBasicBlock* block) {
279 for (size_t i = 0, e = back_edges_.Size(); i < e; ++i) {
280 if (back_edges_.Get(i) == block) return true;
281 }
282 return false;
283 }
284
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000285 size_t NumberOfBackEdges() const {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000286 return back_edges_.Size();
287 }
288
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100289 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100290
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100291 const GrowableArray<HBasicBlock*>& GetBackEdges() const {
292 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100293 }
294
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100295 void ClearBackEdges() {
296 back_edges_.Reset();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100297 }
298
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100299 // Find blocks that are part of this loop. Returns whether the loop is a natural loop,
300 // that is the header dominates the back edge.
301 bool Populate();
302
303 // Returns whether this loop information contains `block`.
304 // Note that this loop information *must* be populated before entering this function.
305 bool Contains(const HBasicBlock& block) const;
306
307 // Returns whether this loop information is an inner loop of `other`.
308 // Note that `other` *must* be populated before entering this function.
309 bool IsIn(const HLoopInformation& other) const;
310
311 const ArenaBitVector& GetBlocks() const { return blocks_; }
312
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000313 void Add(HBasicBlock* block);
314
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000315 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100316 // Internal recursive implementation of `Populate`.
317 void PopulateRecursive(HBasicBlock* block);
318
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000319 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100320 HSuspendCheck* suspend_check_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000321 GrowableArray<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100322 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000323
324 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
325};
326
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100327static constexpr size_t kNoLifetime = -1;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100328static constexpr uint32_t kNoDexPc = -1;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100329
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000330// A block in a method. Contains the list of instructions represented
331// as a double linked list. Each block knows its predecessors and
332// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100333
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700334class HBasicBlock : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000335 public:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100336 explicit HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000337 : graph_(graph),
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000338 predecessors_(graph->GetArena(), kDefaultNumberOfPredecessors),
339 successors_(graph->GetArena(), kDefaultNumberOfSuccessors),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000340 loop_information_(nullptr),
341 dominator_(nullptr),
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100342 dominated_blocks_(graph->GetArena(), kDefaultNumberOfDominatedBlocks),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100343 block_id_(-1),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100344 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100345 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000346 lifetime_end_(kNoLifetime),
347 is_catch_block_(false) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000348
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100349 const GrowableArray<HBasicBlock*>& GetPredecessors() const {
350 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000351 }
352
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100353 const GrowableArray<HBasicBlock*>& GetSuccessors() const {
354 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000355 }
356
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100357 const GrowableArray<HBasicBlock*>& GetDominatedBlocks() const {
358 return dominated_blocks_;
359 }
360
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100361 bool IsEntryBlock() const {
362 return graph_->GetEntryBlock() == this;
363 }
364
365 bool IsExitBlock() const {
366 return graph_->GetExitBlock() == this;
367 }
368
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000369 void AddBackEdge(HBasicBlock* back_edge) {
370 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000371 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000372 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100373 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000374 loop_information_->AddBackEdge(back_edge);
375 }
376
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000377 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000378 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000379
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000380 int GetBlockId() const { return block_id_; }
381 void SetBlockId(int id) { block_id_ = id; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000382
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000383 HBasicBlock* GetDominator() const { return dominator_; }
384 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100385 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.Add(block); }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000386 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
387 for (size_t i = 0, e = dominated_blocks_.Size(); i < e; ++i) {
388 if (dominated_blocks_.Get(i) == existing) {
389 dominated_blocks_.Put(i, new_block);
390 return;
391 }
392 }
393 LOG(FATAL) << "Unreachable";
394 UNREACHABLE();
395 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000396
397 int NumberOfBackEdges() const {
398 return loop_information_ == nullptr
399 ? 0
400 : loop_information_->NumberOfBackEdges();
401 }
402
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100403 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
404 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100405 const HInstructionList& GetInstructions() const { return instructions_; }
406 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100407 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000408
409 void AddSuccessor(HBasicBlock* block) {
410 successors_.Add(block);
411 block->predecessors_.Add(this);
412 }
413
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100414 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
415 size_t successor_index = GetSuccessorIndexOf(existing);
416 DCHECK_NE(successor_index, static_cast<size_t>(-1));
417 existing->RemovePredecessor(this);
418 new_block->predecessors_.Add(this);
419 successors_.Put(successor_index, new_block);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000420 }
421
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000422 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
423 size_t predecessor_index = GetPredecessorIndexOf(existing);
424 DCHECK_NE(predecessor_index, static_cast<size_t>(-1));
425 existing->RemoveSuccessor(this);
426 new_block->successors_.Add(this);
427 predecessors_.Put(predecessor_index, new_block);
428 }
429
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100430 void RemovePredecessor(HBasicBlock* block) {
431 predecessors_.Delete(block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100432 }
433
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000434 void RemoveSuccessor(HBasicBlock* block) {
435 successors_.Delete(block);
436 }
437
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100438 void ClearAllPredecessors() {
439 predecessors_.Reset();
440 }
441
442 void AddPredecessor(HBasicBlock* block) {
443 predecessors_.Add(block);
444 block->successors_.Add(this);
445 }
446
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100447 void SwapPredecessors() {
Nicolas Geoffrayc83d4412014-09-18 16:46:20 +0100448 DCHECK_EQ(predecessors_.Size(), 2u);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100449 HBasicBlock* temp = predecessors_.Get(0);
450 predecessors_.Put(0, predecessors_.Get(1));
451 predecessors_.Put(1, temp);
452 }
453
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100454 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) {
455 for (size_t i = 0, e = predecessors_.Size(); i < e; ++i) {
456 if (predecessors_.Get(i) == predecessor) {
457 return i;
458 }
459 }
460 return -1;
461 }
462
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100463 size_t GetSuccessorIndexOf(HBasicBlock* successor) {
464 for (size_t i = 0, e = successors_.Size(); i < e; ++i) {
465 if (successors_.Get(i) == successor) {
466 return i;
467 }
468 }
469 return -1;
470 }
471
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000472 // Split the block into two blocks just after `cursor`. Returns the newly
473 // created block. Note that this method just updates raw block information,
474 // like predecessors, successors, dominators, and instruction list. It does not
475 // update the graph, reverse post order, loop information, nor make sure the
476 // blocks are consistent (for example ending with a control flow instruction).
477 HBasicBlock* SplitAfter(HInstruction* cursor);
478
479 // Merge `other` at the end of `this`. Successors and dominated blocks of
480 // `other` are changed to be successors and dominated blocks of `this`. Note
481 // that this method does not update the graph, reverse post order, loop
482 // information, nor make sure the blocks are consistent (for example ending
483 // with a control flow instruction).
484 void MergeWith(HBasicBlock* other);
485
486 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
487 // of `this` are moved to `other`.
488 // Note that this method does not update the graph, reverse post order, loop
489 // information, nor make sure the blocks are consistent (for example ending
490 void ReplaceWith(HBasicBlock* other);
491
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000492 void AddInstruction(HInstruction* instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100493 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +0100494 // Replace instruction `initial` with `replacement` within this block.
495 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
496 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100497 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100498 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +0000499 // RemoveInstruction and RemovePhi delete a given instruction from the respective
500 // instruction list. With 'ensure_safety' set to true, it verifies that the
501 // instruction is not in use and removes it from the use lists of its inputs.
502 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
503 void RemovePhi(HPhi* phi, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100504
505 bool IsLoopHeader() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100506 return (loop_information_ != nullptr) && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100507 }
508
Roland Levillain6b879dd2014-09-22 17:13:44 +0100509 bool IsLoopPreHeaderFirstPredecessor() const {
510 DCHECK(IsLoopHeader());
511 DCHECK(!GetPredecessors().IsEmpty());
512 return GetPredecessors().Get(0) == GetLoopInformation()->GetPreHeader();
513 }
514
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100515 HLoopInformation* GetLoopInformation() const {
516 return loop_information_;
517 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000518
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000519 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100520 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000521 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100522 void SetInLoop(HLoopInformation* info) {
523 if (IsLoopHeader()) {
524 // Nothing to do. This just means `info` is an outer loop.
525 } else if (loop_information_ == nullptr) {
526 loop_information_ = info;
527 } else if (loop_information_->Contains(*info->GetHeader())) {
528 // Block is currently part of an outer loop. Make it part of this inner loop.
529 // Note that a non loop header having a loop information means this loop information
530 // has already been populated
531 loop_information_ = info;
532 } else {
533 // Block is part of an inner loop. Do not update the loop information.
534 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
535 // at this point, because this method is being called while populating `info`.
536 }
537 }
538
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000539 // Raw update of the loop information.
540 void SetLoopInformation(HLoopInformation* info) {
541 loop_information_ = info;
542 }
543
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100544 bool IsInLoop() const { return loop_information_ != nullptr; }
545
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100546 // Returns wheter this block dominates the blocked passed as parameter.
547 bool Dominates(HBasicBlock* block) const;
548
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100549 size_t GetLifetimeStart() const { return lifetime_start_; }
550 size_t GetLifetimeEnd() const { return lifetime_end_; }
551
552 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
553 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
554
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100555 uint32_t GetDexPc() const { return dex_pc_; }
556
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000557 bool IsCatchBlock() const { return is_catch_block_; }
558 void SetIsCatchBlock() { is_catch_block_ = true; }
559
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000560 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000561 HGraph* graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000562 GrowableArray<HBasicBlock*> predecessors_;
563 GrowableArray<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100564 HInstructionList instructions_;
565 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000566 HLoopInformation* loop_information_;
567 HBasicBlock* dominator_;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100568 GrowableArray<HBasicBlock*> dominated_blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000569 int block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100570 // The dex program counter of the first instruction of this block.
571 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100572 size_t lifetime_start_;
573 size_t lifetime_end_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000574 bool is_catch_block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000575
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000576 friend class HGraph;
577 friend class HInstruction;
578
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000579 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
580};
581
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100582#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
583 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000584 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000585 M(ArrayGet, Instruction) \
586 M(ArrayLength, Instruction) \
587 M(ArraySet, Instruction) \
588 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +0000589 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000590 M(CheckCast, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100591 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000592 M(Compare, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100593 M(Condition, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000594 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +0000595 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000596 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100597 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000598 M(Exit, Instruction) \
599 M(FloatConstant, Constant) \
600 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100601 M(GreaterThan, Condition) \
602 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100603 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000604 M(InstanceFieldGet, Instruction) \
605 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000606 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100607 M(IntConstant, Constant) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +0000608 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000609 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100610 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000611 M(LessThan, Condition) \
612 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000613 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000614 M(LoadException, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100615 M(LoadLocal, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000616 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100617 M(Local, Instruction) \
618 M(LongConstant, Constant) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +0000619 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000620 M(Mul, BinaryOperation) \
621 M(Neg, UnaryOperation) \
622 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100623 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +0100624 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000625 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000626 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000627 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000628 M(Or, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100629 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000630 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100631 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000632 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100633 M(Return, Instruction) \
634 M(ReturnVoid, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000635 M(Shl, BinaryOperation) \
636 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100637 M(StaticFieldGet, Instruction) \
638 M(StaticFieldSet, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100639 M(StoreLocal, Instruction) \
640 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100641 M(SuspendCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000642 M(Temporary, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000643 M(Throw, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +0000644 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000645 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000646 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000647
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100648#define FOR_EACH_INSTRUCTION(M) \
649 FOR_EACH_CONCRETE_INSTRUCTION(M) \
650 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +0100651 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +0100652 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100653 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -0700654
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100655#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000656FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
657#undef FORWARD_DECLARATION
658
Roland Levillainccc07a92014-09-16 14:48:16 +0100659#define DECLARE_INSTRUCTION(type) \
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100660 virtual InstructionKind GetKind() const { return k##type; } \
Roland Levillainccc07a92014-09-16 14:48:16 +0100661 virtual const char* DebugName() const { return #type; } \
662 virtual const H##type* As##type() const OVERRIDE { return this; } \
663 virtual H##type* As##type() OVERRIDE { return this; } \
664 virtual bool InstructionTypeEquals(HInstruction* other) const { \
665 return other->Is##type(); \
666 } \
667 virtual void Accept(HGraphVisitor* visitor)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000668
David Brazdiled596192015-01-23 10:39:45 +0000669template <typename T> class HUseList;
670
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100671template <typename T>
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700672class HUseListNode : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000673 public:
David Brazdiled596192015-01-23 10:39:45 +0000674 HUseListNode* GetPrevious() const { return prev_; }
675 HUseListNode* GetNext() const { return next_; }
676 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100677 size_t GetIndex() const { return index_; }
678
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000679 private:
David Brazdiled596192015-01-23 10:39:45 +0000680 HUseListNode(T user, size_t index)
681 : user_(user), index_(index), prev_(nullptr), next_(nullptr) {}
682
683 T const user_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100684 const size_t index_;
David Brazdiled596192015-01-23 10:39:45 +0000685 HUseListNode<T>* prev_;
686 HUseListNode<T>* next_;
687
688 friend class HUseList<T>;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000689
690 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
691};
692
David Brazdiled596192015-01-23 10:39:45 +0000693template <typename T>
694class HUseList : public ValueObject {
695 public:
696 HUseList() : first_(nullptr) {}
697
698 void Clear() {
699 first_ = nullptr;
700 }
701
702 // Adds a new entry at the beginning of the use list and returns
703 // the newly created node.
704 HUseListNode<T>* AddUse(T user, size_t index, ArenaAllocator* arena) {
David Brazdilea55b932015-01-27 17:12:29 +0000705 HUseListNode<T>* new_node = new (arena) HUseListNode<T>(user, index);
David Brazdiled596192015-01-23 10:39:45 +0000706 if (IsEmpty()) {
707 first_ = new_node;
708 } else {
709 first_->prev_ = new_node;
710 new_node->next_ = first_;
711 first_ = new_node;
712 }
713 return new_node;
714 }
715
716 HUseListNode<T>* GetFirst() const {
717 return first_;
718 }
719
720 void Remove(HUseListNode<T>* node) {
David Brazdil1abb4192015-02-17 18:33:36 +0000721 DCHECK(node != nullptr);
722 DCHECK(Contains(node));
723
David Brazdiled596192015-01-23 10:39:45 +0000724 if (node->prev_ != nullptr) {
725 node->prev_->next_ = node->next_;
726 }
727 if (node->next_ != nullptr) {
728 node->next_->prev_ = node->prev_;
729 }
730 if (node == first_) {
731 first_ = node->next_;
732 }
733 }
734
David Brazdil1abb4192015-02-17 18:33:36 +0000735 bool Contains(const HUseListNode<T>* node) const {
736 if (node == nullptr) {
737 return false;
738 }
739 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
740 if (current == node) {
741 return true;
742 }
743 }
744 return false;
745 }
746
David Brazdiled596192015-01-23 10:39:45 +0000747 bool IsEmpty() const {
748 return first_ == nullptr;
749 }
750
751 bool HasOnlyOneUse() const {
752 return first_ != nullptr && first_->next_ == nullptr;
753 }
754
755 private:
756 HUseListNode<T>* first_;
757};
758
759template<typename T>
760class HUseIterator : public ValueObject {
761 public:
762 explicit HUseIterator(const HUseList<T>& uses) : current_(uses.GetFirst()) {}
763
764 bool Done() const { return current_ == nullptr; }
765
766 void Advance() {
767 DCHECK(!Done());
768 current_ = current_->GetNext();
769 }
770
771 HUseListNode<T>* Current() const {
772 DCHECK(!Done());
773 return current_;
774 }
775
776 private:
777 HUseListNode<T>* current_;
778
779 friend class HValue;
780};
781
David Brazdil1abb4192015-02-17 18:33:36 +0000782// This class is used by HEnvironment and HInstruction classes to record the
783// instructions they use and pointers to the corresponding HUseListNodes kept
784// by the used instructions.
785template <typename T>
786class HUserRecord : public ValueObject {
787 public:
788 HUserRecord() : instruction_(nullptr), use_node_(nullptr) {}
789 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), use_node_(nullptr) {}
790
791 HUserRecord(const HUserRecord<T>& old_record, HUseListNode<T>* use_node)
792 : instruction_(old_record.instruction_), use_node_(use_node) {
793 DCHECK(instruction_ != nullptr);
794 DCHECK(use_node_ != nullptr);
795 DCHECK(old_record.use_node_ == nullptr);
796 }
797
798 HInstruction* GetInstruction() const { return instruction_; }
799 HUseListNode<T>* GetUseNode() const { return use_node_; }
800
801 private:
802 // Instruction used by the user.
803 HInstruction* instruction_;
804
805 // Corresponding entry in the use list kept by 'instruction_'.
806 HUseListNode<T>* use_node_;
807};
808
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100809// Represents the side effects an instruction may have.
810class SideEffects : public ValueObject {
811 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100812 SideEffects() : flags_(0) {}
813
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100814 static SideEffects None() {
815 return SideEffects(0);
816 }
817
818 static SideEffects All() {
819 return SideEffects(ChangesSomething().flags_ | DependsOnSomething().flags_);
820 }
821
822 static SideEffects ChangesSomething() {
823 return SideEffects((1 << kFlagChangesCount) - 1);
824 }
825
826 static SideEffects DependsOnSomething() {
827 int count = kFlagDependsOnCount - kFlagChangesCount;
828 return SideEffects(((1 << count) - 1) << kFlagChangesCount);
829 }
830
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100831 SideEffects Union(SideEffects other) const {
832 return SideEffects(flags_ | other.flags_);
833 }
834
Roland Levillain72bceff2014-09-15 18:29:00 +0100835 bool HasSideEffects() const {
836 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
837 return (flags_ & all_bits_set) != 0;
838 }
839
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100840 bool HasAllSideEffects() const {
841 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
842 return all_bits_set == (flags_ & all_bits_set);
843 }
844
845 bool DependsOn(SideEffects other) const {
846 size_t depends_flags = other.ComputeDependsFlags();
847 return (flags_ & depends_flags) != 0;
848 }
849
850 bool HasDependencies() const {
851 int count = kFlagDependsOnCount - kFlagChangesCount;
852 size_t all_bits_set = (1 << count) - 1;
853 return ((flags_ >> kFlagChangesCount) & all_bits_set) != 0;
854 }
855
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100856 private:
857 static constexpr int kFlagChangesSomething = 0;
858 static constexpr int kFlagChangesCount = kFlagChangesSomething + 1;
859
860 static constexpr int kFlagDependsOnSomething = kFlagChangesCount;
861 static constexpr int kFlagDependsOnCount = kFlagDependsOnSomething + 1;
862
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100863 explicit SideEffects(size_t flags) : flags_(flags) {}
864
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100865 size_t ComputeDependsFlags() const {
866 return flags_ << kFlagChangesCount;
867 }
868
869 size_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100870};
871
David Brazdiled596192015-01-23 10:39:45 +0000872// A HEnvironment object contains the values of virtual registers at a given location.
873class HEnvironment : public ArenaObject<kArenaAllocMisc> {
874 public:
875 HEnvironment(ArenaAllocator* arena, size_t number_of_vregs)
876 : vregs_(arena, number_of_vregs) {
877 vregs_.SetSize(number_of_vregs);
878 for (size_t i = 0; i < number_of_vregs; i++) {
David Brazdil1abb4192015-02-17 18:33:36 +0000879 vregs_.Put(i, HUserRecord<HEnvironment*>());
David Brazdiled596192015-01-23 10:39:45 +0000880 }
881 }
882
883 void CopyFrom(HEnvironment* env);
884
885 void SetRawEnvAt(size_t index, HInstruction* instruction) {
David Brazdil1abb4192015-02-17 18:33:36 +0000886 vregs_.Put(index, HUserRecord<HEnvironment*>(instruction));
David Brazdiled596192015-01-23 10:39:45 +0000887 }
888
889 HInstruction* GetInstructionAt(size_t index) const {
David Brazdil1abb4192015-02-17 18:33:36 +0000890 return vregs_.Get(index).GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +0000891 }
892
David Brazdil1abb4192015-02-17 18:33:36 +0000893 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +0000894
895 size_t Size() const { return vregs_.Size(); }
896
897 private:
David Brazdil1abb4192015-02-17 18:33:36 +0000898 // Record instructions' use entries of this environment for constant-time removal.
899 // It should only be called by HInstruction when a new environment use is added.
900 void RecordEnvUse(HUseListNode<HEnvironment*>* env_use) {
901 DCHECK(env_use->GetUser() == this);
902 size_t index = env_use->GetIndex();
903 vregs_.Put(index, HUserRecord<HEnvironment*>(vregs_.Get(index), env_use));
904 }
David Brazdiled596192015-01-23 10:39:45 +0000905
David Brazdil1abb4192015-02-17 18:33:36 +0000906 GrowableArray<HUserRecord<HEnvironment*> > vregs_;
David Brazdiled596192015-01-23 10:39:45 +0000907
David Brazdil1abb4192015-02-17 18:33:36 +0000908 friend HInstruction;
David Brazdiled596192015-01-23 10:39:45 +0000909
910 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
911};
912
Calin Juravleacf735c2015-02-12 15:25:22 +0000913class ReferenceTypeInfo : ValueObject {
914 public:
Calin Juravleb1498f62015-02-16 13:13:29 +0000915 typedef Handle<mirror::Class> TypeHandle;
916
917 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact)
918 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
919 if (type_handle->IsObjectClass()) {
920 // Override the type handle to be consistent with the case when we get to
921 // Top but don't have the Object class available. It avoids having to guess
922 // what value the type_handle has when it's Top.
923 return ReferenceTypeInfo(TypeHandle(), is_exact, true);
924 } else {
925 return ReferenceTypeInfo(type_handle, is_exact, false);
926 }
927 }
928
929 static ReferenceTypeInfo CreateTop(bool is_exact) {
930 return ReferenceTypeInfo(TypeHandle(), is_exact, true);
Calin Juravleacf735c2015-02-12 15:25:22 +0000931 }
932
933 bool IsExact() const { return is_exact_; }
934 bool IsTop() const { return is_top_; }
935
936 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
937
Calin Juravleb1498f62015-02-16 13:13:29 +0000938 bool IsSupertypeOf(ReferenceTypeInfo rti) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Calin Juravleacf735c2015-02-12 15:25:22 +0000939 if (IsTop()) {
940 // Top (equivalent for java.lang.Object) is supertype of anything.
941 return true;
942 }
943 if (rti.IsTop()) {
944 // If we get here `this` is not Top() so it can't be a supertype.
945 return false;
946 }
947 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
948 }
949
950 // Returns true if the type information provide the same amount of details.
951 // Note that it does not mean that the instructions have the same actual type
952 // (e.g. tops are equal but they can be the result of a merge).
953 bool IsEqual(ReferenceTypeInfo rti) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
954 if (IsExact() != rti.IsExact()) {
955 return false;
956 }
957 if (IsTop() && rti.IsTop()) {
958 // `Top` means java.lang.Object, so the types are equivalent.
959 return true;
960 }
961 if (IsTop() || rti.IsTop()) {
962 // If only one is top or object than they are not equivalent.
963 // NB: We need this extra check because the type_handle of `Top` is invalid
964 // and we cannot inspect its reference.
965 return false;
966 }
967
968 // Finally check the types.
969 return GetTypeHandle().Get() == rti.GetTypeHandle().Get();
970 }
971
972 private:
Calin Juravleb1498f62015-02-16 13:13:29 +0000973 ReferenceTypeInfo() : ReferenceTypeInfo(TypeHandle(), false, true) {}
974 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact, bool is_top)
975 : type_handle_(type_handle), is_exact_(is_exact), is_top_(is_top) {}
976
Calin Juravleacf735c2015-02-12 15:25:22 +0000977 // The class of the object.
Calin Juravleb1498f62015-02-16 13:13:29 +0000978 TypeHandle type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +0000979 // Whether or not the type is exact or a superclass of the actual type.
Calin Juravleb1498f62015-02-16 13:13:29 +0000980 // Whether or not we have any information about this type.
Calin Juravleacf735c2015-02-12 15:25:22 +0000981 bool is_exact_;
982 // A true value here means that the object type should be java.lang.Object.
983 // We don't have access to the corresponding mirror object every time so this
984 // flag acts as a substitute. When true, the TypeHandle refers to a null
985 // pointer and should not be used.
986 bool is_top_;
987};
988
989std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
990
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700991class HInstruction : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000992 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100993 explicit HInstruction(SideEffects side_effects)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000994 : previous_(nullptr),
995 next_(nullptr),
996 block_(nullptr),
997 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100998 ssa_index_(-1),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100999 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001000 locations_(nullptr),
1001 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001002 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001003 side_effects_(side_effects),
1004 reference_type_info_(ReferenceTypeInfo::CreateTop(/* is_exact */ false)) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001005
Dave Allison20dfc792014-06-16 20:44:29 -07001006 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001007
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001008#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001009 enum InstructionKind {
1010 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1011 };
1012#undef DECLARE_KIND
1013
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001014 HInstruction* GetNext() const { return next_; }
1015 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001016
Calin Juravle77520bc2015-01-12 18:45:46 +00001017 HInstruction* GetNextDisregardingMoves() const;
1018 HInstruction* GetPreviousDisregardingMoves() const;
1019
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001020 HBasicBlock* GetBlock() const { return block_; }
1021 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001022 bool IsInBlock() const { return block_ != nullptr; }
1023 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +01001024 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001025
Roland Levillain6b879dd2014-09-22 17:13:44 +01001026 virtual size_t InputCount() const = 0;
David Brazdil1abb4192015-02-17 18:33:36 +00001027 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001028
1029 virtual void Accept(HGraphVisitor* visitor) = 0;
1030 virtual const char* DebugName() const = 0;
1031
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001032 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
David Brazdil1abb4192015-02-17 18:33:36 +00001033 void SetRawInputAt(size_t index, HInstruction* input) {
1034 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1035 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001036
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001037 virtual bool NeedsEnvironment() const { return false; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001038 virtual bool IsControlFlow() const { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01001039 virtual bool CanThrow() const { return false; }
Roland Levillain72bceff2014-09-15 18:29:00 +01001040 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001041
Calin Juravle61d544b2015-02-23 16:46:57 +00001042 virtual bool ActAsNullConstant() const { return false; }
1043
Calin Juravle10e244f2015-01-26 18:54:32 +00001044 // Does not apply for all instructions, but having this at top level greatly
1045 // simplifies the null check elimination.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001046 virtual bool CanBeNull() const {
1047 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1048 return true;
1049 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001050
Calin Juravle77520bc2015-01-12 18:45:46 +00001051 virtual bool CanDoImplicitNullCheck() const { return false; }
1052
Calin Juravleacf735c2015-02-12 15:25:22 +00001053 void SetReferenceTypeInfo(ReferenceTypeInfo reference_type_info) {
Calin Juravle61d544b2015-02-23 16:46:57 +00001054 DCHECK_EQ(GetType(), Primitive::kPrimNot);
Calin Juravleacf735c2015-02-12 15:25:22 +00001055 reference_type_info_ = reference_type_info;
1056 }
1057
Calin Juravle61d544b2015-02-23 16:46:57 +00001058 ReferenceTypeInfo GetReferenceTypeInfo() const {
1059 DCHECK_EQ(GetType(), Primitive::kPrimNot);
1060 return reference_type_info_;
1061 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001062
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001063 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001064 DCHECK(user != nullptr);
1065 HUseListNode<HInstruction*>* use =
1066 uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1067 user->SetRawInputRecordAt(index, HUserRecord<HInstruction*>(user->InputRecordAt(index), use));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001068 }
1069
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001070 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001071 DCHECK(user != nullptr);
David Brazdiled596192015-01-23 10:39:45 +00001072 HUseListNode<HEnvironment*>* env_use =
1073 env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1074 user->RecordEnvUse(env_use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001075 }
1076
David Brazdil1abb4192015-02-17 18:33:36 +00001077 void RemoveAsUserOfInput(size_t input) {
1078 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
1079 input_use.GetInstruction()->uses_.Remove(input_use.GetUseNode());
1080 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001081
David Brazdil1abb4192015-02-17 18:33:36 +00001082 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1083 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001084
David Brazdiled596192015-01-23 10:39:45 +00001085 bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); }
1086 bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001087
Roland Levillain6c82d402014-10-13 16:10:27 +01001088 // Does this instruction strictly dominate `other_instruction`?
1089 // Returns false if this instruction and `other_instruction` are the same.
1090 // Aborts if this instruction and `other_instruction` are both phis.
1091 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001092
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001093 int GetId() const { return id_; }
1094 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001095
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001096 int GetSsaIndex() const { return ssa_index_; }
1097 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1098 bool HasSsaIndex() const { return ssa_index_ != -1; }
1099
1100 bool HasEnvironment() const { return environment_ != nullptr; }
1101 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001102 void SetEnvironment(HEnvironment* environment) { environment_ = environment; }
1103
Nicolas Geoffray39468442014-09-02 15:17:15 +01001104 // Returns the number of entries in the environment. Typically, that is the
1105 // number of dex registers in a method. It could be more in case of inlining.
1106 size_t EnvironmentSize() const;
1107
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001108 LocationSummary* GetLocations() const { return locations_; }
1109 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001110
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001111 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001112 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001113
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001114 // Move `this` instruction before `cursor`.
1115 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001116
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001117#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01001118 bool Is##type() const { return (As##type() != nullptr); } \
1119 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001120 virtual H##type* As##type() { return nullptr; }
1121
1122 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
1123#undef INSTRUCTION_TYPE_CHECK
1124
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001125 // Returns whether the instruction can be moved within the graph.
1126 virtual bool CanBeMoved() const { return false; }
1127
1128 // Returns whether the two instructions are of the same kind.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001129 virtual bool InstructionTypeEquals(HInstruction* other) const {
1130 UNUSED(other);
1131 return false;
1132 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001133
1134 // Returns whether any data encoded in the two instructions is equal.
1135 // This method does not look at the inputs. Both instructions must be
1136 // of the same type, otherwise the method has undefined behavior.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001137 virtual bool InstructionDataEquals(HInstruction* other) const {
1138 UNUSED(other);
1139 return false;
1140 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001141
1142 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00001143 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001144 // 2) Their inputs are identical.
1145 bool Equals(HInstruction* other) const;
1146
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001147 virtual InstructionKind GetKind() const = 0;
1148
1149 virtual size_t ComputeHashCode() const {
1150 size_t result = GetKind();
1151 for (size_t i = 0, e = InputCount(); i < e; ++i) {
1152 result = (result * 31) + InputAt(i)->GetId();
1153 }
1154 return result;
1155 }
1156
1157 SideEffects GetSideEffects() const { return side_effects_; }
1158
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001159 size_t GetLifetimePosition() const { return lifetime_position_; }
1160 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
1161 LiveInterval* GetLiveInterval() const { return live_interval_; }
1162 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
1163 bool HasLiveInterval() const { return live_interval_ != nullptr; }
1164
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001165 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
1166
1167 // Returns whether the code generation of the instruction will require to have access
1168 // to the current method. Such instructions are:
1169 // (1): Instructions that require an environment, as calling the runtime requires
1170 // to walk the stack and have the current method stored at a specific stack address.
1171 // (2): Object literals like classes and strings, that are loaded from the dex cache
1172 // fields of the current method.
1173 bool NeedsCurrentMethod() const {
1174 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
1175 }
1176
David Brazdil1abb4192015-02-17 18:33:36 +00001177 protected:
1178 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
1179 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
1180
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001181 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001182 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); }
1183
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001184 HInstruction* previous_;
1185 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001186 HBasicBlock* block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001187
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001188 // An instruction gets an id when it is added to the graph.
1189 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01001190 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001191 int id_;
1192
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001193 // When doing liveness analysis, instructions that have uses get an SSA index.
1194 int ssa_index_;
1195
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001196 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00001197 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001198
1199 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00001200 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001201
Nicolas Geoffray39468442014-09-02 15:17:15 +01001202 // The environment associated with this instruction. Not null if the instruction
1203 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001204 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001205
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001206 // Set by the code generator.
1207 LocationSummary* locations_;
1208
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001209 // Set by the liveness analysis.
1210 LiveInterval* live_interval_;
1211
1212 // Set by the liveness analysis, this is the position in a linear
1213 // order of blocks where this instruction's live interval start.
1214 size_t lifetime_position_;
1215
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001216 const SideEffects side_effects_;
1217
Calin Juravleacf735c2015-02-12 15:25:22 +00001218 // TODO: for primitive types this should be marked as invalid.
1219 ReferenceTypeInfo reference_type_info_;
1220
David Brazdil1abb4192015-02-17 18:33:36 +00001221 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001222 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00001223 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001224 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001225 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001226
1227 DISALLOW_COPY_AND_ASSIGN(HInstruction);
1228};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001229std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001230
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001231class HInputIterator : public ValueObject {
1232 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001233 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001234
1235 bool Done() const { return index_ == instruction_->InputCount(); }
1236 HInstruction* Current() const { return instruction_->InputAt(index_); }
1237 void Advance() { index_++; }
1238
1239 private:
1240 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001241 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001242
1243 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
1244};
1245
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001246class HInstructionIterator : public ValueObject {
1247 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001248 explicit HInstructionIterator(const HInstructionList& instructions)
1249 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001250 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001251 }
1252
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001253 bool Done() const { return instruction_ == nullptr; }
1254 HInstruction* Current() const { return instruction_; }
1255 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001256 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001257 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001258 }
1259
1260 private:
1261 HInstruction* instruction_;
1262 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001263
1264 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001265};
1266
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001267class HBackwardInstructionIterator : public ValueObject {
1268 public:
1269 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
1270 : instruction_(instructions.last_instruction_) {
1271 next_ = Done() ? nullptr : instruction_->GetPrevious();
1272 }
1273
1274 bool Done() const { return instruction_ == nullptr; }
1275 HInstruction* Current() const { return instruction_; }
1276 void Advance() {
1277 instruction_ = next_;
1278 next_ = Done() ? nullptr : instruction_->GetPrevious();
1279 }
1280
1281 private:
1282 HInstruction* instruction_;
1283 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001284
1285 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001286};
1287
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001288// An embedded container with N elements of type T. Used (with partial
1289// specialization for N=0) because embedded arrays cannot have size 0.
1290template<typename T, intptr_t N>
1291class EmbeddedArray {
1292 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001293 EmbeddedArray() : elements_() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001294
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001295 intptr_t GetLength() const { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001296
1297 const T& operator[](intptr_t i) const {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001298 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001299 return elements_[i];
1300 }
1301
1302 T& operator[](intptr_t i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001303 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001304 return elements_[i];
1305 }
1306
1307 const T& At(intptr_t i) const {
1308 return (*this)[i];
1309 }
1310
1311 void SetAt(intptr_t i, const T& val) {
1312 (*this)[i] = val;
1313 }
1314
1315 private:
1316 T elements_[N];
1317};
1318
1319template<typename T>
1320class EmbeddedArray<T, 0> {
1321 public:
1322 intptr_t length() const { return 0; }
1323 const T& operator[](intptr_t i) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001324 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001325 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001326 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001327 }
1328 T& operator[](intptr_t i) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001329 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001330 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001331 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001332 }
1333};
1334
1335template<intptr_t N>
1336class HTemplateInstruction: public HInstruction {
1337 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001338 HTemplateInstruction<N>(SideEffects side_effects)
1339 : HInstruction(side_effects), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07001340 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001341
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001342 virtual size_t InputCount() const { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001343
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001344 protected:
David Brazdil1abb4192015-02-17 18:33:36 +00001345 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_[i]; }
1346
1347 void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE {
1348 inputs_[i] = input;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001349 }
1350
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001351 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001352 EmbeddedArray<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001353
1354 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001355};
1356
Dave Allison20dfc792014-06-16 20:44:29 -07001357template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001358class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07001359 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001360 HExpression<N>(Primitive::Type type, SideEffects side_effects)
1361 : HTemplateInstruction<N>(side_effects), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07001362 virtual ~HExpression() {}
1363
1364 virtual Primitive::Type GetType() const { return type_; }
1365
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001366 protected:
1367 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07001368};
1369
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001370// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
1371// instruction that branches to the exit block.
1372class HReturnVoid : public HTemplateInstruction<0> {
1373 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001374 HReturnVoid() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001375
1376 virtual bool IsControlFlow() const { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001377
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001378 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001379
1380 private:
1381 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
1382};
1383
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001384// Represents dex's RETURN opcodes. A HReturn is a control flow
1385// instruction that branches to the exit block.
1386class HReturn : public HTemplateInstruction<1> {
1387 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001388 explicit HReturn(HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001389 SetRawInputAt(0, value);
1390 }
1391
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001392 virtual bool IsControlFlow() const { return true; }
1393
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001394 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001395
1396 private:
1397 DISALLOW_COPY_AND_ASSIGN(HReturn);
1398};
1399
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001400// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001401// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001402// exit block.
1403class HExit : public HTemplateInstruction<0> {
1404 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001405 HExit() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001406
1407 virtual bool IsControlFlow() const { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001408
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001409 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001410
1411 private:
1412 DISALLOW_COPY_AND_ASSIGN(HExit);
1413};
1414
1415// Jumps from one block to another.
1416class HGoto : public HTemplateInstruction<0> {
1417 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001418 HGoto() : HTemplateInstruction(SideEffects::None()) {}
1419
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001420 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001421
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001422 HBasicBlock* GetSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001423 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001424 }
1425
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001426 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001427
1428 private:
1429 DISALLOW_COPY_AND_ASSIGN(HGoto);
1430};
1431
Dave Allison20dfc792014-06-16 20:44:29 -07001432
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001433// Conditional branch. A block ending with an HIf instruction must have
1434// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001435class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001436 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001437 explicit HIf(HInstruction* input) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001438 SetRawInputAt(0, input);
1439 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001440
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001441 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001442
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001443 HBasicBlock* IfTrueSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001444 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001445 }
1446
1447 HBasicBlock* IfFalseSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001448 return GetBlock()->GetSuccessors().Get(1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001449 }
1450
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001451 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001452
Dave Allison20dfc792014-06-16 20:44:29 -07001453 virtual bool IsIfInstruction() const { return true; }
1454
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001455 private:
1456 DISALLOW_COPY_AND_ASSIGN(HIf);
1457};
1458
Roland Levillain88cb1752014-10-20 16:36:47 +01001459class HUnaryOperation : public HExpression<1> {
1460 public:
1461 HUnaryOperation(Primitive::Type result_type, HInstruction* input)
1462 : HExpression(result_type, SideEffects::None()) {
1463 SetRawInputAt(0, input);
1464 }
1465
1466 HInstruction* GetInput() const { return InputAt(0); }
1467 Primitive::Type GetResultType() const { return GetType(); }
1468
1469 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001470 virtual bool InstructionDataEquals(HInstruction* other) const {
1471 UNUSED(other);
1472 return true;
1473 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001474
Roland Levillain9240d6a2014-10-20 16:47:04 +01001475 // Try to statically evaluate `operation` and return a HConstant
1476 // containing the result of this evaluation. If `operation` cannot
1477 // be evaluated as a constant, return nullptr.
1478 HConstant* TryStaticEvaluation() const;
1479
1480 // Apply this operation to `x`.
1481 virtual int32_t Evaluate(int32_t x) const = 0;
1482 virtual int64_t Evaluate(int64_t x) const = 0;
1483
Roland Levillain88cb1752014-10-20 16:36:47 +01001484 DECLARE_INSTRUCTION(UnaryOperation);
1485
1486 private:
1487 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
1488};
1489
Dave Allison20dfc792014-06-16 20:44:29 -07001490class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001491 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001492 HBinaryOperation(Primitive::Type result_type,
1493 HInstruction* left,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001494 HInstruction* right) : HExpression(result_type, SideEffects::None()) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001495 SetRawInputAt(0, left);
1496 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001497 }
1498
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001499 HInstruction* GetLeft() const { return InputAt(0); }
1500 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07001501 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001502
1503 virtual bool IsCommutative() { return false; }
1504
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001505 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001506 virtual bool InstructionDataEquals(HInstruction* other) const {
1507 UNUSED(other);
1508 return true;
1509 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001510
Roland Levillain9240d6a2014-10-20 16:47:04 +01001511 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01001512 // containing the result of this evaluation. If `operation` cannot
1513 // be evaluated as a constant, return nullptr.
Roland Levillain9240d6a2014-10-20 16:47:04 +01001514 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01001515
1516 // Apply this operation to `x` and `y`.
1517 virtual int32_t Evaluate(int32_t x, int32_t y) const = 0;
1518 virtual int64_t Evaluate(int64_t x, int64_t y) const = 0;
1519
Roland Levillainccc07a92014-09-16 14:48:16 +01001520 DECLARE_INSTRUCTION(BinaryOperation);
1521
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001522 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001523 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
1524};
1525
Dave Allison20dfc792014-06-16 20:44:29 -07001526class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001527 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001528 HCondition(HInstruction* first, HInstruction* second)
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001529 : HBinaryOperation(Primitive::kPrimBoolean, first, second),
1530 needs_materialization_(true) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001531
1532 virtual bool IsCommutative() { return true; }
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001533
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001534 bool NeedsMaterialization() const { return needs_materialization_; }
1535 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001536
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001537 // For code generation purposes, returns whether this instruction is just before
1538 // `if_`, and disregard moves in between.
1539 bool IsBeforeWhenDisregardMoves(HIf* if_) const;
1540
Dave Allison20dfc792014-06-16 20:44:29 -07001541 DECLARE_INSTRUCTION(Condition);
1542
1543 virtual IfCondition GetCondition() const = 0;
1544
1545 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001546 // For register allocation purposes, returns whether this instruction needs to be
1547 // materialized (that is, not just be in the processor flags).
1548 bool needs_materialization_;
1549
Dave Allison20dfc792014-06-16 20:44:29 -07001550 DISALLOW_COPY_AND_ASSIGN(HCondition);
1551};
1552
1553// Instruction to check if two inputs are equal to each other.
1554class HEqual : public HCondition {
1555 public:
1556 HEqual(HInstruction* first, HInstruction* second)
1557 : HCondition(first, second) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001558
Roland Levillain93445682014-10-06 19:24:02 +01001559 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1560 return x == y ? 1 : 0;
1561 }
1562 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1563 return x == y ? 1 : 0;
1564 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001565
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001566 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001567
Dave Allison20dfc792014-06-16 20:44:29 -07001568 virtual IfCondition GetCondition() const {
1569 return kCondEQ;
1570 }
1571
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001572 private:
1573 DISALLOW_COPY_AND_ASSIGN(HEqual);
1574};
1575
Dave Allison20dfc792014-06-16 20:44:29 -07001576class HNotEqual : public HCondition {
1577 public:
1578 HNotEqual(HInstruction* first, HInstruction* second)
1579 : HCondition(first, second) {}
1580
Roland Levillain93445682014-10-06 19:24:02 +01001581 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1582 return x != y ? 1 : 0;
1583 }
1584 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1585 return x != y ? 1 : 0;
1586 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001587
Dave Allison20dfc792014-06-16 20:44:29 -07001588 DECLARE_INSTRUCTION(NotEqual);
1589
1590 virtual IfCondition GetCondition() const {
1591 return kCondNE;
1592 }
1593
1594 private:
1595 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
1596};
1597
1598class HLessThan : public HCondition {
1599 public:
1600 HLessThan(HInstruction* first, HInstruction* second)
1601 : HCondition(first, second) {}
1602
Roland Levillain93445682014-10-06 19:24:02 +01001603 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1604 return x < y ? 1 : 0;
1605 }
1606 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1607 return x < y ? 1 : 0;
1608 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001609
Dave Allison20dfc792014-06-16 20:44:29 -07001610 DECLARE_INSTRUCTION(LessThan);
1611
1612 virtual IfCondition GetCondition() const {
1613 return kCondLT;
1614 }
1615
1616 private:
1617 DISALLOW_COPY_AND_ASSIGN(HLessThan);
1618};
1619
1620class HLessThanOrEqual : public HCondition {
1621 public:
1622 HLessThanOrEqual(HInstruction* first, HInstruction* second)
1623 : HCondition(first, second) {}
1624
Roland Levillain93445682014-10-06 19:24:02 +01001625 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1626 return x <= y ? 1 : 0;
1627 }
1628 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1629 return x <= y ? 1 : 0;
1630 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001631
Dave Allison20dfc792014-06-16 20:44:29 -07001632 DECLARE_INSTRUCTION(LessThanOrEqual);
1633
1634 virtual IfCondition GetCondition() const {
1635 return kCondLE;
1636 }
1637
1638 private:
1639 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
1640};
1641
1642class HGreaterThan : public HCondition {
1643 public:
1644 HGreaterThan(HInstruction* first, HInstruction* second)
1645 : HCondition(first, second) {}
1646
Roland Levillain93445682014-10-06 19:24:02 +01001647 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1648 return x > y ? 1 : 0;
1649 }
1650 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1651 return x > y ? 1 : 0;
1652 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001653
Dave Allison20dfc792014-06-16 20:44:29 -07001654 DECLARE_INSTRUCTION(GreaterThan);
1655
1656 virtual IfCondition GetCondition() const {
1657 return kCondGT;
1658 }
1659
1660 private:
1661 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
1662};
1663
1664class HGreaterThanOrEqual : public HCondition {
1665 public:
1666 HGreaterThanOrEqual(HInstruction* first, HInstruction* second)
1667 : HCondition(first, second) {}
1668
Roland Levillain93445682014-10-06 19:24:02 +01001669 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1670 return x >= y ? 1 : 0;
1671 }
1672 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1673 return x >= y ? 1 : 0;
1674 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001675
Dave Allison20dfc792014-06-16 20:44:29 -07001676 DECLARE_INSTRUCTION(GreaterThanOrEqual);
1677
1678 virtual IfCondition GetCondition() const {
1679 return kCondGE;
1680 }
1681
1682 private:
1683 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
1684};
1685
1686
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001687// Instruction to check how two inputs compare to each other.
1688// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
1689class HCompare : public HBinaryOperation {
1690 public:
Calin Juravleddb7df22014-11-25 20:56:51 +00001691 // The bias applies for floating point operations and indicates how NaN
1692 // comparisons are treated:
1693 enum Bias {
1694 kNoBias, // bias is not applicable (i.e. for long operation)
1695 kGtBias, // return 1 for NaN comparisons
1696 kLtBias, // return -1 for NaN comparisons
1697 };
1698
1699 HCompare(Primitive::Type type, HInstruction* first, HInstruction* second, Bias bias)
1700 : HBinaryOperation(Primitive::kPrimInt, first, second), bias_(bias) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001701 DCHECK_EQ(type, first->GetType());
1702 DCHECK_EQ(type, second->GetType());
1703 }
1704
Calin Juravleddb7df22014-11-25 20:56:51 +00001705 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01001706 return
1707 x == y ? 0 :
1708 x > y ? 1 :
1709 -1;
1710 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001711
1712 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01001713 return
1714 x == y ? 0 :
1715 x > y ? 1 :
1716 -1;
1717 }
1718
Calin Juravleddb7df22014-11-25 20:56:51 +00001719 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
1720 return bias_ == other->AsCompare()->bias_;
1721 }
1722
1723 bool IsGtBias() { return bias_ == kGtBias; }
1724
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001725 DECLARE_INSTRUCTION(Compare);
1726
1727 private:
Calin Juravleddb7df22014-11-25 20:56:51 +00001728 const Bias bias_;
1729
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001730 DISALLOW_COPY_AND_ASSIGN(HCompare);
1731};
1732
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001733// A local in the graph. Corresponds to a Dex register.
1734class HLocal : public HTemplateInstruction<0> {
1735 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001736 explicit HLocal(uint16_t reg_number)
1737 : HTemplateInstruction(SideEffects::None()), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001738
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001739 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001740
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001741 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001742
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001743 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001744 // The Dex register number.
1745 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001746
1747 DISALLOW_COPY_AND_ASSIGN(HLocal);
1748};
1749
1750// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07001751class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001752 public:
Roland Levillain5799fc02014-09-25 12:15:20 +01001753 HLoadLocal(HLocal* local, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001754 : HExpression(type, SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001755 SetRawInputAt(0, local);
1756 }
1757
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001758 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1759
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001760 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001761
1762 private:
1763 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
1764};
1765
1766// Store a value in a given local. This instruction has two inputs: the value
1767// and the local.
1768class HStoreLocal : public HTemplateInstruction<2> {
1769 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001770 HStoreLocal(HLocal* local, HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001771 SetRawInputAt(0, local);
1772 SetRawInputAt(1, value);
1773 }
1774
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001775 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1776
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001777 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001778
1779 private:
1780 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
1781};
1782
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001783class HConstant : public HExpression<0> {
1784 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001785 explicit HConstant(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
1786
1787 virtual bool CanBeMoved() const { return true; }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001788
1789 DECLARE_INSTRUCTION(Constant);
1790
1791 private:
1792 DISALLOW_COPY_AND_ASSIGN(HConstant);
1793};
1794
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001795class HFloatConstant : public HConstant {
1796 public:
1797 explicit HFloatConstant(float value) : HConstant(Primitive::kPrimFloat), value_(value) {}
1798
1799 float GetValue() const { return value_; }
1800
1801 virtual bool InstructionDataEquals(HInstruction* other) const {
1802 return bit_cast<float, int32_t>(other->AsFloatConstant()->value_) ==
1803 bit_cast<float, int32_t>(value_);
1804 }
1805
1806 virtual size_t ComputeHashCode() const { return static_cast<size_t>(GetValue()); }
1807
1808 DECLARE_INSTRUCTION(FloatConstant);
1809
1810 private:
1811 const float value_;
1812
1813 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
1814};
1815
1816class HDoubleConstant : public HConstant {
1817 public:
1818 explicit HDoubleConstant(double value) : HConstant(Primitive::kPrimDouble), value_(value) {}
1819
1820 double GetValue() const { return value_; }
1821
1822 virtual bool InstructionDataEquals(HInstruction* other) const {
1823 return bit_cast<double, int64_t>(other->AsDoubleConstant()->value_) ==
1824 bit_cast<double, int64_t>(value_);
1825 }
1826
1827 virtual size_t ComputeHashCode() const { return static_cast<size_t>(GetValue()); }
1828
1829 DECLARE_INSTRUCTION(DoubleConstant);
1830
1831 private:
1832 const double value_;
1833
1834 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
1835};
1836
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001837class HNullConstant : public HConstant {
1838 public:
1839 HNullConstant() : HConstant(Primitive::kPrimNot) {}
1840
1841 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
1842 return true;
1843 }
1844
1845 size_t ComputeHashCode() const OVERRIDE { return 0; }
1846
Calin Juravle61d544b2015-02-23 16:46:57 +00001847 bool ActAsNullConstant() const OVERRIDE { return true; }
1848
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001849 DECLARE_INSTRUCTION(NullConstant);
1850
1851 private:
1852 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
1853};
1854
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001855// Constants of the type int. Those can be from Dex instructions, or
1856// synthesized (for example with the if-eqz instruction).
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001857class HIntConstant : public HConstant {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001858 public:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001859 explicit HIntConstant(int32_t value) : HConstant(Primitive::kPrimInt), value_(value) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001860
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001861 int32_t GetValue() const { return value_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001862
Calin Juravle61d544b2015-02-23 16:46:57 +00001863 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001864 return other->AsIntConstant()->value_ == value_;
1865 }
1866
Calin Juravle61d544b2015-02-23 16:46:57 +00001867 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
1868
1869 // TODO: Null is represented by the `0` constant. In most cases we replace it
1870 // with a HNullConstant but we don't do it when comparing (a != null). This
1871 // method is an workaround until we fix the above.
1872 bool ActAsNullConstant() const OVERRIDE { return value_ == 0; }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001873
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001874 DECLARE_INSTRUCTION(IntConstant);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001875
1876 private:
1877 const int32_t value_;
1878
1879 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
1880};
1881
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001882class HLongConstant : public HConstant {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001883 public:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001884 explicit HLongConstant(int64_t value) : HConstant(Primitive::kPrimLong), value_(value) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001885
1886 int64_t GetValue() const { return value_; }
1887
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001888 virtual bool InstructionDataEquals(HInstruction* other) const {
1889 return other->AsLongConstant()->value_ == value_;
1890 }
1891
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001892 virtual size_t ComputeHashCode() const { return static_cast<size_t>(GetValue()); }
1893
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001894 DECLARE_INSTRUCTION(LongConstant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001895
1896 private:
1897 const int64_t value_;
1898
1899 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
1900};
1901
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001902enum class Intrinsics {
1903#define OPTIMIZING_INTRINSICS(Name, IsStatic) k ## Name,
1904#include "intrinsics_list.h"
1905 kNone,
1906 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
1907#undef INTRINSICS_LIST
1908#undef OPTIMIZING_INTRINSICS
1909};
1910std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
1911
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001912class HInvoke : public HInstruction {
1913 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001914 virtual size_t InputCount() const { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001915
1916 // Runtime needs to walk the stack, so Dex -> Dex calls need to
1917 // know their environment.
Calin Juravle77520bc2015-01-12 18:45:46 +00001918 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001919
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001920 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001921 SetRawInputAt(index, argument);
1922 }
1923
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001924 virtual Primitive::Type GetType() const { return return_type_; }
1925
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001926 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001927
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001928 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
1929
1930 Intrinsics GetIntrinsic() {
1931 return intrinsic_;
1932 }
1933
1934 void SetIntrinsic(Intrinsics intrinsic) {
1935 intrinsic_ = intrinsic;
1936 }
1937
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001938 DECLARE_INSTRUCTION(Invoke);
1939
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001940 protected:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001941 HInvoke(ArenaAllocator* arena,
1942 uint32_t number_of_arguments,
1943 Primitive::Type return_type,
1944 uint32_t dex_pc,
1945 uint32_t dex_method_index)
1946 : HInstruction(SideEffects::All()),
1947 inputs_(arena, number_of_arguments),
1948 return_type_(return_type),
1949 dex_pc_(dex_pc),
1950 dex_method_index_(dex_method_index),
1951 intrinsic_(Intrinsics::kNone) {
1952 inputs_.SetSize(number_of_arguments);
1953 }
1954
David Brazdil1abb4192015-02-17 18:33:36 +00001955 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
1956 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
1957 inputs_.Put(index, input);
1958 }
1959
1960 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001961 const Primitive::Type return_type_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001962 const uint32_t dex_pc_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001963 const uint32_t dex_method_index_;
1964 Intrinsics intrinsic_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001965
1966 private:
1967 DISALLOW_COPY_AND_ASSIGN(HInvoke);
1968};
1969
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001970class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001971 public:
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001972 HInvokeStaticOrDirect(ArenaAllocator* arena,
1973 uint32_t number_of_arguments,
1974 Primitive::Type return_type,
1975 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001976 uint32_t dex_method_index,
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001977 bool is_recursive,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001978 InvokeType invoke_type)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001979 : HInvoke(arena, number_of_arguments, return_type, dex_pc, dex_method_index),
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001980 invoke_type_(invoke_type),
1981 is_recursive_(is_recursive) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001982
Calin Juravle77520bc2015-01-12 18:45:46 +00001983 bool CanDoImplicitNullCheck() const OVERRIDE {
1984 // We access the method via the dex cache so we can't do an implicit null check.
1985 // TODO: for intrinsics we can generate implicit null checks.
1986 return false;
1987 }
1988
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001989 InvokeType GetInvokeType() const { return invoke_type_; }
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001990 bool IsRecursive() const { return is_recursive_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001991
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001992 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001993
1994 private:
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001995 const InvokeType invoke_type_;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001996 const bool is_recursive_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001997
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001998 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001999};
2000
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002001class HInvokeVirtual : public HInvoke {
2002 public:
2003 HInvokeVirtual(ArenaAllocator* arena,
2004 uint32_t number_of_arguments,
2005 Primitive::Type return_type,
2006 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002007 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002008 uint32_t vtable_index)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002009 : HInvoke(arena, number_of_arguments, return_type, dex_pc, dex_method_index),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002010 vtable_index_(vtable_index) {}
2011
Calin Juravle77520bc2015-01-12 18:45:46 +00002012 bool CanDoImplicitNullCheck() const OVERRIDE {
2013 // TODO: Add implicit null checks in intrinsics.
2014 return !GetLocations()->Intrinsified();
2015 }
2016
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002017 uint32_t GetVTableIndex() const { return vtable_index_; }
2018
2019 DECLARE_INSTRUCTION(InvokeVirtual);
2020
2021 private:
2022 const uint32_t vtable_index_;
2023
2024 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
2025};
2026
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002027class HInvokeInterface : public HInvoke {
2028 public:
2029 HInvokeInterface(ArenaAllocator* arena,
2030 uint32_t number_of_arguments,
2031 Primitive::Type return_type,
2032 uint32_t dex_pc,
2033 uint32_t dex_method_index,
2034 uint32_t imt_index)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002035 : HInvoke(arena, number_of_arguments, return_type, dex_pc, dex_method_index),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002036 imt_index_(imt_index) {}
2037
Calin Juravle77520bc2015-01-12 18:45:46 +00002038 bool CanDoImplicitNullCheck() const OVERRIDE {
2039 // TODO: Add implicit null checks in intrinsics.
2040 return !GetLocations()->Intrinsified();
2041 }
2042
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002043 uint32_t GetImtIndex() const { return imt_index_; }
2044 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
2045
2046 DECLARE_INSTRUCTION(InvokeInterface);
2047
2048 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002049 const uint32_t imt_index_;
2050
2051 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
2052};
2053
Dave Allison20dfc792014-06-16 20:44:29 -07002054class HNewInstance : public HExpression<0> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002055 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002056 HNewInstance(uint32_t dex_pc, uint16_t type_index, QuickEntrypointEnum entrypoint)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002057 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2058 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002059 type_index_(type_index),
2060 entrypoint_(entrypoint) {}
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002061
2062 uint32_t GetDexPc() const { return dex_pc_; }
2063 uint16_t GetTypeIndex() const { return type_index_; }
2064
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002065 // Calls runtime so needs an environment.
Calin Juravle92a6ed22014-12-02 18:58:03 +00002066 bool NeedsEnvironment() const OVERRIDE { return true; }
2067 // It may throw when called on:
2068 // - interfaces
2069 // - abstract/innaccessible/unknown classes
2070 // TODO: optimize when possible.
2071 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002072
Calin Juravle10e244f2015-01-26 18:54:32 +00002073 bool CanBeNull() const OVERRIDE { return false; }
2074
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002075 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
2076
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002077 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002078
2079 private:
2080 const uint32_t dex_pc_;
2081 const uint16_t type_index_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002082 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002083
2084 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
2085};
2086
Roland Levillain88cb1752014-10-20 16:36:47 +01002087class HNeg : public HUnaryOperation {
2088 public:
2089 explicit HNeg(Primitive::Type result_type, HInstruction* input)
2090 : HUnaryOperation(result_type, input) {}
2091
Roland Levillain9240d6a2014-10-20 16:47:04 +01002092 virtual int32_t Evaluate(int32_t x) const OVERRIDE { return -x; }
2093 virtual int64_t Evaluate(int64_t x) const OVERRIDE { return -x; }
2094
Roland Levillain88cb1752014-10-20 16:36:47 +01002095 DECLARE_INSTRUCTION(Neg);
2096
2097 private:
2098 DISALLOW_COPY_AND_ASSIGN(HNeg);
2099};
2100
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002101class HNewArray : public HExpression<1> {
2102 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002103 HNewArray(HInstruction* length,
2104 uint32_t dex_pc,
2105 uint16_t type_index,
2106 QuickEntrypointEnum entrypoint)
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002107 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2108 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002109 type_index_(type_index),
2110 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002111 SetRawInputAt(0, length);
2112 }
2113
2114 uint32_t GetDexPc() const { return dex_pc_; }
2115 uint16_t GetTypeIndex() const { return type_index_; }
2116
2117 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00002118 bool NeedsEnvironment() const OVERRIDE { return true; }
2119
2120 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002121
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002122 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
2123
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002124 DECLARE_INSTRUCTION(NewArray);
2125
2126 private:
2127 const uint32_t dex_pc_;
2128 const uint16_t type_index_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002129 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002130
2131 DISALLOW_COPY_AND_ASSIGN(HNewArray);
2132};
2133
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002134class HAdd : public HBinaryOperation {
2135 public:
2136 HAdd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2137 : HBinaryOperation(result_type, left, right) {}
2138
2139 virtual bool IsCommutative() { return true; }
2140
Roland Levillain93445682014-10-06 19:24:02 +01002141 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
2142 return x + y;
2143 }
2144 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
2145 return x + y;
2146 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002147
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002148 DECLARE_INSTRUCTION(Add);
2149
2150 private:
2151 DISALLOW_COPY_AND_ASSIGN(HAdd);
2152};
2153
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002154class HSub : public HBinaryOperation {
2155 public:
2156 HSub(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2157 : HBinaryOperation(result_type, left, right) {}
2158
Roland Levillain93445682014-10-06 19:24:02 +01002159 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
2160 return x - y;
2161 }
2162 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
2163 return x - y;
2164 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002165
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002166 DECLARE_INSTRUCTION(Sub);
2167
2168 private:
2169 DISALLOW_COPY_AND_ASSIGN(HSub);
2170};
2171
Calin Juravle34bacdf2014-10-07 20:23:36 +01002172class HMul : public HBinaryOperation {
2173 public:
2174 HMul(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2175 : HBinaryOperation(result_type, left, right) {}
2176
2177 virtual bool IsCommutative() { return true; }
2178
2179 virtual int32_t Evaluate(int32_t x, int32_t y) const { return x * y; }
2180 virtual int64_t Evaluate(int64_t x, int64_t y) const { return x * y; }
2181
2182 DECLARE_INSTRUCTION(Mul);
2183
2184 private:
2185 DISALLOW_COPY_AND_ASSIGN(HMul);
2186};
2187
Calin Juravle7c4954d2014-10-28 16:57:40 +00002188class HDiv : public HBinaryOperation {
2189 public:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002190 HDiv(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
2191 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00002192
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00002193 virtual int32_t Evaluate(int32_t x, int32_t y) const {
2194 // Our graph structure ensures we never have 0 for `y` during constant folding.
2195 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00002196 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00002197 return (y == -1) ? -x : x / y;
2198 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002199
2200 virtual int64_t Evaluate(int64_t x, int64_t y) const {
2201 DCHECK_NE(y, 0);
2202 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2203 return (y == -1) ? -x : x / y;
2204 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002205
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002206 uint32_t GetDexPc() const { return dex_pc_; }
2207
Calin Juravle7c4954d2014-10-28 16:57:40 +00002208 DECLARE_INSTRUCTION(Div);
2209
2210 private:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002211 const uint32_t dex_pc_;
2212
Calin Juravle7c4954d2014-10-28 16:57:40 +00002213 DISALLOW_COPY_AND_ASSIGN(HDiv);
2214};
2215
Calin Juravlebacfec32014-11-14 15:54:36 +00002216class HRem : public HBinaryOperation {
2217 public:
2218 HRem(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
2219 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
2220
2221 virtual int32_t Evaluate(int32_t x, int32_t y) const {
2222 DCHECK_NE(y, 0);
2223 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2224 return (y == -1) ? 0 : x % y;
2225 }
2226
2227 virtual int64_t Evaluate(int64_t x, int64_t y) const {
2228 DCHECK_NE(y, 0);
2229 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2230 return (y == -1) ? 0 : x % y;
2231 }
2232
2233 uint32_t GetDexPc() const { return dex_pc_; }
2234
2235 DECLARE_INSTRUCTION(Rem);
2236
2237 private:
2238 const uint32_t dex_pc_;
2239
2240 DISALLOW_COPY_AND_ASSIGN(HRem);
2241};
2242
Calin Juravled0d48522014-11-04 16:40:20 +00002243class HDivZeroCheck : public HExpression<1> {
2244 public:
2245 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
2246 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
2247 SetRawInputAt(0, value);
2248 }
2249
2250 bool CanBeMoved() const OVERRIDE { return true; }
2251
2252 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2253 UNUSED(other);
2254 return true;
2255 }
2256
2257 bool NeedsEnvironment() const OVERRIDE { return true; }
2258 bool CanThrow() const OVERRIDE { return true; }
2259
2260 uint32_t GetDexPc() const { return dex_pc_; }
2261
2262 DECLARE_INSTRUCTION(DivZeroCheck);
2263
2264 private:
2265 const uint32_t dex_pc_;
2266
2267 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
2268};
2269
Calin Juravle9aec02f2014-11-18 23:06:35 +00002270class HShl : public HBinaryOperation {
2271 public:
2272 HShl(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2273 : HBinaryOperation(result_type, left, right) {}
2274
2275 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x << (y & kMaxIntShiftValue); }
2276 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x << (y & kMaxLongShiftValue); }
2277
2278 DECLARE_INSTRUCTION(Shl);
2279
2280 private:
2281 DISALLOW_COPY_AND_ASSIGN(HShl);
2282};
2283
2284class HShr : public HBinaryOperation {
2285 public:
2286 HShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2287 : HBinaryOperation(result_type, left, right) {}
2288
2289 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x >> (y & kMaxIntShiftValue); }
2290 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x >> (y & kMaxLongShiftValue); }
2291
2292 DECLARE_INSTRUCTION(Shr);
2293
2294 private:
2295 DISALLOW_COPY_AND_ASSIGN(HShr);
2296};
2297
2298class HUShr : public HBinaryOperation {
2299 public:
2300 HUShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2301 : HBinaryOperation(result_type, left, right) {}
2302
2303 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
2304 uint32_t ux = static_cast<uint32_t>(x);
2305 uint32_t uy = static_cast<uint32_t>(y) & kMaxIntShiftValue;
2306 return static_cast<int32_t>(ux >> uy);
2307 }
2308
2309 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
2310 uint64_t ux = static_cast<uint64_t>(x);
2311 uint64_t uy = static_cast<uint64_t>(y) & kMaxLongShiftValue;
2312 return static_cast<int64_t>(ux >> uy);
2313 }
2314
2315 DECLARE_INSTRUCTION(UShr);
2316
2317 private:
2318 DISALLOW_COPY_AND_ASSIGN(HUShr);
2319};
2320
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002321class HAnd : public HBinaryOperation {
2322 public:
2323 HAnd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2324 : HBinaryOperation(result_type, left, right) {}
2325
2326 bool IsCommutative() OVERRIDE { return true; }
2327
2328 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x & y; }
2329 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x & y; }
2330
2331 DECLARE_INSTRUCTION(And);
2332
2333 private:
2334 DISALLOW_COPY_AND_ASSIGN(HAnd);
2335};
2336
2337class HOr : public HBinaryOperation {
2338 public:
2339 HOr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2340 : HBinaryOperation(result_type, left, right) {}
2341
2342 bool IsCommutative() OVERRIDE { return true; }
2343
2344 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x | y; }
2345 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x | y; }
2346
2347 DECLARE_INSTRUCTION(Or);
2348
2349 private:
2350 DISALLOW_COPY_AND_ASSIGN(HOr);
2351};
2352
2353class HXor : public HBinaryOperation {
2354 public:
2355 HXor(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2356 : HBinaryOperation(result_type, left, right) {}
2357
2358 bool IsCommutative() OVERRIDE { return true; }
2359
2360 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x ^ y; }
2361 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x ^ y; }
2362
2363 DECLARE_INSTRUCTION(Xor);
2364
2365 private:
2366 DISALLOW_COPY_AND_ASSIGN(HXor);
2367};
2368
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002369// The value of a parameter in this method. Its location depends on
2370// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07002371class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002372 public:
Calin Juravle10e244f2015-01-26 18:54:32 +00002373 HParameterValue(uint8_t index, Primitive::Type parameter_type, bool is_this = false)
2374 : HExpression(parameter_type, SideEffects::None()), index_(index), is_this_(is_this) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002375
2376 uint8_t GetIndex() const { return index_; }
2377
Calin Juravle10e244f2015-01-26 18:54:32 +00002378 bool CanBeNull() const OVERRIDE { return !is_this_; }
2379
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002380 DECLARE_INSTRUCTION(ParameterValue);
2381
2382 private:
2383 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00002384 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002385 const uint8_t index_;
2386
Calin Juravle10e244f2015-01-26 18:54:32 +00002387 // Whether or not the parameter value corresponds to 'this' argument.
2388 const bool is_this_;
2389
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002390 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
2391};
2392
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002393class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002394 public:
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002395 explicit HNot(Primitive::Type result_type, HInstruction* input)
2396 : HUnaryOperation(result_type, input) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002397
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002398 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002399 virtual bool InstructionDataEquals(HInstruction* other) const {
2400 UNUSED(other);
2401 return true;
2402 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002403
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002404 virtual int32_t Evaluate(int32_t x) const OVERRIDE { return ~x; }
2405 virtual int64_t Evaluate(int64_t x) const OVERRIDE { return ~x; }
2406
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002407 DECLARE_INSTRUCTION(Not);
2408
2409 private:
2410 DISALLOW_COPY_AND_ASSIGN(HNot);
2411};
2412
Roland Levillaindff1f282014-11-05 14:15:05 +00002413class HTypeConversion : public HExpression<1> {
2414 public:
2415 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00002416 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
2417 : HExpression(result_type, SideEffects::None()), dex_pc_(dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002418 SetRawInputAt(0, input);
2419 DCHECK_NE(input->GetType(), result_type);
2420 }
2421
2422 HInstruction* GetInput() const { return InputAt(0); }
2423 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
2424 Primitive::Type GetResultType() const { return GetType(); }
2425
Roland Levillain624279f2014-12-04 11:54:28 +00002426 // Required by the x86 and ARM code generators when producing calls
2427 // to the runtime.
2428 uint32_t GetDexPc() const { return dex_pc_; }
2429
Roland Levillaindff1f282014-11-05 14:15:05 +00002430 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00002431 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00002432
2433 DECLARE_INSTRUCTION(TypeConversion);
2434
2435 private:
Roland Levillain624279f2014-12-04 11:54:28 +00002436 const uint32_t dex_pc_;
2437
Roland Levillaindff1f282014-11-05 14:15:05 +00002438 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
2439};
2440
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00002441static constexpr uint32_t kNoRegNumber = -1;
2442
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002443class HPhi : public HInstruction {
2444 public:
2445 HPhi(ArenaAllocator* arena, uint32_t reg_number, size_t number_of_inputs, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002446 : HInstruction(SideEffects::None()),
2447 inputs_(arena, number_of_inputs),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002448 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002449 type_(type),
Calin Juravle10e244f2015-01-26 18:54:32 +00002450 is_live_(false),
2451 can_be_null_(true) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002452 inputs_.SetSize(number_of_inputs);
2453 }
2454
Calin Juravle10e244f2015-01-26 18:54:32 +00002455 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002456
2457 void AddInput(HInstruction* input);
2458
Calin Juravle10e244f2015-01-26 18:54:32 +00002459 Primitive::Type GetType() const OVERRIDE { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002460 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002461
Calin Juravle10e244f2015-01-26 18:54:32 +00002462 bool CanBeNull() const OVERRIDE { return can_be_null_; }
2463 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
2464
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002465 uint32_t GetRegNumber() const { return reg_number_; }
2466
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002467 void SetDead() { is_live_ = false; }
2468 void SetLive() { is_live_ = true; }
2469 bool IsDead() const { return !is_live_; }
2470 bool IsLive() const { return is_live_; }
2471
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002472 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002473
David Brazdil1abb4192015-02-17 18:33:36 +00002474 protected:
2475 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
2476
2477 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
2478 inputs_.Put(index, input);
2479 }
2480
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002481 private:
David Brazdil1abb4192015-02-17 18:33:36 +00002482 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002483 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002484 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002485 bool is_live_;
Calin Juravle10e244f2015-01-26 18:54:32 +00002486 bool can_be_null_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002487
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002488 DISALLOW_COPY_AND_ASSIGN(HPhi);
2489};
2490
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002491class HNullCheck : public HExpression<1> {
2492 public:
2493 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002494 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002495 SetRawInputAt(0, value);
2496 }
2497
Calin Juravle10e244f2015-01-26 18:54:32 +00002498 bool CanBeMoved() const OVERRIDE { return true; }
2499 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002500 UNUSED(other);
2501 return true;
2502 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002503
Calin Juravle10e244f2015-01-26 18:54:32 +00002504 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002505
Calin Juravle10e244f2015-01-26 18:54:32 +00002506 bool CanThrow() const OVERRIDE { return true; }
2507
2508 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01002509
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002510 uint32_t GetDexPc() const { return dex_pc_; }
2511
2512 DECLARE_INSTRUCTION(NullCheck);
2513
2514 private:
2515 const uint32_t dex_pc_;
2516
2517 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
2518};
2519
2520class FieldInfo : public ValueObject {
2521 public:
Calin Juravle52c48962014-12-16 17:02:57 +00002522 FieldInfo(MemberOffset field_offset, Primitive::Type field_type, bool is_volatile)
2523 : field_offset_(field_offset), field_type_(field_type), is_volatile_(is_volatile) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002524
2525 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002526 Primitive::Type GetFieldType() const { return field_type_; }
Calin Juravle52c48962014-12-16 17:02:57 +00002527 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002528
2529 private:
2530 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01002531 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00002532 const bool is_volatile_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002533};
2534
2535class HInstanceFieldGet : public HExpression<1> {
2536 public:
2537 HInstanceFieldGet(HInstruction* value,
2538 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00002539 MemberOffset field_offset,
2540 bool is_volatile)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002541 : HExpression(field_type, SideEffects::DependsOnSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00002542 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002543 SetRawInputAt(0, value);
2544 }
2545
Calin Juravle10c9cbe2014-12-19 10:50:19 +00002546 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002547
2548 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2549 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
2550 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002551 }
2552
Calin Juravle77520bc2015-01-12 18:45:46 +00002553 bool CanDoImplicitNullCheck() const OVERRIDE {
2554 return GetFieldOffset().Uint32Value() < kPageSize;
2555 }
2556
2557 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002558 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
2559 }
2560
Calin Juravle52c48962014-12-16 17:02:57 +00002561 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002562 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002563 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002564 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002565
2566 DECLARE_INSTRUCTION(InstanceFieldGet);
2567
2568 private:
2569 const FieldInfo field_info_;
2570
2571 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
2572};
2573
2574class HInstanceFieldSet : public HTemplateInstruction<2> {
2575 public:
2576 HInstanceFieldSet(HInstruction* object,
2577 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01002578 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00002579 MemberOffset field_offset,
2580 bool is_volatile)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002581 : HTemplateInstruction(SideEffects::ChangesSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00002582 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002583 SetRawInputAt(0, object);
2584 SetRawInputAt(1, value);
2585 }
2586
Calin Juravle77520bc2015-01-12 18:45:46 +00002587 bool CanDoImplicitNullCheck() const OVERRIDE {
2588 return GetFieldOffset().Uint32Value() < kPageSize;
2589 }
2590
Calin Juravle52c48962014-12-16 17:02:57 +00002591 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002592 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002593 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002594 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002595 HInstruction* GetValue() const { return InputAt(1); }
2596
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002597 DECLARE_INSTRUCTION(InstanceFieldSet);
2598
2599 private:
2600 const FieldInfo field_info_;
2601
2602 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
2603};
2604
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002605class HArrayGet : public HExpression<2> {
2606 public:
2607 HArrayGet(HInstruction* array, HInstruction* index, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002608 : HExpression(type, SideEffects::DependsOnSomething()) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002609 SetRawInputAt(0, array);
2610 SetRawInputAt(1, index);
2611 }
2612
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002613 bool CanBeMoved() const OVERRIDE { return true; }
2614 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002615 UNUSED(other);
2616 return true;
2617 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002618 bool CanDoImplicitNullCheck() const OVERRIDE {
2619 // TODO: We can be smarter here.
2620 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
2621 // which generates the implicit null check. There are cases when these can be removed
2622 // to produce better code. If we ever add optimizations to do so we should allow an
2623 // implicit check here (as long as the address falls in the first page).
2624 return false;
2625 }
2626
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002627 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002628
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002629 HInstruction* GetArray() const { return InputAt(0); }
2630 HInstruction* GetIndex() const { return InputAt(1); }
2631
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002632 DECLARE_INSTRUCTION(ArrayGet);
2633
2634 private:
2635 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
2636};
2637
2638class HArraySet : public HTemplateInstruction<3> {
2639 public:
2640 HArraySet(HInstruction* array,
2641 HInstruction* index,
2642 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002643 Primitive::Type expected_component_type,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002644 uint32_t dex_pc)
2645 : HTemplateInstruction(SideEffects::ChangesSomething()),
2646 dex_pc_(dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002647 expected_component_type_(expected_component_type),
2648 needs_type_check_(value->GetType() == Primitive::kPrimNot) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002649 SetRawInputAt(0, array);
2650 SetRawInputAt(1, index);
2651 SetRawInputAt(2, value);
2652 }
2653
Calin Juravle77520bc2015-01-12 18:45:46 +00002654 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002655 // We currently always call a runtime method to catch array store
2656 // exceptions.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002657 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002658 }
2659
Calin Juravle77520bc2015-01-12 18:45:46 +00002660 bool CanDoImplicitNullCheck() const OVERRIDE {
2661 // TODO: Same as for ArrayGet.
2662 return false;
2663 }
2664
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002665 void ClearNeedsTypeCheck() {
2666 needs_type_check_ = false;
2667 }
2668
2669 bool NeedsTypeCheck() const { return needs_type_check_; }
2670
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002671 uint32_t GetDexPc() const { return dex_pc_; }
2672
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002673 HInstruction* GetArray() const { return InputAt(0); }
2674 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002675 HInstruction* GetValue() const { return InputAt(2); }
2676
2677 Primitive::Type GetComponentType() const {
2678 // The Dex format does not type floating point index operations. Since the
2679 // `expected_component_type_` is set during building and can therefore not
2680 // be correct, we also check what is the value type. If it is a floating
2681 // point type, we must use that type.
2682 Primitive::Type value_type = GetValue()->GetType();
2683 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
2684 ? value_type
2685 : expected_component_type_;
2686 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002687
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002688 DECLARE_INSTRUCTION(ArraySet);
2689
2690 private:
2691 const uint32_t dex_pc_;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002692 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002693 bool needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002694
2695 DISALLOW_COPY_AND_ASSIGN(HArraySet);
2696};
2697
2698class HArrayLength : public HExpression<1> {
2699 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002700 explicit HArrayLength(HInstruction* array)
2701 : HExpression(Primitive::kPrimInt, SideEffects::None()) {
2702 // Note that arrays do not change length, so the instruction does not
2703 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002704 SetRawInputAt(0, array);
2705 }
2706
Calin Juravle77520bc2015-01-12 18:45:46 +00002707 bool CanBeMoved() const OVERRIDE { return true; }
2708 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002709 UNUSED(other);
2710 return true;
2711 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002712 bool CanDoImplicitNullCheck() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002713
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002714 DECLARE_INSTRUCTION(ArrayLength);
2715
2716 private:
2717 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
2718};
2719
2720class HBoundsCheck : public HExpression<2> {
2721 public:
2722 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002723 : HExpression(index->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002724 DCHECK(index->GetType() == Primitive::kPrimInt);
2725 SetRawInputAt(0, index);
2726 SetRawInputAt(1, length);
2727 }
2728
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002729 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002730 virtual bool InstructionDataEquals(HInstruction* other) const {
2731 UNUSED(other);
2732 return true;
2733 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002734
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002735 virtual bool NeedsEnvironment() const { return true; }
2736
Roland Levillaine161a2a2014-10-03 12:45:18 +01002737 virtual bool CanThrow() const { return true; }
2738
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002739 uint32_t GetDexPc() const { return dex_pc_; }
2740
2741 DECLARE_INSTRUCTION(BoundsCheck);
2742
2743 private:
2744 const uint32_t dex_pc_;
2745
2746 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
2747};
2748
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002749/**
2750 * Some DEX instructions are folded into multiple HInstructions that need
2751 * to stay live until the last HInstruction. This class
2752 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00002753 * HInstruction stays live. `index` represents the stack location index of the
2754 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002755 */
2756class HTemporary : public HTemplateInstruction<0> {
2757 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002758 explicit HTemporary(size_t index) : HTemplateInstruction(SideEffects::None()), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002759
2760 size_t GetIndex() const { return index_; }
2761
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00002762 Primitive::Type GetType() const OVERRIDE {
2763 // The previous instruction is the one that will be stored in the temporary location.
2764 DCHECK(GetPrevious() != nullptr);
2765 return GetPrevious()->GetType();
2766 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00002767
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002768 DECLARE_INSTRUCTION(Temporary);
2769
2770 private:
2771 const size_t index_;
2772
2773 DISALLOW_COPY_AND_ASSIGN(HTemporary);
2774};
2775
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002776class HSuspendCheck : public HTemplateInstruction<0> {
2777 public:
2778 explicit HSuspendCheck(uint32_t dex_pc)
Nicolas Geoffray9ebc72c2014-09-25 16:33:42 +01002779 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002780
2781 virtual bool NeedsEnvironment() const {
2782 return true;
2783 }
2784
2785 uint32_t GetDexPc() const { return dex_pc_; }
2786
2787 DECLARE_INSTRUCTION(SuspendCheck);
2788
2789 private:
2790 const uint32_t dex_pc_;
2791
2792 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
2793};
2794
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002795/**
2796 * Instruction to load a Class object.
2797 */
2798class HLoadClass : public HExpression<0> {
2799 public:
2800 HLoadClass(uint16_t type_index,
2801 bool is_referrers_class,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002802 uint32_t dex_pc)
2803 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2804 type_index_(type_index),
2805 is_referrers_class_(is_referrers_class),
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002806 dex_pc_(dex_pc),
Calin Juravleb1498f62015-02-16 13:13:29 +00002807 generate_clinit_check_(false),
2808 loaded_class_rti_(ReferenceTypeInfo::CreateTop(/* is_exact */ false)) {}
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002809
2810 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002811
2812 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2813 return other->AsLoadClass()->type_index_ == type_index_;
2814 }
2815
2816 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
2817
2818 uint32_t GetDexPc() const { return dex_pc_; }
2819 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002820 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002821
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002822 bool NeedsEnvironment() const OVERRIDE {
2823 // Will call runtime and load the class if the class is not loaded yet.
2824 // TODO: finer grain decision.
2825 return !is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002826 }
2827
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002828 bool MustGenerateClinitCheck() const {
2829 return generate_clinit_check_;
2830 }
2831
2832 void SetMustGenerateClinitCheck() {
2833 generate_clinit_check_ = true;
2834 }
2835
2836 bool CanCallRuntime() const {
2837 return MustGenerateClinitCheck() || !is_referrers_class_;
2838 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002839
Nicolas Geoffray82091da2015-01-26 10:02:45 +00002840 bool CanThrow() const OVERRIDE {
2841 // May call runtime and and therefore can throw.
2842 // TODO: finer grain decision.
2843 return !is_referrers_class_;
2844 }
2845
Calin Juravleacf735c2015-02-12 15:25:22 +00002846 ReferenceTypeInfo GetLoadedClassRTI() {
2847 return loaded_class_rti_;
2848 }
2849
2850 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
2851 // Make sure we only set exact types (the loaded class should never be merged).
2852 DCHECK(rti.IsExact());
2853 loaded_class_rti_ = rti;
2854 }
2855
2856 bool IsResolved() {
2857 return loaded_class_rti_.IsExact();
2858 }
2859
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002860 DECLARE_INSTRUCTION(LoadClass);
2861
2862 private:
2863 const uint16_t type_index_;
2864 const bool is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002865 const uint32_t dex_pc_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002866 // Whether this instruction must generate the initialization check.
2867 // Used for code generation.
2868 bool generate_clinit_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002869
Calin Juravleacf735c2015-02-12 15:25:22 +00002870 ReferenceTypeInfo loaded_class_rti_;
2871
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002872 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
2873};
2874
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002875class HLoadString : public HExpression<0> {
2876 public:
2877 HLoadString(uint32_t string_index, uint32_t dex_pc)
2878 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2879 string_index_(string_index),
2880 dex_pc_(dex_pc) {}
2881
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002882 bool CanBeMoved() const OVERRIDE { return true; }
2883
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002884 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2885 return other->AsLoadString()->string_index_ == string_index_;
2886 }
2887
2888 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
2889
2890 uint32_t GetDexPc() const { return dex_pc_; }
2891 uint32_t GetStringIndex() const { return string_index_; }
2892
2893 // TODO: Can we deopt or debug when we resolve a string?
2894 bool NeedsEnvironment() const OVERRIDE { return false; }
2895
2896 DECLARE_INSTRUCTION(LoadString);
2897
2898 private:
2899 const uint32_t string_index_;
2900 const uint32_t dex_pc_;
2901
2902 DISALLOW_COPY_AND_ASSIGN(HLoadString);
2903};
2904
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002905// TODO: Pass this check to HInvokeStaticOrDirect nodes.
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002906/**
2907 * Performs an initialization check on its Class object input.
2908 */
2909class HClinitCheck : public HExpression<1> {
2910 public:
2911 explicit HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
2912 : HExpression(Primitive::kPrimNot, SideEffects::All()),
2913 dex_pc_(dex_pc) {
2914 SetRawInputAt(0, constant);
2915 }
2916
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002917 bool CanBeMoved() const OVERRIDE { return true; }
2918 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2919 UNUSED(other);
2920 return true;
2921 }
2922
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002923 bool NeedsEnvironment() const OVERRIDE {
2924 // May call runtime to initialize the class.
2925 return true;
2926 }
2927
2928 uint32_t GetDexPc() const { return dex_pc_; }
2929
2930 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
2931
2932 DECLARE_INSTRUCTION(ClinitCheck);
2933
2934 private:
2935 const uint32_t dex_pc_;
2936
2937 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
2938};
2939
2940class HStaticFieldGet : public HExpression<1> {
2941 public:
2942 HStaticFieldGet(HInstruction* cls,
2943 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00002944 MemberOffset field_offset,
2945 bool is_volatile)
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002946 : HExpression(field_type, SideEffects::DependsOnSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00002947 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002948 SetRawInputAt(0, cls);
2949 }
2950
Calin Juravle52c48962014-12-16 17:02:57 +00002951
Calin Juravle10c9cbe2014-12-19 10:50:19 +00002952 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002953
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002954 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00002955 HStaticFieldGet* other_get = other->AsStaticFieldGet();
2956 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002957 }
2958
2959 size_t ComputeHashCode() const OVERRIDE {
2960 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
2961 }
2962
Calin Juravle52c48962014-12-16 17:02:57 +00002963 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002964 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
2965 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002966 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002967
2968 DECLARE_INSTRUCTION(StaticFieldGet);
2969
2970 private:
2971 const FieldInfo field_info_;
2972
2973 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
2974};
2975
2976class HStaticFieldSet : public HTemplateInstruction<2> {
2977 public:
2978 HStaticFieldSet(HInstruction* cls,
2979 HInstruction* value,
2980 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00002981 MemberOffset field_offset,
2982 bool is_volatile)
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002983 : HTemplateInstruction(SideEffects::ChangesSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00002984 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002985 SetRawInputAt(0, cls);
2986 SetRawInputAt(1, value);
2987 }
2988
Calin Juravle52c48962014-12-16 17:02:57 +00002989 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002990 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
2991 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002992 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002993
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002994 HInstruction* GetValue() const { return InputAt(1); }
2995
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002996 DECLARE_INSTRUCTION(StaticFieldSet);
2997
2998 private:
2999 const FieldInfo field_info_;
3000
3001 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
3002};
3003
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003004// Implement the move-exception DEX instruction.
3005class HLoadException : public HExpression<0> {
3006 public:
3007 HLoadException() : HExpression(Primitive::kPrimNot, SideEffects::None()) {}
3008
3009 DECLARE_INSTRUCTION(LoadException);
3010
3011 private:
3012 DISALLOW_COPY_AND_ASSIGN(HLoadException);
3013};
3014
3015class HThrow : public HTemplateInstruction<1> {
3016 public:
3017 HThrow(HInstruction* exception, uint32_t dex_pc)
3018 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {
3019 SetRawInputAt(0, exception);
3020 }
3021
3022 bool IsControlFlow() const OVERRIDE { return true; }
3023
3024 bool NeedsEnvironment() const OVERRIDE { return true; }
3025
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003026 bool CanThrow() const OVERRIDE { return true; }
3027
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003028 uint32_t GetDexPc() const { return dex_pc_; }
3029
3030 DECLARE_INSTRUCTION(Throw);
3031
3032 private:
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003033 const uint32_t dex_pc_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003034
3035 DISALLOW_COPY_AND_ASSIGN(HThrow);
3036};
3037
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003038class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003039 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003040 HInstanceOf(HInstruction* object,
3041 HLoadClass* constant,
3042 bool class_is_final,
3043 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003044 : HExpression(Primitive::kPrimBoolean, SideEffects::None()),
3045 class_is_final_(class_is_final),
3046 dex_pc_(dex_pc) {
3047 SetRawInputAt(0, object);
3048 SetRawInputAt(1, constant);
3049 }
3050
3051 bool CanBeMoved() const OVERRIDE { return true; }
3052
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003053 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003054 return true;
3055 }
3056
3057 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003058 return false;
3059 }
3060
3061 uint32_t GetDexPc() const { return dex_pc_; }
3062
3063 bool IsClassFinal() const { return class_is_final_; }
3064
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003065 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003066
3067 private:
3068 const bool class_is_final_;
3069 const uint32_t dex_pc_;
3070
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003071 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
3072};
3073
Calin Juravleb1498f62015-02-16 13:13:29 +00003074class HBoundType : public HExpression<1> {
3075 public:
3076 HBoundType(HInstruction* input, ReferenceTypeInfo bound_type)
3077 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3078 bound_type_(bound_type) {
Calin Juravle61d544b2015-02-23 16:46:57 +00003079 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00003080 SetRawInputAt(0, input);
3081 }
3082
3083 const ReferenceTypeInfo& GetBoundType() const { return bound_type_; }
3084
3085 bool CanBeNull() const OVERRIDE {
3086 // `null instanceof ClassX` always return false so we can't be null.
3087 return false;
3088 }
3089
3090 DECLARE_INSTRUCTION(BoundType);
3091
3092 private:
3093 // Encodes the most upper class that this instruction can have. In other words
3094 // it is always the case that GetBoundType().IsSupertypeOf(GetReferenceType()).
3095 // It is used to bound the type in cases like `if (x instanceof ClassX) {}`
3096 const ReferenceTypeInfo bound_type_;
3097
3098 DISALLOW_COPY_AND_ASSIGN(HBoundType);
3099};
3100
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003101class HCheckCast : public HTemplateInstruction<2> {
3102 public:
3103 HCheckCast(HInstruction* object,
3104 HLoadClass* constant,
3105 bool class_is_final,
3106 uint32_t dex_pc)
3107 : HTemplateInstruction(SideEffects::None()),
3108 class_is_final_(class_is_final),
3109 dex_pc_(dex_pc) {
3110 SetRawInputAt(0, object);
3111 SetRawInputAt(1, constant);
3112 }
3113
3114 bool CanBeMoved() const OVERRIDE { return true; }
3115
3116 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
3117 return true;
3118 }
3119
3120 bool NeedsEnvironment() const OVERRIDE {
3121 // Instruction may throw a CheckCastError.
3122 return true;
3123 }
3124
3125 bool CanThrow() const OVERRIDE { return true; }
3126
3127 uint32_t GetDexPc() const { return dex_pc_; }
3128
3129 bool IsClassFinal() const { return class_is_final_; }
3130
3131 DECLARE_INSTRUCTION(CheckCast);
3132
3133 private:
3134 const bool class_is_final_;
3135 const uint32_t dex_pc_;
3136
3137 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003138};
3139
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003140class HMonitorOperation : public HTemplateInstruction<1> {
3141 public:
3142 enum OperationKind {
3143 kEnter,
3144 kExit,
3145 };
3146
3147 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
3148 : HTemplateInstruction(SideEffects::None()), kind_(kind), dex_pc_(dex_pc) {
3149 SetRawInputAt(0, object);
3150 }
3151
3152 // Instruction may throw a Java exception, so we need an environment.
3153 bool NeedsEnvironment() const OVERRIDE { return true; }
3154 bool CanThrow() const OVERRIDE { return true; }
3155
3156 uint32_t GetDexPc() const { return dex_pc_; }
3157
3158 bool IsEnter() const { return kind_ == kEnter; }
3159
3160 DECLARE_INSTRUCTION(MonitorOperation);
3161
Calin Juravle52c48962014-12-16 17:02:57 +00003162 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003163 const OperationKind kind_;
3164 const uint32_t dex_pc_;
3165
3166 private:
3167 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
3168};
3169
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003170class MoveOperands : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003171 public:
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003172 MoveOperands(Location source, Location destination, HInstruction* instruction)
3173 : source_(source), destination_(destination), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003174
3175 Location GetSource() const { return source_; }
3176 Location GetDestination() const { return destination_; }
3177
3178 void SetSource(Location value) { source_ = value; }
3179 void SetDestination(Location value) { destination_ = value; }
3180
3181 // The parallel move resolver marks moves as "in-progress" by clearing the
3182 // destination (but not the source).
3183 Location MarkPending() {
3184 DCHECK(!IsPending());
3185 Location dest = destination_;
3186 destination_ = Location::NoLocation();
3187 return dest;
3188 }
3189
3190 void ClearPending(Location dest) {
3191 DCHECK(IsPending());
3192 destination_ = dest;
3193 }
3194
3195 bool IsPending() const {
3196 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
3197 return destination_.IsInvalid() && !source_.IsInvalid();
3198 }
3199
3200 // True if this blocks a move from the given location.
3201 bool Blocks(Location loc) const {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003202 return !IsEliminated() && (source_.Contains(loc) || loc.Contains(source_));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003203 }
3204
3205 // A move is redundant if it's been eliminated, if its source and
3206 // destination are the same, or if its destination is unneeded.
3207 bool IsRedundant() const {
3208 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
3209 }
3210
3211 // We clear both operands to indicate move that's been eliminated.
3212 void Eliminate() {
3213 source_ = destination_ = Location::NoLocation();
3214 }
3215
3216 bool IsEliminated() const {
3217 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
3218 return source_.IsInvalid();
3219 }
3220
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003221 HInstruction* GetInstruction() const { return instruction_; }
3222
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003223 private:
3224 Location source_;
3225 Location destination_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003226 // The instruction this move is assocatied with. Null when this move is
3227 // for moving an input in the expected locations of user (including a phi user).
3228 // This is only used in debug mode, to ensure we do not connect interval siblings
3229 // in the same parallel move.
3230 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003231};
3232
3233static constexpr size_t kDefaultNumberOfMoves = 4;
3234
3235class HParallelMove : public HTemplateInstruction<0> {
3236 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003237 explicit HParallelMove(ArenaAllocator* arena)
3238 : HTemplateInstruction(SideEffects::None()), moves_(arena, kDefaultNumberOfMoves) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003239
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003240 void AddMove(Location source, Location destination, HInstruction* instruction) {
3241 DCHECK(source.IsValid());
3242 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003243 if (kIsDebugBuild) {
3244 if (instruction != nullptr) {
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00003245 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003246 DCHECK_NE(moves_.Get(i).GetInstruction(), instruction)
3247 << "Doing parallel moves for the same instruction.";
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00003248 }
3249 }
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003250 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
3251 DCHECK(!destination.Equals(moves_.Get(i).GetDestination()))
3252 << "Same destination for two moves in a parallel move.";
3253 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003254 }
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003255 moves_.Add(MoveOperands(source, destination, instruction));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003256 }
3257
3258 MoveOperands* MoveOperandsAt(size_t index) const {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003259 return moves_.GetRawStorage() + index;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003260 }
3261
3262 size_t NumMoves() const { return moves_.Size(); }
3263
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003264 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003265
3266 private:
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003267 GrowableArray<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003268
3269 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
3270};
3271
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003272class HGraphVisitor : public ValueObject {
3273 public:
Dave Allison20dfc792014-06-16 20:44:29 -07003274 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
3275 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003276
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003277 virtual void VisitInstruction(HInstruction* instruction) { UNUSED(instruction); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003278 virtual void VisitBasicBlock(HBasicBlock* block);
3279
Roland Levillain633021e2014-10-01 14:12:25 +01003280 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003281 void VisitInsertionOrder();
3282
Roland Levillain633021e2014-10-01 14:12:25 +01003283 // Visit the graph following dominator tree reverse post-order.
3284 void VisitReversePostOrder();
3285
Nicolas Geoffray787c3072014-03-17 10:20:19 +00003286 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00003287
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003288 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003289#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003290 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
3291
3292 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
3293
3294#undef DECLARE_VISIT_INSTRUCTION
3295
3296 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07003297 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003298
3299 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
3300};
3301
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003302class HGraphDelegateVisitor : public HGraphVisitor {
3303 public:
3304 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
3305 virtual ~HGraphDelegateVisitor() {}
3306
3307 // Visit functions that delegate to to super class.
3308#define DECLARE_VISIT_INSTRUCTION(name, super) \
3309 virtual void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
3310
3311 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
3312
3313#undef DECLARE_VISIT_INSTRUCTION
3314
3315 private:
3316 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
3317};
3318
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003319class HInsertionOrderIterator : public ValueObject {
3320 public:
3321 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
3322
3323 bool Done() const { return index_ == graph_.GetBlocks().Size(); }
3324 HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); }
3325 void Advance() { ++index_; }
3326
3327 private:
3328 const HGraph& graph_;
3329 size_t index_;
3330
3331 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
3332};
3333
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003334class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003335 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003336 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003337
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003338 bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); }
3339 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003340 void Advance() { ++index_; }
3341
3342 private:
3343 const HGraph& graph_;
3344 size_t index_;
3345
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003346 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003347};
3348
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003349class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003350 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003351 explicit HPostOrderIterator(const HGraph& graph)
3352 : graph_(graph), index_(graph_.GetReversePostOrder().Size()) {}
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003353
3354 bool Done() const { return index_ == 0; }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003355 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003356 void Advance() { --index_; }
3357
3358 private:
3359 const HGraph& graph_;
3360 size_t index_;
3361
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003362 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003363};
3364
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003365// Iterator over the blocks that art part of the loop. Includes blocks part
3366// of an inner loop. The order in which the blocks are iterated is on their
3367// block id.
3368class HBlocksInLoopIterator : public ValueObject {
3369 public:
3370 explicit HBlocksInLoopIterator(const HLoopInformation& info)
3371 : blocks_in_loop_(info.GetBlocks()),
3372 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
3373 index_(0) {
3374 if (!blocks_in_loop_.IsBitSet(index_)) {
3375 Advance();
3376 }
3377 }
3378
3379 bool Done() const { return index_ == blocks_.Size(); }
3380 HBasicBlock* Current() const { return blocks_.Get(index_); }
3381 void Advance() {
3382 ++index_;
3383 for (size_t e = blocks_.Size(); index_ < e; ++index_) {
3384 if (blocks_in_loop_.IsBitSet(index_)) {
3385 break;
3386 }
3387 }
3388 }
3389
3390 private:
3391 const BitVector& blocks_in_loop_;
3392 const GrowableArray<HBasicBlock*>& blocks_;
3393 size_t index_;
3394
3395 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
3396};
3397
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003398} // namespace art
3399
3400#endif // ART_COMPILER_OPTIMIZING_NODES_H_