blob: 86c36b831381c39822f3eab7f6ad681ad8ce8a62 [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
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +010020#include "locations.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010021#include "offsets.h"
Ian Rogerse63db272014-07-15 15:36:11 -070022#include "primitive.h"
Ian Rogers0279ebb2014-10-08 17:27:48 -070023#include "utils/arena_object.h"
Nicolas Geoffray0e336432014-02-26 18:24:38 +000024#include "utils/arena_bit_vector.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000025#include "utils/growable_array.h"
26
27namespace art {
28
29class HBasicBlock;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010030class HEnvironment;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000031class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000032class HIntConstant;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000033class HGraphVisitor;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010034class HPhi;
Nicolas Geoffray3c049742014-09-24 18:10:46 +010035class HSuspendCheck;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010036class LiveInterval;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000037class LocationSummary;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000038
39static const int kDefaultNumberOfBlocks = 8;
40static const int kDefaultNumberOfSuccessors = 2;
41static const int kDefaultNumberOfPredecessors = 2;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010042static const int kDefaultNumberOfDominatedBlocks = 1;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000043static const int kDefaultNumberOfBackEdges = 1;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000044
Dave Allison20dfc792014-06-16 20:44:29 -070045enum IfCondition {
46 kCondEQ,
47 kCondNE,
48 kCondLT,
49 kCondLE,
50 kCondGT,
51 kCondGE,
52};
53
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010054class HInstructionList {
55 public:
56 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
57
58 void AddInstruction(HInstruction* instruction);
59 void RemoveInstruction(HInstruction* instruction);
60
Roland Levillain6b469232014-09-25 10:10:38 +010061 // Return true if this list contains `instruction`.
62 bool Contains(HInstruction* instruction) const;
63
Roland Levillainccc07a92014-09-16 14:48:16 +010064 // Return true if `instruction1` is found before `instruction2` in
65 // this instruction list and false otherwise. Abort if none
66 // of these instructions is found.
67 bool FoundBefore(const HInstruction* instruction1,
68 const HInstruction* instruction2) const;
69
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010070 private:
71 HInstruction* first_instruction_;
72 HInstruction* last_instruction_;
73
74 friend class HBasicBlock;
75 friend class HInstructionIterator;
Nicolas Geoffray804d0932014-05-02 08:46:00 +010076 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010077
78 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
79};
80
Nicolas Geoffray818f2102014-02-18 16:43:35 +000081// Control-flow graph of a method. Contains a list of basic blocks.
82class HGraph : public ArenaObject {
83 public:
84 explicit HGraph(ArenaAllocator* arena)
85 : arena_(arena),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000086 blocks_(arena, kDefaultNumberOfBlocks),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +010087 reverse_post_order_(arena, kDefaultNumberOfBlocks),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +010088 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +010089 number_of_vregs_(0),
90 number_of_in_vregs_(0),
Nicolas Geoffraye5038322014-07-04 09:41:32 +010091 number_of_temporaries_(0),
Dave Allison20dfc792014-06-16 20:44:29 -070092 current_instruction_id_(0) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +000093
Nicolas Geoffray787c3072014-03-17 10:20:19 +000094 ArenaAllocator* GetArena() const { return arena_; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +010095 const GrowableArray<HBasicBlock*>& GetBlocks() const { return blocks_; }
Roland Levillain93445682014-10-06 19:24:02 +010096 HBasicBlock* GetBlock(size_t id) const { return blocks_.Get(id); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +000097
Nicolas Geoffray787c3072014-03-17 10:20:19 +000098 HBasicBlock* GetEntryBlock() const { return entry_block_; }
99 HBasicBlock* GetExitBlock() const { return exit_block_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000100
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000101 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
102 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000103
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000104 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100105
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000106 void BuildDominatorTree();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100107 void TransformToSSA();
108 void SimplifyCFG();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000109
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100110 // Find all natural loops in this graph. Aborts computation and returns false
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100111 // if one loop is not natural, that is the header does not dominate the back
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100112 // edge.
113 bool FindNaturalLoops() const;
114
115 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
116 void SimplifyLoop(HBasicBlock* header);
117
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000118 int GetNextInstructionId() {
119 return current_instruction_id_++;
120 }
121
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100122 uint16_t GetMaximumNumberOfOutVRegs() const {
123 return maximum_number_of_out_vregs_;
124 }
125
126 void UpdateMaximumNumberOfOutVRegs(uint16_t new_value) {
127 maximum_number_of_out_vregs_ = std::max(new_value, maximum_number_of_out_vregs_);
128 }
129
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100130 void UpdateNumberOfTemporaries(size_t count) {
131 number_of_temporaries_ = std::max(count, number_of_temporaries_);
132 }
133
134 size_t GetNumberOfTemporaries() const {
135 return number_of_temporaries_;
136 }
137
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100138 void SetNumberOfVRegs(uint16_t number_of_vregs) {
139 number_of_vregs_ = number_of_vregs;
140 }
141
142 uint16_t GetNumberOfVRegs() const {
143 return number_of_vregs_;
144 }
145
146 void SetNumberOfInVRegs(uint16_t value) {
147 number_of_in_vregs_ = value;
148 }
149
150 uint16_t GetNumberOfInVRegs() const {
151 return number_of_in_vregs_;
152 }
153
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100154 uint16_t GetNumberOfLocalVRegs() const {
155 return number_of_vregs_ - number_of_in_vregs_;
156 }
157
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100158 const GrowableArray<HBasicBlock*>& GetReversePostOrder() const {
159 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100160 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100161
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000162 private:
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000163 HBasicBlock* FindCommonDominator(HBasicBlock* first, HBasicBlock* second) const;
164 void VisitBlockForDominatorTree(HBasicBlock* block,
165 HBasicBlock* predecessor,
166 GrowableArray<size_t>* visits);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100167 void FindBackEdges(ArenaBitVector* visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000168 void VisitBlockForBackEdges(HBasicBlock* block,
169 ArenaBitVector* visited,
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100170 ArenaBitVector* visiting);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000171 void RemoveDeadBlocks(const ArenaBitVector& visited) const;
172
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000173 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000174
175 // List of blocks in insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000176 GrowableArray<HBasicBlock*> blocks_;
177
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100178 // List of blocks to perform a reverse post order tree traversal.
179 GrowableArray<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000180
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000181 HBasicBlock* entry_block_;
182 HBasicBlock* exit_block_;
183
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100184 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100185 uint16_t maximum_number_of_out_vregs_;
186
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100187 // The number of virtual registers in this method. Contains the parameters.
188 uint16_t number_of_vregs_;
189
190 // The number of virtual registers used by parameters of this method.
191 uint16_t number_of_in_vregs_;
192
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100193 // The number of temporaries that will be needed for the baseline compiler.
194 size_t number_of_temporaries_;
195
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000196 // The current id to assign to a newly added instruction. See HInstruction.id_.
197 int current_instruction_id_;
198
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000199 DISALLOW_COPY_AND_ASSIGN(HGraph);
200};
201
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000202class HLoopInformation : public ArenaObject {
203 public:
204 HLoopInformation(HBasicBlock* header, HGraph* graph)
205 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100206 suspend_check_(nullptr),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100207 back_edges_(graph->GetArena(), kDefaultNumberOfBackEdges),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100208 // Make bit vector growable, as the number of blocks may change.
209 blocks_(graph->GetArena(), graph->GetBlocks().Size(), true) {}
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100210
211 HBasicBlock* GetHeader() const {
212 return header_;
213 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000214
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100215 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
216 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
217 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
218
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000219 void AddBackEdge(HBasicBlock* back_edge) {
220 back_edges_.Add(back_edge);
221 }
222
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100223 void RemoveBackEdge(HBasicBlock* back_edge) {
224 back_edges_.Delete(back_edge);
225 }
226
227 bool IsBackEdge(HBasicBlock* block) {
228 for (size_t i = 0, e = back_edges_.Size(); i < e; ++i) {
229 if (back_edges_.Get(i) == block) return true;
230 }
231 return false;
232 }
233
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000234 int NumberOfBackEdges() const {
235 return back_edges_.Size();
236 }
237
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100238 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100239
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100240 const GrowableArray<HBasicBlock*>& GetBackEdges() const {
241 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100242 }
243
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100244 void ClearBackEdges() {
245 back_edges_.Reset();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100246 }
247
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100248 // Find blocks that are part of this loop. Returns whether the loop is a natural loop,
249 // that is the header dominates the back edge.
250 bool Populate();
251
252 // Returns whether this loop information contains `block`.
253 // Note that this loop information *must* be populated before entering this function.
254 bool Contains(const HBasicBlock& block) const;
255
256 // Returns whether this loop information is an inner loop of `other`.
257 // Note that `other` *must* be populated before entering this function.
258 bool IsIn(const HLoopInformation& other) const;
259
260 const ArenaBitVector& GetBlocks() const { return blocks_; }
261
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000262 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100263 // Internal recursive implementation of `Populate`.
264 void PopulateRecursive(HBasicBlock* block);
265
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000266 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100267 HSuspendCheck* suspend_check_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000268 GrowableArray<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100269 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000270
271 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
272};
273
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100274static constexpr size_t kNoLifetime = -1;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100275static constexpr uint32_t kNoDexPc = -1;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100276
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000277// A block in a method. Contains the list of instructions represented
278// as a double linked list. Each block knows its predecessors and
279// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100280
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000281class HBasicBlock : public ArenaObject {
282 public:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100283 explicit HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000284 : graph_(graph),
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000285 predecessors_(graph->GetArena(), kDefaultNumberOfPredecessors),
286 successors_(graph->GetArena(), kDefaultNumberOfSuccessors),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000287 loop_information_(nullptr),
288 dominator_(nullptr),
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100289 dominated_blocks_(graph->GetArena(), kDefaultNumberOfDominatedBlocks),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100290 block_id_(-1),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100291 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100292 lifetime_start_(kNoLifetime),
293 lifetime_end_(kNoLifetime) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000294
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100295 const GrowableArray<HBasicBlock*>& GetPredecessors() const {
296 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000297 }
298
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100299 const GrowableArray<HBasicBlock*>& GetSuccessors() const {
300 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000301 }
302
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100303 const GrowableArray<HBasicBlock*>& GetDominatedBlocks() const {
304 return dominated_blocks_;
305 }
306
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100307 bool IsEntryBlock() const {
308 return graph_->GetEntryBlock() == this;
309 }
310
311 bool IsExitBlock() const {
312 return graph_->GetExitBlock() == this;
313 }
314
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000315 void AddBackEdge(HBasicBlock* back_edge) {
316 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000317 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000318 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100319 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000320 loop_information_->AddBackEdge(back_edge);
321 }
322
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000323 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000324
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000325 int GetBlockId() const { return block_id_; }
326 void SetBlockId(int id) { block_id_ = id; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000327
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000328 HBasicBlock* GetDominator() const { return dominator_; }
329 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100330 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.Add(block); }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000331
332 int NumberOfBackEdges() const {
333 return loop_information_ == nullptr
334 ? 0
335 : loop_information_->NumberOfBackEdges();
336 }
337
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100338 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
339 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100340 const HInstructionList& GetInstructions() const { return instructions_; }
341 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100342 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000343
344 void AddSuccessor(HBasicBlock* block) {
345 successors_.Add(block);
346 block->predecessors_.Add(this);
347 }
348
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100349 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
350 size_t successor_index = GetSuccessorIndexOf(existing);
351 DCHECK_NE(successor_index, static_cast<size_t>(-1));
352 existing->RemovePredecessor(this);
353 new_block->predecessors_.Add(this);
354 successors_.Put(successor_index, new_block);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000355 }
356
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100357 void RemovePredecessor(HBasicBlock* block) {
358 predecessors_.Delete(block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100359 }
360
361 void ClearAllPredecessors() {
362 predecessors_.Reset();
363 }
364
365 void AddPredecessor(HBasicBlock* block) {
366 predecessors_.Add(block);
367 block->successors_.Add(this);
368 }
369
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100370 void SwapPredecessors() {
Nicolas Geoffrayc83d4412014-09-18 16:46:20 +0100371 DCHECK_EQ(predecessors_.Size(), 2u);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100372 HBasicBlock* temp = predecessors_.Get(0);
373 predecessors_.Put(0, predecessors_.Get(1));
374 predecessors_.Put(1, temp);
375 }
376
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100377 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) {
378 for (size_t i = 0, e = predecessors_.Size(); i < e; ++i) {
379 if (predecessors_.Get(i) == predecessor) {
380 return i;
381 }
382 }
383 return -1;
384 }
385
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100386 size_t GetSuccessorIndexOf(HBasicBlock* successor) {
387 for (size_t i = 0, e = successors_.Size(); i < e; ++i) {
388 if (successors_.Get(i) == successor) {
389 return i;
390 }
391 }
392 return -1;
393 }
394
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000395 void AddInstruction(HInstruction* instruction);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100396 void RemoveInstruction(HInstruction* instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100397 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +0100398 // Replace instruction `initial` with `replacement` within this block.
399 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
400 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100401 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100402 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100403 void RemovePhi(HPhi* phi);
404
405 bool IsLoopHeader() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100406 return (loop_information_ != nullptr) && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100407 }
408
Roland Levillain6b879dd2014-09-22 17:13:44 +0100409 bool IsLoopPreHeaderFirstPredecessor() const {
410 DCHECK(IsLoopHeader());
411 DCHECK(!GetPredecessors().IsEmpty());
412 return GetPredecessors().Get(0) == GetLoopInformation()->GetPreHeader();
413 }
414
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100415 HLoopInformation* GetLoopInformation() const {
416 return loop_information_;
417 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000418
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100419 // Set the loop_information_ on this block. This method overrides the current
420 // loop_information if it is an outer loop of the passed loop information.
421 void SetInLoop(HLoopInformation* info) {
422 if (IsLoopHeader()) {
423 // Nothing to do. This just means `info` is an outer loop.
424 } else if (loop_information_ == nullptr) {
425 loop_information_ = info;
426 } else if (loop_information_->Contains(*info->GetHeader())) {
427 // Block is currently part of an outer loop. Make it part of this inner loop.
428 // Note that a non loop header having a loop information means this loop information
429 // has already been populated
430 loop_information_ = info;
431 } else {
432 // Block is part of an inner loop. Do not update the loop information.
433 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
434 // at this point, because this method is being called while populating `info`.
435 }
436 }
437
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100438 bool IsInLoop() const { return loop_information_ != nullptr; }
439
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100440 // Returns wheter this block dominates the blocked passed as parameter.
441 bool Dominates(HBasicBlock* block) const;
442
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100443 size_t GetLifetimeStart() const { return lifetime_start_; }
444 size_t GetLifetimeEnd() const { return lifetime_end_; }
445
446 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
447 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
448
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100449 uint32_t GetDexPc() const { return dex_pc_; }
450
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000451 private:
452 HGraph* const graph_;
453 GrowableArray<HBasicBlock*> predecessors_;
454 GrowableArray<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100455 HInstructionList instructions_;
456 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000457 HLoopInformation* loop_information_;
458 HBasicBlock* dominator_;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100459 GrowableArray<HBasicBlock*> dominated_blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000460 int block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100461 // The dex program counter of the first instruction of this block.
462 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100463 size_t lifetime_start_;
464 size_t lifetime_end_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000465
466 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
467};
468
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100469#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
470 M(Add, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000471 M(ArrayGet, Instruction) \
472 M(ArrayLength, Instruction) \
473 M(ArraySet, Instruction) \
474 M(BoundsCheck, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100475 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000476 M(Compare, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100477 M(Condition, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000478 M(Div, BinaryOperation) \
479 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100480 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000481 M(Exit, Instruction) \
482 M(FloatConstant, Constant) \
483 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100484 M(GreaterThan, Condition) \
485 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100486 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000487 M(InstanceFieldGet, Instruction) \
488 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100489 M(IntConstant, Constant) \
490 M(InvokeStatic, Invoke) \
491 M(InvokeVirtual, Invoke) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100492 M(LoadClass, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000493 M(LessThan, Condition) \
494 M(LessThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100495 M(LoadLocal, Instruction) \
496 M(Local, Instruction) \
497 M(LongConstant, Constant) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000498 M(Mul, BinaryOperation) \
499 M(Neg, UnaryOperation) \
500 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100501 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +0100502 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000503 M(NotEqual, Condition) \
504 M(NullCheck, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100505 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000506 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100507 M(Phi, Instruction) \
508 M(Return, Instruction) \
509 M(ReturnVoid, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100510 M(StaticFieldGet, Instruction) \
511 M(StaticFieldSet, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100512 M(StoreLocal, Instruction) \
513 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100514 M(SuspendCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000515 M(Temporary, Instruction) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000516
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100517#define FOR_EACH_INSTRUCTION(M) \
518 FOR_EACH_CONCRETE_INSTRUCTION(M) \
519 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +0100520 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +0100521 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100522 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -0700523
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100524#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000525FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
526#undef FORWARD_DECLARATION
527
Roland Levillainccc07a92014-09-16 14:48:16 +0100528#define DECLARE_INSTRUCTION(type) \
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100529 virtual InstructionKind GetKind() const { return k##type; } \
Roland Levillainccc07a92014-09-16 14:48:16 +0100530 virtual const char* DebugName() const { return #type; } \
531 virtual const H##type* As##type() const OVERRIDE { return this; } \
532 virtual H##type* As##type() OVERRIDE { return this; } \
533 virtual bool InstructionTypeEquals(HInstruction* other) const { \
534 return other->Is##type(); \
535 } \
536 virtual void Accept(HGraphVisitor* visitor)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000537
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100538template <typename T>
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000539class HUseListNode : public ArenaObject {
540 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100541 HUseListNode(T* user, size_t index, HUseListNode* tail)
Dave Allison20dfc792014-06-16 20:44:29 -0700542 : user_(user), index_(index), tail_(tail) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000543
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000544 HUseListNode* GetTail() const { return tail_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100545 T* GetUser() const { return user_; }
546 size_t GetIndex() const { return index_; }
547
548 void SetTail(HUseListNode<T>* node) { tail_ = node; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000549
550 private:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100551 T* const user_;
552 const size_t index_;
553 HUseListNode<T>* tail_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000554
555 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
556};
557
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100558// Represents the side effects an instruction may have.
559class SideEffects : public ValueObject {
560 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100561 SideEffects() : flags_(0) {}
562
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100563 static SideEffects None() {
564 return SideEffects(0);
565 }
566
567 static SideEffects All() {
568 return SideEffects(ChangesSomething().flags_ | DependsOnSomething().flags_);
569 }
570
571 static SideEffects ChangesSomething() {
572 return SideEffects((1 << kFlagChangesCount) - 1);
573 }
574
575 static SideEffects DependsOnSomething() {
576 int count = kFlagDependsOnCount - kFlagChangesCount;
577 return SideEffects(((1 << count) - 1) << kFlagChangesCount);
578 }
579
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100580 SideEffects Union(SideEffects other) const {
581 return SideEffects(flags_ | other.flags_);
582 }
583
Roland Levillain72bceff2014-09-15 18:29:00 +0100584 bool HasSideEffects() const {
585 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
586 return (flags_ & all_bits_set) != 0;
587 }
588
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100589 bool HasAllSideEffects() const {
590 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
591 return all_bits_set == (flags_ & all_bits_set);
592 }
593
594 bool DependsOn(SideEffects other) const {
595 size_t depends_flags = other.ComputeDependsFlags();
596 return (flags_ & depends_flags) != 0;
597 }
598
599 bool HasDependencies() const {
600 int count = kFlagDependsOnCount - kFlagChangesCount;
601 size_t all_bits_set = (1 << count) - 1;
602 return ((flags_ >> kFlagChangesCount) & all_bits_set) != 0;
603 }
604
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100605 private:
606 static constexpr int kFlagChangesSomething = 0;
607 static constexpr int kFlagChangesCount = kFlagChangesSomething + 1;
608
609 static constexpr int kFlagDependsOnSomething = kFlagChangesCount;
610 static constexpr int kFlagDependsOnCount = kFlagDependsOnSomething + 1;
611
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100612 explicit SideEffects(size_t flags) : flags_(flags) {}
613
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100614 size_t ComputeDependsFlags() const {
615 return flags_ << kFlagChangesCount;
616 }
617
618 size_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100619};
620
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000621class HInstruction : public ArenaObject {
622 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100623 explicit HInstruction(SideEffects side_effects)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000624 : previous_(nullptr),
625 next_(nullptr),
626 block_(nullptr),
627 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100628 ssa_index_(-1),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000629 uses_(nullptr),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100630 env_uses_(nullptr),
631 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100632 locations_(nullptr),
633 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100634 lifetime_position_(kNoLifetime),
635 side_effects_(side_effects) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000636
Dave Allison20dfc792014-06-16 20:44:29 -0700637 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000638
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100639#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100640 enum InstructionKind {
641 FOR_EACH_INSTRUCTION(DECLARE_KIND)
642 };
643#undef DECLARE_KIND
644
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000645 HInstruction* GetNext() const { return next_; }
646 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000647
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000648 HBasicBlock* GetBlock() const { return block_; }
649 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100650 bool IsInBlock() const { return block_ != nullptr; }
651 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +0100652 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000653
Roland Levillain6b879dd2014-09-22 17:13:44 +0100654 virtual size_t InputCount() const = 0;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100655 virtual HInstruction* InputAt(size_t i) const = 0;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000656
657 virtual void Accept(HGraphVisitor* visitor) = 0;
658 virtual const char* DebugName() const = 0;
659
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100660 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100661 virtual void SetRawInputAt(size_t index, HInstruction* input) = 0;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100662
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100663 virtual bool NeedsEnvironment() const { return false; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100664 virtual bool IsControlFlow() const { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +0100665 virtual bool CanThrow() const { return false; }
Roland Levillain72bceff2014-09-15 18:29:00 +0100666 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100667
668 void AddUseAt(HInstruction* user, size_t index) {
669 uses_ = new (block_->GetGraph()->GetArena()) HUseListNode<HInstruction>(user, index, uses_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000670 }
671
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100672 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +0100673 DCHECK(user != nullptr);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100674 env_uses_ = new (block_->GetGraph()->GetArena()) HUseListNode<HEnvironment>(
675 user, index, env_uses_);
676 }
677
678 void RemoveUser(HInstruction* user, size_t index);
Nicolas Geoffray724c9632014-09-22 12:27:27 +0100679 void RemoveEnvironmentUser(HEnvironment* user, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100680
681 HUseListNode<HInstruction>* GetUses() const { return uses_; }
682 HUseListNode<HEnvironment>* GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000683
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100684 bool HasUses() const { return uses_ != nullptr || env_uses_ != nullptr; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100685 bool HasEnvironmentUses() const { return env_uses_ != nullptr; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000686
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100687 size_t NumberOfUses() const {
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100688 // TODO: Optimize this method if it is used outside of the HGraphVisualizer.
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100689 size_t result = 0;
690 HUseListNode<HInstruction>* current = uses_;
691 while (current != nullptr) {
692 current = current->GetTail();
693 ++result;
694 }
695 return result;
696 }
697
Roland Levillain6c82d402014-10-13 16:10:27 +0100698 // Does this instruction strictly dominate `other_instruction`?
699 // Returns false if this instruction and `other_instruction` are the same.
700 // Aborts if this instruction and `other_instruction` are both phis.
701 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +0100702
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000703 int GetId() const { return id_; }
704 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000705
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100706 int GetSsaIndex() const { return ssa_index_; }
707 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
708 bool HasSsaIndex() const { return ssa_index_ != -1; }
709
710 bool HasEnvironment() const { return environment_ != nullptr; }
711 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100712 void SetEnvironment(HEnvironment* environment) { environment_ = environment; }
713
Nicolas Geoffray39468442014-09-02 15:17:15 +0100714 // Returns the number of entries in the environment. Typically, that is the
715 // number of dex registers in a method. It could be more in case of inlining.
716 size_t EnvironmentSize() const;
717
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000718 LocationSummary* GetLocations() const { return locations_; }
719 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000720
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100721 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100722 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100723
Dave Allison20dfc792014-06-16 20:44:29 -0700724 bool HasOnlyOneUse() const {
725 return uses_ != nullptr && uses_->GetTail() == nullptr;
726 }
727
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100728#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +0100729 bool Is##type() const { return (As##type() != nullptr); } \
730 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000731 virtual H##type* As##type() { return nullptr; }
732
733 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
734#undef INSTRUCTION_TYPE_CHECK
735
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100736 // Returns whether the instruction can be moved within the graph.
737 virtual bool CanBeMoved() const { return false; }
738
739 // Returns whether the two instructions are of the same kind.
740 virtual bool InstructionTypeEquals(HInstruction* other) const { return false; }
741
742 // Returns whether any data encoded in the two instructions is equal.
743 // This method does not look at the inputs. Both instructions must be
744 // of the same type, otherwise the method has undefined behavior.
745 virtual bool InstructionDataEquals(HInstruction* other) const { return false; }
746
747 // Returns whether two instructions are equal, that is:
748 // 1) They have the same type and contain the same data,
749 // 2) Their inputs are identical.
750 bool Equals(HInstruction* other) const;
751
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100752 virtual InstructionKind GetKind() const = 0;
753
754 virtual size_t ComputeHashCode() const {
755 size_t result = GetKind();
756 for (size_t i = 0, e = InputCount(); i < e; ++i) {
757 result = (result * 31) + InputAt(i)->GetId();
758 }
759 return result;
760 }
761
762 SideEffects GetSideEffects() const { return side_effects_; }
763
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100764 size_t GetLifetimePosition() const { return lifetime_position_; }
765 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
766 LiveInterval* GetLiveInterval() const { return live_interval_; }
767 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
768 bool HasLiveInterval() const { return live_interval_ != nullptr; }
769
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000770 private:
771 HInstruction* previous_;
772 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000773 HBasicBlock* block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000774
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000775 // An instruction gets an id when it is added to the graph.
776 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +0100777 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000778 int id_;
779
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100780 // When doing liveness analysis, instructions that have uses get an SSA index.
781 int ssa_index_;
782
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100783 // List of instructions that have this instruction as input.
784 HUseListNode<HInstruction>* uses_;
785
786 // List of environments that contain this instruction.
787 HUseListNode<HEnvironment>* env_uses_;
788
Nicolas Geoffray39468442014-09-02 15:17:15 +0100789 // The environment associated with this instruction. Not null if the instruction
790 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100791 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000792
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000793 // Set by the code generator.
794 LocationSummary* locations_;
795
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100796 // Set by the liveness analysis.
797 LiveInterval* live_interval_;
798
799 // Set by the liveness analysis, this is the position in a linear
800 // order of blocks where this instruction's live interval start.
801 size_t lifetime_position_;
802
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100803 const SideEffects side_effects_;
804
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000805 friend class HBasicBlock;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100806 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000807
808 DISALLOW_COPY_AND_ASSIGN(HInstruction);
809};
810
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100811template<typename T>
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000812class HUseIterator : public ValueObject {
813 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100814 explicit HUseIterator(HUseListNode<T>* uses) : current_(uses) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000815
816 bool Done() const { return current_ == nullptr; }
817
818 void Advance() {
819 DCHECK(!Done());
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000820 current_ = current_->GetTail();
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000821 }
822
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100823 HUseListNode<T>* Current() const {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000824 DCHECK(!Done());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100825 return current_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000826 }
827
828 private:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100829 HUseListNode<T>* current_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000830
831 friend class HValue;
832};
833
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100834// A HEnvironment object contains the values of virtual registers at a given location.
835class HEnvironment : public ArenaObject {
836 public:
837 HEnvironment(ArenaAllocator* arena, size_t number_of_vregs) : vregs_(arena, number_of_vregs) {
838 vregs_.SetSize(number_of_vregs);
839 for (size_t i = 0; i < number_of_vregs; i++) {
840 vregs_.Put(i, nullptr);
841 }
842 }
843
844 void Populate(const GrowableArray<HInstruction*>& env) {
845 for (size_t i = 0; i < env.Size(); i++) {
846 HInstruction* instruction = env.Get(i);
847 vregs_.Put(i, instruction);
848 if (instruction != nullptr) {
849 instruction->AddEnvUseAt(this, i);
850 }
851 }
852 }
853
854 void SetRawEnvAt(size_t index, HInstruction* instruction) {
855 vregs_.Put(index, instruction);
856 }
857
Nicolas Geoffray39468442014-09-02 15:17:15 +0100858 HInstruction* GetInstructionAt(size_t index) const {
859 return vregs_.Get(index);
860 }
861
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100862 GrowableArray<HInstruction*>* GetVRegs() {
863 return &vregs_;
864 }
865
Nicolas Geoffray39468442014-09-02 15:17:15 +0100866 size_t Size() const { return vregs_.Size(); }
867
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100868 private:
869 GrowableArray<HInstruction*> vregs_;
870
871 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
872};
873
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000874class HInputIterator : public ValueObject {
875 public:
Dave Allison20dfc792014-06-16 20:44:29 -0700876 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000877
878 bool Done() const { return index_ == instruction_->InputCount(); }
879 HInstruction* Current() const { return instruction_->InputAt(index_); }
880 void Advance() { index_++; }
881
882 private:
883 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100884 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000885
886 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
887};
888
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000889class HInstructionIterator : public ValueObject {
890 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100891 explicit HInstructionIterator(const HInstructionList& instructions)
892 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000893 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000894 }
895
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000896 bool Done() const { return instruction_ == nullptr; }
897 HInstruction* Current() const { return instruction_; }
898 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000899 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000900 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000901 }
902
903 private:
904 HInstruction* instruction_;
905 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100906
907 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000908};
909
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100910class HBackwardInstructionIterator : public ValueObject {
911 public:
912 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
913 : instruction_(instructions.last_instruction_) {
914 next_ = Done() ? nullptr : instruction_->GetPrevious();
915 }
916
917 bool Done() const { return instruction_ == nullptr; }
918 HInstruction* Current() const { return instruction_; }
919 void Advance() {
920 instruction_ = next_;
921 next_ = Done() ? nullptr : instruction_->GetPrevious();
922 }
923
924 private:
925 HInstruction* instruction_;
926 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100927
928 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100929};
930
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000931// An embedded container with N elements of type T. Used (with partial
932// specialization for N=0) because embedded arrays cannot have size 0.
933template<typename T, intptr_t N>
934class EmbeddedArray {
935 public:
Dave Allison20dfc792014-06-16 20:44:29 -0700936 EmbeddedArray() : elements_() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000937
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000938 intptr_t GetLength() const { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000939
940 const T& operator[](intptr_t i) const {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000941 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000942 return elements_[i];
943 }
944
945 T& operator[](intptr_t i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000946 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000947 return elements_[i];
948 }
949
950 const T& At(intptr_t i) const {
951 return (*this)[i];
952 }
953
954 void SetAt(intptr_t i, const T& val) {
955 (*this)[i] = val;
956 }
957
958 private:
959 T elements_[N];
960};
961
962template<typename T>
963class EmbeddedArray<T, 0> {
964 public:
965 intptr_t length() const { return 0; }
966 const T& operator[](intptr_t i) const {
967 LOG(FATAL) << "Unreachable";
968 static T sentinel = 0;
969 return sentinel;
970 }
971 T& operator[](intptr_t i) {
972 LOG(FATAL) << "Unreachable";
973 static T sentinel = 0;
974 return sentinel;
975 }
976};
977
978template<intptr_t N>
979class HTemplateInstruction: public HInstruction {
980 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100981 HTemplateInstruction<N>(SideEffects side_effects)
982 : HInstruction(side_effects), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -0700983 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000984
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100985 virtual size_t InputCount() const { return N; }
986 virtual HInstruction* InputAt(size_t i) const { return inputs_[i]; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000987
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000988 protected:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100989 virtual void SetRawInputAt(size_t i, HInstruction* instruction) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000990 inputs_[i] = instruction;
991 }
992
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000993 private:
994 EmbeddedArray<HInstruction*, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100995
996 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000997};
998
Dave Allison20dfc792014-06-16 20:44:29 -0700999template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001000class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07001001 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001002 HExpression<N>(Primitive::Type type, SideEffects side_effects)
1003 : HTemplateInstruction<N>(side_effects), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07001004 virtual ~HExpression() {}
1005
1006 virtual Primitive::Type GetType() const { return type_; }
1007
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001008 protected:
1009 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07001010};
1011
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001012// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
1013// instruction that branches to the exit block.
1014class HReturnVoid : public HTemplateInstruction<0> {
1015 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001016 HReturnVoid() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001017
1018 virtual bool IsControlFlow() const { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001019
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001020 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001021
1022 private:
1023 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
1024};
1025
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001026// Represents dex's RETURN opcodes. A HReturn is a control flow
1027// instruction that branches to the exit block.
1028class HReturn : public HTemplateInstruction<1> {
1029 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001030 explicit HReturn(HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001031 SetRawInputAt(0, value);
1032 }
1033
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001034 virtual bool IsControlFlow() const { return true; }
1035
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001036 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001037
1038 private:
1039 DISALLOW_COPY_AND_ASSIGN(HReturn);
1040};
1041
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001042// The exit instruction is the only instruction of the exit block.
1043// Instructions aborting the method (HTrow and HReturn) must branch to the
1044// exit block.
1045class HExit : public HTemplateInstruction<0> {
1046 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001047 HExit() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001048
1049 virtual bool IsControlFlow() const { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001050
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001051 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001052
1053 private:
1054 DISALLOW_COPY_AND_ASSIGN(HExit);
1055};
1056
1057// Jumps from one block to another.
1058class HGoto : public HTemplateInstruction<0> {
1059 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001060 HGoto() : HTemplateInstruction(SideEffects::None()) {}
1061
1062 virtual bool IsControlFlow() const { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001063
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001064 HBasicBlock* GetSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001065 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001066 }
1067
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001068 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001069
1070 private:
1071 DISALLOW_COPY_AND_ASSIGN(HGoto);
1072};
1073
Dave Allison20dfc792014-06-16 20:44:29 -07001074
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001075// Conditional branch. A block ending with an HIf instruction must have
1076// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001077class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001078 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001079 explicit HIf(HInstruction* input) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001080 SetRawInputAt(0, input);
1081 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001082
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001083 virtual bool IsControlFlow() const { return true; }
1084
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001085 HBasicBlock* IfTrueSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001086 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001087 }
1088
1089 HBasicBlock* IfFalseSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001090 return GetBlock()->GetSuccessors().Get(1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001091 }
1092
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001093 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001094
Dave Allison20dfc792014-06-16 20:44:29 -07001095 virtual bool IsIfInstruction() const { return true; }
1096
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001097 private:
1098 DISALLOW_COPY_AND_ASSIGN(HIf);
1099};
1100
Roland Levillain88cb1752014-10-20 16:36:47 +01001101class HUnaryOperation : public HExpression<1> {
1102 public:
1103 HUnaryOperation(Primitive::Type result_type, HInstruction* input)
1104 : HExpression(result_type, SideEffects::None()) {
1105 SetRawInputAt(0, input);
1106 }
1107
1108 HInstruction* GetInput() const { return InputAt(0); }
1109 Primitive::Type GetResultType() const { return GetType(); }
1110
1111 virtual bool CanBeMoved() const { return true; }
1112 virtual bool InstructionDataEquals(HInstruction* other) const { return true; }
1113
Roland Levillain9240d6a2014-10-20 16:47:04 +01001114 // Try to statically evaluate `operation` and return a HConstant
1115 // containing the result of this evaluation. If `operation` cannot
1116 // be evaluated as a constant, return nullptr.
1117 HConstant* TryStaticEvaluation() const;
1118
1119 // Apply this operation to `x`.
1120 virtual int32_t Evaluate(int32_t x) const = 0;
1121 virtual int64_t Evaluate(int64_t x) const = 0;
1122
Roland Levillain88cb1752014-10-20 16:36:47 +01001123 DECLARE_INSTRUCTION(UnaryOperation);
1124
1125 private:
1126 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
1127};
1128
Dave Allison20dfc792014-06-16 20:44:29 -07001129class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001130 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001131 HBinaryOperation(Primitive::Type result_type,
1132 HInstruction* left,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001133 HInstruction* right) : HExpression(result_type, SideEffects::None()) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001134 SetRawInputAt(0, left);
1135 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001136 }
1137
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001138 HInstruction* GetLeft() const { return InputAt(0); }
1139 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07001140 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001141
1142 virtual bool IsCommutative() { return false; }
1143
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001144 virtual bool CanBeMoved() const { return true; }
1145 virtual bool InstructionDataEquals(HInstruction* other) const { return true; }
1146
Roland Levillain9240d6a2014-10-20 16:47:04 +01001147 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01001148 // containing the result of this evaluation. If `operation` cannot
1149 // be evaluated as a constant, return nullptr.
Roland Levillain9240d6a2014-10-20 16:47:04 +01001150 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01001151
1152 // Apply this operation to `x` and `y`.
1153 virtual int32_t Evaluate(int32_t x, int32_t y) const = 0;
1154 virtual int64_t Evaluate(int64_t x, int64_t y) const = 0;
1155
Roland Levillainccc07a92014-09-16 14:48:16 +01001156 DECLARE_INSTRUCTION(BinaryOperation);
1157
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001158 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001159 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
1160};
1161
Dave Allison20dfc792014-06-16 20:44:29 -07001162class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001163 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001164 HCondition(HInstruction* first, HInstruction* second)
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001165 : HBinaryOperation(Primitive::kPrimBoolean, first, second),
1166 needs_materialization_(true) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001167
1168 virtual bool IsCommutative() { return true; }
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001169
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001170 bool NeedsMaterialization() const { return needs_materialization_; }
1171 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001172
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001173 // For code generation purposes, returns whether this instruction is just before
1174 // `if_`, and disregard moves in between.
1175 bool IsBeforeWhenDisregardMoves(HIf* if_) const;
1176
Dave Allison20dfc792014-06-16 20:44:29 -07001177 DECLARE_INSTRUCTION(Condition);
1178
1179 virtual IfCondition GetCondition() const = 0;
1180
1181 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001182 // For register allocation purposes, returns whether this instruction needs to be
1183 // materialized (that is, not just be in the processor flags).
1184 bool needs_materialization_;
1185
Dave Allison20dfc792014-06-16 20:44:29 -07001186 DISALLOW_COPY_AND_ASSIGN(HCondition);
1187};
1188
1189// Instruction to check if two inputs are equal to each other.
1190class HEqual : public HCondition {
1191 public:
1192 HEqual(HInstruction* first, HInstruction* second)
1193 : HCondition(first, second) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001194
Roland Levillain93445682014-10-06 19:24:02 +01001195 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1196 return x == y ? 1 : 0;
1197 }
1198 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1199 return x == y ? 1 : 0;
1200 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001201
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001202 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001203
Dave Allison20dfc792014-06-16 20:44:29 -07001204 virtual IfCondition GetCondition() const {
1205 return kCondEQ;
1206 }
1207
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001208 private:
1209 DISALLOW_COPY_AND_ASSIGN(HEqual);
1210};
1211
Dave Allison20dfc792014-06-16 20:44:29 -07001212class HNotEqual : public HCondition {
1213 public:
1214 HNotEqual(HInstruction* first, HInstruction* second)
1215 : HCondition(first, second) {}
1216
Roland Levillain93445682014-10-06 19:24:02 +01001217 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1218 return x != y ? 1 : 0;
1219 }
1220 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1221 return x != y ? 1 : 0;
1222 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001223
Dave Allison20dfc792014-06-16 20:44:29 -07001224 DECLARE_INSTRUCTION(NotEqual);
1225
1226 virtual IfCondition GetCondition() const {
1227 return kCondNE;
1228 }
1229
1230 private:
1231 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
1232};
1233
1234class HLessThan : public HCondition {
1235 public:
1236 HLessThan(HInstruction* first, HInstruction* second)
1237 : HCondition(first, second) {}
1238
Roland Levillain93445682014-10-06 19:24:02 +01001239 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1240 return x < y ? 1 : 0;
1241 }
1242 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1243 return x < y ? 1 : 0;
1244 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001245
Dave Allison20dfc792014-06-16 20:44:29 -07001246 DECLARE_INSTRUCTION(LessThan);
1247
1248 virtual IfCondition GetCondition() const {
1249 return kCondLT;
1250 }
1251
1252 private:
1253 DISALLOW_COPY_AND_ASSIGN(HLessThan);
1254};
1255
1256class HLessThanOrEqual : public HCondition {
1257 public:
1258 HLessThanOrEqual(HInstruction* first, HInstruction* second)
1259 : HCondition(first, second) {}
1260
Roland Levillain93445682014-10-06 19:24:02 +01001261 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1262 return x <= y ? 1 : 0;
1263 }
1264 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1265 return x <= y ? 1 : 0;
1266 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001267
Dave Allison20dfc792014-06-16 20:44:29 -07001268 DECLARE_INSTRUCTION(LessThanOrEqual);
1269
1270 virtual IfCondition GetCondition() const {
1271 return kCondLE;
1272 }
1273
1274 private:
1275 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
1276};
1277
1278class HGreaterThan : public HCondition {
1279 public:
1280 HGreaterThan(HInstruction* first, HInstruction* second)
1281 : HCondition(first, second) {}
1282
Roland Levillain93445682014-10-06 19:24:02 +01001283 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1284 return x > y ? 1 : 0;
1285 }
1286 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1287 return x > y ? 1 : 0;
1288 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001289
Dave Allison20dfc792014-06-16 20:44:29 -07001290 DECLARE_INSTRUCTION(GreaterThan);
1291
1292 virtual IfCondition GetCondition() const {
1293 return kCondGT;
1294 }
1295
1296 private:
1297 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
1298};
1299
1300class HGreaterThanOrEqual : public HCondition {
1301 public:
1302 HGreaterThanOrEqual(HInstruction* first, HInstruction* second)
1303 : HCondition(first, second) {}
1304
Roland Levillain93445682014-10-06 19:24:02 +01001305 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1306 return x >= y ? 1 : 0;
1307 }
1308 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1309 return x >= y ? 1 : 0;
1310 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001311
Dave Allison20dfc792014-06-16 20:44:29 -07001312 DECLARE_INSTRUCTION(GreaterThanOrEqual);
1313
1314 virtual IfCondition GetCondition() const {
1315 return kCondGE;
1316 }
1317
1318 private:
1319 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
1320};
1321
1322
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001323// Instruction to check how two inputs compare to each other.
1324// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
1325class HCompare : public HBinaryOperation {
1326 public:
1327 HCompare(Primitive::Type type, HInstruction* first, HInstruction* second)
1328 : HBinaryOperation(Primitive::kPrimInt, first, second) {
1329 DCHECK_EQ(type, first->GetType());
1330 DCHECK_EQ(type, second->GetType());
1331 }
1332
Roland Levillain93445682014-10-06 19:24:02 +01001333 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01001334 return
1335 x == y ? 0 :
1336 x > y ? 1 :
1337 -1;
1338 }
Roland Levillain93445682014-10-06 19:24:02 +01001339 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01001340 return
1341 x == y ? 0 :
1342 x > y ? 1 :
1343 -1;
1344 }
1345
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001346 DECLARE_INSTRUCTION(Compare);
1347
1348 private:
1349 DISALLOW_COPY_AND_ASSIGN(HCompare);
1350};
1351
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001352// A local in the graph. Corresponds to a Dex register.
1353class HLocal : public HTemplateInstruction<0> {
1354 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001355 explicit HLocal(uint16_t reg_number)
1356 : HTemplateInstruction(SideEffects::None()), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001357
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001358 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001359
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001360 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001361
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001362 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001363 // The Dex register number.
1364 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001365
1366 DISALLOW_COPY_AND_ASSIGN(HLocal);
1367};
1368
1369// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07001370class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001371 public:
Roland Levillain5799fc02014-09-25 12:15:20 +01001372 HLoadLocal(HLocal* local, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001373 : HExpression(type, SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001374 SetRawInputAt(0, local);
1375 }
1376
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001377 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1378
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001379 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001380
1381 private:
1382 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
1383};
1384
1385// Store a value in a given local. This instruction has two inputs: the value
1386// and the local.
1387class HStoreLocal : public HTemplateInstruction<2> {
1388 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001389 HStoreLocal(HLocal* local, HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001390 SetRawInputAt(0, local);
1391 SetRawInputAt(1, value);
1392 }
1393
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001394 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1395
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001396 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001397
1398 private:
1399 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
1400};
1401
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001402class HConstant : public HExpression<0> {
1403 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001404 explicit HConstant(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
1405
1406 virtual bool CanBeMoved() const { return true; }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001407
1408 DECLARE_INSTRUCTION(Constant);
1409
1410 private:
1411 DISALLOW_COPY_AND_ASSIGN(HConstant);
1412};
1413
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001414class HFloatConstant : public HConstant {
1415 public:
1416 explicit HFloatConstant(float value) : HConstant(Primitive::kPrimFloat), value_(value) {}
1417
1418 float GetValue() const { return value_; }
1419
1420 virtual bool InstructionDataEquals(HInstruction* other) const {
1421 return bit_cast<float, int32_t>(other->AsFloatConstant()->value_) ==
1422 bit_cast<float, int32_t>(value_);
1423 }
1424
1425 virtual size_t ComputeHashCode() const { return static_cast<size_t>(GetValue()); }
1426
1427 DECLARE_INSTRUCTION(FloatConstant);
1428
1429 private:
1430 const float value_;
1431
1432 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
1433};
1434
1435class HDoubleConstant : public HConstant {
1436 public:
1437 explicit HDoubleConstant(double value) : HConstant(Primitive::kPrimDouble), value_(value) {}
1438
1439 double GetValue() const { return value_; }
1440
1441 virtual bool InstructionDataEquals(HInstruction* other) const {
1442 return bit_cast<double, int64_t>(other->AsDoubleConstant()->value_) ==
1443 bit_cast<double, int64_t>(value_);
1444 }
1445
1446 virtual size_t ComputeHashCode() const { return static_cast<size_t>(GetValue()); }
1447
1448 DECLARE_INSTRUCTION(DoubleConstant);
1449
1450 private:
1451 const double value_;
1452
1453 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
1454};
1455
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001456// Constants of the type int. Those can be from Dex instructions, or
1457// synthesized (for example with the if-eqz instruction).
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001458class HIntConstant : public HConstant {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001459 public:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001460 explicit HIntConstant(int32_t value) : HConstant(Primitive::kPrimInt), value_(value) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001461
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001462 int32_t GetValue() const { return value_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001463
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001464 virtual bool InstructionDataEquals(HInstruction* other) const {
1465 return other->AsIntConstant()->value_ == value_;
1466 }
1467
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001468 virtual size_t ComputeHashCode() const { return GetValue(); }
1469
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001470 DECLARE_INSTRUCTION(IntConstant);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001471
1472 private:
1473 const int32_t value_;
1474
1475 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
1476};
1477
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001478class HLongConstant : public HConstant {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001479 public:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001480 explicit HLongConstant(int64_t value) : HConstant(Primitive::kPrimLong), value_(value) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001481
1482 int64_t GetValue() const { return value_; }
1483
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001484 virtual bool InstructionDataEquals(HInstruction* other) const {
1485 return other->AsLongConstant()->value_ == value_;
1486 }
1487
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001488 virtual size_t ComputeHashCode() const { return static_cast<size_t>(GetValue()); }
1489
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001490 DECLARE_INSTRUCTION(LongConstant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001491
1492 private:
1493 const int64_t value_;
1494
1495 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
1496};
1497
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001498class HInvoke : public HInstruction {
1499 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001500 HInvoke(ArenaAllocator* arena,
1501 uint32_t number_of_arguments,
1502 Primitive::Type return_type,
1503 uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001504 : HInstruction(SideEffects::All()),
1505 inputs_(arena, number_of_arguments),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001506 return_type_(return_type),
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001507 dex_pc_(dex_pc) {
1508 inputs_.SetSize(number_of_arguments);
1509 }
1510
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001511 virtual size_t InputCount() const { return inputs_.Size(); }
1512 virtual HInstruction* InputAt(size_t i) const { return inputs_.Get(i); }
1513
1514 // Runtime needs to walk the stack, so Dex -> Dex calls need to
1515 // know their environment.
1516 virtual bool NeedsEnvironment() const { return true; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001517
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001518 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001519 SetRawInputAt(index, argument);
1520 }
1521
1522 virtual void SetRawInputAt(size_t index, HInstruction* input) {
1523 inputs_.Put(index, input);
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001524 }
1525
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001526 virtual Primitive::Type GetType() const { return return_type_; }
1527
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001528 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001529
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001530 DECLARE_INSTRUCTION(Invoke);
1531
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001532 protected:
1533 GrowableArray<HInstruction*> inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001534 const Primitive::Type return_type_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001535 const uint32_t dex_pc_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001536
1537 private:
1538 DISALLOW_COPY_AND_ASSIGN(HInvoke);
1539};
1540
1541class HInvokeStatic : public HInvoke {
1542 public:
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001543 HInvokeStatic(ArenaAllocator* arena,
1544 uint32_t number_of_arguments,
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001545 Primitive::Type return_type,
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001546 uint32_t dex_pc,
1547 uint32_t index_in_dex_cache)
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001548 : HInvoke(arena, number_of_arguments, return_type, dex_pc),
1549 index_in_dex_cache_(index_in_dex_cache) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001550
1551 uint32_t GetIndexInDexCache() const { return index_in_dex_cache_; }
1552
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001553 DECLARE_INSTRUCTION(InvokeStatic);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001554
1555 private:
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001556 const uint32_t index_in_dex_cache_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001557
1558 DISALLOW_COPY_AND_ASSIGN(HInvokeStatic);
1559};
1560
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001561class HInvokeVirtual : public HInvoke {
1562 public:
1563 HInvokeVirtual(ArenaAllocator* arena,
1564 uint32_t number_of_arguments,
1565 Primitive::Type return_type,
1566 uint32_t dex_pc,
1567 uint32_t vtable_index)
1568 : HInvoke(arena, number_of_arguments, return_type, dex_pc),
1569 vtable_index_(vtable_index) {}
1570
1571 uint32_t GetVTableIndex() const { return vtable_index_; }
1572
1573 DECLARE_INSTRUCTION(InvokeVirtual);
1574
1575 private:
1576 const uint32_t vtable_index_;
1577
1578 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
1579};
1580
Dave Allison20dfc792014-06-16 20:44:29 -07001581class HNewInstance : public HExpression<0> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001582 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001583 HNewInstance(uint32_t dex_pc, uint16_t type_index)
1584 : HExpression(Primitive::kPrimNot, SideEffects::None()),
1585 dex_pc_(dex_pc),
1586 type_index_(type_index) {}
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001587
1588 uint32_t GetDexPc() const { return dex_pc_; }
1589 uint16_t GetTypeIndex() const { return type_index_; }
1590
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001591 // Calls runtime so needs an environment.
1592 virtual bool NeedsEnvironment() const { return true; }
1593
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001594 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001595
1596 private:
1597 const uint32_t dex_pc_;
1598 const uint16_t type_index_;
1599
1600 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
1601};
1602
Roland Levillain88cb1752014-10-20 16:36:47 +01001603class HNeg : public HUnaryOperation {
1604 public:
1605 explicit HNeg(Primitive::Type result_type, HInstruction* input)
1606 : HUnaryOperation(result_type, input) {}
1607
Roland Levillain9240d6a2014-10-20 16:47:04 +01001608 virtual int32_t Evaluate(int32_t x) const OVERRIDE { return -x; }
1609 virtual int64_t Evaluate(int64_t x) const OVERRIDE { return -x; }
1610
Roland Levillain88cb1752014-10-20 16:36:47 +01001611 DECLARE_INSTRUCTION(Neg);
1612
1613 private:
1614 DISALLOW_COPY_AND_ASSIGN(HNeg);
1615};
1616
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001617class HNewArray : public HExpression<1> {
1618 public:
1619 HNewArray(HInstruction* length, uint32_t dex_pc, uint16_t type_index)
1620 : HExpression(Primitive::kPrimNot, SideEffects::None()),
1621 dex_pc_(dex_pc),
1622 type_index_(type_index) {
1623 SetRawInputAt(0, length);
1624 }
1625
1626 uint32_t GetDexPc() const { return dex_pc_; }
1627 uint16_t GetTypeIndex() const { return type_index_; }
1628
1629 // Calls runtime so needs an environment.
1630 virtual bool NeedsEnvironment() const { return true; }
1631
1632 DECLARE_INSTRUCTION(NewArray);
1633
1634 private:
1635 const uint32_t dex_pc_;
1636 const uint16_t type_index_;
1637
1638 DISALLOW_COPY_AND_ASSIGN(HNewArray);
1639};
1640
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001641class HAdd : public HBinaryOperation {
1642 public:
1643 HAdd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1644 : HBinaryOperation(result_type, left, right) {}
1645
1646 virtual bool IsCommutative() { return true; }
1647
Roland Levillain93445682014-10-06 19:24:02 +01001648 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1649 return x + y;
1650 }
1651 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1652 return x + y;
1653 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001654
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001655 DECLARE_INSTRUCTION(Add);
1656
1657 private:
1658 DISALLOW_COPY_AND_ASSIGN(HAdd);
1659};
1660
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001661class HSub : public HBinaryOperation {
1662 public:
1663 HSub(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1664 : HBinaryOperation(result_type, left, right) {}
1665
Roland Levillain93445682014-10-06 19:24:02 +01001666 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1667 return x - y;
1668 }
1669 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1670 return x - y;
1671 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001672
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001673 DECLARE_INSTRUCTION(Sub);
1674
1675 private:
1676 DISALLOW_COPY_AND_ASSIGN(HSub);
1677};
1678
Calin Juravle34bacdf2014-10-07 20:23:36 +01001679class HMul : public HBinaryOperation {
1680 public:
1681 HMul(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1682 : HBinaryOperation(result_type, left, right) {}
1683
1684 virtual bool IsCommutative() { return true; }
1685
1686 virtual int32_t Evaluate(int32_t x, int32_t y) const { return x * y; }
1687 virtual int64_t Evaluate(int64_t x, int64_t y) const { return x * y; }
1688
1689 DECLARE_INSTRUCTION(Mul);
1690
1691 private:
1692 DISALLOW_COPY_AND_ASSIGN(HMul);
1693};
1694
Calin Juravle7c4954d2014-10-28 16:57:40 +00001695class HDiv : public HBinaryOperation {
1696 public:
1697 HDiv(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1698 : HBinaryOperation(result_type, left, right) {}
1699
1700 virtual int32_t Evaluate(int32_t x, int32_t y) const { return x / y; }
1701 virtual int64_t Evaluate(int64_t x, int64_t y) const { return x / y; }
1702
1703 DECLARE_INSTRUCTION(Div);
1704
1705 private:
1706 DISALLOW_COPY_AND_ASSIGN(HDiv);
1707};
1708
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001709// The value of a parameter in this method. Its location depends on
1710// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07001711class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001712 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001713 HParameterValue(uint8_t index, Primitive::Type parameter_type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001714 : HExpression(parameter_type, SideEffects::None()), index_(index) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001715
1716 uint8_t GetIndex() const { return index_; }
1717
1718 DECLARE_INSTRUCTION(ParameterValue);
1719
1720 private:
1721 // The index of this parameter in the parameters list. Must be less
1722 // than HGraph::number_of_in_vregs_;
1723 const uint8_t index_;
1724
1725 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
1726};
1727
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001728class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001729 public:
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001730 explicit HNot(Primitive::Type result_type, HInstruction* input)
1731 : HUnaryOperation(result_type, input) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001732
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001733 virtual bool CanBeMoved() const { return true; }
1734 virtual bool InstructionDataEquals(HInstruction* other) const { return true; }
1735
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001736 virtual int32_t Evaluate(int32_t x) const OVERRIDE { return ~x; }
1737 virtual int64_t Evaluate(int64_t x) const OVERRIDE { return ~x; }
1738
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001739 DECLARE_INSTRUCTION(Not);
1740
1741 private:
1742 DISALLOW_COPY_AND_ASSIGN(HNot);
1743};
1744
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001745class HPhi : public HInstruction {
1746 public:
1747 HPhi(ArenaAllocator* arena, uint32_t reg_number, size_t number_of_inputs, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001748 : HInstruction(SideEffects::None()),
1749 inputs_(arena, number_of_inputs),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001750 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001751 type_(type),
1752 is_live_(false) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001753 inputs_.SetSize(number_of_inputs);
1754 }
1755
1756 virtual size_t InputCount() const { return inputs_.Size(); }
1757 virtual HInstruction* InputAt(size_t i) const { return inputs_.Get(i); }
1758
1759 virtual void SetRawInputAt(size_t index, HInstruction* input) {
1760 inputs_.Put(index, input);
1761 }
1762
1763 void AddInput(HInstruction* input);
1764
1765 virtual Primitive::Type GetType() const { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001766 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001767
1768 uint32_t GetRegNumber() const { return reg_number_; }
1769
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001770 void SetDead() { is_live_ = false; }
1771 void SetLive() { is_live_ = true; }
1772 bool IsDead() const { return !is_live_; }
1773 bool IsLive() const { return is_live_; }
1774
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001775 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001776
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001777 private:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001778 GrowableArray<HInstruction*> inputs_;
1779 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001780 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001781 bool is_live_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001782
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001783 DISALLOW_COPY_AND_ASSIGN(HPhi);
1784};
1785
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001786class HNullCheck : public HExpression<1> {
1787 public:
1788 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001789 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001790 SetRawInputAt(0, value);
1791 }
1792
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001793 virtual bool CanBeMoved() const { return true; }
1794 virtual bool InstructionDataEquals(HInstruction* other) const { return true; }
1795
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001796 virtual bool NeedsEnvironment() const { return true; }
1797
Roland Levillaine161a2a2014-10-03 12:45:18 +01001798 virtual bool CanThrow() const { return true; }
1799
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001800 uint32_t GetDexPc() const { return dex_pc_; }
1801
1802 DECLARE_INSTRUCTION(NullCheck);
1803
1804 private:
1805 const uint32_t dex_pc_;
1806
1807 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
1808};
1809
1810class FieldInfo : public ValueObject {
1811 public:
Roland Levillain5799fc02014-09-25 12:15:20 +01001812 FieldInfo(MemberOffset field_offset, Primitive::Type field_type)
Nicolas Geoffray39468442014-09-02 15:17:15 +01001813 : field_offset_(field_offset), field_type_(field_type) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001814
1815 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01001816 Primitive::Type GetFieldType() const { return field_type_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001817
1818 private:
1819 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01001820 const Primitive::Type field_type_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001821};
1822
1823class HInstanceFieldGet : public HExpression<1> {
1824 public:
1825 HInstanceFieldGet(HInstruction* value,
1826 Primitive::Type field_type,
1827 MemberOffset field_offset)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001828 : HExpression(field_type, SideEffects::DependsOnSomething()),
1829 field_info_(field_offset, field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001830 SetRawInputAt(0, value);
1831 }
1832
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001833 virtual bool CanBeMoved() const { return true; }
1834 virtual bool InstructionDataEquals(HInstruction* other) const {
1835 size_t other_offset = other->AsInstanceFieldGet()->GetFieldOffset().SizeValue();
1836 return other_offset == GetFieldOffset().SizeValue();
1837 }
1838
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001839 virtual size_t ComputeHashCode() const {
1840 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
1841 }
1842
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001843 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01001844 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001845
1846 DECLARE_INSTRUCTION(InstanceFieldGet);
1847
1848 private:
1849 const FieldInfo field_info_;
1850
1851 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
1852};
1853
1854class HInstanceFieldSet : public HTemplateInstruction<2> {
1855 public:
1856 HInstanceFieldSet(HInstruction* object,
1857 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01001858 Primitive::Type field_type,
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001859 MemberOffset field_offset)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001860 : HTemplateInstruction(SideEffects::ChangesSomething()),
1861 field_info_(field_offset, field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001862 SetRawInputAt(0, object);
1863 SetRawInputAt(1, value);
1864 }
1865
1866 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01001867 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001868
1869 DECLARE_INSTRUCTION(InstanceFieldSet);
1870
1871 private:
1872 const FieldInfo field_info_;
1873
1874 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
1875};
1876
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001877class HArrayGet : public HExpression<2> {
1878 public:
1879 HArrayGet(HInstruction* array, HInstruction* index, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001880 : HExpression(type, SideEffects::DependsOnSomething()) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001881 SetRawInputAt(0, array);
1882 SetRawInputAt(1, index);
1883 }
1884
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001885 virtual bool CanBeMoved() const { return true; }
1886 virtual bool InstructionDataEquals(HInstruction* other) const { return true; }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001887 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001888
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001889 DECLARE_INSTRUCTION(ArrayGet);
1890
1891 private:
1892 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
1893};
1894
1895class HArraySet : public HTemplateInstruction<3> {
1896 public:
1897 HArraySet(HInstruction* array,
1898 HInstruction* index,
1899 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001900 Primitive::Type expected_component_type,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001901 uint32_t dex_pc)
1902 : HTemplateInstruction(SideEffects::ChangesSomething()),
1903 dex_pc_(dex_pc),
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001904 expected_component_type_(expected_component_type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001905 SetRawInputAt(0, array);
1906 SetRawInputAt(1, index);
1907 SetRawInputAt(2, value);
1908 }
1909
1910 virtual bool NeedsEnvironment() const {
1911 // We currently always call a runtime method to catch array store
1912 // exceptions.
1913 return InputAt(2)->GetType() == Primitive::kPrimNot;
1914 }
1915
1916 uint32_t GetDexPc() const { return dex_pc_; }
1917
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001918 HInstruction* GetValue() const { return InputAt(2); }
1919
1920 Primitive::Type GetComponentType() const {
1921 // The Dex format does not type floating point index operations. Since the
1922 // `expected_component_type_` is set during building and can therefore not
1923 // be correct, we also check what is the value type. If it is a floating
1924 // point type, we must use that type.
1925 Primitive::Type value_type = GetValue()->GetType();
1926 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
1927 ? value_type
1928 : expected_component_type_;
1929 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01001930
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001931 DECLARE_INSTRUCTION(ArraySet);
1932
1933 private:
1934 const uint32_t dex_pc_;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001935 const Primitive::Type expected_component_type_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001936
1937 DISALLOW_COPY_AND_ASSIGN(HArraySet);
1938};
1939
1940class HArrayLength : public HExpression<1> {
1941 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001942 explicit HArrayLength(HInstruction* array)
1943 : HExpression(Primitive::kPrimInt, SideEffects::None()) {
1944 // Note that arrays do not change length, so the instruction does not
1945 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001946 SetRawInputAt(0, array);
1947 }
1948
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001949 virtual bool CanBeMoved() const { return true; }
1950 virtual bool InstructionDataEquals(HInstruction* other) const { return true; }
1951
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001952 DECLARE_INSTRUCTION(ArrayLength);
1953
1954 private:
1955 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
1956};
1957
1958class HBoundsCheck : public HExpression<2> {
1959 public:
1960 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001961 : HExpression(index->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001962 DCHECK(index->GetType() == Primitive::kPrimInt);
1963 SetRawInputAt(0, index);
1964 SetRawInputAt(1, length);
1965 }
1966
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001967 virtual bool CanBeMoved() const { return true; }
1968 virtual bool InstructionDataEquals(HInstruction* other) const { return true; }
1969
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001970 virtual bool NeedsEnvironment() const { return true; }
1971
Roland Levillaine161a2a2014-10-03 12:45:18 +01001972 virtual bool CanThrow() const { return true; }
1973
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001974 uint32_t GetDexPc() const { return dex_pc_; }
1975
1976 DECLARE_INSTRUCTION(BoundsCheck);
1977
1978 private:
1979 const uint32_t dex_pc_;
1980
1981 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
1982};
1983
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001984/**
1985 * Some DEX instructions are folded into multiple HInstructions that need
1986 * to stay live until the last HInstruction. This class
1987 * is used as a marker for the baseline compiler to ensure its preceding
1988 * HInstruction stays live. `index` is the temporary number that is used
1989 * for knowing the stack offset where to store the instruction.
1990 */
1991class HTemporary : public HTemplateInstruction<0> {
1992 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001993 explicit HTemporary(size_t index) : HTemplateInstruction(SideEffects::None()), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001994
1995 size_t GetIndex() const { return index_; }
1996
1997 DECLARE_INSTRUCTION(Temporary);
1998
1999 private:
2000 const size_t index_;
2001
2002 DISALLOW_COPY_AND_ASSIGN(HTemporary);
2003};
2004
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002005class HSuspendCheck : public HTemplateInstruction<0> {
2006 public:
2007 explicit HSuspendCheck(uint32_t dex_pc)
Nicolas Geoffray9ebc72c2014-09-25 16:33:42 +01002008 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002009
2010 virtual bool NeedsEnvironment() const {
2011 return true;
2012 }
2013
2014 uint32_t GetDexPc() const { return dex_pc_; }
2015
2016 DECLARE_INSTRUCTION(SuspendCheck);
2017
2018 private:
2019 const uint32_t dex_pc_;
2020
2021 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
2022};
2023
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002024// TODO: Make this class handle the case the load is null (dex cache
2025// is null).
2026/**
2027 * Instruction to load a Class object.
2028 */
2029class HLoadClass : public HExpression<0> {
2030 public:
2031 HLoadClass(uint16_t type_index,
2032 bool is_referrers_class,
2033 bool is_initialized,
2034 uint32_t dex_pc)
2035 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2036 type_index_(type_index),
2037 is_referrers_class_(is_referrers_class),
2038 is_initialized_(is_initialized),
2039 dex_pc_(dex_pc) {}
2040
2041 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2042 return other->AsLoadClass()->type_index_ == type_index_;
2043 }
2044
2045 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
2046
2047 uint32_t GetDexPc() const { return dex_pc_; }
2048 uint16_t GetTypeIndex() const { return type_index_; }
2049
2050 bool NeedsInitialization() const {
2051 return !is_initialized_ && !is_referrers_class_;
2052 }
2053
2054 bool IsReferrersClass() const { return is_referrers_class_; }
2055
2056 DECLARE_INSTRUCTION(LoadClass);
2057
2058 private:
2059 const uint16_t type_index_;
2060 const bool is_referrers_class_;
2061 const bool is_initialized_;
2062 const uint32_t dex_pc_;
2063
2064 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
2065};
2066
2067// TODO: Pass this check to HInvokeStatic nodes.
2068/**
2069 * Performs an initialization check on its Class object input.
2070 */
2071class HClinitCheck : public HExpression<1> {
2072 public:
2073 explicit HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
2074 : HExpression(Primitive::kPrimNot, SideEffects::All()),
2075 dex_pc_(dex_pc) {
2076 SetRawInputAt(0, constant);
2077 }
2078
2079 bool NeedsEnvironment() const OVERRIDE {
2080 // May call runtime to initialize the class.
2081 return true;
2082 }
2083
2084 uint32_t GetDexPc() const { return dex_pc_; }
2085
2086 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
2087
2088 DECLARE_INSTRUCTION(ClinitCheck);
2089
2090 private:
2091 const uint32_t dex_pc_;
2092
2093 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
2094};
2095
2096class HStaticFieldGet : public HExpression<1> {
2097 public:
2098 HStaticFieldGet(HInstruction* cls,
2099 Primitive::Type field_type,
2100 MemberOffset field_offset)
2101 : HExpression(field_type, SideEffects::DependsOnSomething()),
2102 field_info_(field_offset, field_type) {
2103 SetRawInputAt(0, cls);
2104 }
2105
2106 bool CanBeMoved() const OVERRIDE { return true; }
2107 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2108 size_t other_offset = other->AsStaticFieldGet()->GetFieldOffset().SizeValue();
2109 return other_offset == GetFieldOffset().SizeValue();
2110 }
2111
2112 size_t ComputeHashCode() const OVERRIDE {
2113 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
2114 }
2115
2116 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
2117 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
2118
2119 DECLARE_INSTRUCTION(StaticFieldGet);
2120
2121 private:
2122 const FieldInfo field_info_;
2123
2124 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
2125};
2126
2127class HStaticFieldSet : public HTemplateInstruction<2> {
2128 public:
2129 HStaticFieldSet(HInstruction* cls,
2130 HInstruction* value,
2131 Primitive::Type field_type,
2132 MemberOffset field_offset)
2133 : HTemplateInstruction(SideEffects::ChangesSomething()),
2134 field_info_(field_offset, field_type) {
2135 SetRawInputAt(0, cls);
2136 SetRawInputAt(1, value);
2137 }
2138
2139 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
2140 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
2141
2142 DECLARE_INSTRUCTION(StaticFieldSet);
2143
2144 private:
2145 const FieldInfo field_info_;
2146
2147 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
2148};
2149
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002150class MoveOperands : public ArenaObject {
2151 public:
Nicolas Geoffray740475d2014-09-29 10:33:25 +01002152 MoveOperands(Location source, Location destination, HInstruction* instruction)
2153 : source_(source), destination_(destination), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002154
2155 Location GetSource() const { return source_; }
2156 Location GetDestination() const { return destination_; }
2157
2158 void SetSource(Location value) { source_ = value; }
2159 void SetDestination(Location value) { destination_ = value; }
2160
2161 // The parallel move resolver marks moves as "in-progress" by clearing the
2162 // destination (but not the source).
2163 Location MarkPending() {
2164 DCHECK(!IsPending());
2165 Location dest = destination_;
2166 destination_ = Location::NoLocation();
2167 return dest;
2168 }
2169
2170 void ClearPending(Location dest) {
2171 DCHECK(IsPending());
2172 destination_ = dest;
2173 }
2174
2175 bool IsPending() const {
2176 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
2177 return destination_.IsInvalid() && !source_.IsInvalid();
2178 }
2179
2180 // True if this blocks a move from the given location.
2181 bool Blocks(Location loc) const {
2182 return !IsEliminated() && source_.Equals(loc);
2183 }
2184
2185 // A move is redundant if it's been eliminated, if its source and
2186 // destination are the same, or if its destination is unneeded.
2187 bool IsRedundant() const {
2188 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
2189 }
2190
2191 // We clear both operands to indicate move that's been eliminated.
2192 void Eliminate() {
2193 source_ = destination_ = Location::NoLocation();
2194 }
2195
2196 bool IsEliminated() const {
2197 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
2198 return source_.IsInvalid();
2199 }
2200
Nicolas Geoffray740475d2014-09-29 10:33:25 +01002201 HInstruction* GetInstruction() const { return instruction_; }
2202
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002203 private:
2204 Location source_;
2205 Location destination_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01002206 // The instruction this move is assocatied with. Null when this move is
2207 // for moving an input in the expected locations of user (including a phi user).
2208 // This is only used in debug mode, to ensure we do not connect interval siblings
2209 // in the same parallel move.
2210 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002211
2212 DISALLOW_COPY_AND_ASSIGN(MoveOperands);
2213};
2214
2215static constexpr size_t kDefaultNumberOfMoves = 4;
2216
2217class HParallelMove : public HTemplateInstruction<0> {
2218 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002219 explicit HParallelMove(ArenaAllocator* arena)
2220 : HTemplateInstruction(SideEffects::None()), moves_(arena, kDefaultNumberOfMoves) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002221
2222 void AddMove(MoveOperands* move) {
Nicolas Geoffray740475d2014-09-29 10:33:25 +01002223 if (kIsDebugBuild && move->GetInstruction() != nullptr) {
2224 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
2225 DCHECK_NE(moves_.Get(i)->GetInstruction(), move->GetInstruction())
2226 << "Doing parallel moves for the same instruction.";
2227 }
2228 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002229 moves_.Add(move);
2230 }
2231
2232 MoveOperands* MoveOperandsAt(size_t index) const {
2233 return moves_.Get(index);
2234 }
2235
2236 size_t NumMoves() const { return moves_.Size(); }
2237
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002238 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002239
2240 private:
2241 GrowableArray<MoveOperands*> moves_;
2242
2243 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
2244};
2245
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002246class HGraphVisitor : public ValueObject {
2247 public:
Dave Allison20dfc792014-06-16 20:44:29 -07002248 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
2249 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002250
Dave Allison20dfc792014-06-16 20:44:29 -07002251 virtual void VisitInstruction(HInstruction* instruction) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002252 virtual void VisitBasicBlock(HBasicBlock* block);
2253
Roland Levillain633021e2014-10-01 14:12:25 +01002254 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002255 void VisitInsertionOrder();
2256
Roland Levillain633021e2014-10-01 14:12:25 +01002257 // Visit the graph following dominator tree reverse post-order.
2258 void VisitReversePostOrder();
2259
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002260 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002261
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002262 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002263#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002264 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
2265
2266 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
2267
2268#undef DECLARE_VISIT_INSTRUCTION
2269
2270 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07002271 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002272
2273 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
2274};
2275
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002276class HGraphDelegateVisitor : public HGraphVisitor {
2277 public:
2278 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
2279 virtual ~HGraphDelegateVisitor() {}
2280
2281 // Visit functions that delegate to to super class.
2282#define DECLARE_VISIT_INSTRUCTION(name, super) \
2283 virtual void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
2284
2285 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
2286
2287#undef DECLARE_VISIT_INSTRUCTION
2288
2289 private:
2290 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
2291};
2292
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002293class HInsertionOrderIterator : public ValueObject {
2294 public:
2295 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
2296
2297 bool Done() const { return index_ == graph_.GetBlocks().Size(); }
2298 HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); }
2299 void Advance() { ++index_; }
2300
2301 private:
2302 const HGraph& graph_;
2303 size_t index_;
2304
2305 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
2306};
2307
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002308class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002309 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002310 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002311
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002312 bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); }
2313 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002314 void Advance() { ++index_; }
2315
2316 private:
2317 const HGraph& graph_;
2318 size_t index_;
2319
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002320 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002321};
2322
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002323class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002324 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002325 explicit HPostOrderIterator(const HGraph& graph)
2326 : graph_(graph), index_(graph_.GetReversePostOrder().Size()) {}
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002327
2328 bool Done() const { return index_ == 0; }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002329 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002330 void Advance() { --index_; }
2331
2332 private:
2333 const HGraph& graph_;
2334 size_t index_;
2335
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002336 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002337};
2338
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002339} // namespace art
2340
2341#endif // ART_COMPILER_OPTIMIZING_NODES_H_