blob: de448cc4839660d256549505bdb8bd9381f2a5fd [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 Juravle10e244f2015-01-26 18:54:32 +00001042 // Does not apply for all instructions, but having this at top level greatly
1043 // simplifies the null check elimination.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001044 virtual bool CanBeNull() const {
1045 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1046 return true;
1047 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001048
Calin Juravle77520bc2015-01-12 18:45:46 +00001049 virtual bool CanDoImplicitNullCheck() const { return false; }
1050
Calin Juravleacf735c2015-02-12 15:25:22 +00001051 void SetReferenceTypeInfo(ReferenceTypeInfo reference_type_info) {
1052 reference_type_info_ = reference_type_info;
1053 }
1054
1055 ReferenceTypeInfo GetReferenceTypeInfo() const { return reference_type_info_; }
1056
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001057 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001058 DCHECK(user != nullptr);
1059 HUseListNode<HInstruction*>* use =
1060 uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1061 user->SetRawInputRecordAt(index, HUserRecord<HInstruction*>(user->InputRecordAt(index), use));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001062 }
1063
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001064 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001065 DCHECK(user != nullptr);
David Brazdiled596192015-01-23 10:39:45 +00001066 HUseListNode<HEnvironment*>* env_use =
1067 env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1068 user->RecordEnvUse(env_use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001069 }
1070
David Brazdil1abb4192015-02-17 18:33:36 +00001071 void RemoveAsUserOfInput(size_t input) {
1072 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
1073 input_use.GetInstruction()->uses_.Remove(input_use.GetUseNode());
1074 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001075
David Brazdil1abb4192015-02-17 18:33:36 +00001076 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1077 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001078
David Brazdiled596192015-01-23 10:39:45 +00001079 bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); }
1080 bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001081
Roland Levillain6c82d402014-10-13 16:10:27 +01001082 // Does this instruction strictly dominate `other_instruction`?
1083 // Returns false if this instruction and `other_instruction` are the same.
1084 // Aborts if this instruction and `other_instruction` are both phis.
1085 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001086
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001087 int GetId() const { return id_; }
1088 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001089
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001090 int GetSsaIndex() const { return ssa_index_; }
1091 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1092 bool HasSsaIndex() const { return ssa_index_ != -1; }
1093
1094 bool HasEnvironment() const { return environment_ != nullptr; }
1095 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001096 void SetEnvironment(HEnvironment* environment) { environment_ = environment; }
1097
Nicolas Geoffray39468442014-09-02 15:17:15 +01001098 // Returns the number of entries in the environment. Typically, that is the
1099 // number of dex registers in a method. It could be more in case of inlining.
1100 size_t EnvironmentSize() const;
1101
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001102 LocationSummary* GetLocations() const { return locations_; }
1103 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001104
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001105 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001106 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001107
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001108 // Move `this` instruction before `cursor`.
1109 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001110
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001111#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01001112 bool Is##type() const { return (As##type() != nullptr); } \
1113 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001114 virtual H##type* As##type() { return nullptr; }
1115
1116 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
1117#undef INSTRUCTION_TYPE_CHECK
1118
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001119 // Returns whether the instruction can be moved within the graph.
1120 virtual bool CanBeMoved() const { return false; }
1121
1122 // Returns whether the two instructions are of the same kind.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001123 virtual bool InstructionTypeEquals(HInstruction* other) const {
1124 UNUSED(other);
1125 return false;
1126 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001127
1128 // Returns whether any data encoded in the two instructions is equal.
1129 // This method does not look at the inputs. Both instructions must be
1130 // of the same type, otherwise the method has undefined behavior.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001131 virtual bool InstructionDataEquals(HInstruction* other) const {
1132 UNUSED(other);
1133 return false;
1134 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001135
1136 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00001137 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001138 // 2) Their inputs are identical.
1139 bool Equals(HInstruction* other) const;
1140
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001141 virtual InstructionKind GetKind() const = 0;
1142
1143 virtual size_t ComputeHashCode() const {
1144 size_t result = GetKind();
1145 for (size_t i = 0, e = InputCount(); i < e; ++i) {
1146 result = (result * 31) + InputAt(i)->GetId();
1147 }
1148 return result;
1149 }
1150
1151 SideEffects GetSideEffects() const { return side_effects_; }
1152
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001153 size_t GetLifetimePosition() const { return lifetime_position_; }
1154 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
1155 LiveInterval* GetLiveInterval() const { return live_interval_; }
1156 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
1157 bool HasLiveInterval() const { return live_interval_ != nullptr; }
1158
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001159 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
1160
1161 // Returns whether the code generation of the instruction will require to have access
1162 // to the current method. Such instructions are:
1163 // (1): Instructions that require an environment, as calling the runtime requires
1164 // to walk the stack and have the current method stored at a specific stack address.
1165 // (2): Object literals like classes and strings, that are loaded from the dex cache
1166 // fields of the current method.
1167 bool NeedsCurrentMethod() const {
1168 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
1169 }
1170
David Brazdil1abb4192015-02-17 18:33:36 +00001171 protected:
1172 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
1173 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
1174
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001175 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001176 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); }
1177
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001178 HInstruction* previous_;
1179 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001180 HBasicBlock* block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001181
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001182 // An instruction gets an id when it is added to the graph.
1183 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01001184 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001185 int id_;
1186
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001187 // When doing liveness analysis, instructions that have uses get an SSA index.
1188 int ssa_index_;
1189
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001190 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00001191 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001192
1193 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00001194 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001195
Nicolas Geoffray39468442014-09-02 15:17:15 +01001196 // The environment associated with this instruction. Not null if the instruction
1197 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001198 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001199
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001200 // Set by the code generator.
1201 LocationSummary* locations_;
1202
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001203 // Set by the liveness analysis.
1204 LiveInterval* live_interval_;
1205
1206 // Set by the liveness analysis, this is the position in a linear
1207 // order of blocks where this instruction's live interval start.
1208 size_t lifetime_position_;
1209
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001210 const SideEffects side_effects_;
1211
Calin Juravleacf735c2015-02-12 15:25:22 +00001212 // TODO: for primitive types this should be marked as invalid.
1213 ReferenceTypeInfo reference_type_info_;
1214
David Brazdil1abb4192015-02-17 18:33:36 +00001215 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001216 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00001217 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001218 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001219 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001220
1221 DISALLOW_COPY_AND_ASSIGN(HInstruction);
1222};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001223std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001224
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001225class HInputIterator : public ValueObject {
1226 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001227 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001228
1229 bool Done() const { return index_ == instruction_->InputCount(); }
1230 HInstruction* Current() const { return instruction_->InputAt(index_); }
1231 void Advance() { index_++; }
1232
1233 private:
1234 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001235 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001236
1237 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
1238};
1239
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001240class HInstructionIterator : public ValueObject {
1241 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001242 explicit HInstructionIterator(const HInstructionList& instructions)
1243 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001244 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001245 }
1246
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001247 bool Done() const { return instruction_ == nullptr; }
1248 HInstruction* Current() const { return instruction_; }
1249 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001250 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001251 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001252 }
1253
1254 private:
1255 HInstruction* instruction_;
1256 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001257
1258 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001259};
1260
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001261class HBackwardInstructionIterator : public ValueObject {
1262 public:
1263 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
1264 : instruction_(instructions.last_instruction_) {
1265 next_ = Done() ? nullptr : instruction_->GetPrevious();
1266 }
1267
1268 bool Done() const { return instruction_ == nullptr; }
1269 HInstruction* Current() const { return instruction_; }
1270 void Advance() {
1271 instruction_ = next_;
1272 next_ = Done() ? nullptr : instruction_->GetPrevious();
1273 }
1274
1275 private:
1276 HInstruction* instruction_;
1277 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001278
1279 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001280};
1281
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001282// An embedded container with N elements of type T. Used (with partial
1283// specialization for N=0) because embedded arrays cannot have size 0.
1284template<typename T, intptr_t N>
1285class EmbeddedArray {
1286 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001287 EmbeddedArray() : elements_() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001288
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001289 intptr_t GetLength() const { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001290
1291 const T& operator[](intptr_t i) const {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001292 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001293 return elements_[i];
1294 }
1295
1296 T& operator[](intptr_t i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001297 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001298 return elements_[i];
1299 }
1300
1301 const T& At(intptr_t i) const {
1302 return (*this)[i];
1303 }
1304
1305 void SetAt(intptr_t i, const T& val) {
1306 (*this)[i] = val;
1307 }
1308
1309 private:
1310 T elements_[N];
1311};
1312
1313template<typename T>
1314class EmbeddedArray<T, 0> {
1315 public:
1316 intptr_t length() const { return 0; }
1317 const T& operator[](intptr_t i) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001318 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001319 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001320 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001321 }
1322 T& operator[](intptr_t i) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001323 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001324 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001325 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001326 }
1327};
1328
1329template<intptr_t N>
1330class HTemplateInstruction: public HInstruction {
1331 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001332 HTemplateInstruction<N>(SideEffects side_effects)
1333 : HInstruction(side_effects), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07001334 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001335
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001336 virtual size_t InputCount() const { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001337
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001338 protected:
David Brazdil1abb4192015-02-17 18:33:36 +00001339 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_[i]; }
1340
1341 void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE {
1342 inputs_[i] = input;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001343 }
1344
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001345 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001346 EmbeddedArray<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001347
1348 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001349};
1350
Dave Allison20dfc792014-06-16 20:44:29 -07001351template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001352class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07001353 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001354 HExpression<N>(Primitive::Type type, SideEffects side_effects)
1355 : HTemplateInstruction<N>(side_effects), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07001356 virtual ~HExpression() {}
1357
1358 virtual Primitive::Type GetType() const { return type_; }
1359
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001360 protected:
1361 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07001362};
1363
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001364// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
1365// instruction that branches to the exit block.
1366class HReturnVoid : public HTemplateInstruction<0> {
1367 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001368 HReturnVoid() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001369
1370 virtual bool IsControlFlow() const { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001371
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001372 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001373
1374 private:
1375 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
1376};
1377
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001378// Represents dex's RETURN opcodes. A HReturn is a control flow
1379// instruction that branches to the exit block.
1380class HReturn : public HTemplateInstruction<1> {
1381 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001382 explicit HReturn(HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001383 SetRawInputAt(0, value);
1384 }
1385
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001386 virtual bool IsControlFlow() const { return true; }
1387
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001388 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001389
1390 private:
1391 DISALLOW_COPY_AND_ASSIGN(HReturn);
1392};
1393
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001394// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001395// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001396// exit block.
1397class HExit : public HTemplateInstruction<0> {
1398 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001399 HExit() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001400
1401 virtual bool IsControlFlow() const { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001402
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001403 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001404
1405 private:
1406 DISALLOW_COPY_AND_ASSIGN(HExit);
1407};
1408
1409// Jumps from one block to another.
1410class HGoto : public HTemplateInstruction<0> {
1411 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001412 HGoto() : HTemplateInstruction(SideEffects::None()) {}
1413
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001414 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001415
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001416 HBasicBlock* GetSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001417 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001418 }
1419
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001420 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001421
1422 private:
1423 DISALLOW_COPY_AND_ASSIGN(HGoto);
1424};
1425
Dave Allison20dfc792014-06-16 20:44:29 -07001426
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001427// Conditional branch. A block ending with an HIf instruction must have
1428// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001429class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001430 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001431 explicit HIf(HInstruction* input) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001432 SetRawInputAt(0, input);
1433 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001434
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001435 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001436
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001437 HBasicBlock* IfTrueSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001438 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001439 }
1440
1441 HBasicBlock* IfFalseSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001442 return GetBlock()->GetSuccessors().Get(1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001443 }
1444
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001445 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001446
Dave Allison20dfc792014-06-16 20:44:29 -07001447 virtual bool IsIfInstruction() const { return true; }
1448
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001449 private:
1450 DISALLOW_COPY_AND_ASSIGN(HIf);
1451};
1452
Roland Levillain88cb1752014-10-20 16:36:47 +01001453class HUnaryOperation : public HExpression<1> {
1454 public:
1455 HUnaryOperation(Primitive::Type result_type, HInstruction* input)
1456 : HExpression(result_type, SideEffects::None()) {
1457 SetRawInputAt(0, input);
1458 }
1459
1460 HInstruction* GetInput() const { return InputAt(0); }
1461 Primitive::Type GetResultType() const { return GetType(); }
1462
1463 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001464 virtual bool InstructionDataEquals(HInstruction* other) const {
1465 UNUSED(other);
1466 return true;
1467 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001468
Roland Levillain9240d6a2014-10-20 16:47:04 +01001469 // Try to statically evaluate `operation` and return a HConstant
1470 // containing the result of this evaluation. If `operation` cannot
1471 // be evaluated as a constant, return nullptr.
1472 HConstant* TryStaticEvaluation() const;
1473
1474 // Apply this operation to `x`.
1475 virtual int32_t Evaluate(int32_t x) const = 0;
1476 virtual int64_t Evaluate(int64_t x) const = 0;
1477
Roland Levillain88cb1752014-10-20 16:36:47 +01001478 DECLARE_INSTRUCTION(UnaryOperation);
1479
1480 private:
1481 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
1482};
1483
Dave Allison20dfc792014-06-16 20:44:29 -07001484class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001485 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001486 HBinaryOperation(Primitive::Type result_type,
1487 HInstruction* left,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001488 HInstruction* right) : HExpression(result_type, SideEffects::None()) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001489 SetRawInputAt(0, left);
1490 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001491 }
1492
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001493 HInstruction* GetLeft() const { return InputAt(0); }
1494 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07001495 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001496
1497 virtual bool IsCommutative() { return false; }
1498
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001499 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001500 virtual bool InstructionDataEquals(HInstruction* other) const {
1501 UNUSED(other);
1502 return true;
1503 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001504
Roland Levillain9240d6a2014-10-20 16:47:04 +01001505 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01001506 // containing the result of this evaluation. If `operation` cannot
1507 // be evaluated as a constant, return nullptr.
Roland Levillain9240d6a2014-10-20 16:47:04 +01001508 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01001509
1510 // Apply this operation to `x` and `y`.
1511 virtual int32_t Evaluate(int32_t x, int32_t y) const = 0;
1512 virtual int64_t Evaluate(int64_t x, int64_t y) const = 0;
1513
Roland Levillainccc07a92014-09-16 14:48:16 +01001514 DECLARE_INSTRUCTION(BinaryOperation);
1515
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001516 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001517 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
1518};
1519
Dave Allison20dfc792014-06-16 20:44:29 -07001520class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001521 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001522 HCondition(HInstruction* first, HInstruction* second)
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001523 : HBinaryOperation(Primitive::kPrimBoolean, first, second),
1524 needs_materialization_(true) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001525
1526 virtual bool IsCommutative() { return true; }
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001527
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001528 bool NeedsMaterialization() const { return needs_materialization_; }
1529 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001530
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001531 // For code generation purposes, returns whether this instruction is just before
1532 // `if_`, and disregard moves in between.
1533 bool IsBeforeWhenDisregardMoves(HIf* if_) const;
1534
Dave Allison20dfc792014-06-16 20:44:29 -07001535 DECLARE_INSTRUCTION(Condition);
1536
1537 virtual IfCondition GetCondition() const = 0;
1538
1539 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001540 // For register allocation purposes, returns whether this instruction needs to be
1541 // materialized (that is, not just be in the processor flags).
1542 bool needs_materialization_;
1543
Dave Allison20dfc792014-06-16 20:44:29 -07001544 DISALLOW_COPY_AND_ASSIGN(HCondition);
1545};
1546
1547// Instruction to check if two inputs are equal to each other.
1548class HEqual : public HCondition {
1549 public:
1550 HEqual(HInstruction* first, HInstruction* second)
1551 : HCondition(first, second) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001552
Roland Levillain93445682014-10-06 19:24:02 +01001553 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1554 return x == y ? 1 : 0;
1555 }
1556 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1557 return x == y ? 1 : 0;
1558 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001559
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001560 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001561
Dave Allison20dfc792014-06-16 20:44:29 -07001562 virtual IfCondition GetCondition() const {
1563 return kCondEQ;
1564 }
1565
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001566 private:
1567 DISALLOW_COPY_AND_ASSIGN(HEqual);
1568};
1569
Dave Allison20dfc792014-06-16 20:44:29 -07001570class HNotEqual : public HCondition {
1571 public:
1572 HNotEqual(HInstruction* first, HInstruction* second)
1573 : HCondition(first, second) {}
1574
Roland Levillain93445682014-10-06 19:24:02 +01001575 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1576 return x != y ? 1 : 0;
1577 }
1578 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1579 return x != y ? 1 : 0;
1580 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001581
Dave Allison20dfc792014-06-16 20:44:29 -07001582 DECLARE_INSTRUCTION(NotEqual);
1583
1584 virtual IfCondition GetCondition() const {
1585 return kCondNE;
1586 }
1587
1588 private:
1589 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
1590};
1591
1592class HLessThan : public HCondition {
1593 public:
1594 HLessThan(HInstruction* first, HInstruction* second)
1595 : HCondition(first, second) {}
1596
Roland Levillain93445682014-10-06 19:24:02 +01001597 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1598 return x < y ? 1 : 0;
1599 }
1600 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1601 return x < y ? 1 : 0;
1602 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001603
Dave Allison20dfc792014-06-16 20:44:29 -07001604 DECLARE_INSTRUCTION(LessThan);
1605
1606 virtual IfCondition GetCondition() const {
1607 return kCondLT;
1608 }
1609
1610 private:
1611 DISALLOW_COPY_AND_ASSIGN(HLessThan);
1612};
1613
1614class HLessThanOrEqual : public HCondition {
1615 public:
1616 HLessThanOrEqual(HInstruction* first, HInstruction* second)
1617 : HCondition(first, second) {}
1618
Roland Levillain93445682014-10-06 19:24:02 +01001619 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1620 return x <= y ? 1 : 0;
1621 }
1622 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1623 return x <= y ? 1 : 0;
1624 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001625
Dave Allison20dfc792014-06-16 20:44:29 -07001626 DECLARE_INSTRUCTION(LessThanOrEqual);
1627
1628 virtual IfCondition GetCondition() const {
1629 return kCondLE;
1630 }
1631
1632 private:
1633 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
1634};
1635
1636class HGreaterThan : public HCondition {
1637 public:
1638 HGreaterThan(HInstruction* first, HInstruction* second)
1639 : HCondition(first, second) {}
1640
Roland Levillain93445682014-10-06 19:24:02 +01001641 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1642 return x > y ? 1 : 0;
1643 }
1644 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1645 return x > y ? 1 : 0;
1646 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001647
Dave Allison20dfc792014-06-16 20:44:29 -07001648 DECLARE_INSTRUCTION(GreaterThan);
1649
1650 virtual IfCondition GetCondition() const {
1651 return kCondGT;
1652 }
1653
1654 private:
1655 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
1656};
1657
1658class HGreaterThanOrEqual : public HCondition {
1659 public:
1660 HGreaterThanOrEqual(HInstruction* first, HInstruction* second)
1661 : HCondition(first, second) {}
1662
Roland Levillain93445682014-10-06 19:24:02 +01001663 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1664 return x >= y ? 1 : 0;
1665 }
1666 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1667 return x >= y ? 1 : 0;
1668 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001669
Dave Allison20dfc792014-06-16 20:44:29 -07001670 DECLARE_INSTRUCTION(GreaterThanOrEqual);
1671
1672 virtual IfCondition GetCondition() const {
1673 return kCondGE;
1674 }
1675
1676 private:
1677 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
1678};
1679
1680
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001681// Instruction to check how two inputs compare to each other.
1682// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
1683class HCompare : public HBinaryOperation {
1684 public:
Calin Juravleddb7df22014-11-25 20:56:51 +00001685 // The bias applies for floating point operations and indicates how NaN
1686 // comparisons are treated:
1687 enum Bias {
1688 kNoBias, // bias is not applicable (i.e. for long operation)
1689 kGtBias, // return 1 for NaN comparisons
1690 kLtBias, // return -1 for NaN comparisons
1691 };
1692
1693 HCompare(Primitive::Type type, HInstruction* first, HInstruction* second, Bias bias)
1694 : HBinaryOperation(Primitive::kPrimInt, first, second), bias_(bias) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001695 DCHECK_EQ(type, first->GetType());
1696 DCHECK_EQ(type, second->GetType());
1697 }
1698
Calin Juravleddb7df22014-11-25 20:56:51 +00001699 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01001700 return
1701 x == y ? 0 :
1702 x > y ? 1 :
1703 -1;
1704 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001705
1706 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01001707 return
1708 x == y ? 0 :
1709 x > y ? 1 :
1710 -1;
1711 }
1712
Calin Juravleddb7df22014-11-25 20:56:51 +00001713 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
1714 return bias_ == other->AsCompare()->bias_;
1715 }
1716
1717 bool IsGtBias() { return bias_ == kGtBias; }
1718
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001719 DECLARE_INSTRUCTION(Compare);
1720
1721 private:
Calin Juravleddb7df22014-11-25 20:56:51 +00001722 const Bias bias_;
1723
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001724 DISALLOW_COPY_AND_ASSIGN(HCompare);
1725};
1726
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001727// A local in the graph. Corresponds to a Dex register.
1728class HLocal : public HTemplateInstruction<0> {
1729 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001730 explicit HLocal(uint16_t reg_number)
1731 : HTemplateInstruction(SideEffects::None()), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001732
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001733 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001734
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001735 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001736
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001737 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001738 // The Dex register number.
1739 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001740
1741 DISALLOW_COPY_AND_ASSIGN(HLocal);
1742};
1743
1744// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07001745class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001746 public:
Roland Levillain5799fc02014-09-25 12:15:20 +01001747 HLoadLocal(HLocal* local, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001748 : HExpression(type, SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001749 SetRawInputAt(0, local);
1750 }
1751
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001752 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1753
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001754 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001755
1756 private:
1757 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
1758};
1759
1760// Store a value in a given local. This instruction has two inputs: the value
1761// and the local.
1762class HStoreLocal : public HTemplateInstruction<2> {
1763 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001764 HStoreLocal(HLocal* local, HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001765 SetRawInputAt(0, local);
1766 SetRawInputAt(1, value);
1767 }
1768
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001769 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1770
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001771 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001772
1773 private:
1774 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
1775};
1776
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001777class HConstant : public HExpression<0> {
1778 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001779 explicit HConstant(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
1780
1781 virtual bool CanBeMoved() const { return true; }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001782
1783 DECLARE_INSTRUCTION(Constant);
1784
1785 private:
1786 DISALLOW_COPY_AND_ASSIGN(HConstant);
1787};
1788
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001789class HFloatConstant : public HConstant {
1790 public:
1791 explicit HFloatConstant(float value) : HConstant(Primitive::kPrimFloat), value_(value) {}
1792
1793 float GetValue() const { return value_; }
1794
1795 virtual bool InstructionDataEquals(HInstruction* other) const {
1796 return bit_cast<float, int32_t>(other->AsFloatConstant()->value_) ==
1797 bit_cast<float, int32_t>(value_);
1798 }
1799
1800 virtual size_t ComputeHashCode() const { return static_cast<size_t>(GetValue()); }
1801
1802 DECLARE_INSTRUCTION(FloatConstant);
1803
1804 private:
1805 const float value_;
1806
1807 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
1808};
1809
1810class HDoubleConstant : public HConstant {
1811 public:
1812 explicit HDoubleConstant(double value) : HConstant(Primitive::kPrimDouble), value_(value) {}
1813
1814 double GetValue() const { return value_; }
1815
1816 virtual bool InstructionDataEquals(HInstruction* other) const {
1817 return bit_cast<double, int64_t>(other->AsDoubleConstant()->value_) ==
1818 bit_cast<double, int64_t>(value_);
1819 }
1820
1821 virtual size_t ComputeHashCode() const { return static_cast<size_t>(GetValue()); }
1822
1823 DECLARE_INSTRUCTION(DoubleConstant);
1824
1825 private:
1826 const double value_;
1827
1828 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
1829};
1830
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001831class HNullConstant : public HConstant {
1832 public:
1833 HNullConstant() : HConstant(Primitive::kPrimNot) {}
1834
1835 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
1836 return true;
1837 }
1838
1839 size_t ComputeHashCode() const OVERRIDE { return 0; }
1840
1841 DECLARE_INSTRUCTION(NullConstant);
1842
1843 private:
1844 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
1845};
1846
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001847// Constants of the type int. Those can be from Dex instructions, or
1848// synthesized (for example with the if-eqz instruction).
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001849class HIntConstant : public HConstant {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001850 public:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001851 explicit HIntConstant(int32_t value) : HConstant(Primitive::kPrimInt), value_(value) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001852
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001853 int32_t GetValue() const { return value_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001854
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001855 virtual bool InstructionDataEquals(HInstruction* other) const {
1856 return other->AsIntConstant()->value_ == value_;
1857 }
1858
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001859 virtual size_t ComputeHashCode() const { return GetValue(); }
1860
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001861 DECLARE_INSTRUCTION(IntConstant);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001862
1863 private:
1864 const int32_t value_;
1865
1866 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
1867};
1868
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001869class HLongConstant : public HConstant {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001870 public:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001871 explicit HLongConstant(int64_t value) : HConstant(Primitive::kPrimLong), value_(value) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001872
1873 int64_t GetValue() const { return value_; }
1874
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001875 virtual bool InstructionDataEquals(HInstruction* other) const {
1876 return other->AsLongConstant()->value_ == value_;
1877 }
1878
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001879 virtual size_t ComputeHashCode() const { return static_cast<size_t>(GetValue()); }
1880
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001881 DECLARE_INSTRUCTION(LongConstant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001882
1883 private:
1884 const int64_t value_;
1885
1886 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
1887};
1888
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001889enum class Intrinsics {
1890#define OPTIMIZING_INTRINSICS(Name, IsStatic) k ## Name,
1891#include "intrinsics_list.h"
1892 kNone,
1893 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
1894#undef INTRINSICS_LIST
1895#undef OPTIMIZING_INTRINSICS
1896};
1897std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
1898
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001899class HInvoke : public HInstruction {
1900 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001901 virtual size_t InputCount() const { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001902
1903 // Runtime needs to walk the stack, so Dex -> Dex calls need to
1904 // know their environment.
Calin Juravle77520bc2015-01-12 18:45:46 +00001905 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001906
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001907 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001908 SetRawInputAt(index, argument);
1909 }
1910
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001911 virtual Primitive::Type GetType() const { return return_type_; }
1912
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001913 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001914
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001915 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
1916
1917 Intrinsics GetIntrinsic() {
1918 return intrinsic_;
1919 }
1920
1921 void SetIntrinsic(Intrinsics intrinsic) {
1922 intrinsic_ = intrinsic;
1923 }
1924
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001925 DECLARE_INSTRUCTION(Invoke);
1926
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001927 protected:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001928 HInvoke(ArenaAllocator* arena,
1929 uint32_t number_of_arguments,
1930 Primitive::Type return_type,
1931 uint32_t dex_pc,
1932 uint32_t dex_method_index)
1933 : HInstruction(SideEffects::All()),
1934 inputs_(arena, number_of_arguments),
1935 return_type_(return_type),
1936 dex_pc_(dex_pc),
1937 dex_method_index_(dex_method_index),
1938 intrinsic_(Intrinsics::kNone) {
1939 inputs_.SetSize(number_of_arguments);
1940 }
1941
David Brazdil1abb4192015-02-17 18:33:36 +00001942 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
1943 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
1944 inputs_.Put(index, input);
1945 }
1946
1947 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001948 const Primitive::Type return_type_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001949 const uint32_t dex_pc_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001950 const uint32_t dex_method_index_;
1951 Intrinsics intrinsic_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001952
1953 private:
1954 DISALLOW_COPY_AND_ASSIGN(HInvoke);
1955};
1956
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001957class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001958 public:
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001959 HInvokeStaticOrDirect(ArenaAllocator* arena,
1960 uint32_t number_of_arguments,
1961 Primitive::Type return_type,
1962 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001963 uint32_t dex_method_index,
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001964 bool is_recursive,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001965 InvokeType invoke_type)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001966 : HInvoke(arena, number_of_arguments, return_type, dex_pc, dex_method_index),
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001967 invoke_type_(invoke_type),
1968 is_recursive_(is_recursive) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001969
Calin Juravle77520bc2015-01-12 18:45:46 +00001970 bool CanDoImplicitNullCheck() const OVERRIDE {
1971 // We access the method via the dex cache so we can't do an implicit null check.
1972 // TODO: for intrinsics we can generate implicit null checks.
1973 return false;
1974 }
1975
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001976 InvokeType GetInvokeType() const { return invoke_type_; }
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001977 bool IsRecursive() const { return is_recursive_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001978
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001979 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001980
1981 private:
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001982 const InvokeType invoke_type_;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001983 const bool is_recursive_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001984
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001985 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001986};
1987
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001988class HInvokeVirtual : public HInvoke {
1989 public:
1990 HInvokeVirtual(ArenaAllocator* arena,
1991 uint32_t number_of_arguments,
1992 Primitive::Type return_type,
1993 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001994 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001995 uint32_t vtable_index)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001996 : HInvoke(arena, number_of_arguments, return_type, dex_pc, dex_method_index),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001997 vtable_index_(vtable_index) {}
1998
Calin Juravle77520bc2015-01-12 18:45:46 +00001999 bool CanDoImplicitNullCheck() const OVERRIDE {
2000 // TODO: Add implicit null checks in intrinsics.
2001 return !GetLocations()->Intrinsified();
2002 }
2003
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002004 uint32_t GetVTableIndex() const { return vtable_index_; }
2005
2006 DECLARE_INSTRUCTION(InvokeVirtual);
2007
2008 private:
2009 const uint32_t vtable_index_;
2010
2011 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
2012};
2013
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002014class HInvokeInterface : public HInvoke {
2015 public:
2016 HInvokeInterface(ArenaAllocator* arena,
2017 uint32_t number_of_arguments,
2018 Primitive::Type return_type,
2019 uint32_t dex_pc,
2020 uint32_t dex_method_index,
2021 uint32_t imt_index)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002022 : HInvoke(arena, number_of_arguments, return_type, dex_pc, dex_method_index),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002023 imt_index_(imt_index) {}
2024
Calin Juravle77520bc2015-01-12 18:45:46 +00002025 bool CanDoImplicitNullCheck() const OVERRIDE {
2026 // TODO: Add implicit null checks in intrinsics.
2027 return !GetLocations()->Intrinsified();
2028 }
2029
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002030 uint32_t GetImtIndex() const { return imt_index_; }
2031 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
2032
2033 DECLARE_INSTRUCTION(InvokeInterface);
2034
2035 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002036 const uint32_t imt_index_;
2037
2038 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
2039};
2040
Dave Allison20dfc792014-06-16 20:44:29 -07002041class HNewInstance : public HExpression<0> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002042 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002043 HNewInstance(uint32_t dex_pc, uint16_t type_index, QuickEntrypointEnum entrypoint)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002044 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2045 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002046 type_index_(type_index),
2047 entrypoint_(entrypoint) {}
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002048
2049 uint32_t GetDexPc() const { return dex_pc_; }
2050 uint16_t GetTypeIndex() const { return type_index_; }
2051
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002052 // Calls runtime so needs an environment.
Calin Juravle92a6ed22014-12-02 18:58:03 +00002053 bool NeedsEnvironment() const OVERRIDE { return true; }
2054 // It may throw when called on:
2055 // - interfaces
2056 // - abstract/innaccessible/unknown classes
2057 // TODO: optimize when possible.
2058 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002059
Calin Juravle10e244f2015-01-26 18:54:32 +00002060 bool CanBeNull() const OVERRIDE { return false; }
2061
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002062 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
2063
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002064 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002065
2066 private:
2067 const uint32_t dex_pc_;
2068 const uint16_t type_index_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002069 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002070
2071 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
2072};
2073
Roland Levillain88cb1752014-10-20 16:36:47 +01002074class HNeg : public HUnaryOperation {
2075 public:
2076 explicit HNeg(Primitive::Type result_type, HInstruction* input)
2077 : HUnaryOperation(result_type, input) {}
2078
Roland Levillain9240d6a2014-10-20 16:47:04 +01002079 virtual int32_t Evaluate(int32_t x) const OVERRIDE { return -x; }
2080 virtual int64_t Evaluate(int64_t x) const OVERRIDE { return -x; }
2081
Roland Levillain88cb1752014-10-20 16:36:47 +01002082 DECLARE_INSTRUCTION(Neg);
2083
2084 private:
2085 DISALLOW_COPY_AND_ASSIGN(HNeg);
2086};
2087
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002088class HNewArray : public HExpression<1> {
2089 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002090 HNewArray(HInstruction* length,
2091 uint32_t dex_pc,
2092 uint16_t type_index,
2093 QuickEntrypointEnum entrypoint)
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002094 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2095 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002096 type_index_(type_index),
2097 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002098 SetRawInputAt(0, length);
2099 }
2100
2101 uint32_t GetDexPc() const { return dex_pc_; }
2102 uint16_t GetTypeIndex() const { return type_index_; }
2103
2104 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00002105 bool NeedsEnvironment() const OVERRIDE { return true; }
2106
2107 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002108
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002109 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
2110
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002111 DECLARE_INSTRUCTION(NewArray);
2112
2113 private:
2114 const uint32_t dex_pc_;
2115 const uint16_t type_index_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002116 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002117
2118 DISALLOW_COPY_AND_ASSIGN(HNewArray);
2119};
2120
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002121class HAdd : public HBinaryOperation {
2122 public:
2123 HAdd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2124 : HBinaryOperation(result_type, left, right) {}
2125
2126 virtual bool IsCommutative() { return true; }
2127
Roland Levillain93445682014-10-06 19:24:02 +01002128 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
2129 return x + y;
2130 }
2131 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
2132 return x + y;
2133 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002134
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002135 DECLARE_INSTRUCTION(Add);
2136
2137 private:
2138 DISALLOW_COPY_AND_ASSIGN(HAdd);
2139};
2140
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002141class HSub : public HBinaryOperation {
2142 public:
2143 HSub(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2144 : HBinaryOperation(result_type, left, right) {}
2145
Roland Levillain93445682014-10-06 19:24:02 +01002146 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
2147 return x - y;
2148 }
2149 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
2150 return x - y;
2151 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002152
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002153 DECLARE_INSTRUCTION(Sub);
2154
2155 private:
2156 DISALLOW_COPY_AND_ASSIGN(HSub);
2157};
2158
Calin Juravle34bacdf2014-10-07 20:23:36 +01002159class HMul : public HBinaryOperation {
2160 public:
2161 HMul(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2162 : HBinaryOperation(result_type, left, right) {}
2163
2164 virtual bool IsCommutative() { return true; }
2165
2166 virtual int32_t Evaluate(int32_t x, int32_t y) const { return x * y; }
2167 virtual int64_t Evaluate(int64_t x, int64_t y) const { return x * y; }
2168
2169 DECLARE_INSTRUCTION(Mul);
2170
2171 private:
2172 DISALLOW_COPY_AND_ASSIGN(HMul);
2173};
2174
Calin Juravle7c4954d2014-10-28 16:57:40 +00002175class HDiv : public HBinaryOperation {
2176 public:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002177 HDiv(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
2178 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00002179
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00002180 virtual int32_t Evaluate(int32_t x, int32_t y) const {
2181 // Our graph structure ensures we never have 0 for `y` during constant folding.
2182 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00002183 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00002184 return (y == -1) ? -x : x / y;
2185 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002186
2187 virtual int64_t Evaluate(int64_t x, int64_t y) const {
2188 DCHECK_NE(y, 0);
2189 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2190 return (y == -1) ? -x : x / y;
2191 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002192
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002193 uint32_t GetDexPc() const { return dex_pc_; }
2194
Calin Juravle7c4954d2014-10-28 16:57:40 +00002195 DECLARE_INSTRUCTION(Div);
2196
2197 private:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002198 const uint32_t dex_pc_;
2199
Calin Juravle7c4954d2014-10-28 16:57:40 +00002200 DISALLOW_COPY_AND_ASSIGN(HDiv);
2201};
2202
Calin Juravlebacfec32014-11-14 15:54:36 +00002203class HRem : public HBinaryOperation {
2204 public:
2205 HRem(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
2206 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
2207
2208 virtual int32_t Evaluate(int32_t x, int32_t y) const {
2209 DCHECK_NE(y, 0);
2210 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2211 return (y == -1) ? 0 : x % y;
2212 }
2213
2214 virtual int64_t Evaluate(int64_t x, int64_t y) const {
2215 DCHECK_NE(y, 0);
2216 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2217 return (y == -1) ? 0 : x % y;
2218 }
2219
2220 uint32_t GetDexPc() const { return dex_pc_; }
2221
2222 DECLARE_INSTRUCTION(Rem);
2223
2224 private:
2225 const uint32_t dex_pc_;
2226
2227 DISALLOW_COPY_AND_ASSIGN(HRem);
2228};
2229
Calin Juravled0d48522014-11-04 16:40:20 +00002230class HDivZeroCheck : public HExpression<1> {
2231 public:
2232 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
2233 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
2234 SetRawInputAt(0, value);
2235 }
2236
2237 bool CanBeMoved() const OVERRIDE { return true; }
2238
2239 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2240 UNUSED(other);
2241 return true;
2242 }
2243
2244 bool NeedsEnvironment() const OVERRIDE { return true; }
2245 bool CanThrow() const OVERRIDE { return true; }
2246
2247 uint32_t GetDexPc() const { return dex_pc_; }
2248
2249 DECLARE_INSTRUCTION(DivZeroCheck);
2250
2251 private:
2252 const uint32_t dex_pc_;
2253
2254 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
2255};
2256
Calin Juravle9aec02f2014-11-18 23:06:35 +00002257class HShl : public HBinaryOperation {
2258 public:
2259 HShl(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2260 : HBinaryOperation(result_type, left, right) {}
2261
2262 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x << (y & kMaxIntShiftValue); }
2263 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x << (y & kMaxLongShiftValue); }
2264
2265 DECLARE_INSTRUCTION(Shl);
2266
2267 private:
2268 DISALLOW_COPY_AND_ASSIGN(HShl);
2269};
2270
2271class HShr : public HBinaryOperation {
2272 public:
2273 HShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2274 : HBinaryOperation(result_type, left, right) {}
2275
2276 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x >> (y & kMaxIntShiftValue); }
2277 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x >> (y & kMaxLongShiftValue); }
2278
2279 DECLARE_INSTRUCTION(Shr);
2280
2281 private:
2282 DISALLOW_COPY_AND_ASSIGN(HShr);
2283};
2284
2285class HUShr : public HBinaryOperation {
2286 public:
2287 HUShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2288 : HBinaryOperation(result_type, left, right) {}
2289
2290 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
2291 uint32_t ux = static_cast<uint32_t>(x);
2292 uint32_t uy = static_cast<uint32_t>(y) & kMaxIntShiftValue;
2293 return static_cast<int32_t>(ux >> uy);
2294 }
2295
2296 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
2297 uint64_t ux = static_cast<uint64_t>(x);
2298 uint64_t uy = static_cast<uint64_t>(y) & kMaxLongShiftValue;
2299 return static_cast<int64_t>(ux >> uy);
2300 }
2301
2302 DECLARE_INSTRUCTION(UShr);
2303
2304 private:
2305 DISALLOW_COPY_AND_ASSIGN(HUShr);
2306};
2307
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002308class HAnd : public HBinaryOperation {
2309 public:
2310 HAnd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2311 : HBinaryOperation(result_type, left, right) {}
2312
2313 bool IsCommutative() OVERRIDE { return true; }
2314
2315 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x & y; }
2316 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x & y; }
2317
2318 DECLARE_INSTRUCTION(And);
2319
2320 private:
2321 DISALLOW_COPY_AND_ASSIGN(HAnd);
2322};
2323
2324class HOr : public HBinaryOperation {
2325 public:
2326 HOr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2327 : HBinaryOperation(result_type, left, right) {}
2328
2329 bool IsCommutative() OVERRIDE { return true; }
2330
2331 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x | y; }
2332 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x | y; }
2333
2334 DECLARE_INSTRUCTION(Or);
2335
2336 private:
2337 DISALLOW_COPY_AND_ASSIGN(HOr);
2338};
2339
2340class HXor : public HBinaryOperation {
2341 public:
2342 HXor(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2343 : HBinaryOperation(result_type, left, right) {}
2344
2345 bool IsCommutative() OVERRIDE { return true; }
2346
2347 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x ^ y; }
2348 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x ^ y; }
2349
2350 DECLARE_INSTRUCTION(Xor);
2351
2352 private:
2353 DISALLOW_COPY_AND_ASSIGN(HXor);
2354};
2355
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002356// The value of a parameter in this method. Its location depends on
2357// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07002358class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002359 public:
Calin Juravle10e244f2015-01-26 18:54:32 +00002360 HParameterValue(uint8_t index, Primitive::Type parameter_type, bool is_this = false)
2361 : HExpression(parameter_type, SideEffects::None()), index_(index), is_this_(is_this) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002362
2363 uint8_t GetIndex() const { return index_; }
2364
Calin Juravle10e244f2015-01-26 18:54:32 +00002365 bool CanBeNull() const OVERRIDE { return !is_this_; }
2366
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002367 DECLARE_INSTRUCTION(ParameterValue);
2368
2369 private:
2370 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00002371 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002372 const uint8_t index_;
2373
Calin Juravle10e244f2015-01-26 18:54:32 +00002374 // Whether or not the parameter value corresponds to 'this' argument.
2375 const bool is_this_;
2376
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002377 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
2378};
2379
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002380class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002381 public:
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002382 explicit HNot(Primitive::Type result_type, HInstruction* input)
2383 : HUnaryOperation(result_type, input) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002384
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002385 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002386 virtual bool InstructionDataEquals(HInstruction* other) const {
2387 UNUSED(other);
2388 return true;
2389 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002390
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002391 virtual int32_t Evaluate(int32_t x) const OVERRIDE { return ~x; }
2392 virtual int64_t Evaluate(int64_t x) const OVERRIDE { return ~x; }
2393
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002394 DECLARE_INSTRUCTION(Not);
2395
2396 private:
2397 DISALLOW_COPY_AND_ASSIGN(HNot);
2398};
2399
Roland Levillaindff1f282014-11-05 14:15:05 +00002400class HTypeConversion : public HExpression<1> {
2401 public:
2402 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00002403 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
2404 : HExpression(result_type, SideEffects::None()), dex_pc_(dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002405 SetRawInputAt(0, input);
2406 DCHECK_NE(input->GetType(), result_type);
2407 }
2408
2409 HInstruction* GetInput() const { return InputAt(0); }
2410 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
2411 Primitive::Type GetResultType() const { return GetType(); }
2412
Roland Levillain624279f2014-12-04 11:54:28 +00002413 // Required by the x86 and ARM code generators when producing calls
2414 // to the runtime.
2415 uint32_t GetDexPc() const { return dex_pc_; }
2416
Roland Levillaindff1f282014-11-05 14:15:05 +00002417 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00002418 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00002419
2420 DECLARE_INSTRUCTION(TypeConversion);
2421
2422 private:
Roland Levillain624279f2014-12-04 11:54:28 +00002423 const uint32_t dex_pc_;
2424
Roland Levillaindff1f282014-11-05 14:15:05 +00002425 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
2426};
2427
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00002428static constexpr uint32_t kNoRegNumber = -1;
2429
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002430class HPhi : public HInstruction {
2431 public:
2432 HPhi(ArenaAllocator* arena, uint32_t reg_number, size_t number_of_inputs, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002433 : HInstruction(SideEffects::None()),
2434 inputs_(arena, number_of_inputs),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002435 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002436 type_(type),
Calin Juravle10e244f2015-01-26 18:54:32 +00002437 is_live_(false),
2438 can_be_null_(true) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002439 inputs_.SetSize(number_of_inputs);
2440 }
2441
Calin Juravle10e244f2015-01-26 18:54:32 +00002442 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002443
2444 void AddInput(HInstruction* input);
2445
Calin Juravle10e244f2015-01-26 18:54:32 +00002446 Primitive::Type GetType() const OVERRIDE { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002447 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002448
Calin Juravle10e244f2015-01-26 18:54:32 +00002449 bool CanBeNull() const OVERRIDE { return can_be_null_; }
2450 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
2451
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002452 uint32_t GetRegNumber() const { return reg_number_; }
2453
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002454 void SetDead() { is_live_ = false; }
2455 void SetLive() { is_live_ = true; }
2456 bool IsDead() const { return !is_live_; }
2457 bool IsLive() const { return is_live_; }
2458
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002459 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002460
David Brazdil1abb4192015-02-17 18:33:36 +00002461 protected:
2462 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
2463
2464 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
2465 inputs_.Put(index, input);
2466 }
2467
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002468 private:
David Brazdil1abb4192015-02-17 18:33:36 +00002469 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002470 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002471 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002472 bool is_live_;
Calin Juravle10e244f2015-01-26 18:54:32 +00002473 bool can_be_null_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002474
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002475 DISALLOW_COPY_AND_ASSIGN(HPhi);
2476};
2477
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002478class HNullCheck : public HExpression<1> {
2479 public:
2480 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002481 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002482 SetRawInputAt(0, value);
2483 }
2484
Calin Juravle10e244f2015-01-26 18:54:32 +00002485 bool CanBeMoved() const OVERRIDE { return true; }
2486 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002487 UNUSED(other);
2488 return true;
2489 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002490
Calin Juravle10e244f2015-01-26 18:54:32 +00002491 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002492
Calin Juravle10e244f2015-01-26 18:54:32 +00002493 bool CanThrow() const OVERRIDE { return true; }
2494
2495 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01002496
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002497 uint32_t GetDexPc() const { return dex_pc_; }
2498
2499 DECLARE_INSTRUCTION(NullCheck);
2500
2501 private:
2502 const uint32_t dex_pc_;
2503
2504 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
2505};
2506
2507class FieldInfo : public ValueObject {
2508 public:
Calin Juravle52c48962014-12-16 17:02:57 +00002509 FieldInfo(MemberOffset field_offset, Primitive::Type field_type, bool is_volatile)
2510 : field_offset_(field_offset), field_type_(field_type), is_volatile_(is_volatile) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002511
2512 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002513 Primitive::Type GetFieldType() const { return field_type_; }
Calin Juravle52c48962014-12-16 17:02:57 +00002514 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002515
2516 private:
2517 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01002518 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00002519 const bool is_volatile_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002520};
2521
2522class HInstanceFieldGet : public HExpression<1> {
2523 public:
2524 HInstanceFieldGet(HInstruction* value,
2525 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00002526 MemberOffset field_offset,
2527 bool is_volatile)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002528 : HExpression(field_type, SideEffects::DependsOnSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00002529 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002530 SetRawInputAt(0, value);
2531 }
2532
Calin Juravle10c9cbe2014-12-19 10:50:19 +00002533 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002534
2535 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2536 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
2537 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002538 }
2539
Calin Juravle77520bc2015-01-12 18:45:46 +00002540 bool CanDoImplicitNullCheck() const OVERRIDE {
2541 return GetFieldOffset().Uint32Value() < kPageSize;
2542 }
2543
2544 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002545 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
2546 }
2547
Calin Juravle52c48962014-12-16 17:02:57 +00002548 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002549 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002550 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002551 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002552
2553 DECLARE_INSTRUCTION(InstanceFieldGet);
2554
2555 private:
2556 const FieldInfo field_info_;
2557
2558 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
2559};
2560
2561class HInstanceFieldSet : public HTemplateInstruction<2> {
2562 public:
2563 HInstanceFieldSet(HInstruction* object,
2564 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01002565 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00002566 MemberOffset field_offset,
2567 bool is_volatile)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002568 : HTemplateInstruction(SideEffects::ChangesSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00002569 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002570 SetRawInputAt(0, object);
2571 SetRawInputAt(1, value);
2572 }
2573
Calin Juravle77520bc2015-01-12 18:45:46 +00002574 bool CanDoImplicitNullCheck() const OVERRIDE {
2575 return GetFieldOffset().Uint32Value() < kPageSize;
2576 }
2577
Calin Juravle52c48962014-12-16 17:02:57 +00002578 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002579 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002580 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002581 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002582 HInstruction* GetValue() const { return InputAt(1); }
2583
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002584 DECLARE_INSTRUCTION(InstanceFieldSet);
2585
2586 private:
2587 const FieldInfo field_info_;
2588
2589 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
2590};
2591
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002592class HArrayGet : public HExpression<2> {
2593 public:
2594 HArrayGet(HInstruction* array, HInstruction* index, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002595 : HExpression(type, SideEffects::DependsOnSomething()) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002596 SetRawInputAt(0, array);
2597 SetRawInputAt(1, index);
2598 }
2599
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002600 bool CanBeMoved() const OVERRIDE { return true; }
2601 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002602 UNUSED(other);
2603 return true;
2604 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002605 bool CanDoImplicitNullCheck() const OVERRIDE {
2606 // TODO: We can be smarter here.
2607 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
2608 // which generates the implicit null check. There are cases when these can be removed
2609 // to produce better code. If we ever add optimizations to do so we should allow an
2610 // implicit check here (as long as the address falls in the first page).
2611 return false;
2612 }
2613
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002614 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002615
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002616 HInstruction* GetArray() const { return InputAt(0); }
2617 HInstruction* GetIndex() const { return InputAt(1); }
2618
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002619 DECLARE_INSTRUCTION(ArrayGet);
2620
2621 private:
2622 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
2623};
2624
2625class HArraySet : public HTemplateInstruction<3> {
2626 public:
2627 HArraySet(HInstruction* array,
2628 HInstruction* index,
2629 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002630 Primitive::Type expected_component_type,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002631 uint32_t dex_pc)
2632 : HTemplateInstruction(SideEffects::ChangesSomething()),
2633 dex_pc_(dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002634 expected_component_type_(expected_component_type),
2635 needs_type_check_(value->GetType() == Primitive::kPrimNot) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002636 SetRawInputAt(0, array);
2637 SetRawInputAt(1, index);
2638 SetRawInputAt(2, value);
2639 }
2640
Calin Juravle77520bc2015-01-12 18:45:46 +00002641 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002642 // We currently always call a runtime method to catch array store
2643 // exceptions.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002644 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002645 }
2646
Calin Juravle77520bc2015-01-12 18:45:46 +00002647 bool CanDoImplicitNullCheck() const OVERRIDE {
2648 // TODO: Same as for ArrayGet.
2649 return false;
2650 }
2651
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002652 void ClearNeedsTypeCheck() {
2653 needs_type_check_ = false;
2654 }
2655
2656 bool NeedsTypeCheck() const { return needs_type_check_; }
2657
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002658 uint32_t GetDexPc() const { return dex_pc_; }
2659
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002660 HInstruction* GetArray() const { return InputAt(0); }
2661 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002662 HInstruction* GetValue() const { return InputAt(2); }
2663
2664 Primitive::Type GetComponentType() const {
2665 // The Dex format does not type floating point index operations. Since the
2666 // `expected_component_type_` is set during building and can therefore not
2667 // be correct, we also check what is the value type. If it is a floating
2668 // point type, we must use that type.
2669 Primitive::Type value_type = GetValue()->GetType();
2670 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
2671 ? value_type
2672 : expected_component_type_;
2673 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002674
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002675 DECLARE_INSTRUCTION(ArraySet);
2676
2677 private:
2678 const uint32_t dex_pc_;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002679 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002680 bool needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002681
2682 DISALLOW_COPY_AND_ASSIGN(HArraySet);
2683};
2684
2685class HArrayLength : public HExpression<1> {
2686 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002687 explicit HArrayLength(HInstruction* array)
2688 : HExpression(Primitive::kPrimInt, SideEffects::None()) {
2689 // Note that arrays do not change length, so the instruction does not
2690 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002691 SetRawInputAt(0, array);
2692 }
2693
Calin Juravle77520bc2015-01-12 18:45:46 +00002694 bool CanBeMoved() const OVERRIDE { return true; }
2695 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002696 UNUSED(other);
2697 return true;
2698 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002699 bool CanDoImplicitNullCheck() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002700
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002701 DECLARE_INSTRUCTION(ArrayLength);
2702
2703 private:
2704 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
2705};
2706
2707class HBoundsCheck : public HExpression<2> {
2708 public:
2709 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002710 : HExpression(index->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002711 DCHECK(index->GetType() == Primitive::kPrimInt);
2712 SetRawInputAt(0, index);
2713 SetRawInputAt(1, length);
2714 }
2715
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002716 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002717 virtual bool InstructionDataEquals(HInstruction* other) const {
2718 UNUSED(other);
2719 return true;
2720 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002721
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002722 virtual bool NeedsEnvironment() const { return true; }
2723
Roland Levillaine161a2a2014-10-03 12:45:18 +01002724 virtual bool CanThrow() const { return true; }
2725
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002726 uint32_t GetDexPc() const { return dex_pc_; }
2727
2728 DECLARE_INSTRUCTION(BoundsCheck);
2729
2730 private:
2731 const uint32_t dex_pc_;
2732
2733 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
2734};
2735
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002736/**
2737 * Some DEX instructions are folded into multiple HInstructions that need
2738 * to stay live until the last HInstruction. This class
2739 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00002740 * HInstruction stays live. `index` represents the stack location index of the
2741 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002742 */
2743class HTemporary : public HTemplateInstruction<0> {
2744 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002745 explicit HTemporary(size_t index) : HTemplateInstruction(SideEffects::None()), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002746
2747 size_t GetIndex() const { return index_; }
2748
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00002749 Primitive::Type GetType() const OVERRIDE {
2750 // The previous instruction is the one that will be stored in the temporary location.
2751 DCHECK(GetPrevious() != nullptr);
2752 return GetPrevious()->GetType();
2753 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00002754
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002755 DECLARE_INSTRUCTION(Temporary);
2756
2757 private:
2758 const size_t index_;
2759
2760 DISALLOW_COPY_AND_ASSIGN(HTemporary);
2761};
2762
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002763class HSuspendCheck : public HTemplateInstruction<0> {
2764 public:
2765 explicit HSuspendCheck(uint32_t dex_pc)
Nicolas Geoffray9ebc72c2014-09-25 16:33:42 +01002766 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002767
2768 virtual bool NeedsEnvironment() const {
2769 return true;
2770 }
2771
2772 uint32_t GetDexPc() const { return dex_pc_; }
2773
2774 DECLARE_INSTRUCTION(SuspendCheck);
2775
2776 private:
2777 const uint32_t dex_pc_;
2778
2779 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
2780};
2781
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002782/**
2783 * Instruction to load a Class object.
2784 */
2785class HLoadClass : public HExpression<0> {
2786 public:
2787 HLoadClass(uint16_t type_index,
2788 bool is_referrers_class,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002789 uint32_t dex_pc)
2790 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2791 type_index_(type_index),
2792 is_referrers_class_(is_referrers_class),
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002793 dex_pc_(dex_pc),
Calin Juravleb1498f62015-02-16 13:13:29 +00002794 generate_clinit_check_(false),
2795 loaded_class_rti_(ReferenceTypeInfo::CreateTop(/* is_exact */ false)) {}
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002796
2797 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002798
2799 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2800 return other->AsLoadClass()->type_index_ == type_index_;
2801 }
2802
2803 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
2804
2805 uint32_t GetDexPc() const { return dex_pc_; }
2806 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002807 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002808
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002809 bool NeedsEnvironment() const OVERRIDE {
2810 // Will call runtime and load the class if the class is not loaded yet.
2811 // TODO: finer grain decision.
2812 return !is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002813 }
2814
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002815 bool MustGenerateClinitCheck() const {
2816 return generate_clinit_check_;
2817 }
2818
2819 void SetMustGenerateClinitCheck() {
2820 generate_clinit_check_ = true;
2821 }
2822
2823 bool CanCallRuntime() const {
2824 return MustGenerateClinitCheck() || !is_referrers_class_;
2825 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002826
Nicolas Geoffray82091da2015-01-26 10:02:45 +00002827 bool CanThrow() const OVERRIDE {
2828 // May call runtime and and therefore can throw.
2829 // TODO: finer grain decision.
2830 return !is_referrers_class_;
2831 }
2832
Calin Juravleacf735c2015-02-12 15:25:22 +00002833 ReferenceTypeInfo GetLoadedClassRTI() {
2834 return loaded_class_rti_;
2835 }
2836
2837 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
2838 // Make sure we only set exact types (the loaded class should never be merged).
2839 DCHECK(rti.IsExact());
2840 loaded_class_rti_ = rti;
2841 }
2842
2843 bool IsResolved() {
2844 return loaded_class_rti_.IsExact();
2845 }
2846
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002847 DECLARE_INSTRUCTION(LoadClass);
2848
2849 private:
2850 const uint16_t type_index_;
2851 const bool is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002852 const uint32_t dex_pc_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002853 // Whether this instruction must generate the initialization check.
2854 // Used for code generation.
2855 bool generate_clinit_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002856
Calin Juravleacf735c2015-02-12 15:25:22 +00002857 ReferenceTypeInfo loaded_class_rti_;
2858
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002859 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
2860};
2861
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002862class HLoadString : public HExpression<0> {
2863 public:
2864 HLoadString(uint32_t string_index, uint32_t dex_pc)
2865 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2866 string_index_(string_index),
2867 dex_pc_(dex_pc) {}
2868
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002869 bool CanBeMoved() const OVERRIDE { return true; }
2870
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002871 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2872 return other->AsLoadString()->string_index_ == string_index_;
2873 }
2874
2875 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
2876
2877 uint32_t GetDexPc() const { return dex_pc_; }
2878 uint32_t GetStringIndex() const { return string_index_; }
2879
2880 // TODO: Can we deopt or debug when we resolve a string?
2881 bool NeedsEnvironment() const OVERRIDE { return false; }
2882
2883 DECLARE_INSTRUCTION(LoadString);
2884
2885 private:
2886 const uint32_t string_index_;
2887 const uint32_t dex_pc_;
2888
2889 DISALLOW_COPY_AND_ASSIGN(HLoadString);
2890};
2891
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002892// TODO: Pass this check to HInvokeStaticOrDirect nodes.
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002893/**
2894 * Performs an initialization check on its Class object input.
2895 */
2896class HClinitCheck : public HExpression<1> {
2897 public:
2898 explicit HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
2899 : HExpression(Primitive::kPrimNot, SideEffects::All()),
2900 dex_pc_(dex_pc) {
2901 SetRawInputAt(0, constant);
2902 }
2903
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002904 bool CanBeMoved() const OVERRIDE { return true; }
2905 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2906 UNUSED(other);
2907 return true;
2908 }
2909
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002910 bool NeedsEnvironment() const OVERRIDE {
2911 // May call runtime to initialize the class.
2912 return true;
2913 }
2914
2915 uint32_t GetDexPc() const { return dex_pc_; }
2916
2917 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
2918
2919 DECLARE_INSTRUCTION(ClinitCheck);
2920
2921 private:
2922 const uint32_t dex_pc_;
2923
2924 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
2925};
2926
2927class HStaticFieldGet : public HExpression<1> {
2928 public:
2929 HStaticFieldGet(HInstruction* cls,
2930 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00002931 MemberOffset field_offset,
2932 bool is_volatile)
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002933 : HExpression(field_type, SideEffects::DependsOnSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00002934 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002935 SetRawInputAt(0, cls);
2936 }
2937
Calin Juravle52c48962014-12-16 17:02:57 +00002938
Calin Juravle10c9cbe2014-12-19 10:50:19 +00002939 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002940
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002941 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00002942 HStaticFieldGet* other_get = other->AsStaticFieldGet();
2943 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002944 }
2945
2946 size_t ComputeHashCode() const OVERRIDE {
2947 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
2948 }
2949
Calin Juravle52c48962014-12-16 17:02:57 +00002950 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002951 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
2952 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002953 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002954
2955 DECLARE_INSTRUCTION(StaticFieldGet);
2956
2957 private:
2958 const FieldInfo field_info_;
2959
2960 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
2961};
2962
2963class HStaticFieldSet : public HTemplateInstruction<2> {
2964 public:
2965 HStaticFieldSet(HInstruction* cls,
2966 HInstruction* value,
2967 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00002968 MemberOffset field_offset,
2969 bool is_volatile)
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002970 : HTemplateInstruction(SideEffects::ChangesSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00002971 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002972 SetRawInputAt(0, cls);
2973 SetRawInputAt(1, value);
2974 }
2975
Calin Juravle52c48962014-12-16 17:02:57 +00002976 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002977 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
2978 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002979 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002980
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002981 HInstruction* GetValue() const { return InputAt(1); }
2982
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002983 DECLARE_INSTRUCTION(StaticFieldSet);
2984
2985 private:
2986 const FieldInfo field_info_;
2987
2988 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
2989};
2990
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002991// Implement the move-exception DEX instruction.
2992class HLoadException : public HExpression<0> {
2993 public:
2994 HLoadException() : HExpression(Primitive::kPrimNot, SideEffects::None()) {}
2995
2996 DECLARE_INSTRUCTION(LoadException);
2997
2998 private:
2999 DISALLOW_COPY_AND_ASSIGN(HLoadException);
3000};
3001
3002class HThrow : public HTemplateInstruction<1> {
3003 public:
3004 HThrow(HInstruction* exception, uint32_t dex_pc)
3005 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {
3006 SetRawInputAt(0, exception);
3007 }
3008
3009 bool IsControlFlow() const OVERRIDE { return true; }
3010
3011 bool NeedsEnvironment() const OVERRIDE { return true; }
3012
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003013 bool CanThrow() const OVERRIDE { return true; }
3014
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003015 uint32_t GetDexPc() const { return dex_pc_; }
3016
3017 DECLARE_INSTRUCTION(Throw);
3018
3019 private:
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003020 const uint32_t dex_pc_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003021
3022 DISALLOW_COPY_AND_ASSIGN(HThrow);
3023};
3024
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003025class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003026 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003027 HInstanceOf(HInstruction* object,
3028 HLoadClass* constant,
3029 bool class_is_final,
3030 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003031 : HExpression(Primitive::kPrimBoolean, SideEffects::None()),
3032 class_is_final_(class_is_final),
3033 dex_pc_(dex_pc) {
3034 SetRawInputAt(0, object);
3035 SetRawInputAt(1, constant);
3036 }
3037
3038 bool CanBeMoved() const OVERRIDE { return true; }
3039
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003040 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003041 return true;
3042 }
3043
3044 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003045 return false;
3046 }
3047
3048 uint32_t GetDexPc() const { return dex_pc_; }
3049
3050 bool IsClassFinal() const { return class_is_final_; }
3051
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003052 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003053
3054 private:
3055 const bool class_is_final_;
3056 const uint32_t dex_pc_;
3057
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003058 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
3059};
3060
Calin Juravleb1498f62015-02-16 13:13:29 +00003061class HBoundType : public HExpression<1> {
3062 public:
3063 HBoundType(HInstruction* input, ReferenceTypeInfo bound_type)
3064 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3065 bound_type_(bound_type) {
3066 SetRawInputAt(0, input);
3067 }
3068
3069 const ReferenceTypeInfo& GetBoundType() const { return bound_type_; }
3070
3071 bool CanBeNull() const OVERRIDE {
3072 // `null instanceof ClassX` always return false so we can't be null.
3073 return false;
3074 }
3075
3076 DECLARE_INSTRUCTION(BoundType);
3077
3078 private:
3079 // Encodes the most upper class that this instruction can have. In other words
3080 // it is always the case that GetBoundType().IsSupertypeOf(GetReferenceType()).
3081 // It is used to bound the type in cases like `if (x instanceof ClassX) {}`
3082 const ReferenceTypeInfo bound_type_;
3083
3084 DISALLOW_COPY_AND_ASSIGN(HBoundType);
3085};
3086
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003087class HCheckCast : public HTemplateInstruction<2> {
3088 public:
3089 HCheckCast(HInstruction* object,
3090 HLoadClass* constant,
3091 bool class_is_final,
3092 uint32_t dex_pc)
3093 : HTemplateInstruction(SideEffects::None()),
3094 class_is_final_(class_is_final),
3095 dex_pc_(dex_pc) {
3096 SetRawInputAt(0, object);
3097 SetRawInputAt(1, constant);
3098 }
3099
3100 bool CanBeMoved() const OVERRIDE { return true; }
3101
3102 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
3103 return true;
3104 }
3105
3106 bool NeedsEnvironment() const OVERRIDE {
3107 // Instruction may throw a CheckCastError.
3108 return true;
3109 }
3110
3111 bool CanThrow() const OVERRIDE { return true; }
3112
3113 uint32_t GetDexPc() const { return dex_pc_; }
3114
3115 bool IsClassFinal() const { return class_is_final_; }
3116
3117 DECLARE_INSTRUCTION(CheckCast);
3118
3119 private:
3120 const bool class_is_final_;
3121 const uint32_t dex_pc_;
3122
3123 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003124};
3125
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003126class HMonitorOperation : public HTemplateInstruction<1> {
3127 public:
3128 enum OperationKind {
3129 kEnter,
3130 kExit,
3131 };
3132
3133 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
3134 : HTemplateInstruction(SideEffects::None()), kind_(kind), dex_pc_(dex_pc) {
3135 SetRawInputAt(0, object);
3136 }
3137
3138 // Instruction may throw a Java exception, so we need an environment.
3139 bool NeedsEnvironment() const OVERRIDE { return true; }
3140 bool CanThrow() const OVERRIDE { return true; }
3141
3142 uint32_t GetDexPc() const { return dex_pc_; }
3143
3144 bool IsEnter() const { return kind_ == kEnter; }
3145
3146 DECLARE_INSTRUCTION(MonitorOperation);
3147
Calin Juravle52c48962014-12-16 17:02:57 +00003148 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003149 const OperationKind kind_;
3150 const uint32_t dex_pc_;
3151
3152 private:
3153 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
3154};
3155
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003156class MoveOperands : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003157 public:
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003158 MoveOperands(Location source, Location destination, HInstruction* instruction)
3159 : source_(source), destination_(destination), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003160
3161 Location GetSource() const { return source_; }
3162 Location GetDestination() const { return destination_; }
3163
3164 void SetSource(Location value) { source_ = value; }
3165 void SetDestination(Location value) { destination_ = value; }
3166
3167 // The parallel move resolver marks moves as "in-progress" by clearing the
3168 // destination (but not the source).
3169 Location MarkPending() {
3170 DCHECK(!IsPending());
3171 Location dest = destination_;
3172 destination_ = Location::NoLocation();
3173 return dest;
3174 }
3175
3176 void ClearPending(Location dest) {
3177 DCHECK(IsPending());
3178 destination_ = dest;
3179 }
3180
3181 bool IsPending() const {
3182 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
3183 return destination_.IsInvalid() && !source_.IsInvalid();
3184 }
3185
3186 // True if this blocks a move from the given location.
3187 bool Blocks(Location loc) const {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003188 return !IsEliminated() && (source_.Contains(loc) || loc.Contains(source_));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003189 }
3190
3191 // A move is redundant if it's been eliminated, if its source and
3192 // destination are the same, or if its destination is unneeded.
3193 bool IsRedundant() const {
3194 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
3195 }
3196
3197 // We clear both operands to indicate move that's been eliminated.
3198 void Eliminate() {
3199 source_ = destination_ = Location::NoLocation();
3200 }
3201
3202 bool IsEliminated() const {
3203 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
3204 return source_.IsInvalid();
3205 }
3206
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003207 HInstruction* GetInstruction() const { return instruction_; }
3208
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003209 private:
3210 Location source_;
3211 Location destination_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003212 // The instruction this move is assocatied with. Null when this move is
3213 // for moving an input in the expected locations of user (including a phi user).
3214 // This is only used in debug mode, to ensure we do not connect interval siblings
3215 // in the same parallel move.
3216 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003217};
3218
3219static constexpr size_t kDefaultNumberOfMoves = 4;
3220
3221class HParallelMove : public HTemplateInstruction<0> {
3222 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003223 explicit HParallelMove(ArenaAllocator* arena)
3224 : HTemplateInstruction(SideEffects::None()), moves_(arena, kDefaultNumberOfMoves) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003225
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003226 void AddMove(Location source, Location destination, HInstruction* instruction) {
3227 DCHECK(source.IsValid());
3228 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003229 if (kIsDebugBuild) {
3230 if (instruction != nullptr) {
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00003231 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003232 DCHECK_NE(moves_.Get(i).GetInstruction(), instruction)
3233 << "Doing parallel moves for the same instruction.";
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00003234 }
3235 }
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003236 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
3237 DCHECK(!destination.Equals(moves_.Get(i).GetDestination()))
3238 << "Same destination for two moves in a parallel move.";
3239 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003240 }
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003241 moves_.Add(MoveOperands(source, destination, instruction));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003242 }
3243
3244 MoveOperands* MoveOperandsAt(size_t index) const {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003245 return moves_.GetRawStorage() + index;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003246 }
3247
3248 size_t NumMoves() const { return moves_.Size(); }
3249
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003250 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003251
3252 private:
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003253 GrowableArray<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003254
3255 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
3256};
3257
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003258class HGraphVisitor : public ValueObject {
3259 public:
Dave Allison20dfc792014-06-16 20:44:29 -07003260 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
3261 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003262
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003263 virtual void VisitInstruction(HInstruction* instruction) { UNUSED(instruction); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003264 virtual void VisitBasicBlock(HBasicBlock* block);
3265
Roland Levillain633021e2014-10-01 14:12:25 +01003266 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003267 void VisitInsertionOrder();
3268
Roland Levillain633021e2014-10-01 14:12:25 +01003269 // Visit the graph following dominator tree reverse post-order.
3270 void VisitReversePostOrder();
3271
Nicolas Geoffray787c3072014-03-17 10:20:19 +00003272 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00003273
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003274 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003275#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003276 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
3277
3278 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
3279
3280#undef DECLARE_VISIT_INSTRUCTION
3281
3282 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07003283 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003284
3285 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
3286};
3287
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003288class HGraphDelegateVisitor : public HGraphVisitor {
3289 public:
3290 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
3291 virtual ~HGraphDelegateVisitor() {}
3292
3293 // Visit functions that delegate to to super class.
3294#define DECLARE_VISIT_INSTRUCTION(name, super) \
3295 virtual void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
3296
3297 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
3298
3299#undef DECLARE_VISIT_INSTRUCTION
3300
3301 private:
3302 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
3303};
3304
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003305class HInsertionOrderIterator : public ValueObject {
3306 public:
3307 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
3308
3309 bool Done() const { return index_ == graph_.GetBlocks().Size(); }
3310 HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); }
3311 void Advance() { ++index_; }
3312
3313 private:
3314 const HGraph& graph_;
3315 size_t index_;
3316
3317 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
3318};
3319
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003320class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003321 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003322 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003323
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003324 bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); }
3325 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003326 void Advance() { ++index_; }
3327
3328 private:
3329 const HGraph& graph_;
3330 size_t index_;
3331
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003332 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003333};
3334
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003335class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003336 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003337 explicit HPostOrderIterator(const HGraph& graph)
3338 : graph_(graph), index_(graph_.GetReversePostOrder().Size()) {}
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003339
3340 bool Done() const { return index_ == 0; }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003341 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003342 void Advance() { --index_; }
3343
3344 private:
3345 const HGraph& graph_;
3346 size_t index_;
3347
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003348 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003349};
3350
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003351// Iterator over the blocks that art part of the loop. Includes blocks part
3352// of an inner loop. The order in which the blocks are iterated is on their
3353// block id.
3354class HBlocksInLoopIterator : public ValueObject {
3355 public:
3356 explicit HBlocksInLoopIterator(const HLoopInformation& info)
3357 : blocks_in_loop_(info.GetBlocks()),
3358 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
3359 index_(0) {
3360 if (!blocks_in_loop_.IsBitSet(index_)) {
3361 Advance();
3362 }
3363 }
3364
3365 bool Done() const { return index_ == blocks_.Size(); }
3366 HBasicBlock* Current() const { return blocks_.Get(index_); }
3367 void Advance() {
3368 ++index_;
3369 for (size_t e = blocks_.Size(); index_ < e; ++index_) {
3370 if (blocks_in_loop_.IsBitSet(index_)) {
3371 break;
3372 }
3373 }
3374 }
3375
3376 private:
3377 const BitVector& blocks_in_loop_;
3378 const GrowableArray<HBasicBlock*>& blocks_;
3379 size_t index_;
3380
3381 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
3382};
3383
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003384} // namespace art
3385
3386#endif // ART_COMPILER_OPTIMIZING_NODES_H_