blob: 98076a05f273606c171906cb562544617a79f202 [file] [log] [blame]
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ART_COMPILER_OPTIMIZING_NODES_H_
18#define ART_COMPILER_OPTIMIZING_NODES_H_
19
Mathieu Chartierb666f482015-02-18 14:33:14 -080020#include "base/arena_object.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000021#include "entrypoints/quick/quick_entrypoints_enum.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000022#include "handle.h"
23#include "handle_scope.h"
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000024#include "invoke_type.h"
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +010025#include "locations.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000026#include "mirror/class.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010027#include "offsets.h"
Ian Rogerse63db272014-07-15 15:36:11 -070028#include "primitive.h"
Nicolas Geoffray0e336432014-02-26 18:24:38 +000029#include "utils/arena_bit_vector.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000030#include "utils/growable_array.h"
31
32namespace art {
33
David Brazdil1abb4192015-02-17 18:33:36 +000034class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000035class HBasicBlock;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010036class HEnvironment;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000037class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000038class HIntConstant;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000039class HInvoke;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000040class HGraphVisitor;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +000041class HNullConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010042class HPhi;
Nicolas Geoffray3c049742014-09-24 18:10:46 +010043class HSuspendCheck;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010044class LiveInterval;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000045class LocationSummary;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000046
47static const int kDefaultNumberOfBlocks = 8;
48static const int kDefaultNumberOfSuccessors = 2;
49static const int kDefaultNumberOfPredecessors = 2;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010050static const int kDefaultNumberOfDominatedBlocks = 1;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000051static const int kDefaultNumberOfBackEdges = 1;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000052
Calin Juravle9aec02f2014-11-18 23:06:35 +000053static constexpr uint32_t kMaxIntShiftValue = 0x1f;
54static constexpr uint64_t kMaxLongShiftValue = 0x3f;
55
Dave Allison20dfc792014-06-16 20:44:29 -070056enum IfCondition {
57 kCondEQ,
58 kCondNE,
59 kCondLT,
60 kCondLE,
61 kCondGT,
62 kCondGE,
63};
64
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010065class HInstructionList {
66 public:
67 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
68
69 void AddInstruction(HInstruction* instruction);
70 void RemoveInstruction(HInstruction* instruction);
71
Roland Levillain6b469232014-09-25 10:10:38 +010072 // Return true if this list contains `instruction`.
73 bool Contains(HInstruction* instruction) const;
74
Roland Levillainccc07a92014-09-16 14:48:16 +010075 // Return true if `instruction1` is found before `instruction2` in
76 // this instruction list and false otherwise. Abort if none
77 // of these instructions is found.
78 bool FoundBefore(const HInstruction* instruction1,
79 const HInstruction* instruction2) const;
80
Nicolas Geoffray276d9da2015-02-02 18:24:11 +000081 bool IsEmpty() const { return first_instruction_ == nullptr; }
82 void Clear() { first_instruction_ = last_instruction_ = nullptr; }
83
84 // Update the block of all instructions to be `block`.
85 void SetBlockOfInstructions(HBasicBlock* block) const;
86
87 void AddAfter(HInstruction* cursor, const HInstructionList& instruction_list);
88 void Add(const HInstructionList& instruction_list);
89
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010090 private:
91 HInstruction* first_instruction_;
92 HInstruction* last_instruction_;
93
94 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000095 friend class HGraph;
96 friend class HInstruction;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010097 friend class HInstructionIterator;
Nicolas Geoffray804d0932014-05-02 08:46:00 +010098 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010099
100 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
101};
102
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000103// Control-flow graph of a method. Contains a list of basic blocks.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700104class HGraph : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000105 public:
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000106 HGraph(ArenaAllocator* arena, int start_instruction_id = 0)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000107 : arena_(arena),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000108 blocks_(arena, kDefaultNumberOfBlocks),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100109 reverse_post_order_(arena, kDefaultNumberOfBlocks),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700110 entry_block_(nullptr),
111 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100112 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100113 number_of_vregs_(0),
114 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000115 temporaries_vreg_slots_(0),
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000116 current_instruction_id_(start_instruction_id) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000117
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000118 ArenaAllocator* GetArena() const { return arena_; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100119 const GrowableArray<HBasicBlock*>& GetBlocks() const { return blocks_; }
Roland Levillain93445682014-10-06 19:24:02 +0100120 HBasicBlock* GetBlock(size_t id) const { return blocks_.Get(id); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000121
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000122 HBasicBlock* GetEntryBlock() const { return entry_block_; }
123 HBasicBlock* GetExitBlock() const { return exit_block_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000124
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000125 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
126 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000127
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000128 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100129
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000130 // Try building the SSA form of this graph, with dominance computation and loop
131 // recognition. Returns whether it was successful in doing all these steps.
132 bool TryBuildingSsa() {
133 BuildDominatorTree();
134 TransformToSsa();
135 return AnalyzeNaturalLoops();
136 }
137
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000138 void BuildDominatorTree();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000139 void TransformToSsa();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100140 void SimplifyCFG();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000141
Nicolas Geoffrayf5370122014-12-02 11:51:19 +0000142 // Analyze all natural loops in this graph. Returns false if one
143 // loop is not natural, that is the header does not dominate the
144 // back edge.
145 bool AnalyzeNaturalLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100146
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000147 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
148 void InlineInto(HGraph* outer_graph, HInvoke* invoke);
149
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100150 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
151 void SimplifyLoop(HBasicBlock* header);
152
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000153 int32_t GetNextInstructionId() {
154 DCHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000155 return current_instruction_id_++;
156 }
157
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000158 int32_t GetCurrentInstructionId() const {
159 return current_instruction_id_;
160 }
161
162 void SetCurrentInstructionId(int32_t id) {
163 current_instruction_id_ = id;
164 }
165
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100166 uint16_t GetMaximumNumberOfOutVRegs() const {
167 return maximum_number_of_out_vregs_;
168 }
169
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000170 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
171 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100172 }
173
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000174 void UpdateTemporariesVRegSlots(size_t slots) {
175 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100176 }
177
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000178 size_t GetTemporariesVRegSlots() const {
179 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100180 }
181
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100182 void SetNumberOfVRegs(uint16_t number_of_vregs) {
183 number_of_vregs_ = number_of_vregs;
184 }
185
186 uint16_t GetNumberOfVRegs() const {
187 return number_of_vregs_;
188 }
189
190 void SetNumberOfInVRegs(uint16_t value) {
191 number_of_in_vregs_ = value;
192 }
193
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100194 uint16_t GetNumberOfLocalVRegs() const {
195 return number_of_vregs_ - number_of_in_vregs_;
196 }
197
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100198 const GrowableArray<HBasicBlock*>& GetReversePostOrder() const {
199 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100200 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100201
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000202 HNullConstant* GetNullConstant();
203
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000204 private:
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000205 HBasicBlock* FindCommonDominator(HBasicBlock* first, HBasicBlock* second) const;
206 void VisitBlockForDominatorTree(HBasicBlock* block,
207 HBasicBlock* predecessor,
208 GrowableArray<size_t>* visits);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100209 void FindBackEdges(ArenaBitVector* visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000210 void VisitBlockForBackEdges(HBasicBlock* block,
211 ArenaBitVector* visited,
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100212 ArenaBitVector* visiting);
Roland Levillainfc600dc2014-12-02 17:16:31 +0000213 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000214 void RemoveDeadBlocks(const ArenaBitVector& visited) const;
215
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000216 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000217
218 // List of blocks in insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000219 GrowableArray<HBasicBlock*> blocks_;
220
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100221 // List of blocks to perform a reverse post order tree traversal.
222 GrowableArray<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000223
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000224 HBasicBlock* entry_block_;
225 HBasicBlock* exit_block_;
226
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100227 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100228 uint16_t maximum_number_of_out_vregs_;
229
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100230 // The number of virtual registers in this method. Contains the parameters.
231 uint16_t number_of_vregs_;
232
233 // The number of virtual registers used by parameters of this method.
234 uint16_t number_of_in_vregs_;
235
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000236 // Number of vreg size slots that the temporaries use (used in baseline compiler).
237 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100238
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000239 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000240 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000241
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000242 // Cached null constant that might be created when building SSA form.
243 HNullConstant* cached_null_constant_;
244
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000245 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000246 DISALLOW_COPY_AND_ASSIGN(HGraph);
247};
248
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700249class HLoopInformation : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000250 public:
251 HLoopInformation(HBasicBlock* header, HGraph* graph)
252 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100253 suspend_check_(nullptr),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100254 back_edges_(graph->GetArena(), kDefaultNumberOfBackEdges),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100255 // Make bit vector growable, as the number of blocks may change.
256 blocks_(graph->GetArena(), graph->GetBlocks().Size(), true) {}
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100257
258 HBasicBlock* GetHeader() const {
259 return header_;
260 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000261
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000262 void SetHeader(HBasicBlock* block) {
263 header_ = block;
264 }
265
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100266 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
267 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
268 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
269
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000270 void AddBackEdge(HBasicBlock* back_edge) {
271 back_edges_.Add(back_edge);
272 }
273
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100274 void RemoveBackEdge(HBasicBlock* back_edge) {
275 back_edges_.Delete(back_edge);
276 }
277
278 bool IsBackEdge(HBasicBlock* block) {
279 for (size_t i = 0, e = back_edges_.Size(); i < e; ++i) {
280 if (back_edges_.Get(i) == block) return true;
281 }
282 return false;
283 }
284
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000285 size_t NumberOfBackEdges() const {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000286 return back_edges_.Size();
287 }
288
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100289 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100290
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100291 const GrowableArray<HBasicBlock*>& GetBackEdges() const {
292 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100293 }
294
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100295 void ClearBackEdges() {
296 back_edges_.Reset();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100297 }
298
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100299 // Find blocks that are part of this loop. Returns whether the loop is a natural loop,
300 // that is the header dominates the back edge.
301 bool Populate();
302
303 // Returns whether this loop information contains `block`.
304 // Note that this loop information *must* be populated before entering this function.
305 bool Contains(const HBasicBlock& block) const;
306
307 // Returns whether this loop information is an inner loop of `other`.
308 // Note that `other` *must* be populated before entering this function.
309 bool IsIn(const HLoopInformation& other) const;
310
311 const ArenaBitVector& GetBlocks() const { return blocks_; }
312
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000313 void Add(HBasicBlock* block);
314
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000315 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100316 // Internal recursive implementation of `Populate`.
317 void PopulateRecursive(HBasicBlock* block);
318
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000319 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100320 HSuspendCheck* suspend_check_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000321 GrowableArray<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100322 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000323
324 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
325};
326
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100327static constexpr size_t kNoLifetime = -1;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100328static constexpr uint32_t kNoDexPc = -1;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100329
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000330// A block in a method. Contains the list of instructions represented
331// as a double linked list. Each block knows its predecessors and
332// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100333
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700334class HBasicBlock : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000335 public:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100336 explicit HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000337 : graph_(graph),
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000338 predecessors_(graph->GetArena(), kDefaultNumberOfPredecessors),
339 successors_(graph->GetArena(), kDefaultNumberOfSuccessors),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000340 loop_information_(nullptr),
341 dominator_(nullptr),
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100342 dominated_blocks_(graph->GetArena(), kDefaultNumberOfDominatedBlocks),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100343 block_id_(-1),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100344 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100345 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000346 lifetime_end_(kNoLifetime),
347 is_catch_block_(false) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000348
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100349 const GrowableArray<HBasicBlock*>& GetPredecessors() const {
350 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000351 }
352
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100353 const GrowableArray<HBasicBlock*>& GetSuccessors() const {
354 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000355 }
356
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100357 const GrowableArray<HBasicBlock*>& GetDominatedBlocks() const {
358 return dominated_blocks_;
359 }
360
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100361 bool IsEntryBlock() const {
362 return graph_->GetEntryBlock() == this;
363 }
364
365 bool IsExitBlock() const {
366 return graph_->GetExitBlock() == this;
367 }
368
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000369 void AddBackEdge(HBasicBlock* back_edge) {
370 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000371 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000372 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100373 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000374 loop_information_->AddBackEdge(back_edge);
375 }
376
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000377 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000378 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000379
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000380 int GetBlockId() const { return block_id_; }
381 void SetBlockId(int id) { block_id_ = id; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000382
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000383 HBasicBlock* GetDominator() const { return dominator_; }
384 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100385 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.Add(block); }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000386 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
387 for (size_t i = 0, e = dominated_blocks_.Size(); i < e; ++i) {
388 if (dominated_blocks_.Get(i) == existing) {
389 dominated_blocks_.Put(i, new_block);
390 return;
391 }
392 }
393 LOG(FATAL) << "Unreachable";
394 UNREACHABLE();
395 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000396
397 int NumberOfBackEdges() const {
398 return loop_information_ == nullptr
399 ? 0
400 : loop_information_->NumberOfBackEdges();
401 }
402
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100403 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
404 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100405 const HInstructionList& GetInstructions() const { return instructions_; }
406 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100407 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000408
409 void AddSuccessor(HBasicBlock* block) {
410 successors_.Add(block);
411 block->predecessors_.Add(this);
412 }
413
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100414 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
415 size_t successor_index = GetSuccessorIndexOf(existing);
416 DCHECK_NE(successor_index, static_cast<size_t>(-1));
417 existing->RemovePredecessor(this);
418 new_block->predecessors_.Add(this);
419 successors_.Put(successor_index, new_block);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000420 }
421
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000422 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
423 size_t predecessor_index = GetPredecessorIndexOf(existing);
424 DCHECK_NE(predecessor_index, static_cast<size_t>(-1));
425 existing->RemoveSuccessor(this);
426 new_block->successors_.Add(this);
427 predecessors_.Put(predecessor_index, new_block);
428 }
429
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100430 void RemovePredecessor(HBasicBlock* block) {
431 predecessors_.Delete(block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100432 }
433
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000434 void RemoveSuccessor(HBasicBlock* block) {
435 successors_.Delete(block);
436 }
437
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100438 void ClearAllPredecessors() {
439 predecessors_.Reset();
440 }
441
442 void AddPredecessor(HBasicBlock* block) {
443 predecessors_.Add(block);
444 block->successors_.Add(this);
445 }
446
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100447 void SwapPredecessors() {
Nicolas Geoffrayc83d4412014-09-18 16:46:20 +0100448 DCHECK_EQ(predecessors_.Size(), 2u);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100449 HBasicBlock* temp = predecessors_.Get(0);
450 predecessors_.Put(0, predecessors_.Get(1));
451 predecessors_.Put(1, temp);
452 }
453
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100454 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) {
455 for (size_t i = 0, e = predecessors_.Size(); i < e; ++i) {
456 if (predecessors_.Get(i) == predecessor) {
457 return i;
458 }
459 }
460 return -1;
461 }
462
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100463 size_t GetSuccessorIndexOf(HBasicBlock* successor) {
464 for (size_t i = 0, e = successors_.Size(); i < e; ++i) {
465 if (successors_.Get(i) == successor) {
466 return i;
467 }
468 }
469 return -1;
470 }
471
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000472 // Split the block into two blocks just after `cursor`. Returns the newly
473 // created block. Note that this method just updates raw block information,
474 // like predecessors, successors, dominators, and instruction list. It does not
475 // update the graph, reverse post order, loop information, nor make sure the
476 // blocks are consistent (for example ending with a control flow instruction).
477 HBasicBlock* SplitAfter(HInstruction* cursor);
478
479 // Merge `other` at the end of `this`. Successors and dominated blocks of
480 // `other` are changed to be successors and dominated blocks of `this`. Note
481 // that this method does not update the graph, reverse post order, loop
482 // information, nor make sure the blocks are consistent (for example ending
483 // with a control flow instruction).
484 void MergeWith(HBasicBlock* other);
485
486 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
487 // of `this` are moved to `other`.
488 // Note that this method does not update the graph, reverse post order, loop
489 // information, nor make sure the blocks are consistent (for example ending
490 void ReplaceWith(HBasicBlock* other);
491
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000492 void AddInstruction(HInstruction* instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100493 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +0100494 // Replace instruction `initial` with `replacement` within this block.
495 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
496 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100497 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100498 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +0000499 // RemoveInstruction and RemovePhi delete a given instruction from the respective
500 // instruction list. With 'ensure_safety' set to true, it verifies that the
501 // instruction is not in use and removes it from the use lists of its inputs.
502 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
503 void RemovePhi(HPhi* phi, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100504
505 bool IsLoopHeader() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100506 return (loop_information_ != nullptr) && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100507 }
508
Roland Levillain6b879dd2014-09-22 17:13:44 +0100509 bool IsLoopPreHeaderFirstPredecessor() const {
510 DCHECK(IsLoopHeader());
511 DCHECK(!GetPredecessors().IsEmpty());
512 return GetPredecessors().Get(0) == GetLoopInformation()->GetPreHeader();
513 }
514
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100515 HLoopInformation* GetLoopInformation() const {
516 return loop_information_;
517 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000518
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000519 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100520 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000521 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100522 void SetInLoop(HLoopInformation* info) {
523 if (IsLoopHeader()) {
524 // Nothing to do. This just means `info` is an outer loop.
525 } else if (loop_information_ == nullptr) {
526 loop_information_ = info;
527 } else if (loop_information_->Contains(*info->GetHeader())) {
528 // Block is currently part of an outer loop. Make it part of this inner loop.
529 // Note that a non loop header having a loop information means this loop information
530 // has already been populated
531 loop_information_ = info;
532 } else {
533 // Block is part of an inner loop. Do not update the loop information.
534 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
535 // at this point, because this method is being called while populating `info`.
536 }
537 }
538
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000539 // Raw update of the loop information.
540 void SetLoopInformation(HLoopInformation* info) {
541 loop_information_ = info;
542 }
543
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100544 bool IsInLoop() const { return loop_information_ != nullptr; }
545
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100546 // Returns wheter this block dominates the blocked passed as parameter.
547 bool Dominates(HBasicBlock* block) const;
548
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100549 size_t GetLifetimeStart() const { return lifetime_start_; }
550 size_t GetLifetimeEnd() const { return lifetime_end_; }
551
552 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
553 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
554
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100555 uint32_t GetDexPc() const { return dex_pc_; }
556
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000557 bool IsCatchBlock() const { return is_catch_block_; }
558 void SetIsCatchBlock() { is_catch_block_ = true; }
559
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000560 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000561 HGraph* graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000562 GrowableArray<HBasicBlock*> predecessors_;
563 GrowableArray<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100564 HInstructionList instructions_;
565 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000566 HLoopInformation* loop_information_;
567 HBasicBlock* dominator_;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100568 GrowableArray<HBasicBlock*> dominated_blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000569 int block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100570 // The dex program counter of the first instruction of this block.
571 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100572 size_t lifetime_start_;
573 size_t lifetime_end_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000574 bool is_catch_block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000575
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000576 friend class HGraph;
577 friend class HInstruction;
578
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000579 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
580};
581
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100582#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
583 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000584 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000585 M(ArrayGet, Instruction) \
586 M(ArrayLength, Instruction) \
587 M(ArraySet, Instruction) \
588 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +0000589 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000590 M(CheckCast, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100591 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000592 M(Compare, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100593 M(Condition, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000594 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +0000595 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000596 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100597 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000598 M(Exit, Instruction) \
599 M(FloatConstant, Constant) \
600 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100601 M(GreaterThan, Condition) \
602 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100603 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000604 M(InstanceFieldGet, Instruction) \
605 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000606 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100607 M(IntConstant, Constant) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +0000608 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000609 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100610 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000611 M(LessThan, Condition) \
612 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000613 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000614 M(LoadException, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100615 M(LoadLocal, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000616 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100617 M(Local, Instruction) \
618 M(LongConstant, Constant) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +0000619 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000620 M(Mul, BinaryOperation) \
621 M(Neg, UnaryOperation) \
622 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100623 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +0100624 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000625 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000626 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000627 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000628 M(Or, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100629 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000630 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100631 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000632 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100633 M(Return, Instruction) \
634 M(ReturnVoid, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000635 M(Shl, BinaryOperation) \
636 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100637 M(StaticFieldGet, Instruction) \
638 M(StaticFieldSet, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100639 M(StoreLocal, Instruction) \
640 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100641 M(SuspendCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000642 M(Temporary, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000643 M(Throw, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +0000644 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000645 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000646 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000647
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100648#define FOR_EACH_INSTRUCTION(M) \
649 FOR_EACH_CONCRETE_INSTRUCTION(M) \
650 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +0100651 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +0100652 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100653 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -0700654
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100655#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000656FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
657#undef FORWARD_DECLARATION
658
Roland Levillainccc07a92014-09-16 14:48:16 +0100659#define DECLARE_INSTRUCTION(type) \
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100660 virtual InstructionKind GetKind() const { return k##type; } \
Roland Levillainccc07a92014-09-16 14:48:16 +0100661 virtual const char* DebugName() const { return #type; } \
662 virtual const H##type* As##type() const OVERRIDE { return this; } \
663 virtual H##type* As##type() OVERRIDE { return this; } \
664 virtual bool InstructionTypeEquals(HInstruction* other) const { \
665 return other->Is##type(); \
666 } \
667 virtual void Accept(HGraphVisitor* visitor)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000668
David Brazdiled596192015-01-23 10:39:45 +0000669template <typename T> class HUseList;
670
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100671template <typename T>
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700672class HUseListNode : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000673 public:
David Brazdiled596192015-01-23 10:39:45 +0000674 HUseListNode* GetPrevious() const { return prev_; }
675 HUseListNode* GetNext() const { return next_; }
676 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100677 size_t GetIndex() const { return index_; }
678
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000679 private:
David Brazdiled596192015-01-23 10:39:45 +0000680 HUseListNode(T user, size_t index)
681 : user_(user), index_(index), prev_(nullptr), next_(nullptr) {}
682
683 T const user_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100684 const size_t index_;
David Brazdiled596192015-01-23 10:39:45 +0000685 HUseListNode<T>* prev_;
686 HUseListNode<T>* next_;
687
688 friend class HUseList<T>;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000689
690 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
691};
692
David Brazdiled596192015-01-23 10:39:45 +0000693template <typename T>
694class HUseList : public ValueObject {
695 public:
696 HUseList() : first_(nullptr) {}
697
698 void Clear() {
699 first_ = nullptr;
700 }
701
702 // Adds a new entry at the beginning of the use list and returns
703 // the newly created node.
704 HUseListNode<T>* AddUse(T user, size_t index, ArenaAllocator* arena) {
David Brazdilea55b932015-01-27 17:12:29 +0000705 HUseListNode<T>* new_node = new (arena) HUseListNode<T>(user, index);
David Brazdiled596192015-01-23 10:39:45 +0000706 if (IsEmpty()) {
707 first_ = new_node;
708 } else {
709 first_->prev_ = new_node;
710 new_node->next_ = first_;
711 first_ = new_node;
712 }
713 return new_node;
714 }
715
716 HUseListNode<T>* GetFirst() const {
717 return first_;
718 }
719
720 void Remove(HUseListNode<T>* node) {
David Brazdil1abb4192015-02-17 18:33:36 +0000721 DCHECK(node != nullptr);
722 DCHECK(Contains(node));
723
David Brazdiled596192015-01-23 10:39:45 +0000724 if (node->prev_ != nullptr) {
725 node->prev_->next_ = node->next_;
726 }
727 if (node->next_ != nullptr) {
728 node->next_->prev_ = node->prev_;
729 }
730 if (node == first_) {
731 first_ = node->next_;
732 }
733 }
734
David Brazdil1abb4192015-02-17 18:33:36 +0000735 bool Contains(const HUseListNode<T>* node) const {
736 if (node == nullptr) {
737 return false;
738 }
739 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
740 if (current == node) {
741 return true;
742 }
743 }
744 return false;
745 }
746
David Brazdiled596192015-01-23 10:39:45 +0000747 bool IsEmpty() const {
748 return first_ == nullptr;
749 }
750
751 bool HasOnlyOneUse() const {
752 return first_ != nullptr && first_->next_ == nullptr;
753 }
754
755 private:
756 HUseListNode<T>* first_;
757};
758
759template<typename T>
760class HUseIterator : public ValueObject {
761 public:
762 explicit HUseIterator(const HUseList<T>& uses) : current_(uses.GetFirst()) {}
763
764 bool Done() const { return current_ == nullptr; }
765
766 void Advance() {
767 DCHECK(!Done());
768 current_ = current_->GetNext();
769 }
770
771 HUseListNode<T>* Current() const {
772 DCHECK(!Done());
773 return current_;
774 }
775
776 private:
777 HUseListNode<T>* current_;
778
779 friend class HValue;
780};
781
David Brazdil1abb4192015-02-17 18:33:36 +0000782// This class is used by HEnvironment and HInstruction classes to record the
783// instructions they use and pointers to the corresponding HUseListNodes kept
784// by the used instructions.
785template <typename T>
786class HUserRecord : public ValueObject {
787 public:
788 HUserRecord() : instruction_(nullptr), use_node_(nullptr) {}
789 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), use_node_(nullptr) {}
790
791 HUserRecord(const HUserRecord<T>& old_record, HUseListNode<T>* use_node)
792 : instruction_(old_record.instruction_), use_node_(use_node) {
793 DCHECK(instruction_ != nullptr);
794 DCHECK(use_node_ != nullptr);
795 DCHECK(old_record.use_node_ == nullptr);
796 }
797
798 HInstruction* GetInstruction() const { return instruction_; }
799 HUseListNode<T>* GetUseNode() const { return use_node_; }
800
801 private:
802 // Instruction used by the user.
803 HInstruction* instruction_;
804
805 // Corresponding entry in the use list kept by 'instruction_'.
806 HUseListNode<T>* use_node_;
807};
808
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100809// Represents the side effects an instruction may have.
810class SideEffects : public ValueObject {
811 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100812 SideEffects() : flags_(0) {}
813
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100814 static SideEffects None() {
815 return SideEffects(0);
816 }
817
818 static SideEffects All() {
819 return SideEffects(ChangesSomething().flags_ | DependsOnSomething().flags_);
820 }
821
822 static SideEffects ChangesSomething() {
823 return SideEffects((1 << kFlagChangesCount) - 1);
824 }
825
826 static SideEffects DependsOnSomething() {
827 int count = kFlagDependsOnCount - kFlagChangesCount;
828 return SideEffects(((1 << count) - 1) << kFlagChangesCount);
829 }
830
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100831 SideEffects Union(SideEffects other) const {
832 return SideEffects(flags_ | other.flags_);
833 }
834
Roland Levillain72bceff2014-09-15 18:29:00 +0100835 bool HasSideEffects() const {
836 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
837 return (flags_ & all_bits_set) != 0;
838 }
839
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100840 bool HasAllSideEffects() const {
841 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
842 return all_bits_set == (flags_ & all_bits_set);
843 }
844
845 bool DependsOn(SideEffects other) const {
846 size_t depends_flags = other.ComputeDependsFlags();
847 return (flags_ & depends_flags) != 0;
848 }
849
850 bool HasDependencies() const {
851 int count = kFlagDependsOnCount - kFlagChangesCount;
852 size_t all_bits_set = (1 << count) - 1;
853 return ((flags_ >> kFlagChangesCount) & all_bits_set) != 0;
854 }
855
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100856 private:
857 static constexpr int kFlagChangesSomething = 0;
858 static constexpr int kFlagChangesCount = kFlagChangesSomething + 1;
859
860 static constexpr int kFlagDependsOnSomething = kFlagChangesCount;
861 static constexpr int kFlagDependsOnCount = kFlagDependsOnSomething + 1;
862
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100863 explicit SideEffects(size_t flags) : flags_(flags) {}
864
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100865 size_t ComputeDependsFlags() const {
866 return flags_ << kFlagChangesCount;
867 }
868
869 size_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100870};
871
David Brazdiled596192015-01-23 10:39:45 +0000872// A HEnvironment object contains the values of virtual registers at a given location.
873class HEnvironment : public ArenaObject<kArenaAllocMisc> {
874 public:
875 HEnvironment(ArenaAllocator* arena, size_t number_of_vregs)
876 : vregs_(arena, number_of_vregs) {
877 vregs_.SetSize(number_of_vregs);
878 for (size_t i = 0; i < number_of_vregs; i++) {
David Brazdil1abb4192015-02-17 18:33:36 +0000879 vregs_.Put(i, HUserRecord<HEnvironment*>());
David Brazdiled596192015-01-23 10:39:45 +0000880 }
881 }
882
883 void CopyFrom(HEnvironment* env);
884
885 void SetRawEnvAt(size_t index, HInstruction* instruction) {
David Brazdil1abb4192015-02-17 18:33:36 +0000886 vregs_.Put(index, HUserRecord<HEnvironment*>(instruction));
David Brazdiled596192015-01-23 10:39:45 +0000887 }
888
889 HInstruction* GetInstructionAt(size_t index) const {
David Brazdil1abb4192015-02-17 18:33:36 +0000890 return vregs_.Get(index).GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +0000891 }
892
David Brazdil1abb4192015-02-17 18:33:36 +0000893 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +0000894
895 size_t Size() const { return vregs_.Size(); }
896
897 private:
David Brazdil1abb4192015-02-17 18:33:36 +0000898 // Record instructions' use entries of this environment for constant-time removal.
899 // It should only be called by HInstruction when a new environment use is added.
900 void RecordEnvUse(HUseListNode<HEnvironment*>* env_use) {
901 DCHECK(env_use->GetUser() == this);
902 size_t index = env_use->GetIndex();
903 vregs_.Put(index, HUserRecord<HEnvironment*>(vregs_.Get(index), env_use));
904 }
David Brazdiled596192015-01-23 10:39:45 +0000905
David Brazdil1abb4192015-02-17 18:33:36 +0000906 GrowableArray<HUserRecord<HEnvironment*> > vregs_;
David Brazdiled596192015-01-23 10:39:45 +0000907
David Brazdil1abb4192015-02-17 18:33:36 +0000908 friend HInstruction;
David Brazdiled596192015-01-23 10:39:45 +0000909
910 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
911};
912
Calin Juravleacf735c2015-02-12 15:25:22 +0000913class ReferenceTypeInfo : ValueObject {
914 public:
Calin Juravleb1498f62015-02-16 13:13:29 +0000915 typedef Handle<mirror::Class> TypeHandle;
916
917 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact)
918 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
919 if (type_handle->IsObjectClass()) {
920 // Override the type handle to be consistent with the case when we get to
921 // Top but don't have the Object class available. It avoids having to guess
922 // what value the type_handle has when it's Top.
923 return ReferenceTypeInfo(TypeHandle(), is_exact, true);
924 } else {
925 return ReferenceTypeInfo(type_handle, is_exact, false);
926 }
927 }
928
929 static ReferenceTypeInfo CreateTop(bool is_exact) {
930 return ReferenceTypeInfo(TypeHandle(), is_exact, true);
Calin Juravleacf735c2015-02-12 15:25:22 +0000931 }
932
933 bool IsExact() const { return is_exact_; }
934 bool IsTop() const { return is_top_; }
935
936 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
937
Calin Juravleb1498f62015-02-16 13:13:29 +0000938 bool IsSupertypeOf(ReferenceTypeInfo rti) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Calin Juravleacf735c2015-02-12 15:25:22 +0000939 if (IsTop()) {
940 // Top (equivalent for java.lang.Object) is supertype of anything.
941 return true;
942 }
943 if (rti.IsTop()) {
944 // If we get here `this` is not Top() so it can't be a supertype.
945 return false;
946 }
947 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
948 }
949
950 // Returns true if the type information provide the same amount of details.
951 // Note that it does not mean that the instructions have the same actual type
952 // (e.g. tops are equal but they can be the result of a merge).
953 bool IsEqual(ReferenceTypeInfo rti) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
954 if (IsExact() != rti.IsExact()) {
955 return false;
956 }
957 if (IsTop() && rti.IsTop()) {
958 // `Top` means java.lang.Object, so the types are equivalent.
959 return true;
960 }
961 if (IsTop() || rti.IsTop()) {
962 // If only one is top or object than they are not equivalent.
963 // NB: We need this extra check because the type_handle of `Top` is invalid
964 // and we cannot inspect its reference.
965 return false;
966 }
967
968 // Finally check the types.
969 return GetTypeHandle().Get() == rti.GetTypeHandle().Get();
970 }
971
972 private:
Calin Juravleb1498f62015-02-16 13:13:29 +0000973 ReferenceTypeInfo() : ReferenceTypeInfo(TypeHandle(), false, true) {}
974 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact, bool is_top)
975 : type_handle_(type_handle), is_exact_(is_exact), is_top_(is_top) {}
976
Calin Juravleacf735c2015-02-12 15:25:22 +0000977 // The class of the object.
Calin Juravleb1498f62015-02-16 13:13:29 +0000978 TypeHandle type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +0000979 // Whether or not the type is exact or a superclass of the actual type.
Calin Juravleb1498f62015-02-16 13:13:29 +0000980 // Whether or not we have any information about this type.
Calin Juravleacf735c2015-02-12 15:25:22 +0000981 bool is_exact_;
982 // A true value here means that the object type should be java.lang.Object.
983 // We don't have access to the corresponding mirror object every time so this
984 // flag acts as a substitute. When true, the TypeHandle refers to a null
985 // pointer and should not be used.
986 bool is_top_;
987};
988
989std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
990
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700991class HInstruction : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000992 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100993 explicit HInstruction(SideEffects side_effects)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000994 : previous_(nullptr),
995 next_(nullptr),
996 block_(nullptr),
997 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100998 ssa_index_(-1),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100999 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001000 locations_(nullptr),
1001 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001002 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001003 side_effects_(side_effects),
1004 reference_type_info_(ReferenceTypeInfo::CreateTop(/* is_exact */ false)) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001005
Dave Allison20dfc792014-06-16 20:44:29 -07001006 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001007
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001008#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001009 enum InstructionKind {
1010 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1011 };
1012#undef DECLARE_KIND
1013
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001014 HInstruction* GetNext() const { return next_; }
1015 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001016
Calin Juravle77520bc2015-01-12 18:45:46 +00001017 HInstruction* GetNextDisregardingMoves() const;
1018 HInstruction* GetPreviousDisregardingMoves() const;
1019
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001020 HBasicBlock* GetBlock() const { return block_; }
1021 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001022 bool IsInBlock() const { return block_ != nullptr; }
1023 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +01001024 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001025
Roland Levillain6b879dd2014-09-22 17:13:44 +01001026 virtual size_t InputCount() const = 0;
David Brazdil1abb4192015-02-17 18:33:36 +00001027 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001028
1029 virtual void Accept(HGraphVisitor* visitor) = 0;
1030 virtual const char* DebugName() const = 0;
1031
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001032 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
David Brazdil1abb4192015-02-17 18:33:36 +00001033 void SetRawInputAt(size_t index, HInstruction* input) {
1034 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1035 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001036
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001037 virtual bool NeedsEnvironment() const { return false; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001038 virtual bool IsControlFlow() const { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01001039 virtual bool CanThrow() const { return false; }
Roland Levillain72bceff2014-09-15 18:29:00 +01001040 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001041
Calin Juravle61d544b2015-02-23 16:46:57 +00001042 virtual bool ActAsNullConstant() const { return false; }
1043
Calin Juravle10e244f2015-01-26 18:54:32 +00001044 // Does not apply for all instructions, but having this at top level greatly
1045 // simplifies the null check elimination.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001046 virtual bool CanBeNull() const {
1047 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1048 return true;
1049 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001050
Calin Juravle77520bc2015-01-12 18:45:46 +00001051 virtual bool CanDoImplicitNullCheck() const { return false; }
1052
Calin Juravleacf735c2015-02-12 15:25:22 +00001053 void SetReferenceTypeInfo(ReferenceTypeInfo reference_type_info) {
Calin Juravle61d544b2015-02-23 16:46:57 +00001054 DCHECK_EQ(GetType(), Primitive::kPrimNot);
Calin Juravleacf735c2015-02-12 15:25:22 +00001055 reference_type_info_ = reference_type_info;
1056 }
1057
Calin Juravle61d544b2015-02-23 16:46:57 +00001058 ReferenceTypeInfo GetReferenceTypeInfo() const {
1059 DCHECK_EQ(GetType(), Primitive::kPrimNot);
1060 return reference_type_info_;
1061 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001062
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001063 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001064 DCHECK(user != nullptr);
1065 HUseListNode<HInstruction*>* use =
1066 uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1067 user->SetRawInputRecordAt(index, HUserRecord<HInstruction*>(user->InputRecordAt(index), use));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001068 }
1069
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001070 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001071 DCHECK(user != nullptr);
David Brazdiled596192015-01-23 10:39:45 +00001072 HUseListNode<HEnvironment*>* env_use =
1073 env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1074 user->RecordEnvUse(env_use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001075 }
1076
David Brazdil1abb4192015-02-17 18:33:36 +00001077 void RemoveAsUserOfInput(size_t input) {
1078 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
1079 input_use.GetInstruction()->uses_.Remove(input_use.GetUseNode());
1080 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001081
David Brazdil1abb4192015-02-17 18:33:36 +00001082 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1083 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001084
David Brazdiled596192015-01-23 10:39:45 +00001085 bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); }
1086 bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001087
Roland Levillain6c82d402014-10-13 16:10:27 +01001088 // Does this instruction strictly dominate `other_instruction`?
1089 // Returns false if this instruction and `other_instruction` are the same.
1090 // Aborts if this instruction and `other_instruction` are both phis.
1091 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001092
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001093 int GetId() const { return id_; }
1094 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001095
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001096 int GetSsaIndex() const { return ssa_index_; }
1097 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1098 bool HasSsaIndex() const { return ssa_index_ != -1; }
1099
1100 bool HasEnvironment() const { return environment_ != nullptr; }
1101 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001102 void SetEnvironment(HEnvironment* environment) { environment_ = environment; }
1103
Nicolas Geoffray39468442014-09-02 15:17:15 +01001104 // Returns the number of entries in the environment. Typically, that is the
1105 // number of dex registers in a method. It could be more in case of inlining.
1106 size_t EnvironmentSize() const;
1107
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001108 LocationSummary* GetLocations() const { return locations_; }
1109 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001110
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001111 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001112 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001113
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001114 // Move `this` instruction before `cursor`.
1115 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001116
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001117#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01001118 bool Is##type() const { return (As##type() != nullptr); } \
1119 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001120 virtual H##type* As##type() { return nullptr; }
1121
1122 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
1123#undef INSTRUCTION_TYPE_CHECK
1124
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001125 // Returns whether the instruction can be moved within the graph.
1126 virtual bool CanBeMoved() const { return false; }
1127
1128 // Returns whether the two instructions are of the same kind.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001129 virtual bool InstructionTypeEquals(HInstruction* other) const {
1130 UNUSED(other);
1131 return false;
1132 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001133
1134 // Returns whether any data encoded in the two instructions is equal.
1135 // This method does not look at the inputs. Both instructions must be
1136 // of the same type, otherwise the method has undefined behavior.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001137 virtual bool InstructionDataEquals(HInstruction* other) const {
1138 UNUSED(other);
1139 return false;
1140 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001141
1142 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00001143 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001144 // 2) Their inputs are identical.
1145 bool Equals(HInstruction* other) const;
1146
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001147 virtual InstructionKind GetKind() const = 0;
1148
1149 virtual size_t ComputeHashCode() const {
1150 size_t result = GetKind();
1151 for (size_t i = 0, e = InputCount(); i < e; ++i) {
1152 result = (result * 31) + InputAt(i)->GetId();
1153 }
1154 return result;
1155 }
1156
1157 SideEffects GetSideEffects() const { return side_effects_; }
1158
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001159 size_t GetLifetimePosition() const { return lifetime_position_; }
1160 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
1161 LiveInterval* GetLiveInterval() const { return live_interval_; }
1162 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
1163 bool HasLiveInterval() const { return live_interval_ != nullptr; }
1164
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001165 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
1166
1167 // Returns whether the code generation of the instruction will require to have access
1168 // to the current method. Such instructions are:
1169 // (1): Instructions that require an environment, as calling the runtime requires
1170 // to walk the stack and have the current method stored at a specific stack address.
1171 // (2): Object literals like classes and strings, that are loaded from the dex cache
1172 // fields of the current method.
1173 bool NeedsCurrentMethod() const {
1174 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
1175 }
1176
David Brazdil1abb4192015-02-17 18:33:36 +00001177 protected:
1178 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
1179 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
1180
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001181 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001182 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); }
1183
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001184 HInstruction* previous_;
1185 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001186 HBasicBlock* block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001187
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001188 // An instruction gets an id when it is added to the graph.
1189 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01001190 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001191 int id_;
1192
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001193 // When doing liveness analysis, instructions that have uses get an SSA index.
1194 int ssa_index_;
1195
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001196 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00001197 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001198
1199 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00001200 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001201
Nicolas Geoffray39468442014-09-02 15:17:15 +01001202 // The environment associated with this instruction. Not null if the instruction
1203 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001204 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001205
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001206 // Set by the code generator.
1207 LocationSummary* locations_;
1208
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001209 // Set by the liveness analysis.
1210 LiveInterval* live_interval_;
1211
1212 // Set by the liveness analysis, this is the position in a linear
1213 // order of blocks where this instruction's live interval start.
1214 size_t lifetime_position_;
1215
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001216 const SideEffects side_effects_;
1217
Calin Juravleacf735c2015-02-12 15:25:22 +00001218 // TODO: for primitive types this should be marked as invalid.
1219 ReferenceTypeInfo reference_type_info_;
1220
David Brazdil1abb4192015-02-17 18:33:36 +00001221 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001222 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00001223 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001224 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001225 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001226
1227 DISALLOW_COPY_AND_ASSIGN(HInstruction);
1228};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001229std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001230
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001231class HInputIterator : public ValueObject {
1232 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001233 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001234
1235 bool Done() const { return index_ == instruction_->InputCount(); }
1236 HInstruction* Current() const { return instruction_->InputAt(index_); }
1237 void Advance() { index_++; }
1238
1239 private:
1240 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001241 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001242
1243 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
1244};
1245
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001246class HInstructionIterator : public ValueObject {
1247 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001248 explicit HInstructionIterator(const HInstructionList& instructions)
1249 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001250 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001251 }
1252
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001253 bool Done() const { return instruction_ == nullptr; }
1254 HInstruction* Current() const { return instruction_; }
1255 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001256 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001257 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001258 }
1259
1260 private:
1261 HInstruction* instruction_;
1262 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001263
1264 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001265};
1266
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001267class HBackwardInstructionIterator : public ValueObject {
1268 public:
1269 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
1270 : instruction_(instructions.last_instruction_) {
1271 next_ = Done() ? nullptr : instruction_->GetPrevious();
1272 }
1273
1274 bool Done() const { return instruction_ == nullptr; }
1275 HInstruction* Current() const { return instruction_; }
1276 void Advance() {
1277 instruction_ = next_;
1278 next_ = Done() ? nullptr : instruction_->GetPrevious();
1279 }
1280
1281 private:
1282 HInstruction* instruction_;
1283 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001284
1285 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001286};
1287
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001288// An embedded container with N elements of type T. Used (with partial
1289// specialization for N=0) because embedded arrays cannot have size 0.
1290template<typename T, intptr_t N>
1291class EmbeddedArray {
1292 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001293 EmbeddedArray() : elements_() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001294
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001295 intptr_t GetLength() const { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001296
1297 const T& operator[](intptr_t i) const {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001298 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001299 return elements_[i];
1300 }
1301
1302 T& operator[](intptr_t i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001303 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001304 return elements_[i];
1305 }
1306
1307 const T& At(intptr_t i) const {
1308 return (*this)[i];
1309 }
1310
1311 void SetAt(intptr_t i, const T& val) {
1312 (*this)[i] = val;
1313 }
1314
1315 private:
1316 T elements_[N];
1317};
1318
1319template<typename T>
1320class EmbeddedArray<T, 0> {
1321 public:
1322 intptr_t length() const { return 0; }
1323 const T& operator[](intptr_t i) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001324 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001325 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001326 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001327 }
1328 T& operator[](intptr_t i) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001329 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001330 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001331 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001332 }
1333};
1334
1335template<intptr_t N>
1336class HTemplateInstruction: public HInstruction {
1337 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001338 HTemplateInstruction<N>(SideEffects side_effects)
1339 : HInstruction(side_effects), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07001340 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001341
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001342 virtual size_t InputCount() const { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001343
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001344 protected:
David Brazdil1abb4192015-02-17 18:33:36 +00001345 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_[i]; }
1346
1347 void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE {
1348 inputs_[i] = input;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001349 }
1350
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001351 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001352 EmbeddedArray<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001353
1354 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001355};
1356
Dave Allison20dfc792014-06-16 20:44:29 -07001357template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001358class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07001359 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001360 HExpression<N>(Primitive::Type type, SideEffects side_effects)
1361 : HTemplateInstruction<N>(side_effects), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07001362 virtual ~HExpression() {}
1363
1364 virtual Primitive::Type GetType() const { return type_; }
1365
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001366 protected:
1367 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07001368};
1369
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001370// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
1371// instruction that branches to the exit block.
1372class HReturnVoid : public HTemplateInstruction<0> {
1373 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001374 HReturnVoid() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001375
1376 virtual bool IsControlFlow() const { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001377
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001378 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001379
1380 private:
1381 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
1382};
1383
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001384// Represents dex's RETURN opcodes. A HReturn is a control flow
1385// instruction that branches to the exit block.
1386class HReturn : public HTemplateInstruction<1> {
1387 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001388 explicit HReturn(HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001389 SetRawInputAt(0, value);
1390 }
1391
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001392 virtual bool IsControlFlow() const { return true; }
1393
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001394 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001395
1396 private:
1397 DISALLOW_COPY_AND_ASSIGN(HReturn);
1398};
1399
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001400// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001401// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001402// exit block.
1403class HExit : public HTemplateInstruction<0> {
1404 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001405 HExit() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001406
1407 virtual bool IsControlFlow() const { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001408
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001409 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001410
1411 private:
1412 DISALLOW_COPY_AND_ASSIGN(HExit);
1413};
1414
1415// Jumps from one block to another.
1416class HGoto : public HTemplateInstruction<0> {
1417 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001418 HGoto() : HTemplateInstruction(SideEffects::None()) {}
1419
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001420 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001421
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001422 HBasicBlock* GetSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001423 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001424 }
1425
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001426 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001427
1428 private:
1429 DISALLOW_COPY_AND_ASSIGN(HGoto);
1430};
1431
Dave Allison20dfc792014-06-16 20:44:29 -07001432
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001433// Conditional branch. A block ending with an HIf instruction must have
1434// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001435class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001436 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001437 explicit HIf(HInstruction* input) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001438 SetRawInputAt(0, input);
1439 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001440
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001441 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001442
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001443 HBasicBlock* IfTrueSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001444 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001445 }
1446
1447 HBasicBlock* IfFalseSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001448 return GetBlock()->GetSuccessors().Get(1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001449 }
1450
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001451 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001452
Dave Allison20dfc792014-06-16 20:44:29 -07001453 virtual bool IsIfInstruction() const { return true; }
1454
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001455 private:
1456 DISALLOW_COPY_AND_ASSIGN(HIf);
1457};
1458
Roland Levillain88cb1752014-10-20 16:36:47 +01001459class HUnaryOperation : public HExpression<1> {
1460 public:
1461 HUnaryOperation(Primitive::Type result_type, HInstruction* input)
1462 : HExpression(result_type, SideEffects::None()) {
1463 SetRawInputAt(0, input);
1464 }
1465
1466 HInstruction* GetInput() const { return InputAt(0); }
1467 Primitive::Type GetResultType() const { return GetType(); }
1468
1469 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001470 virtual bool InstructionDataEquals(HInstruction* other) const {
1471 UNUSED(other);
1472 return true;
1473 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001474
Roland Levillain9240d6a2014-10-20 16:47:04 +01001475 // Try to statically evaluate `operation` and return a HConstant
1476 // containing the result of this evaluation. If `operation` cannot
1477 // be evaluated as a constant, return nullptr.
1478 HConstant* TryStaticEvaluation() const;
1479
1480 // Apply this operation to `x`.
1481 virtual int32_t Evaluate(int32_t x) const = 0;
1482 virtual int64_t Evaluate(int64_t x) const = 0;
1483
Roland Levillain88cb1752014-10-20 16:36:47 +01001484 DECLARE_INSTRUCTION(UnaryOperation);
1485
1486 private:
1487 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
1488};
1489
Dave Allison20dfc792014-06-16 20:44:29 -07001490class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001491 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001492 HBinaryOperation(Primitive::Type result_type,
1493 HInstruction* left,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001494 HInstruction* right) : HExpression(result_type, SideEffects::None()) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001495 SetRawInputAt(0, left);
1496 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001497 }
1498
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001499 HInstruction* GetLeft() const { return InputAt(0); }
1500 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07001501 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001502
Mingyao Yangdc5ac732015-02-25 11:28:05 -08001503 virtual bool IsCommutative() const { return false; }
1504
1505 // Put constant on the right.
1506 // Returns whether order is changed.
1507 bool OrderInputsWithConstantOnTheRight() {
1508 HInstruction* left = InputAt(0);
1509 HInstruction* right = InputAt(1);
1510 if (left->IsConstant() && !right->IsConstant()) {
1511 ReplaceInput(right, 0);
1512 ReplaceInput(left, 1);
1513 return true;
1514 }
1515 return false;
1516 }
1517
1518 // Order inputs by instruction id, but favor constant on the right side.
1519 // This helps GVN for commutative ops.
1520 void OrderInputs() {
1521 DCHECK(IsCommutative());
1522 HInstruction* left = InputAt(0);
1523 HInstruction* right = InputAt(1);
1524 if (left == right || (!left->IsConstant() && right->IsConstant())) {
1525 return;
1526 }
1527 if (OrderInputsWithConstantOnTheRight()) {
1528 return;
1529 }
1530 // Order according to instruction id.
1531 if (left->GetId() > right->GetId()) {
1532 ReplaceInput(right, 0);
1533 ReplaceInput(left, 1);
1534 }
1535 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001536
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001537 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001538 virtual bool InstructionDataEquals(HInstruction* other) const {
1539 UNUSED(other);
1540 return true;
1541 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001542
Roland Levillain9240d6a2014-10-20 16:47:04 +01001543 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01001544 // containing the result of this evaluation. If `operation` cannot
1545 // be evaluated as a constant, return nullptr.
Roland Levillain9240d6a2014-10-20 16:47:04 +01001546 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01001547
1548 // Apply this operation to `x` and `y`.
1549 virtual int32_t Evaluate(int32_t x, int32_t y) const = 0;
1550 virtual int64_t Evaluate(int64_t x, int64_t y) const = 0;
1551
Roland Levillainccc07a92014-09-16 14:48:16 +01001552 DECLARE_INSTRUCTION(BinaryOperation);
1553
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001554 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001555 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
1556};
1557
Dave Allison20dfc792014-06-16 20:44:29 -07001558class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001559 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001560 HCondition(HInstruction* first, HInstruction* second)
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001561 : HBinaryOperation(Primitive::kPrimBoolean, first, second),
1562 needs_materialization_(true) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001563
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001564 bool NeedsMaterialization() const { return needs_materialization_; }
1565 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001566
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001567 // For code generation purposes, returns whether this instruction is just before
1568 // `if_`, and disregard moves in between.
1569 bool IsBeforeWhenDisregardMoves(HIf* if_) const;
1570
Dave Allison20dfc792014-06-16 20:44:29 -07001571 DECLARE_INSTRUCTION(Condition);
1572
1573 virtual IfCondition GetCondition() const = 0;
1574
1575 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001576 // For register allocation purposes, returns whether this instruction needs to be
1577 // materialized (that is, not just be in the processor flags).
1578 bool needs_materialization_;
1579
Dave Allison20dfc792014-06-16 20:44:29 -07001580 DISALLOW_COPY_AND_ASSIGN(HCondition);
1581};
1582
1583// Instruction to check if two inputs are equal to each other.
1584class HEqual : public HCondition {
1585 public:
1586 HEqual(HInstruction* first, HInstruction* second)
1587 : HCondition(first, second) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001588
Mingyao Yangdc5ac732015-02-25 11:28:05 -08001589 bool IsCommutative() const OVERRIDE { return true; }
1590
Roland Levillain93445682014-10-06 19:24:02 +01001591 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1592 return x == y ? 1 : 0;
1593 }
1594 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1595 return x == y ? 1 : 0;
1596 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001597
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001598 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001599
Dave Allison20dfc792014-06-16 20:44:29 -07001600 virtual IfCondition GetCondition() const {
1601 return kCondEQ;
1602 }
1603
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001604 private:
1605 DISALLOW_COPY_AND_ASSIGN(HEqual);
1606};
1607
Dave Allison20dfc792014-06-16 20:44:29 -07001608class HNotEqual : public HCondition {
1609 public:
1610 HNotEqual(HInstruction* first, HInstruction* second)
1611 : HCondition(first, second) {}
1612
Mingyao Yangdc5ac732015-02-25 11:28:05 -08001613 bool IsCommutative() const OVERRIDE { return true; }
1614
Roland Levillain93445682014-10-06 19:24:02 +01001615 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1616 return x != y ? 1 : 0;
1617 }
1618 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1619 return x != y ? 1 : 0;
1620 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001621
Dave Allison20dfc792014-06-16 20:44:29 -07001622 DECLARE_INSTRUCTION(NotEqual);
1623
1624 virtual IfCondition GetCondition() const {
1625 return kCondNE;
1626 }
1627
1628 private:
1629 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
1630};
1631
1632class HLessThan : public HCondition {
1633 public:
1634 HLessThan(HInstruction* first, HInstruction* second)
1635 : HCondition(first, second) {}
1636
Roland Levillain93445682014-10-06 19:24:02 +01001637 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1638 return x < y ? 1 : 0;
1639 }
1640 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1641 return x < y ? 1 : 0;
1642 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001643
Dave Allison20dfc792014-06-16 20:44:29 -07001644 DECLARE_INSTRUCTION(LessThan);
1645
1646 virtual IfCondition GetCondition() const {
1647 return kCondLT;
1648 }
1649
1650 private:
1651 DISALLOW_COPY_AND_ASSIGN(HLessThan);
1652};
1653
1654class HLessThanOrEqual : public HCondition {
1655 public:
1656 HLessThanOrEqual(HInstruction* first, HInstruction* second)
1657 : HCondition(first, second) {}
1658
Roland Levillain93445682014-10-06 19:24:02 +01001659 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1660 return x <= y ? 1 : 0;
1661 }
1662 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1663 return x <= y ? 1 : 0;
1664 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001665
Dave Allison20dfc792014-06-16 20:44:29 -07001666 DECLARE_INSTRUCTION(LessThanOrEqual);
1667
1668 virtual IfCondition GetCondition() const {
1669 return kCondLE;
1670 }
1671
1672 private:
1673 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
1674};
1675
1676class HGreaterThan : public HCondition {
1677 public:
1678 HGreaterThan(HInstruction* first, HInstruction* second)
1679 : HCondition(first, second) {}
1680
Roland Levillain93445682014-10-06 19:24:02 +01001681 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1682 return x > y ? 1 : 0;
1683 }
1684 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1685 return x > y ? 1 : 0;
1686 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001687
Dave Allison20dfc792014-06-16 20:44:29 -07001688 DECLARE_INSTRUCTION(GreaterThan);
1689
1690 virtual IfCondition GetCondition() const {
1691 return kCondGT;
1692 }
1693
1694 private:
1695 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
1696};
1697
1698class HGreaterThanOrEqual : public HCondition {
1699 public:
1700 HGreaterThanOrEqual(HInstruction* first, HInstruction* second)
1701 : HCondition(first, second) {}
1702
Roland Levillain93445682014-10-06 19:24:02 +01001703 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1704 return x >= y ? 1 : 0;
1705 }
1706 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1707 return x >= y ? 1 : 0;
1708 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001709
Dave Allison20dfc792014-06-16 20:44:29 -07001710 DECLARE_INSTRUCTION(GreaterThanOrEqual);
1711
1712 virtual IfCondition GetCondition() const {
1713 return kCondGE;
1714 }
1715
1716 private:
1717 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
1718};
1719
1720
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001721// Instruction to check how two inputs compare to each other.
1722// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
1723class HCompare : public HBinaryOperation {
1724 public:
Calin Juravleddb7df22014-11-25 20:56:51 +00001725 // The bias applies for floating point operations and indicates how NaN
1726 // comparisons are treated:
1727 enum Bias {
1728 kNoBias, // bias is not applicable (i.e. for long operation)
1729 kGtBias, // return 1 for NaN comparisons
1730 kLtBias, // return -1 for NaN comparisons
1731 };
1732
1733 HCompare(Primitive::Type type, HInstruction* first, HInstruction* second, Bias bias)
1734 : HBinaryOperation(Primitive::kPrimInt, first, second), bias_(bias) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001735 DCHECK_EQ(type, first->GetType());
1736 DCHECK_EQ(type, second->GetType());
1737 }
1738
Calin Juravleddb7df22014-11-25 20:56:51 +00001739 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01001740 return
1741 x == y ? 0 :
1742 x > y ? 1 :
1743 -1;
1744 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001745
1746 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01001747 return
1748 x == y ? 0 :
1749 x > y ? 1 :
1750 -1;
1751 }
1752
Calin Juravleddb7df22014-11-25 20:56:51 +00001753 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
1754 return bias_ == other->AsCompare()->bias_;
1755 }
1756
1757 bool IsGtBias() { return bias_ == kGtBias; }
1758
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001759 DECLARE_INSTRUCTION(Compare);
1760
1761 private:
Calin Juravleddb7df22014-11-25 20:56:51 +00001762 const Bias bias_;
1763
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001764 DISALLOW_COPY_AND_ASSIGN(HCompare);
1765};
1766
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001767// A local in the graph. Corresponds to a Dex register.
1768class HLocal : public HTemplateInstruction<0> {
1769 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001770 explicit HLocal(uint16_t reg_number)
1771 : HTemplateInstruction(SideEffects::None()), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001772
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001773 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001774
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001775 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001776
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001777 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001778 // The Dex register number.
1779 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001780
1781 DISALLOW_COPY_AND_ASSIGN(HLocal);
1782};
1783
1784// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07001785class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001786 public:
Roland Levillain5799fc02014-09-25 12:15:20 +01001787 HLoadLocal(HLocal* local, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001788 : HExpression(type, SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001789 SetRawInputAt(0, local);
1790 }
1791
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001792 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1793
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001794 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001795
1796 private:
1797 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
1798};
1799
1800// Store a value in a given local. This instruction has two inputs: the value
1801// and the local.
1802class HStoreLocal : public HTemplateInstruction<2> {
1803 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001804 HStoreLocal(HLocal* local, HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001805 SetRawInputAt(0, local);
1806 SetRawInputAt(1, value);
1807 }
1808
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001809 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1810
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001811 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001812
1813 private:
1814 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
1815};
1816
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001817class HConstant : public HExpression<0> {
1818 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001819 explicit HConstant(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
1820
1821 virtual bool CanBeMoved() const { return true; }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001822
1823 DECLARE_INSTRUCTION(Constant);
1824
1825 private:
1826 DISALLOW_COPY_AND_ASSIGN(HConstant);
1827};
1828
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001829class HFloatConstant : public HConstant {
1830 public:
1831 explicit HFloatConstant(float value) : HConstant(Primitive::kPrimFloat), value_(value) {}
1832
1833 float GetValue() const { return value_; }
1834
1835 virtual bool InstructionDataEquals(HInstruction* other) const {
1836 return bit_cast<float, int32_t>(other->AsFloatConstant()->value_) ==
1837 bit_cast<float, int32_t>(value_);
1838 }
1839
1840 virtual size_t ComputeHashCode() const { return static_cast<size_t>(GetValue()); }
1841
1842 DECLARE_INSTRUCTION(FloatConstant);
1843
1844 private:
1845 const float value_;
1846
1847 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
1848};
1849
1850class HDoubleConstant : public HConstant {
1851 public:
1852 explicit HDoubleConstant(double value) : HConstant(Primitive::kPrimDouble), value_(value) {}
1853
1854 double GetValue() const { return value_; }
1855
1856 virtual bool InstructionDataEquals(HInstruction* other) const {
1857 return bit_cast<double, int64_t>(other->AsDoubleConstant()->value_) ==
1858 bit_cast<double, int64_t>(value_);
1859 }
1860
1861 virtual size_t ComputeHashCode() const { return static_cast<size_t>(GetValue()); }
1862
1863 DECLARE_INSTRUCTION(DoubleConstant);
1864
1865 private:
1866 const double value_;
1867
1868 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
1869};
1870
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001871class HNullConstant : public HConstant {
1872 public:
1873 HNullConstant() : HConstant(Primitive::kPrimNot) {}
1874
1875 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
1876 return true;
1877 }
1878
1879 size_t ComputeHashCode() const OVERRIDE { return 0; }
1880
Calin Juravle61d544b2015-02-23 16:46:57 +00001881 bool ActAsNullConstant() const OVERRIDE { return true; }
1882
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001883 DECLARE_INSTRUCTION(NullConstant);
1884
1885 private:
1886 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
1887};
1888
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001889// Constants of the type int. Those can be from Dex instructions, or
1890// synthesized (for example with the if-eqz instruction).
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001891class HIntConstant : public HConstant {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001892 public:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001893 explicit HIntConstant(int32_t value) : HConstant(Primitive::kPrimInt), value_(value) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001894
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001895 int32_t GetValue() const { return value_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001896
Calin Juravle61d544b2015-02-23 16:46:57 +00001897 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001898 return other->AsIntConstant()->value_ == value_;
1899 }
1900
Calin Juravle61d544b2015-02-23 16:46:57 +00001901 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
1902
1903 // TODO: Null is represented by the `0` constant. In most cases we replace it
1904 // with a HNullConstant but we don't do it when comparing (a != null). This
1905 // method is an workaround until we fix the above.
1906 bool ActAsNullConstant() const OVERRIDE { return value_ == 0; }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001907
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001908 DECLARE_INSTRUCTION(IntConstant);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001909
1910 private:
1911 const int32_t value_;
1912
1913 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
1914};
1915
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001916class HLongConstant : public HConstant {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001917 public:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001918 explicit HLongConstant(int64_t value) : HConstant(Primitive::kPrimLong), value_(value) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001919
1920 int64_t GetValue() const { return value_; }
1921
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001922 virtual bool InstructionDataEquals(HInstruction* other) const {
1923 return other->AsLongConstant()->value_ == value_;
1924 }
1925
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001926 virtual size_t ComputeHashCode() const { return static_cast<size_t>(GetValue()); }
1927
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001928 DECLARE_INSTRUCTION(LongConstant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001929
1930 private:
1931 const int64_t value_;
1932
1933 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
1934};
1935
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001936enum class Intrinsics {
1937#define OPTIMIZING_INTRINSICS(Name, IsStatic) k ## Name,
1938#include "intrinsics_list.h"
1939 kNone,
1940 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
1941#undef INTRINSICS_LIST
1942#undef OPTIMIZING_INTRINSICS
1943};
1944std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
1945
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001946class HInvoke : public HInstruction {
1947 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001948 virtual size_t InputCount() const { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001949
1950 // Runtime needs to walk the stack, so Dex -> Dex calls need to
1951 // know their environment.
Calin Juravle77520bc2015-01-12 18:45:46 +00001952 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001953
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001954 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001955 SetRawInputAt(index, argument);
1956 }
1957
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001958 virtual Primitive::Type GetType() const { return return_type_; }
1959
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001960 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001961
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001962 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
1963
1964 Intrinsics GetIntrinsic() {
1965 return intrinsic_;
1966 }
1967
1968 void SetIntrinsic(Intrinsics intrinsic) {
1969 intrinsic_ = intrinsic;
1970 }
1971
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001972 DECLARE_INSTRUCTION(Invoke);
1973
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001974 protected:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001975 HInvoke(ArenaAllocator* arena,
1976 uint32_t number_of_arguments,
1977 Primitive::Type return_type,
1978 uint32_t dex_pc,
1979 uint32_t dex_method_index)
1980 : HInstruction(SideEffects::All()),
1981 inputs_(arena, number_of_arguments),
1982 return_type_(return_type),
1983 dex_pc_(dex_pc),
1984 dex_method_index_(dex_method_index),
1985 intrinsic_(Intrinsics::kNone) {
1986 inputs_.SetSize(number_of_arguments);
1987 }
1988
David Brazdil1abb4192015-02-17 18:33:36 +00001989 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
1990 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
1991 inputs_.Put(index, input);
1992 }
1993
1994 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001995 const Primitive::Type return_type_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001996 const uint32_t dex_pc_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001997 const uint32_t dex_method_index_;
1998 Intrinsics intrinsic_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001999
2000 private:
2001 DISALLOW_COPY_AND_ASSIGN(HInvoke);
2002};
2003
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002004class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002005 public:
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002006 HInvokeStaticOrDirect(ArenaAllocator* arena,
2007 uint32_t number_of_arguments,
2008 Primitive::Type return_type,
2009 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002010 uint32_t dex_method_index,
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002011 bool is_recursive,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002012 InvokeType invoke_type)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002013 : HInvoke(arena, number_of_arguments, return_type, dex_pc, dex_method_index),
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002014 invoke_type_(invoke_type),
2015 is_recursive_(is_recursive) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002016
Calin Juravle77520bc2015-01-12 18:45:46 +00002017 bool CanDoImplicitNullCheck() const OVERRIDE {
2018 // We access the method via the dex cache so we can't do an implicit null check.
2019 // TODO: for intrinsics we can generate implicit null checks.
2020 return false;
2021 }
2022
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002023 InvokeType GetInvokeType() const { return invoke_type_; }
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002024 bool IsRecursive() const { return is_recursive_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002025
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002026 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002027
2028 private:
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002029 const InvokeType invoke_type_;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002030 const bool is_recursive_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002031
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002032 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002033};
2034
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002035class HInvokeVirtual : public HInvoke {
2036 public:
2037 HInvokeVirtual(ArenaAllocator* arena,
2038 uint32_t number_of_arguments,
2039 Primitive::Type return_type,
2040 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002041 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002042 uint32_t vtable_index)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002043 : HInvoke(arena, number_of_arguments, return_type, dex_pc, dex_method_index),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002044 vtable_index_(vtable_index) {}
2045
Calin Juravle77520bc2015-01-12 18:45:46 +00002046 bool CanDoImplicitNullCheck() const OVERRIDE {
2047 // TODO: Add implicit null checks in intrinsics.
2048 return !GetLocations()->Intrinsified();
2049 }
2050
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002051 uint32_t GetVTableIndex() const { return vtable_index_; }
2052
2053 DECLARE_INSTRUCTION(InvokeVirtual);
2054
2055 private:
2056 const uint32_t vtable_index_;
2057
2058 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
2059};
2060
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002061class HInvokeInterface : public HInvoke {
2062 public:
2063 HInvokeInterface(ArenaAllocator* arena,
2064 uint32_t number_of_arguments,
2065 Primitive::Type return_type,
2066 uint32_t dex_pc,
2067 uint32_t dex_method_index,
2068 uint32_t imt_index)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002069 : HInvoke(arena, number_of_arguments, return_type, dex_pc, dex_method_index),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002070 imt_index_(imt_index) {}
2071
Calin Juravle77520bc2015-01-12 18:45:46 +00002072 bool CanDoImplicitNullCheck() const OVERRIDE {
2073 // TODO: Add implicit null checks in intrinsics.
2074 return !GetLocations()->Intrinsified();
2075 }
2076
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002077 uint32_t GetImtIndex() const { return imt_index_; }
2078 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
2079
2080 DECLARE_INSTRUCTION(InvokeInterface);
2081
2082 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002083 const uint32_t imt_index_;
2084
2085 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
2086};
2087
Dave Allison20dfc792014-06-16 20:44:29 -07002088class HNewInstance : public HExpression<0> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002089 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002090 HNewInstance(uint32_t dex_pc, uint16_t type_index, QuickEntrypointEnum entrypoint)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002091 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2092 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002093 type_index_(type_index),
2094 entrypoint_(entrypoint) {}
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002095
2096 uint32_t GetDexPc() const { return dex_pc_; }
2097 uint16_t GetTypeIndex() const { return type_index_; }
2098
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002099 // Calls runtime so needs an environment.
Calin Juravle92a6ed22014-12-02 18:58:03 +00002100 bool NeedsEnvironment() const OVERRIDE { return true; }
2101 // It may throw when called on:
2102 // - interfaces
2103 // - abstract/innaccessible/unknown classes
2104 // TODO: optimize when possible.
2105 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002106
Calin Juravle10e244f2015-01-26 18:54:32 +00002107 bool CanBeNull() const OVERRIDE { return false; }
2108
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002109 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
2110
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002111 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002112
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 Geoffray2e7038a2014-04-03 18:49:58 +01002117
2118 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
2119};
2120
Roland Levillain88cb1752014-10-20 16:36:47 +01002121class HNeg : public HUnaryOperation {
2122 public:
2123 explicit HNeg(Primitive::Type result_type, HInstruction* input)
2124 : HUnaryOperation(result_type, input) {}
2125
Roland Levillain9240d6a2014-10-20 16:47:04 +01002126 virtual int32_t Evaluate(int32_t x) const OVERRIDE { return -x; }
2127 virtual int64_t Evaluate(int64_t x) const OVERRIDE { return -x; }
2128
Roland Levillain88cb1752014-10-20 16:36:47 +01002129 DECLARE_INSTRUCTION(Neg);
2130
2131 private:
2132 DISALLOW_COPY_AND_ASSIGN(HNeg);
2133};
2134
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002135class HNewArray : public HExpression<1> {
2136 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002137 HNewArray(HInstruction* length,
2138 uint32_t dex_pc,
2139 uint16_t type_index,
2140 QuickEntrypointEnum entrypoint)
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002141 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2142 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002143 type_index_(type_index),
2144 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002145 SetRawInputAt(0, length);
2146 }
2147
2148 uint32_t GetDexPc() const { return dex_pc_; }
2149 uint16_t GetTypeIndex() const { return type_index_; }
2150
2151 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00002152 bool NeedsEnvironment() const OVERRIDE { return true; }
2153
2154 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002155
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002156 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
2157
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002158 DECLARE_INSTRUCTION(NewArray);
2159
2160 private:
2161 const uint32_t dex_pc_;
2162 const uint16_t type_index_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002163 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002164
2165 DISALLOW_COPY_AND_ASSIGN(HNewArray);
2166};
2167
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002168class HAdd : public HBinaryOperation {
2169 public:
2170 HAdd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2171 : HBinaryOperation(result_type, left, right) {}
2172
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002173 virtual bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002174
Roland Levillain93445682014-10-06 19:24:02 +01002175 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
2176 return x + y;
2177 }
2178 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
2179 return x + y;
2180 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002181
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002182 DECLARE_INSTRUCTION(Add);
2183
2184 private:
2185 DISALLOW_COPY_AND_ASSIGN(HAdd);
2186};
2187
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002188class HSub : public HBinaryOperation {
2189 public:
2190 HSub(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2191 : HBinaryOperation(result_type, left, right) {}
2192
Roland Levillain93445682014-10-06 19:24:02 +01002193 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
2194 return x - y;
2195 }
2196 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
2197 return x - y;
2198 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002199
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002200 DECLARE_INSTRUCTION(Sub);
2201
2202 private:
2203 DISALLOW_COPY_AND_ASSIGN(HSub);
2204};
2205
Calin Juravle34bacdf2014-10-07 20:23:36 +01002206class HMul : public HBinaryOperation {
2207 public:
2208 HMul(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2209 : HBinaryOperation(result_type, left, right) {}
2210
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002211 virtual bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002212
2213 virtual int32_t Evaluate(int32_t x, int32_t y) const { return x * y; }
2214 virtual int64_t Evaluate(int64_t x, int64_t y) const { return x * y; }
2215
2216 DECLARE_INSTRUCTION(Mul);
2217
2218 private:
2219 DISALLOW_COPY_AND_ASSIGN(HMul);
2220};
2221
Calin Juravle7c4954d2014-10-28 16:57:40 +00002222class HDiv : public HBinaryOperation {
2223 public:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002224 HDiv(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
2225 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00002226
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00002227 virtual int32_t Evaluate(int32_t x, int32_t y) const {
2228 // Our graph structure ensures we never have 0 for `y` during constant folding.
2229 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00002230 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00002231 return (y == -1) ? -x : x / y;
2232 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002233
2234 virtual int64_t Evaluate(int64_t x, int64_t y) const {
2235 DCHECK_NE(y, 0);
2236 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2237 return (y == -1) ? -x : x / y;
2238 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002239
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002240 uint32_t GetDexPc() const { return dex_pc_; }
2241
Calin Juravle7c4954d2014-10-28 16:57:40 +00002242 DECLARE_INSTRUCTION(Div);
2243
2244 private:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002245 const uint32_t dex_pc_;
2246
Calin Juravle7c4954d2014-10-28 16:57:40 +00002247 DISALLOW_COPY_AND_ASSIGN(HDiv);
2248};
2249
Calin Juravlebacfec32014-11-14 15:54:36 +00002250class HRem : public HBinaryOperation {
2251 public:
2252 HRem(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
2253 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
2254
2255 virtual int32_t Evaluate(int32_t x, int32_t y) const {
2256 DCHECK_NE(y, 0);
2257 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2258 return (y == -1) ? 0 : x % y;
2259 }
2260
2261 virtual int64_t Evaluate(int64_t x, int64_t y) const {
2262 DCHECK_NE(y, 0);
2263 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2264 return (y == -1) ? 0 : x % y;
2265 }
2266
2267 uint32_t GetDexPc() const { return dex_pc_; }
2268
2269 DECLARE_INSTRUCTION(Rem);
2270
2271 private:
2272 const uint32_t dex_pc_;
2273
2274 DISALLOW_COPY_AND_ASSIGN(HRem);
2275};
2276
Calin Juravled0d48522014-11-04 16:40:20 +00002277class HDivZeroCheck : public HExpression<1> {
2278 public:
2279 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
2280 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
2281 SetRawInputAt(0, value);
2282 }
2283
2284 bool CanBeMoved() const OVERRIDE { return true; }
2285
2286 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2287 UNUSED(other);
2288 return true;
2289 }
2290
2291 bool NeedsEnvironment() const OVERRIDE { return true; }
2292 bool CanThrow() const OVERRIDE { return true; }
2293
2294 uint32_t GetDexPc() const { return dex_pc_; }
2295
2296 DECLARE_INSTRUCTION(DivZeroCheck);
2297
2298 private:
2299 const uint32_t dex_pc_;
2300
2301 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
2302};
2303
Calin Juravle9aec02f2014-11-18 23:06:35 +00002304class HShl : public HBinaryOperation {
2305 public:
2306 HShl(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2307 : HBinaryOperation(result_type, left, right) {}
2308
2309 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x << (y & kMaxIntShiftValue); }
2310 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x << (y & kMaxLongShiftValue); }
2311
2312 DECLARE_INSTRUCTION(Shl);
2313
2314 private:
2315 DISALLOW_COPY_AND_ASSIGN(HShl);
2316};
2317
2318class HShr : public HBinaryOperation {
2319 public:
2320 HShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2321 : HBinaryOperation(result_type, left, right) {}
2322
2323 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x >> (y & kMaxIntShiftValue); }
2324 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x >> (y & kMaxLongShiftValue); }
2325
2326 DECLARE_INSTRUCTION(Shr);
2327
2328 private:
2329 DISALLOW_COPY_AND_ASSIGN(HShr);
2330};
2331
2332class HUShr : public HBinaryOperation {
2333 public:
2334 HUShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2335 : HBinaryOperation(result_type, left, right) {}
2336
2337 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
2338 uint32_t ux = static_cast<uint32_t>(x);
2339 uint32_t uy = static_cast<uint32_t>(y) & kMaxIntShiftValue;
2340 return static_cast<int32_t>(ux >> uy);
2341 }
2342
2343 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
2344 uint64_t ux = static_cast<uint64_t>(x);
2345 uint64_t uy = static_cast<uint64_t>(y) & kMaxLongShiftValue;
2346 return static_cast<int64_t>(ux >> uy);
2347 }
2348
2349 DECLARE_INSTRUCTION(UShr);
2350
2351 private:
2352 DISALLOW_COPY_AND_ASSIGN(HUShr);
2353};
2354
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002355class HAnd : public HBinaryOperation {
2356 public:
2357 HAnd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2358 : HBinaryOperation(result_type, left, right) {}
2359
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002360 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002361
2362 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x & y; }
2363 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x & y; }
2364
2365 DECLARE_INSTRUCTION(And);
2366
2367 private:
2368 DISALLOW_COPY_AND_ASSIGN(HAnd);
2369};
2370
2371class HOr : public HBinaryOperation {
2372 public:
2373 HOr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2374 : HBinaryOperation(result_type, left, right) {}
2375
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002376 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002377
2378 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x | y; }
2379 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x | y; }
2380
2381 DECLARE_INSTRUCTION(Or);
2382
2383 private:
2384 DISALLOW_COPY_AND_ASSIGN(HOr);
2385};
2386
2387class HXor : public HBinaryOperation {
2388 public:
2389 HXor(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2390 : HBinaryOperation(result_type, left, right) {}
2391
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002392 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002393
2394 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x ^ y; }
2395 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x ^ y; }
2396
2397 DECLARE_INSTRUCTION(Xor);
2398
2399 private:
2400 DISALLOW_COPY_AND_ASSIGN(HXor);
2401};
2402
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002403// The value of a parameter in this method. Its location depends on
2404// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07002405class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002406 public:
Calin Juravle10e244f2015-01-26 18:54:32 +00002407 HParameterValue(uint8_t index, Primitive::Type parameter_type, bool is_this = false)
2408 : HExpression(parameter_type, SideEffects::None()), index_(index), is_this_(is_this) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002409
2410 uint8_t GetIndex() const { return index_; }
2411
Calin Juravle10e244f2015-01-26 18:54:32 +00002412 bool CanBeNull() const OVERRIDE { return !is_this_; }
2413
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002414 DECLARE_INSTRUCTION(ParameterValue);
2415
2416 private:
2417 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00002418 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002419 const uint8_t index_;
2420
Calin Juravle10e244f2015-01-26 18:54:32 +00002421 // Whether or not the parameter value corresponds to 'this' argument.
2422 const bool is_this_;
2423
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002424 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
2425};
2426
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002427class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002428 public:
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002429 explicit HNot(Primitive::Type result_type, HInstruction* input)
2430 : HUnaryOperation(result_type, input) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002431
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002432 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002433 virtual bool InstructionDataEquals(HInstruction* other) const {
2434 UNUSED(other);
2435 return true;
2436 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002437
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002438 virtual int32_t Evaluate(int32_t x) const OVERRIDE { return ~x; }
2439 virtual int64_t Evaluate(int64_t x) const OVERRIDE { return ~x; }
2440
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002441 DECLARE_INSTRUCTION(Not);
2442
2443 private:
2444 DISALLOW_COPY_AND_ASSIGN(HNot);
2445};
2446
Roland Levillaindff1f282014-11-05 14:15:05 +00002447class HTypeConversion : public HExpression<1> {
2448 public:
2449 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00002450 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
2451 : HExpression(result_type, SideEffects::None()), dex_pc_(dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002452 SetRawInputAt(0, input);
2453 DCHECK_NE(input->GetType(), result_type);
2454 }
2455
2456 HInstruction* GetInput() const { return InputAt(0); }
2457 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
2458 Primitive::Type GetResultType() const { return GetType(); }
2459
Roland Levillain624279f2014-12-04 11:54:28 +00002460 // Required by the x86 and ARM code generators when producing calls
2461 // to the runtime.
2462 uint32_t GetDexPc() const { return dex_pc_; }
2463
Roland Levillaindff1f282014-11-05 14:15:05 +00002464 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00002465 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00002466
2467 DECLARE_INSTRUCTION(TypeConversion);
2468
2469 private:
Roland Levillain624279f2014-12-04 11:54:28 +00002470 const uint32_t dex_pc_;
2471
Roland Levillaindff1f282014-11-05 14:15:05 +00002472 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
2473};
2474
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00002475static constexpr uint32_t kNoRegNumber = -1;
2476
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002477class HPhi : public HInstruction {
2478 public:
2479 HPhi(ArenaAllocator* arena, uint32_t reg_number, size_t number_of_inputs, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002480 : HInstruction(SideEffects::None()),
2481 inputs_(arena, number_of_inputs),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002482 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002483 type_(type),
Calin Juravle10e244f2015-01-26 18:54:32 +00002484 is_live_(false),
2485 can_be_null_(true) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002486 inputs_.SetSize(number_of_inputs);
2487 }
2488
Calin Juravle10e244f2015-01-26 18:54:32 +00002489 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002490
2491 void AddInput(HInstruction* input);
2492
Calin Juravle10e244f2015-01-26 18:54:32 +00002493 Primitive::Type GetType() const OVERRIDE { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002494 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002495
Calin Juravle10e244f2015-01-26 18:54:32 +00002496 bool CanBeNull() const OVERRIDE { return can_be_null_; }
2497 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
2498
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002499 uint32_t GetRegNumber() const { return reg_number_; }
2500
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002501 void SetDead() { is_live_ = false; }
2502 void SetLive() { is_live_ = true; }
2503 bool IsDead() const { return !is_live_; }
2504 bool IsLive() const { return is_live_; }
2505
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002506 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002507
David Brazdil1abb4192015-02-17 18:33:36 +00002508 protected:
2509 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
2510
2511 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
2512 inputs_.Put(index, input);
2513 }
2514
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002515 private:
David Brazdil1abb4192015-02-17 18:33:36 +00002516 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002517 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002518 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002519 bool is_live_;
Calin Juravle10e244f2015-01-26 18:54:32 +00002520 bool can_be_null_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002521
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002522 DISALLOW_COPY_AND_ASSIGN(HPhi);
2523};
2524
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002525class HNullCheck : public HExpression<1> {
2526 public:
2527 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002528 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002529 SetRawInputAt(0, value);
2530 }
2531
Calin Juravle10e244f2015-01-26 18:54:32 +00002532 bool CanBeMoved() const OVERRIDE { return true; }
2533 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002534 UNUSED(other);
2535 return true;
2536 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002537
Calin Juravle10e244f2015-01-26 18:54:32 +00002538 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002539
Calin Juravle10e244f2015-01-26 18:54:32 +00002540 bool CanThrow() const OVERRIDE { return true; }
2541
2542 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01002543
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002544 uint32_t GetDexPc() const { return dex_pc_; }
2545
2546 DECLARE_INSTRUCTION(NullCheck);
2547
2548 private:
2549 const uint32_t dex_pc_;
2550
2551 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
2552};
2553
2554class FieldInfo : public ValueObject {
2555 public:
Calin Juravle52c48962014-12-16 17:02:57 +00002556 FieldInfo(MemberOffset field_offset, Primitive::Type field_type, bool is_volatile)
2557 : field_offset_(field_offset), field_type_(field_type), is_volatile_(is_volatile) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002558
2559 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002560 Primitive::Type GetFieldType() const { return field_type_; }
Calin Juravle52c48962014-12-16 17:02:57 +00002561 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002562
2563 private:
2564 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01002565 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00002566 const bool is_volatile_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002567};
2568
2569class HInstanceFieldGet : public HExpression<1> {
2570 public:
2571 HInstanceFieldGet(HInstruction* value,
2572 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00002573 MemberOffset field_offset,
2574 bool is_volatile)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002575 : HExpression(field_type, SideEffects::DependsOnSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00002576 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002577 SetRawInputAt(0, value);
2578 }
2579
Calin Juravle10c9cbe2014-12-19 10:50:19 +00002580 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002581
2582 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2583 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
2584 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002585 }
2586
Calin Juravle77520bc2015-01-12 18:45:46 +00002587 bool CanDoImplicitNullCheck() const OVERRIDE {
2588 return GetFieldOffset().Uint32Value() < kPageSize;
2589 }
2590
2591 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002592 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
2593 }
2594
Calin Juravle52c48962014-12-16 17:02:57 +00002595 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002596 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002597 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002598 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002599
2600 DECLARE_INSTRUCTION(InstanceFieldGet);
2601
2602 private:
2603 const FieldInfo field_info_;
2604
2605 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
2606};
2607
2608class HInstanceFieldSet : public HTemplateInstruction<2> {
2609 public:
2610 HInstanceFieldSet(HInstruction* object,
2611 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01002612 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00002613 MemberOffset field_offset,
2614 bool is_volatile)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002615 : HTemplateInstruction(SideEffects::ChangesSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00002616 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002617 SetRawInputAt(0, object);
2618 SetRawInputAt(1, value);
2619 }
2620
Calin Juravle77520bc2015-01-12 18:45:46 +00002621 bool CanDoImplicitNullCheck() const OVERRIDE {
2622 return GetFieldOffset().Uint32Value() < kPageSize;
2623 }
2624
Calin Juravle52c48962014-12-16 17:02:57 +00002625 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002626 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002627 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002628 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002629 HInstruction* GetValue() const { return InputAt(1); }
2630
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002631 DECLARE_INSTRUCTION(InstanceFieldSet);
2632
2633 private:
2634 const FieldInfo field_info_;
2635
2636 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
2637};
2638
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002639class HArrayGet : public HExpression<2> {
2640 public:
2641 HArrayGet(HInstruction* array, HInstruction* index, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002642 : HExpression(type, SideEffects::DependsOnSomething()) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002643 SetRawInputAt(0, array);
2644 SetRawInputAt(1, index);
2645 }
2646
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002647 bool CanBeMoved() const OVERRIDE { return true; }
2648 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002649 UNUSED(other);
2650 return true;
2651 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002652 bool CanDoImplicitNullCheck() const OVERRIDE {
2653 // TODO: We can be smarter here.
2654 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
2655 // which generates the implicit null check. There are cases when these can be removed
2656 // to produce better code. If we ever add optimizations to do so we should allow an
2657 // implicit check here (as long as the address falls in the first page).
2658 return false;
2659 }
2660
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002661 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002662
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002663 HInstruction* GetArray() const { return InputAt(0); }
2664 HInstruction* GetIndex() const { return InputAt(1); }
2665
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002666 DECLARE_INSTRUCTION(ArrayGet);
2667
2668 private:
2669 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
2670};
2671
2672class HArraySet : public HTemplateInstruction<3> {
2673 public:
2674 HArraySet(HInstruction* array,
2675 HInstruction* index,
2676 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002677 Primitive::Type expected_component_type,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002678 uint32_t dex_pc)
2679 : HTemplateInstruction(SideEffects::ChangesSomething()),
2680 dex_pc_(dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002681 expected_component_type_(expected_component_type),
2682 needs_type_check_(value->GetType() == Primitive::kPrimNot) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002683 SetRawInputAt(0, array);
2684 SetRawInputAt(1, index);
2685 SetRawInputAt(2, value);
2686 }
2687
Calin Juravle77520bc2015-01-12 18:45:46 +00002688 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002689 // We currently always call a runtime method to catch array store
2690 // exceptions.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002691 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002692 }
2693
Calin Juravle77520bc2015-01-12 18:45:46 +00002694 bool CanDoImplicitNullCheck() const OVERRIDE {
2695 // TODO: Same as for ArrayGet.
2696 return false;
2697 }
2698
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002699 void ClearNeedsTypeCheck() {
2700 needs_type_check_ = false;
2701 }
2702
2703 bool NeedsTypeCheck() const { return needs_type_check_; }
2704
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002705 uint32_t GetDexPc() const { return dex_pc_; }
2706
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002707 HInstruction* GetArray() const { return InputAt(0); }
2708 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002709 HInstruction* GetValue() const { return InputAt(2); }
2710
2711 Primitive::Type GetComponentType() const {
2712 // The Dex format does not type floating point index operations. Since the
2713 // `expected_component_type_` is set during building and can therefore not
2714 // be correct, we also check what is the value type. If it is a floating
2715 // point type, we must use that type.
2716 Primitive::Type value_type = GetValue()->GetType();
2717 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
2718 ? value_type
2719 : expected_component_type_;
2720 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002721
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002722 DECLARE_INSTRUCTION(ArraySet);
2723
2724 private:
2725 const uint32_t dex_pc_;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002726 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002727 bool needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002728
2729 DISALLOW_COPY_AND_ASSIGN(HArraySet);
2730};
2731
2732class HArrayLength : public HExpression<1> {
2733 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002734 explicit HArrayLength(HInstruction* array)
2735 : HExpression(Primitive::kPrimInt, SideEffects::None()) {
2736 // Note that arrays do not change length, so the instruction does not
2737 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002738 SetRawInputAt(0, array);
2739 }
2740
Calin Juravle77520bc2015-01-12 18:45:46 +00002741 bool CanBeMoved() const OVERRIDE { return true; }
2742 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002743 UNUSED(other);
2744 return true;
2745 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002746 bool CanDoImplicitNullCheck() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002747
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002748 DECLARE_INSTRUCTION(ArrayLength);
2749
2750 private:
2751 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
2752};
2753
2754class HBoundsCheck : public HExpression<2> {
2755 public:
2756 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002757 : HExpression(index->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002758 DCHECK(index->GetType() == Primitive::kPrimInt);
2759 SetRawInputAt(0, index);
2760 SetRawInputAt(1, length);
2761 }
2762
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002763 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002764 virtual bool InstructionDataEquals(HInstruction* other) const {
2765 UNUSED(other);
2766 return true;
2767 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002768
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002769 virtual bool NeedsEnvironment() const { return true; }
2770
Roland Levillaine161a2a2014-10-03 12:45:18 +01002771 virtual bool CanThrow() const { return true; }
2772
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002773 uint32_t GetDexPc() const { return dex_pc_; }
2774
2775 DECLARE_INSTRUCTION(BoundsCheck);
2776
2777 private:
2778 const uint32_t dex_pc_;
2779
2780 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
2781};
2782
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002783/**
2784 * Some DEX instructions are folded into multiple HInstructions that need
2785 * to stay live until the last HInstruction. This class
2786 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00002787 * HInstruction stays live. `index` represents the stack location index of the
2788 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002789 */
2790class HTemporary : public HTemplateInstruction<0> {
2791 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002792 explicit HTemporary(size_t index) : HTemplateInstruction(SideEffects::None()), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002793
2794 size_t GetIndex() const { return index_; }
2795
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00002796 Primitive::Type GetType() const OVERRIDE {
2797 // The previous instruction is the one that will be stored in the temporary location.
2798 DCHECK(GetPrevious() != nullptr);
2799 return GetPrevious()->GetType();
2800 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00002801
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002802 DECLARE_INSTRUCTION(Temporary);
2803
2804 private:
2805 const size_t index_;
2806
2807 DISALLOW_COPY_AND_ASSIGN(HTemporary);
2808};
2809
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002810class HSuspendCheck : public HTemplateInstruction<0> {
2811 public:
2812 explicit HSuspendCheck(uint32_t dex_pc)
Nicolas Geoffray9ebc72c2014-09-25 16:33:42 +01002813 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002814
2815 virtual bool NeedsEnvironment() const {
2816 return true;
2817 }
2818
2819 uint32_t GetDexPc() const { return dex_pc_; }
2820
2821 DECLARE_INSTRUCTION(SuspendCheck);
2822
2823 private:
2824 const uint32_t dex_pc_;
2825
2826 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
2827};
2828
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002829/**
2830 * Instruction to load a Class object.
2831 */
2832class HLoadClass : public HExpression<0> {
2833 public:
2834 HLoadClass(uint16_t type_index,
2835 bool is_referrers_class,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002836 uint32_t dex_pc)
2837 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2838 type_index_(type_index),
2839 is_referrers_class_(is_referrers_class),
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002840 dex_pc_(dex_pc),
Calin Juravleb1498f62015-02-16 13:13:29 +00002841 generate_clinit_check_(false),
2842 loaded_class_rti_(ReferenceTypeInfo::CreateTop(/* is_exact */ false)) {}
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002843
2844 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002845
2846 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2847 return other->AsLoadClass()->type_index_ == type_index_;
2848 }
2849
2850 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
2851
2852 uint32_t GetDexPc() const { return dex_pc_; }
2853 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002854 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002855
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002856 bool NeedsEnvironment() const OVERRIDE {
2857 // Will call runtime and load the class if the class is not loaded yet.
2858 // TODO: finer grain decision.
2859 return !is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002860 }
2861
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002862 bool MustGenerateClinitCheck() const {
2863 return generate_clinit_check_;
2864 }
2865
2866 void SetMustGenerateClinitCheck() {
2867 generate_clinit_check_ = true;
2868 }
2869
2870 bool CanCallRuntime() const {
2871 return MustGenerateClinitCheck() || !is_referrers_class_;
2872 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002873
Nicolas Geoffray82091da2015-01-26 10:02:45 +00002874 bool CanThrow() const OVERRIDE {
2875 // May call runtime and and therefore can throw.
2876 // TODO: finer grain decision.
2877 return !is_referrers_class_;
2878 }
2879
Calin Juravleacf735c2015-02-12 15:25:22 +00002880 ReferenceTypeInfo GetLoadedClassRTI() {
2881 return loaded_class_rti_;
2882 }
2883
2884 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
2885 // Make sure we only set exact types (the loaded class should never be merged).
2886 DCHECK(rti.IsExact());
2887 loaded_class_rti_ = rti;
2888 }
2889
2890 bool IsResolved() {
2891 return loaded_class_rti_.IsExact();
2892 }
2893
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002894 DECLARE_INSTRUCTION(LoadClass);
2895
2896 private:
2897 const uint16_t type_index_;
2898 const bool is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002899 const uint32_t dex_pc_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002900 // Whether this instruction must generate the initialization check.
2901 // Used for code generation.
2902 bool generate_clinit_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002903
Calin Juravleacf735c2015-02-12 15:25:22 +00002904 ReferenceTypeInfo loaded_class_rti_;
2905
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002906 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
2907};
2908
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002909class HLoadString : public HExpression<0> {
2910 public:
2911 HLoadString(uint32_t string_index, uint32_t dex_pc)
2912 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2913 string_index_(string_index),
2914 dex_pc_(dex_pc) {}
2915
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002916 bool CanBeMoved() const OVERRIDE { return true; }
2917
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002918 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2919 return other->AsLoadString()->string_index_ == string_index_;
2920 }
2921
2922 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
2923
2924 uint32_t GetDexPc() const { return dex_pc_; }
2925 uint32_t GetStringIndex() const { return string_index_; }
2926
2927 // TODO: Can we deopt or debug when we resolve a string?
2928 bool NeedsEnvironment() const OVERRIDE { return false; }
2929
2930 DECLARE_INSTRUCTION(LoadString);
2931
2932 private:
2933 const uint32_t string_index_;
2934 const uint32_t dex_pc_;
2935
2936 DISALLOW_COPY_AND_ASSIGN(HLoadString);
2937};
2938
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002939// TODO: Pass this check to HInvokeStaticOrDirect nodes.
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002940/**
2941 * Performs an initialization check on its Class object input.
2942 */
2943class HClinitCheck : public HExpression<1> {
2944 public:
2945 explicit HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
2946 : HExpression(Primitive::kPrimNot, SideEffects::All()),
2947 dex_pc_(dex_pc) {
2948 SetRawInputAt(0, constant);
2949 }
2950
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002951 bool CanBeMoved() const OVERRIDE { return true; }
2952 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2953 UNUSED(other);
2954 return true;
2955 }
2956
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002957 bool NeedsEnvironment() const OVERRIDE {
2958 // May call runtime to initialize the class.
2959 return true;
2960 }
2961
2962 uint32_t GetDexPc() const { return dex_pc_; }
2963
2964 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
2965
2966 DECLARE_INSTRUCTION(ClinitCheck);
2967
2968 private:
2969 const uint32_t dex_pc_;
2970
2971 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
2972};
2973
2974class HStaticFieldGet : public HExpression<1> {
2975 public:
2976 HStaticFieldGet(HInstruction* cls,
2977 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00002978 MemberOffset field_offset,
2979 bool is_volatile)
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002980 : HExpression(field_type, SideEffects::DependsOnSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00002981 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002982 SetRawInputAt(0, cls);
2983 }
2984
Calin Juravle52c48962014-12-16 17:02:57 +00002985
Calin Juravle10c9cbe2014-12-19 10:50:19 +00002986 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002987
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002988 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00002989 HStaticFieldGet* other_get = other->AsStaticFieldGet();
2990 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002991 }
2992
2993 size_t ComputeHashCode() const OVERRIDE {
2994 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
2995 }
2996
Calin Juravle52c48962014-12-16 17:02:57 +00002997 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002998 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
2999 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003000 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003001
3002 DECLARE_INSTRUCTION(StaticFieldGet);
3003
3004 private:
3005 const FieldInfo field_info_;
3006
3007 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
3008};
3009
3010class HStaticFieldSet : public HTemplateInstruction<2> {
3011 public:
3012 HStaticFieldSet(HInstruction* cls,
3013 HInstruction* value,
3014 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003015 MemberOffset field_offset,
3016 bool is_volatile)
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003017 : HTemplateInstruction(SideEffects::ChangesSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00003018 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003019 SetRawInputAt(0, cls);
3020 SetRawInputAt(1, value);
3021 }
3022
Calin Juravle52c48962014-12-16 17:02:57 +00003023 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003024 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
3025 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003026 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003027
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003028 HInstruction* GetValue() const { return InputAt(1); }
3029
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003030 DECLARE_INSTRUCTION(StaticFieldSet);
3031
3032 private:
3033 const FieldInfo field_info_;
3034
3035 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
3036};
3037
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003038// Implement the move-exception DEX instruction.
3039class HLoadException : public HExpression<0> {
3040 public:
3041 HLoadException() : HExpression(Primitive::kPrimNot, SideEffects::None()) {}
3042
3043 DECLARE_INSTRUCTION(LoadException);
3044
3045 private:
3046 DISALLOW_COPY_AND_ASSIGN(HLoadException);
3047};
3048
3049class HThrow : public HTemplateInstruction<1> {
3050 public:
3051 HThrow(HInstruction* exception, uint32_t dex_pc)
3052 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {
3053 SetRawInputAt(0, exception);
3054 }
3055
3056 bool IsControlFlow() const OVERRIDE { return true; }
3057
3058 bool NeedsEnvironment() const OVERRIDE { return true; }
3059
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003060 bool CanThrow() const OVERRIDE { return true; }
3061
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003062 uint32_t GetDexPc() const { return dex_pc_; }
3063
3064 DECLARE_INSTRUCTION(Throw);
3065
3066 private:
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003067 const uint32_t dex_pc_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003068
3069 DISALLOW_COPY_AND_ASSIGN(HThrow);
3070};
3071
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003072class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003073 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003074 HInstanceOf(HInstruction* object,
3075 HLoadClass* constant,
3076 bool class_is_final,
3077 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003078 : HExpression(Primitive::kPrimBoolean, SideEffects::None()),
3079 class_is_final_(class_is_final),
3080 dex_pc_(dex_pc) {
3081 SetRawInputAt(0, object);
3082 SetRawInputAt(1, constant);
3083 }
3084
3085 bool CanBeMoved() const OVERRIDE { return true; }
3086
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003087 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003088 return true;
3089 }
3090
3091 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003092 return false;
3093 }
3094
3095 uint32_t GetDexPc() const { return dex_pc_; }
3096
3097 bool IsClassFinal() const { return class_is_final_; }
3098
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003099 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003100
3101 private:
3102 const bool class_is_final_;
3103 const uint32_t dex_pc_;
3104
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003105 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
3106};
3107
Calin Juravleb1498f62015-02-16 13:13:29 +00003108class HBoundType : public HExpression<1> {
3109 public:
3110 HBoundType(HInstruction* input, ReferenceTypeInfo bound_type)
3111 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3112 bound_type_(bound_type) {
Calin Juravle61d544b2015-02-23 16:46:57 +00003113 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00003114 SetRawInputAt(0, input);
3115 }
3116
3117 const ReferenceTypeInfo& GetBoundType() const { return bound_type_; }
3118
3119 bool CanBeNull() const OVERRIDE {
3120 // `null instanceof ClassX` always return false so we can't be null.
3121 return false;
3122 }
3123
3124 DECLARE_INSTRUCTION(BoundType);
3125
3126 private:
3127 // Encodes the most upper class that this instruction can have. In other words
3128 // it is always the case that GetBoundType().IsSupertypeOf(GetReferenceType()).
3129 // It is used to bound the type in cases like `if (x instanceof ClassX) {}`
3130 const ReferenceTypeInfo bound_type_;
3131
3132 DISALLOW_COPY_AND_ASSIGN(HBoundType);
3133};
3134
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003135class HCheckCast : public HTemplateInstruction<2> {
3136 public:
3137 HCheckCast(HInstruction* object,
3138 HLoadClass* constant,
3139 bool class_is_final,
3140 uint32_t dex_pc)
3141 : HTemplateInstruction(SideEffects::None()),
3142 class_is_final_(class_is_final),
3143 dex_pc_(dex_pc) {
3144 SetRawInputAt(0, object);
3145 SetRawInputAt(1, constant);
3146 }
3147
3148 bool CanBeMoved() const OVERRIDE { return true; }
3149
3150 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
3151 return true;
3152 }
3153
3154 bool NeedsEnvironment() const OVERRIDE {
3155 // Instruction may throw a CheckCastError.
3156 return true;
3157 }
3158
3159 bool CanThrow() const OVERRIDE { return true; }
3160
3161 uint32_t GetDexPc() const { return dex_pc_; }
3162
3163 bool IsClassFinal() const { return class_is_final_; }
3164
3165 DECLARE_INSTRUCTION(CheckCast);
3166
3167 private:
3168 const bool class_is_final_;
3169 const uint32_t dex_pc_;
3170
3171 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003172};
3173
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003174class HMonitorOperation : public HTemplateInstruction<1> {
3175 public:
3176 enum OperationKind {
3177 kEnter,
3178 kExit,
3179 };
3180
3181 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
3182 : HTemplateInstruction(SideEffects::None()), kind_(kind), dex_pc_(dex_pc) {
3183 SetRawInputAt(0, object);
3184 }
3185
3186 // Instruction may throw a Java exception, so we need an environment.
3187 bool NeedsEnvironment() const OVERRIDE { return true; }
3188 bool CanThrow() const OVERRIDE { return true; }
3189
3190 uint32_t GetDexPc() const { return dex_pc_; }
3191
3192 bool IsEnter() const { return kind_ == kEnter; }
3193
3194 DECLARE_INSTRUCTION(MonitorOperation);
3195
Calin Juravle52c48962014-12-16 17:02:57 +00003196 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003197 const OperationKind kind_;
3198 const uint32_t dex_pc_;
3199
3200 private:
3201 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
3202};
3203
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003204class MoveOperands : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003205 public:
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003206 MoveOperands(Location source, Location destination, HInstruction* instruction)
3207 : source_(source), destination_(destination), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003208
3209 Location GetSource() const { return source_; }
3210 Location GetDestination() const { return destination_; }
3211
3212 void SetSource(Location value) { source_ = value; }
3213 void SetDestination(Location value) { destination_ = value; }
3214
3215 // The parallel move resolver marks moves as "in-progress" by clearing the
3216 // destination (but not the source).
3217 Location MarkPending() {
3218 DCHECK(!IsPending());
3219 Location dest = destination_;
3220 destination_ = Location::NoLocation();
3221 return dest;
3222 }
3223
3224 void ClearPending(Location dest) {
3225 DCHECK(IsPending());
3226 destination_ = dest;
3227 }
3228
3229 bool IsPending() const {
3230 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
3231 return destination_.IsInvalid() && !source_.IsInvalid();
3232 }
3233
3234 // True if this blocks a move from the given location.
3235 bool Blocks(Location loc) const {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003236 return !IsEliminated() && (source_.Contains(loc) || loc.Contains(source_));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003237 }
3238
3239 // A move is redundant if it's been eliminated, if its source and
3240 // destination are the same, or if its destination is unneeded.
3241 bool IsRedundant() const {
3242 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
3243 }
3244
3245 // We clear both operands to indicate move that's been eliminated.
3246 void Eliminate() {
3247 source_ = destination_ = Location::NoLocation();
3248 }
3249
3250 bool IsEliminated() const {
3251 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
3252 return source_.IsInvalid();
3253 }
3254
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003255 HInstruction* GetInstruction() const { return instruction_; }
3256
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003257 private:
3258 Location source_;
3259 Location destination_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003260 // The instruction this move is assocatied with. Null when this move is
3261 // for moving an input in the expected locations of user (including a phi user).
3262 // This is only used in debug mode, to ensure we do not connect interval siblings
3263 // in the same parallel move.
3264 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003265};
3266
3267static constexpr size_t kDefaultNumberOfMoves = 4;
3268
3269class HParallelMove : public HTemplateInstruction<0> {
3270 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003271 explicit HParallelMove(ArenaAllocator* arena)
3272 : HTemplateInstruction(SideEffects::None()), moves_(arena, kDefaultNumberOfMoves) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003273
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003274 void AddMove(Location source, Location destination, HInstruction* instruction) {
3275 DCHECK(source.IsValid());
3276 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003277 if (kIsDebugBuild) {
3278 if (instruction != nullptr) {
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00003279 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003280 DCHECK_NE(moves_.Get(i).GetInstruction(), instruction)
3281 << "Doing parallel moves for the same instruction.";
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00003282 }
3283 }
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003284 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
3285 DCHECK(!destination.Equals(moves_.Get(i).GetDestination()))
3286 << "Same destination for two moves in a parallel move.";
3287 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003288 }
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003289 moves_.Add(MoveOperands(source, destination, instruction));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003290 }
3291
3292 MoveOperands* MoveOperandsAt(size_t index) const {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003293 return moves_.GetRawStorage() + index;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003294 }
3295
3296 size_t NumMoves() const { return moves_.Size(); }
3297
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003298 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003299
3300 private:
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003301 GrowableArray<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003302
3303 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
3304};
3305
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003306class HGraphVisitor : public ValueObject {
3307 public:
Dave Allison20dfc792014-06-16 20:44:29 -07003308 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
3309 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003310
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003311 virtual void VisitInstruction(HInstruction* instruction) { UNUSED(instruction); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003312 virtual void VisitBasicBlock(HBasicBlock* block);
3313
Roland Levillain633021e2014-10-01 14:12:25 +01003314 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003315 void VisitInsertionOrder();
3316
Roland Levillain633021e2014-10-01 14:12:25 +01003317 // Visit the graph following dominator tree reverse post-order.
3318 void VisitReversePostOrder();
3319
Nicolas Geoffray787c3072014-03-17 10:20:19 +00003320 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00003321
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003322 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003323#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003324 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
3325
3326 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
3327
3328#undef DECLARE_VISIT_INSTRUCTION
3329
3330 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07003331 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003332
3333 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
3334};
3335
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003336class HGraphDelegateVisitor : public HGraphVisitor {
3337 public:
3338 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
3339 virtual ~HGraphDelegateVisitor() {}
3340
3341 // Visit functions that delegate to to super class.
3342#define DECLARE_VISIT_INSTRUCTION(name, super) \
3343 virtual void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
3344
3345 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
3346
3347#undef DECLARE_VISIT_INSTRUCTION
3348
3349 private:
3350 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
3351};
3352
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003353class HInsertionOrderIterator : public ValueObject {
3354 public:
3355 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
3356
3357 bool Done() const { return index_ == graph_.GetBlocks().Size(); }
3358 HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); }
3359 void Advance() { ++index_; }
3360
3361 private:
3362 const HGraph& graph_;
3363 size_t index_;
3364
3365 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
3366};
3367
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003368class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003369 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003370 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003371
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003372 bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); }
3373 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003374 void Advance() { ++index_; }
3375
3376 private:
3377 const HGraph& graph_;
3378 size_t index_;
3379
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003380 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003381};
3382
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003383class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003384 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003385 explicit HPostOrderIterator(const HGraph& graph)
3386 : graph_(graph), index_(graph_.GetReversePostOrder().Size()) {}
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003387
3388 bool Done() const { return index_ == 0; }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003389 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003390 void Advance() { --index_; }
3391
3392 private:
3393 const HGraph& graph_;
3394 size_t index_;
3395
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003396 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003397};
3398
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003399// Iterator over the blocks that art part of the loop. Includes blocks part
3400// of an inner loop. The order in which the blocks are iterated is on their
3401// block id.
3402class HBlocksInLoopIterator : public ValueObject {
3403 public:
3404 explicit HBlocksInLoopIterator(const HLoopInformation& info)
3405 : blocks_in_loop_(info.GetBlocks()),
3406 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
3407 index_(0) {
3408 if (!blocks_in_loop_.IsBitSet(index_)) {
3409 Advance();
3410 }
3411 }
3412
3413 bool Done() const { return index_ == blocks_.Size(); }
3414 HBasicBlock* Current() const { return blocks_.Get(index_); }
3415 void Advance() {
3416 ++index_;
3417 for (size_t e = blocks_.Size(); index_ < e; ++index_) {
3418 if (blocks_in_loop_.IsBitSet(index_)) {
3419 break;
3420 }
3421 }
3422 }
3423
3424 private:
3425 const BitVector& blocks_in_loop_;
3426 const GrowableArray<HBasicBlock*>& blocks_;
3427 size_t index_;
3428
3429 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
3430};
3431
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003432} // namespace art
3433
3434#endif // ART_COMPILER_OPTIMIZING_NODES_H_