blob: c963b704929d5c4fa6ddeadd0846f84de95df82c [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 Geoffraye53798a2014-12-01 10:31:54 +000020#include "invoke_type.h"
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +010021#include "locations.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010022#include "offsets.h"
Ian Rogerse63db272014-07-15 15:36:11 -070023#include "primitive.h"
Ian Rogers0279ebb2014-10-08 17:27:48 -070024#include "utils/arena_object.h"
Nicolas Geoffray0e336432014-02-26 18:24:38 +000025#include "utils/arena_bit_vector.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000026#include "utils/growable_array.h"
27
28namespace art {
29
30class HBasicBlock;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010031class HEnvironment;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000032class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000033class HIntConstant;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000034class HInvoke;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000035class HGraphVisitor;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010036class HPhi;
Nicolas Geoffray3c049742014-09-24 18:10:46 +010037class HSuspendCheck;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010038class LiveInterval;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000039class LocationSummary;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000040
41static const int kDefaultNumberOfBlocks = 8;
42static const int kDefaultNumberOfSuccessors = 2;
43static const int kDefaultNumberOfPredecessors = 2;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010044static const int kDefaultNumberOfDominatedBlocks = 1;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000045static const int kDefaultNumberOfBackEdges = 1;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000046
Calin Juravle9aec02f2014-11-18 23:06:35 +000047static constexpr uint32_t kMaxIntShiftValue = 0x1f;
48static constexpr uint64_t kMaxLongShiftValue = 0x3f;
49
Dave Allison20dfc792014-06-16 20:44:29 -070050enum IfCondition {
51 kCondEQ,
52 kCondNE,
53 kCondLT,
54 kCondLE,
55 kCondGT,
56 kCondGE,
57};
58
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010059class HInstructionList {
60 public:
61 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
62
63 void AddInstruction(HInstruction* instruction);
64 void RemoveInstruction(HInstruction* instruction);
65
Roland Levillain6b469232014-09-25 10:10:38 +010066 // Return true if this list contains `instruction`.
67 bool Contains(HInstruction* instruction) const;
68
Roland Levillainccc07a92014-09-16 14:48:16 +010069 // Return true if `instruction1` is found before `instruction2` in
70 // this instruction list and false otherwise. Abort if none
71 // of these instructions is found.
72 bool FoundBefore(const HInstruction* instruction1,
73 const HInstruction* instruction2) const;
74
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010075 private:
76 HInstruction* first_instruction_;
77 HInstruction* last_instruction_;
78
79 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000080 friend class HGraph;
81 friend class HInstruction;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010082 friend class HInstructionIterator;
Nicolas Geoffray804d0932014-05-02 08:46:00 +010083 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010084
85 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
86};
87
Nicolas Geoffray818f2102014-02-18 16:43:35 +000088// Control-flow graph of a method. Contains a list of basic blocks.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070089class HGraph : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +000090 public:
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000091 HGraph(ArenaAllocator* arena, int start_instruction_id = 0)
Nicolas Geoffray818f2102014-02-18 16:43:35 +000092 : arena_(arena),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000093 blocks_(arena, kDefaultNumberOfBlocks),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +010094 reverse_post_order_(arena, kDefaultNumberOfBlocks),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070095 entry_block_(nullptr),
96 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +010097 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +010098 number_of_vregs_(0),
99 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000100 temporaries_vreg_slots_(0),
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000101 current_instruction_id_(start_instruction_id) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000102
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000103 ArenaAllocator* GetArena() const { return arena_; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100104 const GrowableArray<HBasicBlock*>& GetBlocks() const { return blocks_; }
Roland Levillain93445682014-10-06 19:24:02 +0100105 HBasicBlock* GetBlock(size_t id) const { return blocks_.Get(id); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000106
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000107 HBasicBlock* GetEntryBlock() const { return entry_block_; }
108 HBasicBlock* GetExitBlock() const { return exit_block_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000109
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000110 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
111 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000112
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000113 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100114
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000115 // Try building the SSA form of this graph, with dominance computation and loop
116 // recognition. Returns whether it was successful in doing all these steps.
117 bool TryBuildingSsa() {
118 BuildDominatorTree();
119 TransformToSsa();
120 return AnalyzeNaturalLoops();
121 }
122
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000123 void BuildDominatorTree();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000124 void TransformToSsa();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100125 void SimplifyCFG();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000126
Nicolas Geoffrayf5370122014-12-02 11:51:19 +0000127 // Analyze all natural loops in this graph. Returns false if one
128 // loop is not natural, that is the header does not dominate the
129 // back edge.
130 bool AnalyzeNaturalLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100131
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000132 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
133 void InlineInto(HGraph* outer_graph, HInvoke* invoke);
134
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100135 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
136 void SimplifyLoop(HBasicBlock* header);
137
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000138 int32_t GetNextInstructionId() {
139 DCHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000140 return current_instruction_id_++;
141 }
142
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000143 int32_t GetCurrentInstructionId() const {
144 return current_instruction_id_;
145 }
146
147 void SetCurrentInstructionId(int32_t id) {
148 current_instruction_id_ = id;
149 }
150
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100151 uint16_t GetMaximumNumberOfOutVRegs() const {
152 return maximum_number_of_out_vregs_;
153 }
154
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000155 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
156 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100157 }
158
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000159 void UpdateTemporariesVRegSlots(size_t slots) {
160 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100161 }
162
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000163 size_t GetTemporariesVRegSlots() const {
164 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100165 }
166
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100167 void SetNumberOfVRegs(uint16_t number_of_vregs) {
168 number_of_vregs_ = number_of_vregs;
169 }
170
171 uint16_t GetNumberOfVRegs() const {
172 return number_of_vregs_;
173 }
174
175 void SetNumberOfInVRegs(uint16_t value) {
176 number_of_in_vregs_ = value;
177 }
178
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100179 uint16_t GetNumberOfLocalVRegs() const {
180 return number_of_vregs_ - number_of_in_vregs_;
181 }
182
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100183 const GrowableArray<HBasicBlock*>& GetReversePostOrder() const {
184 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100185 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100186
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000187 private:
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000188 HBasicBlock* FindCommonDominator(HBasicBlock* first, HBasicBlock* second) const;
189 void VisitBlockForDominatorTree(HBasicBlock* block,
190 HBasicBlock* predecessor,
191 GrowableArray<size_t>* visits);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100192 void FindBackEdges(ArenaBitVector* visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000193 void VisitBlockForBackEdges(HBasicBlock* block,
194 ArenaBitVector* visited,
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100195 ArenaBitVector* visiting);
Roland Levillainfc600dc2014-12-02 17:16:31 +0000196 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000197 void RemoveDeadBlocks(const ArenaBitVector& visited) const;
198
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000199 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000200
201 // List of blocks in insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000202 GrowableArray<HBasicBlock*> blocks_;
203
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100204 // List of blocks to perform a reverse post order tree traversal.
205 GrowableArray<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000206
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000207 HBasicBlock* entry_block_;
208 HBasicBlock* exit_block_;
209
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100210 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100211 uint16_t maximum_number_of_out_vregs_;
212
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100213 // The number of virtual registers in this method. Contains the parameters.
214 uint16_t number_of_vregs_;
215
216 // The number of virtual registers used by parameters of this method.
217 uint16_t number_of_in_vregs_;
218
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000219 // Number of vreg size slots that the temporaries use (used in baseline compiler).
220 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100221
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000222 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000223 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000224
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000225 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000226 DISALLOW_COPY_AND_ASSIGN(HGraph);
227};
228
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700229class HLoopInformation : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000230 public:
231 HLoopInformation(HBasicBlock* header, HGraph* graph)
232 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100233 suspend_check_(nullptr),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100234 back_edges_(graph->GetArena(), kDefaultNumberOfBackEdges),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100235 // Make bit vector growable, as the number of blocks may change.
236 blocks_(graph->GetArena(), graph->GetBlocks().Size(), true) {}
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100237
238 HBasicBlock* GetHeader() const {
239 return header_;
240 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000241
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100242 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
243 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
244 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
245
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000246 void AddBackEdge(HBasicBlock* back_edge) {
247 back_edges_.Add(back_edge);
248 }
249
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100250 void RemoveBackEdge(HBasicBlock* back_edge) {
251 back_edges_.Delete(back_edge);
252 }
253
254 bool IsBackEdge(HBasicBlock* block) {
255 for (size_t i = 0, e = back_edges_.Size(); i < e; ++i) {
256 if (back_edges_.Get(i) == block) return true;
257 }
258 return false;
259 }
260
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000261 size_t NumberOfBackEdges() const {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000262 return back_edges_.Size();
263 }
264
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100265 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100266
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100267 const GrowableArray<HBasicBlock*>& GetBackEdges() const {
268 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100269 }
270
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100271 void ClearBackEdges() {
272 back_edges_.Reset();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100273 }
274
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100275 // Find blocks that are part of this loop. Returns whether the loop is a natural loop,
276 // that is the header dominates the back edge.
277 bool Populate();
278
279 // Returns whether this loop information contains `block`.
280 // Note that this loop information *must* be populated before entering this function.
281 bool Contains(const HBasicBlock& block) const;
282
283 // Returns whether this loop information is an inner loop of `other`.
284 // Note that `other` *must* be populated before entering this function.
285 bool IsIn(const HLoopInformation& other) const;
286
287 const ArenaBitVector& GetBlocks() const { return blocks_; }
288
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000289 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100290 // Internal recursive implementation of `Populate`.
291 void PopulateRecursive(HBasicBlock* block);
292
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000293 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100294 HSuspendCheck* suspend_check_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000295 GrowableArray<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100296 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000297
298 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
299};
300
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100301static constexpr size_t kNoLifetime = -1;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100302static constexpr uint32_t kNoDexPc = -1;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100303
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000304// A block in a method. Contains the list of instructions represented
305// as a double linked list. Each block knows its predecessors and
306// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100307
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700308class HBasicBlock : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000309 public:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100310 explicit HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000311 : graph_(graph),
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000312 predecessors_(graph->GetArena(), kDefaultNumberOfPredecessors),
313 successors_(graph->GetArena(), kDefaultNumberOfSuccessors),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000314 loop_information_(nullptr),
315 dominator_(nullptr),
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100316 dominated_blocks_(graph->GetArena(), kDefaultNumberOfDominatedBlocks),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100317 block_id_(-1),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100318 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100319 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000320 lifetime_end_(kNoLifetime),
321 is_catch_block_(false) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000322
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100323 const GrowableArray<HBasicBlock*>& GetPredecessors() const {
324 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000325 }
326
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100327 const GrowableArray<HBasicBlock*>& GetSuccessors() const {
328 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000329 }
330
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100331 const GrowableArray<HBasicBlock*>& GetDominatedBlocks() const {
332 return dominated_blocks_;
333 }
334
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100335 bool IsEntryBlock() const {
336 return graph_->GetEntryBlock() == this;
337 }
338
339 bool IsExitBlock() const {
340 return graph_->GetExitBlock() == this;
341 }
342
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000343 void AddBackEdge(HBasicBlock* back_edge) {
344 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000345 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000346 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100347 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000348 loop_information_->AddBackEdge(back_edge);
349 }
350
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000351 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000352
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000353 int GetBlockId() const { return block_id_; }
354 void SetBlockId(int id) { block_id_ = id; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000355
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000356 HBasicBlock* GetDominator() const { return dominator_; }
357 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100358 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.Add(block); }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000359
360 int NumberOfBackEdges() const {
361 return loop_information_ == nullptr
362 ? 0
363 : loop_information_->NumberOfBackEdges();
364 }
365
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100366 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
367 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100368 const HInstructionList& GetInstructions() const { return instructions_; }
369 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100370 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000371
372 void AddSuccessor(HBasicBlock* block) {
373 successors_.Add(block);
374 block->predecessors_.Add(this);
375 }
376
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100377 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
378 size_t successor_index = GetSuccessorIndexOf(existing);
379 DCHECK_NE(successor_index, static_cast<size_t>(-1));
380 existing->RemovePredecessor(this);
381 new_block->predecessors_.Add(this);
382 successors_.Put(successor_index, new_block);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000383 }
384
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100385 void RemovePredecessor(HBasicBlock* block) {
386 predecessors_.Delete(block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100387 }
388
389 void ClearAllPredecessors() {
390 predecessors_.Reset();
391 }
392
393 void AddPredecessor(HBasicBlock* block) {
394 predecessors_.Add(block);
395 block->successors_.Add(this);
396 }
397
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100398 void SwapPredecessors() {
Nicolas Geoffrayc83d4412014-09-18 16:46:20 +0100399 DCHECK_EQ(predecessors_.Size(), 2u);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100400 HBasicBlock* temp = predecessors_.Get(0);
401 predecessors_.Put(0, predecessors_.Get(1));
402 predecessors_.Put(1, temp);
403 }
404
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100405 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) {
406 for (size_t i = 0, e = predecessors_.Size(); i < e; ++i) {
407 if (predecessors_.Get(i) == predecessor) {
408 return i;
409 }
410 }
411 return -1;
412 }
413
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100414 size_t GetSuccessorIndexOf(HBasicBlock* successor) {
415 for (size_t i = 0, e = successors_.Size(); i < e; ++i) {
416 if (successors_.Get(i) == successor) {
417 return i;
418 }
419 }
420 return -1;
421 }
422
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000423 void AddInstruction(HInstruction* instruction);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100424 void RemoveInstruction(HInstruction* instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100425 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +0100426 // Replace instruction `initial` with `replacement` within this block.
427 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
428 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100429 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100430 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100431 void RemovePhi(HPhi* phi);
432
433 bool IsLoopHeader() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100434 return (loop_information_ != nullptr) && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100435 }
436
Roland Levillain6b879dd2014-09-22 17:13:44 +0100437 bool IsLoopPreHeaderFirstPredecessor() const {
438 DCHECK(IsLoopHeader());
439 DCHECK(!GetPredecessors().IsEmpty());
440 return GetPredecessors().Get(0) == GetLoopInformation()->GetPreHeader();
441 }
442
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100443 HLoopInformation* GetLoopInformation() const {
444 return loop_information_;
445 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000446
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100447 // Set the loop_information_ on this block. This method overrides the current
448 // loop_information if it is an outer loop of the passed loop information.
449 void SetInLoop(HLoopInformation* info) {
450 if (IsLoopHeader()) {
451 // Nothing to do. This just means `info` is an outer loop.
452 } else if (loop_information_ == nullptr) {
453 loop_information_ = info;
454 } else if (loop_information_->Contains(*info->GetHeader())) {
455 // Block is currently part of an outer loop. Make it part of this inner loop.
456 // Note that a non loop header having a loop information means this loop information
457 // has already been populated
458 loop_information_ = info;
459 } else {
460 // Block is part of an inner loop. Do not update the loop information.
461 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
462 // at this point, because this method is being called while populating `info`.
463 }
464 }
465
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100466 bool IsInLoop() const { return loop_information_ != nullptr; }
467
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100468 // Returns wheter this block dominates the blocked passed as parameter.
469 bool Dominates(HBasicBlock* block) const;
470
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100471 size_t GetLifetimeStart() const { return lifetime_start_; }
472 size_t GetLifetimeEnd() const { return lifetime_end_; }
473
474 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
475 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
476
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100477 uint32_t GetDexPc() const { return dex_pc_; }
478
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000479 bool IsCatchBlock() const { return is_catch_block_; }
480 void SetIsCatchBlock() { is_catch_block_ = true; }
481
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000482 private:
483 HGraph* const graph_;
484 GrowableArray<HBasicBlock*> predecessors_;
485 GrowableArray<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100486 HInstructionList instructions_;
487 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000488 HLoopInformation* loop_information_;
489 HBasicBlock* dominator_;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100490 GrowableArray<HBasicBlock*> dominated_blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000491 int block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100492 // The dex program counter of the first instruction of this block.
493 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100494 size_t lifetime_start_;
495 size_t lifetime_end_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000496 bool is_catch_block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000497
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000498 friend class HGraph;
499 friend class HInstruction;
500
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000501 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
502};
503
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100504#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
505 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000506 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000507 M(ArrayGet, Instruction) \
508 M(ArrayLength, Instruction) \
509 M(ArraySet, Instruction) \
510 M(BoundsCheck, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000511 M(CheckCast, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100512 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000513 M(Compare, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100514 M(Condition, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000515 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +0000516 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000517 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100518 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000519 M(Exit, Instruction) \
520 M(FloatConstant, Constant) \
521 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100522 M(GreaterThan, Condition) \
523 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100524 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000525 M(InstanceFieldGet, Instruction) \
526 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000527 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100528 M(IntConstant, Constant) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +0000529 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000530 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100531 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000532 M(LessThan, Condition) \
533 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000534 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000535 M(LoadException, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100536 M(LoadLocal, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000537 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100538 M(Local, Instruction) \
539 M(LongConstant, Constant) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +0000540 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000541 M(Mul, BinaryOperation) \
542 M(Neg, UnaryOperation) \
543 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100544 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +0100545 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000546 M(NotEqual, Condition) \
547 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000548 M(Or, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100549 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000550 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100551 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000552 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100553 M(Return, Instruction) \
554 M(ReturnVoid, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000555 M(Shl, BinaryOperation) \
556 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100557 M(StaticFieldGet, Instruction) \
558 M(StaticFieldSet, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100559 M(StoreLocal, Instruction) \
560 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100561 M(SuspendCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000562 M(Temporary, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000563 M(Throw, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +0000564 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000565 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000566 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000567
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100568#define FOR_EACH_INSTRUCTION(M) \
569 FOR_EACH_CONCRETE_INSTRUCTION(M) \
570 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +0100571 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +0100572 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100573 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -0700574
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100575#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000576FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
577#undef FORWARD_DECLARATION
578
Roland Levillainccc07a92014-09-16 14:48:16 +0100579#define DECLARE_INSTRUCTION(type) \
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100580 virtual InstructionKind GetKind() const { return k##type; } \
Roland Levillainccc07a92014-09-16 14:48:16 +0100581 virtual const char* DebugName() const { return #type; } \
582 virtual const H##type* As##type() const OVERRIDE { return this; } \
583 virtual H##type* As##type() OVERRIDE { return this; } \
584 virtual bool InstructionTypeEquals(HInstruction* other) const { \
585 return other->Is##type(); \
586 } \
587 virtual void Accept(HGraphVisitor* visitor)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000588
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100589template <typename T>
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700590class HUseListNode : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000591 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100592 HUseListNode(T* user, size_t index, HUseListNode* tail)
Dave Allison20dfc792014-06-16 20:44:29 -0700593 : user_(user), index_(index), tail_(tail) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000594
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000595 HUseListNode* GetTail() const { return tail_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100596 T* GetUser() const { return user_; }
597 size_t GetIndex() const { return index_; }
598
599 void SetTail(HUseListNode<T>* node) { tail_ = node; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000600
601 private:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100602 T* const user_;
603 const size_t index_;
604 HUseListNode<T>* tail_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000605
606 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
607};
608
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100609// Represents the side effects an instruction may have.
610class SideEffects : public ValueObject {
611 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100612 SideEffects() : flags_(0) {}
613
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100614 static SideEffects None() {
615 return SideEffects(0);
616 }
617
618 static SideEffects All() {
619 return SideEffects(ChangesSomething().flags_ | DependsOnSomething().flags_);
620 }
621
622 static SideEffects ChangesSomething() {
623 return SideEffects((1 << kFlagChangesCount) - 1);
624 }
625
626 static SideEffects DependsOnSomething() {
627 int count = kFlagDependsOnCount - kFlagChangesCount;
628 return SideEffects(((1 << count) - 1) << kFlagChangesCount);
629 }
630
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100631 SideEffects Union(SideEffects other) const {
632 return SideEffects(flags_ | other.flags_);
633 }
634
Roland Levillain72bceff2014-09-15 18:29:00 +0100635 bool HasSideEffects() const {
636 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
637 return (flags_ & all_bits_set) != 0;
638 }
639
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100640 bool HasAllSideEffects() const {
641 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
642 return all_bits_set == (flags_ & all_bits_set);
643 }
644
645 bool DependsOn(SideEffects other) const {
646 size_t depends_flags = other.ComputeDependsFlags();
647 return (flags_ & depends_flags) != 0;
648 }
649
650 bool HasDependencies() const {
651 int count = kFlagDependsOnCount - kFlagChangesCount;
652 size_t all_bits_set = (1 << count) - 1;
653 return ((flags_ >> kFlagChangesCount) & all_bits_set) != 0;
654 }
655
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100656 private:
657 static constexpr int kFlagChangesSomething = 0;
658 static constexpr int kFlagChangesCount = kFlagChangesSomething + 1;
659
660 static constexpr int kFlagDependsOnSomething = kFlagChangesCount;
661 static constexpr int kFlagDependsOnCount = kFlagDependsOnSomething + 1;
662
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100663 explicit SideEffects(size_t flags) : flags_(flags) {}
664
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100665 size_t ComputeDependsFlags() const {
666 return flags_ << kFlagChangesCount;
667 }
668
669 size_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100670};
671
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700672class HInstruction : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000673 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100674 explicit HInstruction(SideEffects side_effects)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000675 : previous_(nullptr),
676 next_(nullptr),
677 block_(nullptr),
678 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100679 ssa_index_(-1),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000680 uses_(nullptr),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100681 env_uses_(nullptr),
682 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100683 locations_(nullptr),
684 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100685 lifetime_position_(kNoLifetime),
686 side_effects_(side_effects) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000687
Dave Allison20dfc792014-06-16 20:44:29 -0700688 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000689
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100690#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100691 enum InstructionKind {
692 FOR_EACH_INSTRUCTION(DECLARE_KIND)
693 };
694#undef DECLARE_KIND
695
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000696 HInstruction* GetNext() const { return next_; }
697 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000698
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000699 HBasicBlock* GetBlock() const { return block_; }
700 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100701 bool IsInBlock() const { return block_ != nullptr; }
702 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +0100703 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000704
Roland Levillain6b879dd2014-09-22 17:13:44 +0100705 virtual size_t InputCount() const = 0;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100706 virtual HInstruction* InputAt(size_t i) const = 0;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000707
708 virtual void Accept(HGraphVisitor* visitor) = 0;
709 virtual const char* DebugName() const = 0;
710
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100711 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100712 virtual void SetRawInputAt(size_t index, HInstruction* input) = 0;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100713
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100714 virtual bool NeedsEnvironment() const { return false; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100715 virtual bool IsControlFlow() const { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +0100716 virtual bool CanThrow() const { return false; }
Roland Levillain72bceff2014-09-15 18:29:00 +0100717 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100718
719 void AddUseAt(HInstruction* user, size_t index) {
720 uses_ = new (block_->GetGraph()->GetArena()) HUseListNode<HInstruction>(user, index, uses_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000721 }
722
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100723 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +0100724 DCHECK(user != nullptr);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100725 env_uses_ = new (block_->GetGraph()->GetArena()) HUseListNode<HEnvironment>(
726 user, index, env_uses_);
727 }
728
729 void RemoveUser(HInstruction* user, size_t index);
Nicolas Geoffray724c9632014-09-22 12:27:27 +0100730 void RemoveEnvironmentUser(HEnvironment* user, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100731
732 HUseListNode<HInstruction>* GetUses() const { return uses_; }
733 HUseListNode<HEnvironment>* GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000734
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100735 bool HasUses() const { return uses_ != nullptr || env_uses_ != nullptr; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100736 bool HasEnvironmentUses() const { return env_uses_ != nullptr; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000737
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100738 size_t NumberOfUses() const {
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100739 // TODO: Optimize this method if it is used outside of the HGraphVisualizer.
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100740 size_t result = 0;
741 HUseListNode<HInstruction>* current = uses_;
742 while (current != nullptr) {
743 current = current->GetTail();
744 ++result;
745 }
746 return result;
747 }
748
Roland Levillain6c82d402014-10-13 16:10:27 +0100749 // Does this instruction strictly dominate `other_instruction`?
750 // Returns false if this instruction and `other_instruction` are the same.
751 // Aborts if this instruction and `other_instruction` are both phis.
752 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +0100753
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000754 int GetId() const { return id_; }
755 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000756
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100757 int GetSsaIndex() const { return ssa_index_; }
758 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
759 bool HasSsaIndex() const { return ssa_index_ != -1; }
760
761 bool HasEnvironment() const { return environment_ != nullptr; }
762 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100763 void SetEnvironment(HEnvironment* environment) { environment_ = environment; }
764
Nicolas Geoffray39468442014-09-02 15:17:15 +0100765 // Returns the number of entries in the environment. Typically, that is the
766 // number of dex registers in a method. It could be more in case of inlining.
767 size_t EnvironmentSize() const;
768
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000769 LocationSummary* GetLocations() const { return locations_; }
770 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000771
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100772 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100773 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100774
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000775 // Insert `this` instruction in `cursor`'s graph, just before `cursor`.
776 void InsertBefore(HInstruction* cursor);
777
Dave Allison20dfc792014-06-16 20:44:29 -0700778 bool HasOnlyOneUse() const {
779 return uses_ != nullptr && uses_->GetTail() == nullptr;
780 }
781
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100782#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +0100783 bool Is##type() const { return (As##type() != nullptr); } \
784 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000785 virtual H##type* As##type() { return nullptr; }
786
787 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
788#undef INSTRUCTION_TYPE_CHECK
789
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100790 // Returns whether the instruction can be moved within the graph.
791 virtual bool CanBeMoved() const { return false; }
792
793 // Returns whether the two instructions are of the same kind.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700794 virtual bool InstructionTypeEquals(HInstruction* other) const {
795 UNUSED(other);
796 return false;
797 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100798
799 // Returns whether any data encoded in the two instructions is equal.
800 // This method does not look at the inputs. Both instructions must be
801 // of the same type, otherwise the method has undefined behavior.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700802 virtual bool InstructionDataEquals(HInstruction* other) const {
803 UNUSED(other);
804 return false;
805 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100806
807 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +0000808 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100809 // 2) Their inputs are identical.
810 bool Equals(HInstruction* other) const;
811
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100812 virtual InstructionKind GetKind() const = 0;
813
814 virtual size_t ComputeHashCode() const {
815 size_t result = GetKind();
816 for (size_t i = 0, e = InputCount(); i < e; ++i) {
817 result = (result * 31) + InputAt(i)->GetId();
818 }
819 return result;
820 }
821
822 SideEffects GetSideEffects() const { return side_effects_; }
823
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100824 size_t GetLifetimePosition() const { return lifetime_position_; }
825 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
826 LiveInterval* GetLiveInterval() const { return live_interval_; }
827 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
828 bool HasLiveInterval() const { return live_interval_ != nullptr; }
829
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000830 private:
831 HInstruction* previous_;
832 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000833 HBasicBlock* block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000834
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000835 // An instruction gets an id when it is added to the graph.
836 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +0100837 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000838 int id_;
839
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100840 // When doing liveness analysis, instructions that have uses get an SSA index.
841 int ssa_index_;
842
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100843 // List of instructions that have this instruction as input.
844 HUseListNode<HInstruction>* uses_;
845
846 // List of environments that contain this instruction.
847 HUseListNode<HEnvironment>* env_uses_;
848
Nicolas Geoffray39468442014-09-02 15:17:15 +0100849 // The environment associated with this instruction. Not null if the instruction
850 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100851 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000852
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000853 // Set by the code generator.
854 LocationSummary* locations_;
855
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100856 // Set by the liveness analysis.
857 LiveInterval* live_interval_;
858
859 // Set by the liveness analysis, this is the position in a linear
860 // order of blocks where this instruction's live interval start.
861 size_t lifetime_position_;
862
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100863 const SideEffects side_effects_;
864
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000865 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000866 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100867 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000868
869 DISALLOW_COPY_AND_ASSIGN(HInstruction);
870};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700871std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000872
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100873template<typename T>
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000874class HUseIterator : public ValueObject {
875 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100876 explicit HUseIterator(HUseListNode<T>* uses) : current_(uses) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000877
878 bool Done() const { return current_ == nullptr; }
879
880 void Advance() {
881 DCHECK(!Done());
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000882 current_ = current_->GetTail();
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000883 }
884
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100885 HUseListNode<T>* Current() const {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000886 DCHECK(!Done());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100887 return current_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000888 }
889
890 private:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100891 HUseListNode<T>* current_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000892
893 friend class HValue;
894};
895
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100896// A HEnvironment object contains the values of virtual registers at a given location.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700897class HEnvironment : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100898 public:
899 HEnvironment(ArenaAllocator* arena, size_t number_of_vregs) : vregs_(arena, number_of_vregs) {
900 vregs_.SetSize(number_of_vregs);
901 for (size_t i = 0; i < number_of_vregs; i++) {
902 vregs_.Put(i, nullptr);
903 }
904 }
905
906 void Populate(const GrowableArray<HInstruction*>& env) {
907 for (size_t i = 0; i < env.Size(); i++) {
908 HInstruction* instruction = env.Get(i);
909 vregs_.Put(i, instruction);
910 if (instruction != nullptr) {
911 instruction->AddEnvUseAt(this, i);
912 }
913 }
914 }
915
916 void SetRawEnvAt(size_t index, HInstruction* instruction) {
917 vregs_.Put(index, instruction);
918 }
919
Nicolas Geoffray39468442014-09-02 15:17:15 +0100920 HInstruction* GetInstructionAt(size_t index) const {
921 return vregs_.Get(index);
922 }
923
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100924 GrowableArray<HInstruction*>* GetVRegs() {
925 return &vregs_;
926 }
927
Nicolas Geoffray39468442014-09-02 15:17:15 +0100928 size_t Size() const { return vregs_.Size(); }
929
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100930 private:
931 GrowableArray<HInstruction*> vregs_;
932
933 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
934};
935
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000936class HInputIterator : public ValueObject {
937 public:
Dave Allison20dfc792014-06-16 20:44:29 -0700938 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000939
940 bool Done() const { return index_ == instruction_->InputCount(); }
941 HInstruction* Current() const { return instruction_->InputAt(index_); }
942 void Advance() { index_++; }
943
944 private:
945 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100946 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000947
948 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
949};
950
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000951class HInstructionIterator : public ValueObject {
952 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100953 explicit HInstructionIterator(const HInstructionList& instructions)
954 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000955 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000956 }
957
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000958 bool Done() const { return instruction_ == nullptr; }
959 HInstruction* Current() const { return instruction_; }
960 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000961 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000962 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000963 }
964
965 private:
966 HInstruction* instruction_;
967 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100968
969 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000970};
971
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100972class HBackwardInstructionIterator : public ValueObject {
973 public:
974 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
975 : instruction_(instructions.last_instruction_) {
976 next_ = Done() ? nullptr : instruction_->GetPrevious();
977 }
978
979 bool Done() const { return instruction_ == nullptr; }
980 HInstruction* Current() const { return instruction_; }
981 void Advance() {
982 instruction_ = next_;
983 next_ = Done() ? nullptr : instruction_->GetPrevious();
984 }
985
986 private:
987 HInstruction* instruction_;
988 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100989
990 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100991};
992
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000993// An embedded container with N elements of type T. Used (with partial
994// specialization for N=0) because embedded arrays cannot have size 0.
995template<typename T, intptr_t N>
996class EmbeddedArray {
997 public:
Dave Allison20dfc792014-06-16 20:44:29 -0700998 EmbeddedArray() : elements_() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000999
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001000 intptr_t GetLength() const { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001001
1002 const T& operator[](intptr_t i) const {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001003 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001004 return elements_[i];
1005 }
1006
1007 T& operator[](intptr_t i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001008 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001009 return elements_[i];
1010 }
1011
1012 const T& At(intptr_t i) const {
1013 return (*this)[i];
1014 }
1015
1016 void SetAt(intptr_t i, const T& val) {
1017 (*this)[i] = val;
1018 }
1019
1020 private:
1021 T elements_[N];
1022};
1023
1024template<typename T>
1025class EmbeddedArray<T, 0> {
1026 public:
1027 intptr_t length() const { return 0; }
1028 const T& operator[](intptr_t i) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001029 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001030 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001031 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001032 }
1033 T& operator[](intptr_t i) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001034 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001035 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001036 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001037 }
1038};
1039
1040template<intptr_t N>
1041class HTemplateInstruction: public HInstruction {
1042 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001043 HTemplateInstruction<N>(SideEffects side_effects)
1044 : HInstruction(side_effects), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07001045 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001046
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001047 virtual size_t InputCount() const { return N; }
1048 virtual HInstruction* InputAt(size_t i) const { return inputs_[i]; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001049
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001050 protected:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001051 virtual void SetRawInputAt(size_t i, HInstruction* instruction) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001052 inputs_[i] = instruction;
1053 }
1054
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001055 private:
1056 EmbeddedArray<HInstruction*, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001057
1058 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001059};
1060
Dave Allison20dfc792014-06-16 20:44:29 -07001061template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001062class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07001063 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001064 HExpression<N>(Primitive::Type type, SideEffects side_effects)
1065 : HTemplateInstruction<N>(side_effects), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07001066 virtual ~HExpression() {}
1067
1068 virtual Primitive::Type GetType() const { return type_; }
1069
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001070 protected:
1071 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07001072};
1073
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001074// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
1075// instruction that branches to the exit block.
1076class HReturnVoid : public HTemplateInstruction<0> {
1077 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001078 HReturnVoid() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001079
1080 virtual bool IsControlFlow() const { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001081
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001082 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001083
1084 private:
1085 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
1086};
1087
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001088// Represents dex's RETURN opcodes. A HReturn is a control flow
1089// instruction that branches to the exit block.
1090class HReturn : public HTemplateInstruction<1> {
1091 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001092 explicit HReturn(HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001093 SetRawInputAt(0, value);
1094 }
1095
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001096 virtual bool IsControlFlow() const { return true; }
1097
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001098 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001099
1100 private:
1101 DISALLOW_COPY_AND_ASSIGN(HReturn);
1102};
1103
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001104// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001105// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001106// exit block.
1107class HExit : public HTemplateInstruction<0> {
1108 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001109 HExit() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001110
1111 virtual bool IsControlFlow() const { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001112
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001113 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001114
1115 private:
1116 DISALLOW_COPY_AND_ASSIGN(HExit);
1117};
1118
1119// Jumps from one block to another.
1120class HGoto : public HTemplateInstruction<0> {
1121 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001122 HGoto() : HTemplateInstruction(SideEffects::None()) {}
1123
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001124 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001125
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001126 HBasicBlock* GetSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001127 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001128 }
1129
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001130 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001131
1132 private:
1133 DISALLOW_COPY_AND_ASSIGN(HGoto);
1134};
1135
Dave Allison20dfc792014-06-16 20:44:29 -07001136
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001137// Conditional branch. A block ending with an HIf instruction must have
1138// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001139class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001140 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001141 explicit HIf(HInstruction* input) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001142 SetRawInputAt(0, input);
1143 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001144
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001145 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001146
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001147 HBasicBlock* IfTrueSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001148 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001149 }
1150
1151 HBasicBlock* IfFalseSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001152 return GetBlock()->GetSuccessors().Get(1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001153 }
1154
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001155 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001156
Dave Allison20dfc792014-06-16 20:44:29 -07001157 virtual bool IsIfInstruction() const { return true; }
1158
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001159 private:
1160 DISALLOW_COPY_AND_ASSIGN(HIf);
1161};
1162
Roland Levillain88cb1752014-10-20 16:36:47 +01001163class HUnaryOperation : public HExpression<1> {
1164 public:
1165 HUnaryOperation(Primitive::Type result_type, HInstruction* input)
1166 : HExpression(result_type, SideEffects::None()) {
1167 SetRawInputAt(0, input);
1168 }
1169
1170 HInstruction* GetInput() const { return InputAt(0); }
1171 Primitive::Type GetResultType() const { return GetType(); }
1172
1173 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001174 virtual bool InstructionDataEquals(HInstruction* other) const {
1175 UNUSED(other);
1176 return true;
1177 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001178
Roland Levillain9240d6a2014-10-20 16:47:04 +01001179 // Try to statically evaluate `operation` and return a HConstant
1180 // containing the result of this evaluation. If `operation` cannot
1181 // be evaluated as a constant, return nullptr.
1182 HConstant* TryStaticEvaluation() const;
1183
1184 // Apply this operation to `x`.
1185 virtual int32_t Evaluate(int32_t x) const = 0;
1186 virtual int64_t Evaluate(int64_t x) const = 0;
1187
Roland Levillain88cb1752014-10-20 16:36:47 +01001188 DECLARE_INSTRUCTION(UnaryOperation);
1189
1190 private:
1191 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
1192};
1193
Dave Allison20dfc792014-06-16 20:44:29 -07001194class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001195 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001196 HBinaryOperation(Primitive::Type result_type,
1197 HInstruction* left,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001198 HInstruction* right) : HExpression(result_type, SideEffects::None()) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001199 SetRawInputAt(0, left);
1200 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001201 }
1202
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001203 HInstruction* GetLeft() const { return InputAt(0); }
1204 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07001205 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001206
1207 virtual bool IsCommutative() { return false; }
1208
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001209 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001210 virtual bool InstructionDataEquals(HInstruction* other) const {
1211 UNUSED(other);
1212 return true;
1213 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001214
Roland Levillain9240d6a2014-10-20 16:47:04 +01001215 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01001216 // containing the result of this evaluation. If `operation` cannot
1217 // be evaluated as a constant, return nullptr.
Roland Levillain9240d6a2014-10-20 16:47:04 +01001218 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01001219
1220 // Apply this operation to `x` and `y`.
1221 virtual int32_t Evaluate(int32_t x, int32_t y) const = 0;
1222 virtual int64_t Evaluate(int64_t x, int64_t y) const = 0;
1223
Roland Levillainccc07a92014-09-16 14:48:16 +01001224 DECLARE_INSTRUCTION(BinaryOperation);
1225
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001226 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001227 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
1228};
1229
Dave Allison20dfc792014-06-16 20:44:29 -07001230class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001231 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001232 HCondition(HInstruction* first, HInstruction* second)
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001233 : HBinaryOperation(Primitive::kPrimBoolean, first, second),
1234 needs_materialization_(true) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001235
1236 virtual bool IsCommutative() { return true; }
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001237
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001238 bool NeedsMaterialization() const { return needs_materialization_; }
1239 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001240
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001241 // For code generation purposes, returns whether this instruction is just before
1242 // `if_`, and disregard moves in between.
1243 bool IsBeforeWhenDisregardMoves(HIf* if_) const;
1244
Dave Allison20dfc792014-06-16 20:44:29 -07001245 DECLARE_INSTRUCTION(Condition);
1246
1247 virtual IfCondition GetCondition() const = 0;
1248
1249 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001250 // For register allocation purposes, returns whether this instruction needs to be
1251 // materialized (that is, not just be in the processor flags).
1252 bool needs_materialization_;
1253
Dave Allison20dfc792014-06-16 20:44:29 -07001254 DISALLOW_COPY_AND_ASSIGN(HCondition);
1255};
1256
1257// Instruction to check if two inputs are equal to each other.
1258class HEqual : public HCondition {
1259 public:
1260 HEqual(HInstruction* first, HInstruction* second)
1261 : HCondition(first, second) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001262
Roland Levillain93445682014-10-06 19:24:02 +01001263 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1264 return x == y ? 1 : 0;
1265 }
1266 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1267 return x == y ? 1 : 0;
1268 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001269
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001270 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001271
Dave Allison20dfc792014-06-16 20:44:29 -07001272 virtual IfCondition GetCondition() const {
1273 return kCondEQ;
1274 }
1275
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001276 private:
1277 DISALLOW_COPY_AND_ASSIGN(HEqual);
1278};
1279
Dave Allison20dfc792014-06-16 20:44:29 -07001280class HNotEqual : public HCondition {
1281 public:
1282 HNotEqual(HInstruction* first, HInstruction* second)
1283 : HCondition(first, second) {}
1284
Roland Levillain93445682014-10-06 19:24:02 +01001285 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1286 return x != y ? 1 : 0;
1287 }
1288 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1289 return x != y ? 1 : 0;
1290 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001291
Dave Allison20dfc792014-06-16 20:44:29 -07001292 DECLARE_INSTRUCTION(NotEqual);
1293
1294 virtual IfCondition GetCondition() const {
1295 return kCondNE;
1296 }
1297
1298 private:
1299 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
1300};
1301
1302class HLessThan : public HCondition {
1303 public:
1304 HLessThan(HInstruction* first, HInstruction* second)
1305 : HCondition(first, second) {}
1306
Roland Levillain93445682014-10-06 19:24:02 +01001307 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1308 return x < y ? 1 : 0;
1309 }
1310 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1311 return x < y ? 1 : 0;
1312 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001313
Dave Allison20dfc792014-06-16 20:44:29 -07001314 DECLARE_INSTRUCTION(LessThan);
1315
1316 virtual IfCondition GetCondition() const {
1317 return kCondLT;
1318 }
1319
1320 private:
1321 DISALLOW_COPY_AND_ASSIGN(HLessThan);
1322};
1323
1324class HLessThanOrEqual : public HCondition {
1325 public:
1326 HLessThanOrEqual(HInstruction* first, HInstruction* second)
1327 : HCondition(first, second) {}
1328
Roland Levillain93445682014-10-06 19:24:02 +01001329 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1330 return x <= y ? 1 : 0;
1331 }
1332 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1333 return x <= y ? 1 : 0;
1334 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001335
Dave Allison20dfc792014-06-16 20:44:29 -07001336 DECLARE_INSTRUCTION(LessThanOrEqual);
1337
1338 virtual IfCondition GetCondition() const {
1339 return kCondLE;
1340 }
1341
1342 private:
1343 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
1344};
1345
1346class HGreaterThan : public HCondition {
1347 public:
1348 HGreaterThan(HInstruction* first, HInstruction* second)
1349 : HCondition(first, second) {}
1350
Roland Levillain93445682014-10-06 19:24:02 +01001351 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1352 return x > y ? 1 : 0;
1353 }
1354 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1355 return x > y ? 1 : 0;
1356 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001357
Dave Allison20dfc792014-06-16 20:44:29 -07001358 DECLARE_INSTRUCTION(GreaterThan);
1359
1360 virtual IfCondition GetCondition() const {
1361 return kCondGT;
1362 }
1363
1364 private:
1365 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
1366};
1367
1368class HGreaterThanOrEqual : public HCondition {
1369 public:
1370 HGreaterThanOrEqual(HInstruction* first, HInstruction* second)
1371 : HCondition(first, second) {}
1372
Roland Levillain93445682014-10-06 19:24:02 +01001373 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1374 return x >= y ? 1 : 0;
1375 }
1376 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1377 return x >= y ? 1 : 0;
1378 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001379
Dave Allison20dfc792014-06-16 20:44:29 -07001380 DECLARE_INSTRUCTION(GreaterThanOrEqual);
1381
1382 virtual IfCondition GetCondition() const {
1383 return kCondGE;
1384 }
1385
1386 private:
1387 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
1388};
1389
1390
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001391// Instruction to check how two inputs compare to each other.
1392// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
1393class HCompare : public HBinaryOperation {
1394 public:
Calin Juravleddb7df22014-11-25 20:56:51 +00001395 // The bias applies for floating point operations and indicates how NaN
1396 // comparisons are treated:
1397 enum Bias {
1398 kNoBias, // bias is not applicable (i.e. for long operation)
1399 kGtBias, // return 1 for NaN comparisons
1400 kLtBias, // return -1 for NaN comparisons
1401 };
1402
1403 HCompare(Primitive::Type type, HInstruction* first, HInstruction* second, Bias bias)
1404 : HBinaryOperation(Primitive::kPrimInt, first, second), bias_(bias) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001405 DCHECK_EQ(type, first->GetType());
1406 DCHECK_EQ(type, second->GetType());
1407 }
1408
Calin Juravleddb7df22014-11-25 20:56:51 +00001409 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01001410 return
1411 x == y ? 0 :
1412 x > y ? 1 :
1413 -1;
1414 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001415
1416 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01001417 return
1418 x == y ? 0 :
1419 x > y ? 1 :
1420 -1;
1421 }
1422
Calin Juravleddb7df22014-11-25 20:56:51 +00001423 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
1424 return bias_ == other->AsCompare()->bias_;
1425 }
1426
1427 bool IsGtBias() { return bias_ == kGtBias; }
1428
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001429 DECLARE_INSTRUCTION(Compare);
1430
1431 private:
Calin Juravleddb7df22014-11-25 20:56:51 +00001432 const Bias bias_;
1433
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001434 DISALLOW_COPY_AND_ASSIGN(HCompare);
1435};
1436
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001437// A local in the graph. Corresponds to a Dex register.
1438class HLocal : public HTemplateInstruction<0> {
1439 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001440 explicit HLocal(uint16_t reg_number)
1441 : HTemplateInstruction(SideEffects::None()), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001442
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001443 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001444
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001445 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001446
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001447 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001448 // The Dex register number.
1449 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001450
1451 DISALLOW_COPY_AND_ASSIGN(HLocal);
1452};
1453
1454// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07001455class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001456 public:
Roland Levillain5799fc02014-09-25 12:15:20 +01001457 HLoadLocal(HLocal* local, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001458 : HExpression(type, SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001459 SetRawInputAt(0, local);
1460 }
1461
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001462 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1463
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001464 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001465
1466 private:
1467 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
1468};
1469
1470// Store a value in a given local. This instruction has two inputs: the value
1471// and the local.
1472class HStoreLocal : public HTemplateInstruction<2> {
1473 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001474 HStoreLocal(HLocal* local, HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001475 SetRawInputAt(0, local);
1476 SetRawInputAt(1, value);
1477 }
1478
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001479 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1480
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001481 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001482
1483 private:
1484 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
1485};
1486
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001487class HConstant : public HExpression<0> {
1488 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001489 explicit HConstant(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
1490
1491 virtual bool CanBeMoved() const { return true; }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001492
1493 DECLARE_INSTRUCTION(Constant);
1494
1495 private:
1496 DISALLOW_COPY_AND_ASSIGN(HConstant);
1497};
1498
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001499class HFloatConstant : public HConstant {
1500 public:
1501 explicit HFloatConstant(float value) : HConstant(Primitive::kPrimFloat), value_(value) {}
1502
1503 float GetValue() const { return value_; }
1504
1505 virtual bool InstructionDataEquals(HInstruction* other) const {
1506 return bit_cast<float, int32_t>(other->AsFloatConstant()->value_) ==
1507 bit_cast<float, int32_t>(value_);
1508 }
1509
1510 virtual size_t ComputeHashCode() const { return static_cast<size_t>(GetValue()); }
1511
1512 DECLARE_INSTRUCTION(FloatConstant);
1513
1514 private:
1515 const float value_;
1516
1517 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
1518};
1519
1520class HDoubleConstant : public HConstant {
1521 public:
1522 explicit HDoubleConstant(double value) : HConstant(Primitive::kPrimDouble), value_(value) {}
1523
1524 double GetValue() const { return value_; }
1525
1526 virtual bool InstructionDataEquals(HInstruction* other) const {
1527 return bit_cast<double, int64_t>(other->AsDoubleConstant()->value_) ==
1528 bit_cast<double, int64_t>(value_);
1529 }
1530
1531 virtual size_t ComputeHashCode() const { return static_cast<size_t>(GetValue()); }
1532
1533 DECLARE_INSTRUCTION(DoubleConstant);
1534
1535 private:
1536 const double value_;
1537
1538 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
1539};
1540
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001541// Constants of the type int. Those can be from Dex instructions, or
1542// synthesized (for example with the if-eqz instruction).
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001543class HIntConstant : public HConstant {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001544 public:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001545 explicit HIntConstant(int32_t value) : HConstant(Primitive::kPrimInt), value_(value) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001546
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001547 int32_t GetValue() const { return value_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001548
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001549 virtual bool InstructionDataEquals(HInstruction* other) const {
1550 return other->AsIntConstant()->value_ == value_;
1551 }
1552
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001553 virtual size_t ComputeHashCode() const { return GetValue(); }
1554
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001555 DECLARE_INSTRUCTION(IntConstant);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001556
1557 private:
1558 const int32_t value_;
1559
1560 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
1561};
1562
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001563class HLongConstant : public HConstant {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001564 public:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001565 explicit HLongConstant(int64_t value) : HConstant(Primitive::kPrimLong), value_(value) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001566
1567 int64_t GetValue() const { return value_; }
1568
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001569 virtual bool InstructionDataEquals(HInstruction* other) const {
1570 return other->AsLongConstant()->value_ == value_;
1571 }
1572
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001573 virtual size_t ComputeHashCode() const { return static_cast<size_t>(GetValue()); }
1574
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001575 DECLARE_INSTRUCTION(LongConstant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001576
1577 private:
1578 const int64_t value_;
1579
1580 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
1581};
1582
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001583class HInvoke : public HInstruction {
1584 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001585 HInvoke(ArenaAllocator* arena,
1586 uint32_t number_of_arguments,
1587 Primitive::Type return_type,
1588 uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001589 : HInstruction(SideEffects::All()),
1590 inputs_(arena, number_of_arguments),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001591 return_type_(return_type),
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001592 dex_pc_(dex_pc) {
1593 inputs_.SetSize(number_of_arguments);
1594 }
1595
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001596 virtual size_t InputCount() const { return inputs_.Size(); }
1597 virtual HInstruction* InputAt(size_t i) const { return inputs_.Get(i); }
1598
1599 // Runtime needs to walk the stack, so Dex -> Dex calls need to
1600 // know their environment.
1601 virtual bool NeedsEnvironment() const { return true; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001602
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001603 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001604 SetRawInputAt(index, argument);
1605 }
1606
1607 virtual void SetRawInputAt(size_t index, HInstruction* input) {
1608 inputs_.Put(index, input);
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001609 }
1610
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001611 virtual Primitive::Type GetType() const { return return_type_; }
1612
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001613 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001614
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001615 DECLARE_INSTRUCTION(Invoke);
1616
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001617 protected:
1618 GrowableArray<HInstruction*> inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001619 const Primitive::Type return_type_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001620 const uint32_t dex_pc_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001621
1622 private:
1623 DISALLOW_COPY_AND_ASSIGN(HInvoke);
1624};
1625
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001626class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001627 public:
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001628 HInvokeStaticOrDirect(ArenaAllocator* arena,
1629 uint32_t number_of_arguments,
1630 Primitive::Type return_type,
1631 uint32_t dex_pc,
1632 uint32_t index_in_dex_cache,
1633 InvokeType invoke_type)
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001634 : HInvoke(arena, number_of_arguments, return_type, dex_pc),
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001635 index_in_dex_cache_(index_in_dex_cache),
Nicolas Geoffray4e44c822014-12-17 12:25:12 +00001636 invoke_type_(invoke_type) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001637
1638 uint32_t GetIndexInDexCache() const { return index_in_dex_cache_; }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001639 InvokeType GetInvokeType() const { return invoke_type_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001640
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001641 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001642
1643 private:
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001644 const uint32_t index_in_dex_cache_;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001645 const InvokeType invoke_type_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001646
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001647 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001648};
1649
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001650class HInvokeVirtual : public HInvoke {
1651 public:
1652 HInvokeVirtual(ArenaAllocator* arena,
1653 uint32_t number_of_arguments,
1654 Primitive::Type return_type,
1655 uint32_t dex_pc,
1656 uint32_t vtable_index)
1657 : HInvoke(arena, number_of_arguments, return_type, dex_pc),
1658 vtable_index_(vtable_index) {}
1659
1660 uint32_t GetVTableIndex() const { return vtable_index_; }
1661
1662 DECLARE_INSTRUCTION(InvokeVirtual);
1663
1664 private:
1665 const uint32_t vtable_index_;
1666
1667 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
1668};
1669
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001670class HInvokeInterface : public HInvoke {
1671 public:
1672 HInvokeInterface(ArenaAllocator* arena,
1673 uint32_t number_of_arguments,
1674 Primitive::Type return_type,
1675 uint32_t dex_pc,
1676 uint32_t dex_method_index,
1677 uint32_t imt_index)
1678 : HInvoke(arena, number_of_arguments, return_type, dex_pc),
1679 dex_method_index_(dex_method_index),
1680 imt_index_(imt_index) {}
1681
1682 uint32_t GetImtIndex() const { return imt_index_; }
1683 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
1684
1685 DECLARE_INSTRUCTION(InvokeInterface);
1686
1687 private:
1688 const uint32_t dex_method_index_;
1689 const uint32_t imt_index_;
1690
1691 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
1692};
1693
Dave Allison20dfc792014-06-16 20:44:29 -07001694class HNewInstance : public HExpression<0> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001695 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001696 HNewInstance(uint32_t dex_pc, uint16_t type_index)
1697 : HExpression(Primitive::kPrimNot, SideEffects::None()),
1698 dex_pc_(dex_pc),
1699 type_index_(type_index) {}
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001700
1701 uint32_t GetDexPc() const { return dex_pc_; }
1702 uint16_t GetTypeIndex() const { return type_index_; }
1703
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001704 // Calls runtime so needs an environment.
Calin Juravle92a6ed22014-12-02 18:58:03 +00001705 bool NeedsEnvironment() const OVERRIDE { return true; }
1706 // It may throw when called on:
1707 // - interfaces
1708 // - abstract/innaccessible/unknown classes
1709 // TODO: optimize when possible.
1710 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001711
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001712 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001713
1714 private:
1715 const uint32_t dex_pc_;
1716 const uint16_t type_index_;
1717
1718 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
1719};
1720
Roland Levillain88cb1752014-10-20 16:36:47 +01001721class HNeg : public HUnaryOperation {
1722 public:
1723 explicit HNeg(Primitive::Type result_type, HInstruction* input)
1724 : HUnaryOperation(result_type, input) {}
1725
Roland Levillain9240d6a2014-10-20 16:47:04 +01001726 virtual int32_t Evaluate(int32_t x) const OVERRIDE { return -x; }
1727 virtual int64_t Evaluate(int64_t x) const OVERRIDE { return -x; }
1728
Roland Levillain88cb1752014-10-20 16:36:47 +01001729 DECLARE_INSTRUCTION(Neg);
1730
1731 private:
1732 DISALLOW_COPY_AND_ASSIGN(HNeg);
1733};
1734
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001735class HNewArray : public HExpression<1> {
1736 public:
1737 HNewArray(HInstruction* length, uint32_t dex_pc, uint16_t type_index)
1738 : HExpression(Primitive::kPrimNot, SideEffects::None()),
1739 dex_pc_(dex_pc),
1740 type_index_(type_index) {
1741 SetRawInputAt(0, length);
1742 }
1743
1744 uint32_t GetDexPc() const { return dex_pc_; }
1745 uint16_t GetTypeIndex() const { return type_index_; }
1746
1747 // Calls runtime so needs an environment.
1748 virtual bool NeedsEnvironment() const { return true; }
1749
1750 DECLARE_INSTRUCTION(NewArray);
1751
1752 private:
1753 const uint32_t dex_pc_;
1754 const uint16_t type_index_;
1755
1756 DISALLOW_COPY_AND_ASSIGN(HNewArray);
1757};
1758
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001759class HAdd : public HBinaryOperation {
1760 public:
1761 HAdd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1762 : HBinaryOperation(result_type, left, right) {}
1763
1764 virtual bool IsCommutative() { return true; }
1765
Roland Levillain93445682014-10-06 19:24:02 +01001766 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1767 return x + y;
1768 }
1769 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1770 return x + y;
1771 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001772
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001773 DECLARE_INSTRUCTION(Add);
1774
1775 private:
1776 DISALLOW_COPY_AND_ASSIGN(HAdd);
1777};
1778
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001779class HSub : public HBinaryOperation {
1780 public:
1781 HSub(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1782 : HBinaryOperation(result_type, left, right) {}
1783
Roland Levillain93445682014-10-06 19:24:02 +01001784 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1785 return x - y;
1786 }
1787 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1788 return x - y;
1789 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001790
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001791 DECLARE_INSTRUCTION(Sub);
1792
1793 private:
1794 DISALLOW_COPY_AND_ASSIGN(HSub);
1795};
1796
Calin Juravle34bacdf2014-10-07 20:23:36 +01001797class HMul : public HBinaryOperation {
1798 public:
1799 HMul(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1800 : HBinaryOperation(result_type, left, right) {}
1801
1802 virtual bool IsCommutative() { return true; }
1803
1804 virtual int32_t Evaluate(int32_t x, int32_t y) const { return x * y; }
1805 virtual int64_t Evaluate(int64_t x, int64_t y) const { return x * y; }
1806
1807 DECLARE_INSTRUCTION(Mul);
1808
1809 private:
1810 DISALLOW_COPY_AND_ASSIGN(HMul);
1811};
1812
Calin Juravle7c4954d2014-10-28 16:57:40 +00001813class HDiv : public HBinaryOperation {
1814 public:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001815 HDiv(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
1816 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00001817
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00001818 virtual int32_t Evaluate(int32_t x, int32_t y) const {
1819 // Our graph structure ensures we never have 0 for `y` during constant folding.
1820 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00001821 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00001822 return (y == -1) ? -x : x / y;
1823 }
Calin Juravlebacfec32014-11-14 15:54:36 +00001824
1825 virtual int64_t Evaluate(int64_t x, int64_t y) const {
1826 DCHECK_NE(y, 0);
1827 // Special case -1 to avoid getting a SIGFPE on x86(_64).
1828 return (y == -1) ? -x : x / y;
1829 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00001830
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001831 uint32_t GetDexPc() const { return dex_pc_; }
1832
Calin Juravle7c4954d2014-10-28 16:57:40 +00001833 DECLARE_INSTRUCTION(Div);
1834
1835 private:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001836 const uint32_t dex_pc_;
1837
Calin Juravle7c4954d2014-10-28 16:57:40 +00001838 DISALLOW_COPY_AND_ASSIGN(HDiv);
1839};
1840
Calin Juravlebacfec32014-11-14 15:54:36 +00001841class HRem : public HBinaryOperation {
1842 public:
1843 HRem(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
1844 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
1845
1846 virtual int32_t Evaluate(int32_t x, int32_t y) const {
1847 DCHECK_NE(y, 0);
1848 // Special case -1 to avoid getting a SIGFPE on x86(_64).
1849 return (y == -1) ? 0 : x % y;
1850 }
1851
1852 virtual int64_t Evaluate(int64_t x, int64_t y) const {
1853 DCHECK_NE(y, 0);
1854 // Special case -1 to avoid getting a SIGFPE on x86(_64).
1855 return (y == -1) ? 0 : x % y;
1856 }
1857
1858 uint32_t GetDexPc() const { return dex_pc_; }
1859
1860 DECLARE_INSTRUCTION(Rem);
1861
1862 private:
1863 const uint32_t dex_pc_;
1864
1865 DISALLOW_COPY_AND_ASSIGN(HRem);
1866};
1867
Calin Juravled0d48522014-11-04 16:40:20 +00001868class HDivZeroCheck : public HExpression<1> {
1869 public:
1870 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
1871 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
1872 SetRawInputAt(0, value);
1873 }
1874
1875 bool CanBeMoved() const OVERRIDE { return true; }
1876
1877 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
1878 UNUSED(other);
1879 return true;
1880 }
1881
1882 bool NeedsEnvironment() const OVERRIDE { return true; }
1883 bool CanThrow() const OVERRIDE { return true; }
1884
1885 uint32_t GetDexPc() const { return dex_pc_; }
1886
1887 DECLARE_INSTRUCTION(DivZeroCheck);
1888
1889 private:
1890 const uint32_t dex_pc_;
1891
1892 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
1893};
1894
Calin Juravle9aec02f2014-11-18 23:06:35 +00001895class HShl : public HBinaryOperation {
1896 public:
1897 HShl(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1898 : HBinaryOperation(result_type, left, right) {}
1899
1900 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x << (y & kMaxIntShiftValue); }
1901 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x << (y & kMaxLongShiftValue); }
1902
1903 DECLARE_INSTRUCTION(Shl);
1904
1905 private:
1906 DISALLOW_COPY_AND_ASSIGN(HShl);
1907};
1908
1909class HShr : public HBinaryOperation {
1910 public:
1911 HShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1912 : HBinaryOperation(result_type, left, right) {}
1913
1914 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x >> (y & kMaxIntShiftValue); }
1915 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x >> (y & kMaxLongShiftValue); }
1916
1917 DECLARE_INSTRUCTION(Shr);
1918
1919 private:
1920 DISALLOW_COPY_AND_ASSIGN(HShr);
1921};
1922
1923class HUShr : public HBinaryOperation {
1924 public:
1925 HUShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1926 : HBinaryOperation(result_type, left, right) {}
1927
1928 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1929 uint32_t ux = static_cast<uint32_t>(x);
1930 uint32_t uy = static_cast<uint32_t>(y) & kMaxIntShiftValue;
1931 return static_cast<int32_t>(ux >> uy);
1932 }
1933
1934 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1935 uint64_t ux = static_cast<uint64_t>(x);
1936 uint64_t uy = static_cast<uint64_t>(y) & kMaxLongShiftValue;
1937 return static_cast<int64_t>(ux >> uy);
1938 }
1939
1940 DECLARE_INSTRUCTION(UShr);
1941
1942 private:
1943 DISALLOW_COPY_AND_ASSIGN(HUShr);
1944};
1945
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001946class HAnd : public HBinaryOperation {
1947 public:
1948 HAnd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1949 : HBinaryOperation(result_type, left, right) {}
1950
1951 bool IsCommutative() OVERRIDE { return true; }
1952
1953 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x & y; }
1954 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x & y; }
1955
1956 DECLARE_INSTRUCTION(And);
1957
1958 private:
1959 DISALLOW_COPY_AND_ASSIGN(HAnd);
1960};
1961
1962class HOr : public HBinaryOperation {
1963 public:
1964 HOr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1965 : HBinaryOperation(result_type, left, right) {}
1966
1967 bool IsCommutative() OVERRIDE { return true; }
1968
1969 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x | y; }
1970 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x | y; }
1971
1972 DECLARE_INSTRUCTION(Or);
1973
1974 private:
1975 DISALLOW_COPY_AND_ASSIGN(HOr);
1976};
1977
1978class HXor : public HBinaryOperation {
1979 public:
1980 HXor(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1981 : HBinaryOperation(result_type, left, right) {}
1982
1983 bool IsCommutative() OVERRIDE { return true; }
1984
1985 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x ^ y; }
1986 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x ^ y; }
1987
1988 DECLARE_INSTRUCTION(Xor);
1989
1990 private:
1991 DISALLOW_COPY_AND_ASSIGN(HXor);
1992};
1993
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001994// The value of a parameter in this method. Its location depends on
1995// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07001996class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001997 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001998 HParameterValue(uint8_t index, Primitive::Type parameter_type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001999 : HExpression(parameter_type, SideEffects::None()), index_(index) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002000
2001 uint8_t GetIndex() const { return index_; }
2002
2003 DECLARE_INSTRUCTION(ParameterValue);
2004
2005 private:
2006 // The index of this parameter in the parameters list. Must be less
2007 // than HGraph::number_of_in_vregs_;
2008 const uint8_t index_;
2009
2010 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
2011};
2012
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002013class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002014 public:
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002015 explicit HNot(Primitive::Type result_type, HInstruction* input)
2016 : HUnaryOperation(result_type, input) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002017
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002018 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002019 virtual bool InstructionDataEquals(HInstruction* other) const {
2020 UNUSED(other);
2021 return true;
2022 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002023
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002024 virtual int32_t Evaluate(int32_t x) const OVERRIDE { return ~x; }
2025 virtual int64_t Evaluate(int64_t x) const OVERRIDE { return ~x; }
2026
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002027 DECLARE_INSTRUCTION(Not);
2028
2029 private:
2030 DISALLOW_COPY_AND_ASSIGN(HNot);
2031};
2032
Roland Levillaindff1f282014-11-05 14:15:05 +00002033class HTypeConversion : public HExpression<1> {
2034 public:
2035 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00002036 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
2037 : HExpression(result_type, SideEffects::None()), dex_pc_(dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002038 SetRawInputAt(0, input);
2039 DCHECK_NE(input->GetType(), result_type);
2040 }
2041
2042 HInstruction* GetInput() const { return InputAt(0); }
2043 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
2044 Primitive::Type GetResultType() const { return GetType(); }
2045
Roland Levillain624279f2014-12-04 11:54:28 +00002046 // Required by the x86 and ARM code generators when producing calls
2047 // to the runtime.
2048 uint32_t GetDexPc() const { return dex_pc_; }
2049
Roland Levillaindff1f282014-11-05 14:15:05 +00002050 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00002051 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00002052
2053 DECLARE_INSTRUCTION(TypeConversion);
2054
2055 private:
Roland Levillain624279f2014-12-04 11:54:28 +00002056 const uint32_t dex_pc_;
2057
Roland Levillaindff1f282014-11-05 14:15:05 +00002058 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
2059};
2060
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002061class HPhi : public HInstruction {
2062 public:
2063 HPhi(ArenaAllocator* arena, uint32_t reg_number, size_t number_of_inputs, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002064 : HInstruction(SideEffects::None()),
2065 inputs_(arena, number_of_inputs),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002066 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002067 type_(type),
2068 is_live_(false) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002069 inputs_.SetSize(number_of_inputs);
2070 }
2071
2072 virtual size_t InputCount() const { return inputs_.Size(); }
2073 virtual HInstruction* InputAt(size_t i) const { return inputs_.Get(i); }
2074
2075 virtual void SetRawInputAt(size_t index, HInstruction* input) {
2076 inputs_.Put(index, input);
2077 }
2078
2079 void AddInput(HInstruction* input);
2080
2081 virtual Primitive::Type GetType() const { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002082 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002083
2084 uint32_t GetRegNumber() const { return reg_number_; }
2085
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002086 void SetDead() { is_live_ = false; }
2087 void SetLive() { is_live_ = true; }
2088 bool IsDead() const { return !is_live_; }
2089 bool IsLive() const { return is_live_; }
2090
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002091 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002092
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002093 private:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002094 GrowableArray<HInstruction*> inputs_;
2095 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002096 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002097 bool is_live_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002098
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002099 DISALLOW_COPY_AND_ASSIGN(HPhi);
2100};
2101
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002102class HNullCheck : public HExpression<1> {
2103 public:
2104 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002105 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002106 SetRawInputAt(0, value);
2107 }
2108
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002109 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002110 virtual bool InstructionDataEquals(HInstruction* other) const {
2111 UNUSED(other);
2112 return true;
2113 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002114
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002115 virtual bool NeedsEnvironment() const { return true; }
2116
Roland Levillaine161a2a2014-10-03 12:45:18 +01002117 virtual bool CanThrow() const { return true; }
2118
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002119 uint32_t GetDexPc() const { return dex_pc_; }
2120
2121 DECLARE_INSTRUCTION(NullCheck);
2122
2123 private:
2124 const uint32_t dex_pc_;
2125
2126 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
2127};
2128
2129class FieldInfo : public ValueObject {
2130 public:
Roland Levillain5799fc02014-09-25 12:15:20 +01002131 FieldInfo(MemberOffset field_offset, Primitive::Type field_type)
Nicolas Geoffray39468442014-09-02 15:17:15 +01002132 : field_offset_(field_offset), field_type_(field_type) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002133
2134 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002135 Primitive::Type GetFieldType() const { return field_type_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002136
2137 private:
2138 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01002139 const Primitive::Type field_type_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002140};
2141
2142class HInstanceFieldGet : public HExpression<1> {
2143 public:
2144 HInstanceFieldGet(HInstruction* value,
2145 Primitive::Type field_type,
2146 MemberOffset field_offset)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002147 : HExpression(field_type, SideEffects::DependsOnSomething()),
2148 field_info_(field_offset, field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002149 SetRawInputAt(0, value);
2150 }
2151
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002152 virtual bool CanBeMoved() const { return true; }
2153 virtual bool InstructionDataEquals(HInstruction* other) const {
2154 size_t other_offset = other->AsInstanceFieldGet()->GetFieldOffset().SizeValue();
2155 return other_offset == GetFieldOffset().SizeValue();
2156 }
2157
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002158 virtual size_t ComputeHashCode() const {
2159 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
2160 }
2161
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002162 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002163 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002164
2165 DECLARE_INSTRUCTION(InstanceFieldGet);
2166
2167 private:
2168 const FieldInfo field_info_;
2169
2170 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
2171};
2172
2173class HInstanceFieldSet : public HTemplateInstruction<2> {
2174 public:
2175 HInstanceFieldSet(HInstruction* object,
2176 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01002177 Primitive::Type field_type,
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002178 MemberOffset field_offset)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002179 : HTemplateInstruction(SideEffects::ChangesSomething()),
2180 field_info_(field_offset, field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002181 SetRawInputAt(0, object);
2182 SetRawInputAt(1, value);
2183 }
2184
2185 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002186 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002187
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002188 HInstruction* GetValue() const { return InputAt(1); }
2189
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002190 DECLARE_INSTRUCTION(InstanceFieldSet);
2191
2192 private:
2193 const FieldInfo field_info_;
2194
2195 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
2196};
2197
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002198class HArrayGet : public HExpression<2> {
2199 public:
2200 HArrayGet(HInstruction* array, HInstruction* index, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002201 : HExpression(type, SideEffects::DependsOnSomething()) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002202 SetRawInputAt(0, array);
2203 SetRawInputAt(1, index);
2204 }
2205
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002206 bool CanBeMoved() const OVERRIDE { return true; }
2207 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002208 UNUSED(other);
2209 return true;
2210 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002211 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002212
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002213 HInstruction* GetArray() const { return InputAt(0); }
2214 HInstruction* GetIndex() const { return InputAt(1); }
2215
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002216 DECLARE_INSTRUCTION(ArrayGet);
2217
2218 private:
2219 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
2220};
2221
2222class HArraySet : public HTemplateInstruction<3> {
2223 public:
2224 HArraySet(HInstruction* array,
2225 HInstruction* index,
2226 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002227 Primitive::Type expected_component_type,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002228 uint32_t dex_pc)
2229 : HTemplateInstruction(SideEffects::ChangesSomething()),
2230 dex_pc_(dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002231 expected_component_type_(expected_component_type),
2232 needs_type_check_(value->GetType() == Primitive::kPrimNot) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002233 SetRawInputAt(0, array);
2234 SetRawInputAt(1, index);
2235 SetRawInputAt(2, value);
2236 }
2237
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002238 bool NeedsEnvironment() const {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002239 // We currently always call a runtime method to catch array store
2240 // exceptions.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002241 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002242 }
2243
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002244 void ClearNeedsTypeCheck() {
2245 needs_type_check_ = false;
2246 }
2247
2248 bool NeedsTypeCheck() const { return needs_type_check_; }
2249
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002250 uint32_t GetDexPc() const { return dex_pc_; }
2251
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002252 HInstruction* GetArray() const { return InputAt(0); }
2253 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002254 HInstruction* GetValue() const { return InputAt(2); }
2255
2256 Primitive::Type GetComponentType() const {
2257 // The Dex format does not type floating point index operations. Since the
2258 // `expected_component_type_` is set during building and can therefore not
2259 // be correct, we also check what is the value type. If it is a floating
2260 // point type, we must use that type.
2261 Primitive::Type value_type = GetValue()->GetType();
2262 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
2263 ? value_type
2264 : expected_component_type_;
2265 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002266
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002267 DECLARE_INSTRUCTION(ArraySet);
2268
2269 private:
2270 const uint32_t dex_pc_;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002271 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002272 bool needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002273
2274 DISALLOW_COPY_AND_ASSIGN(HArraySet);
2275};
2276
2277class HArrayLength : public HExpression<1> {
2278 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002279 explicit HArrayLength(HInstruction* array)
2280 : HExpression(Primitive::kPrimInt, SideEffects::None()) {
2281 // Note that arrays do not change length, so the instruction does not
2282 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002283 SetRawInputAt(0, array);
2284 }
2285
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002286 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002287 virtual bool InstructionDataEquals(HInstruction* other) const {
2288 UNUSED(other);
2289 return true;
2290 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002291
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002292 DECLARE_INSTRUCTION(ArrayLength);
2293
2294 private:
2295 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
2296};
2297
2298class HBoundsCheck : public HExpression<2> {
2299 public:
2300 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002301 : HExpression(index->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002302 DCHECK(index->GetType() == Primitive::kPrimInt);
2303 SetRawInputAt(0, index);
2304 SetRawInputAt(1, length);
2305 }
2306
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002307 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002308 virtual bool InstructionDataEquals(HInstruction* other) const {
2309 UNUSED(other);
2310 return true;
2311 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002312
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002313 virtual bool NeedsEnvironment() const { return true; }
2314
Roland Levillaine161a2a2014-10-03 12:45:18 +01002315 virtual bool CanThrow() const { return true; }
2316
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002317 uint32_t GetDexPc() const { return dex_pc_; }
2318
2319 DECLARE_INSTRUCTION(BoundsCheck);
2320
2321 private:
2322 const uint32_t dex_pc_;
2323
2324 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
2325};
2326
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002327/**
2328 * Some DEX instructions are folded into multiple HInstructions that need
2329 * to stay live until the last HInstruction. This class
2330 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00002331 * HInstruction stays live. `index` represents the stack location index of the
2332 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002333 */
2334class HTemporary : public HTemplateInstruction<0> {
2335 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002336 explicit HTemporary(size_t index) : HTemplateInstruction(SideEffects::None()), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002337
2338 size_t GetIndex() const { return index_; }
2339
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00002340 Primitive::Type GetType() const OVERRIDE {
2341 // The previous instruction is the one that will be stored in the temporary location.
2342 DCHECK(GetPrevious() != nullptr);
2343 return GetPrevious()->GetType();
2344 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00002345
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002346 DECLARE_INSTRUCTION(Temporary);
2347
2348 private:
2349 const size_t index_;
2350
2351 DISALLOW_COPY_AND_ASSIGN(HTemporary);
2352};
2353
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002354class HSuspendCheck : public HTemplateInstruction<0> {
2355 public:
2356 explicit HSuspendCheck(uint32_t dex_pc)
Nicolas Geoffray9ebc72c2014-09-25 16:33:42 +01002357 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002358
2359 virtual bool NeedsEnvironment() const {
2360 return true;
2361 }
2362
2363 uint32_t GetDexPc() const { return dex_pc_; }
2364
2365 DECLARE_INSTRUCTION(SuspendCheck);
2366
2367 private:
2368 const uint32_t dex_pc_;
2369
2370 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
2371};
2372
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002373/**
2374 * Instruction to load a Class object.
2375 */
2376class HLoadClass : public HExpression<0> {
2377 public:
2378 HLoadClass(uint16_t type_index,
2379 bool is_referrers_class,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002380 uint32_t dex_pc)
2381 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2382 type_index_(type_index),
2383 is_referrers_class_(is_referrers_class),
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002384 dex_pc_(dex_pc),
2385 generate_clinit_check_(false) {}
2386
2387 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002388
2389 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2390 return other->AsLoadClass()->type_index_ == type_index_;
2391 }
2392
2393 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
2394
2395 uint32_t GetDexPc() const { return dex_pc_; }
2396 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002397 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002398
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002399 bool NeedsEnvironment() const OVERRIDE {
2400 // Will call runtime and load the class if the class is not loaded yet.
2401 // TODO: finer grain decision.
2402 return !is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002403 }
2404
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002405 bool MustGenerateClinitCheck() const {
2406 return generate_clinit_check_;
2407 }
2408
2409 void SetMustGenerateClinitCheck() {
2410 generate_clinit_check_ = true;
2411 }
2412
2413 bool CanCallRuntime() const {
2414 return MustGenerateClinitCheck() || !is_referrers_class_;
2415 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002416
2417 DECLARE_INSTRUCTION(LoadClass);
2418
2419 private:
2420 const uint16_t type_index_;
2421 const bool is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002422 const uint32_t dex_pc_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002423 // Whether this instruction must generate the initialization check.
2424 // Used for code generation.
2425 bool generate_clinit_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002426
2427 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
2428};
2429
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002430class HLoadString : public HExpression<0> {
2431 public:
2432 HLoadString(uint32_t string_index, uint32_t dex_pc)
2433 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2434 string_index_(string_index),
2435 dex_pc_(dex_pc) {}
2436
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002437 bool CanBeMoved() const OVERRIDE { return true; }
2438
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002439 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2440 return other->AsLoadString()->string_index_ == string_index_;
2441 }
2442
2443 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
2444
2445 uint32_t GetDexPc() const { return dex_pc_; }
2446 uint32_t GetStringIndex() const { return string_index_; }
2447
2448 // TODO: Can we deopt or debug when we resolve a string?
2449 bool NeedsEnvironment() const OVERRIDE { return false; }
2450
2451 DECLARE_INSTRUCTION(LoadString);
2452
2453 private:
2454 const uint32_t string_index_;
2455 const uint32_t dex_pc_;
2456
2457 DISALLOW_COPY_AND_ASSIGN(HLoadString);
2458};
2459
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002460// TODO: Pass this check to HInvokeStaticOrDirect nodes.
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002461/**
2462 * Performs an initialization check on its Class object input.
2463 */
2464class HClinitCheck : public HExpression<1> {
2465 public:
2466 explicit HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
2467 : HExpression(Primitive::kPrimNot, SideEffects::All()),
2468 dex_pc_(dex_pc) {
2469 SetRawInputAt(0, constant);
2470 }
2471
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002472 bool CanBeMoved() const OVERRIDE { return true; }
2473 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2474 UNUSED(other);
2475 return true;
2476 }
2477
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002478 bool NeedsEnvironment() const OVERRIDE {
2479 // May call runtime to initialize the class.
2480 return true;
2481 }
2482
2483 uint32_t GetDexPc() const { return dex_pc_; }
2484
2485 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
2486
2487 DECLARE_INSTRUCTION(ClinitCheck);
2488
2489 private:
2490 const uint32_t dex_pc_;
2491
2492 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
2493};
2494
2495class HStaticFieldGet : public HExpression<1> {
2496 public:
2497 HStaticFieldGet(HInstruction* cls,
2498 Primitive::Type field_type,
2499 MemberOffset field_offset)
2500 : HExpression(field_type, SideEffects::DependsOnSomething()),
2501 field_info_(field_offset, field_type) {
2502 SetRawInputAt(0, cls);
2503 }
2504
2505 bool CanBeMoved() const OVERRIDE { return true; }
2506 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2507 size_t other_offset = other->AsStaticFieldGet()->GetFieldOffset().SizeValue();
2508 return other_offset == GetFieldOffset().SizeValue();
2509 }
2510
2511 size_t ComputeHashCode() const OVERRIDE {
2512 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
2513 }
2514
2515 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
2516 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
2517
2518 DECLARE_INSTRUCTION(StaticFieldGet);
2519
2520 private:
2521 const FieldInfo field_info_;
2522
2523 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
2524};
2525
2526class HStaticFieldSet : public HTemplateInstruction<2> {
2527 public:
2528 HStaticFieldSet(HInstruction* cls,
2529 HInstruction* value,
2530 Primitive::Type field_type,
2531 MemberOffset field_offset)
2532 : HTemplateInstruction(SideEffects::ChangesSomething()),
2533 field_info_(field_offset, field_type) {
2534 SetRawInputAt(0, cls);
2535 SetRawInputAt(1, value);
2536 }
2537
2538 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
2539 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
2540
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002541 HInstruction* GetValue() const { return InputAt(1); }
2542
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002543 DECLARE_INSTRUCTION(StaticFieldSet);
2544
2545 private:
2546 const FieldInfo field_info_;
2547
2548 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
2549};
2550
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002551// Implement the move-exception DEX instruction.
2552class HLoadException : public HExpression<0> {
2553 public:
2554 HLoadException() : HExpression(Primitive::kPrimNot, SideEffects::None()) {}
2555
2556 DECLARE_INSTRUCTION(LoadException);
2557
2558 private:
2559 DISALLOW_COPY_AND_ASSIGN(HLoadException);
2560};
2561
2562class HThrow : public HTemplateInstruction<1> {
2563 public:
2564 HThrow(HInstruction* exception, uint32_t dex_pc)
2565 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {
2566 SetRawInputAt(0, exception);
2567 }
2568
2569 bool IsControlFlow() const OVERRIDE { return true; }
2570
2571 bool NeedsEnvironment() const OVERRIDE { return true; }
2572
2573 uint32_t GetDexPc() const { return dex_pc_; }
2574
2575 DECLARE_INSTRUCTION(Throw);
2576
2577 private:
2578 uint32_t dex_pc_;
2579
2580 DISALLOW_COPY_AND_ASSIGN(HThrow);
2581};
2582
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002583class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002584 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002585 HInstanceOf(HInstruction* object,
2586 HLoadClass* constant,
2587 bool class_is_final,
2588 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002589 : HExpression(Primitive::kPrimBoolean, SideEffects::None()),
2590 class_is_final_(class_is_final),
2591 dex_pc_(dex_pc) {
2592 SetRawInputAt(0, object);
2593 SetRawInputAt(1, constant);
2594 }
2595
2596 bool CanBeMoved() const OVERRIDE { return true; }
2597
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002598 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002599 return true;
2600 }
2601
2602 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002603 return false;
2604 }
2605
2606 uint32_t GetDexPc() const { return dex_pc_; }
2607
2608 bool IsClassFinal() const { return class_is_final_; }
2609
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002610 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002611
2612 private:
2613 const bool class_is_final_;
2614 const uint32_t dex_pc_;
2615
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002616 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
2617};
2618
2619class HCheckCast : public HTemplateInstruction<2> {
2620 public:
2621 HCheckCast(HInstruction* object,
2622 HLoadClass* constant,
2623 bool class_is_final,
2624 uint32_t dex_pc)
2625 : HTemplateInstruction(SideEffects::None()),
2626 class_is_final_(class_is_final),
2627 dex_pc_(dex_pc) {
2628 SetRawInputAt(0, object);
2629 SetRawInputAt(1, constant);
2630 }
2631
2632 bool CanBeMoved() const OVERRIDE { return true; }
2633
2634 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2635 return true;
2636 }
2637
2638 bool NeedsEnvironment() const OVERRIDE {
2639 // Instruction may throw a CheckCastError.
2640 return true;
2641 }
2642
2643 bool CanThrow() const OVERRIDE { return true; }
2644
2645 uint32_t GetDexPc() const { return dex_pc_; }
2646
2647 bool IsClassFinal() const { return class_is_final_; }
2648
2649 DECLARE_INSTRUCTION(CheckCast);
2650
2651 private:
2652 const bool class_is_final_;
2653 const uint32_t dex_pc_;
2654
2655 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002656};
2657
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00002658class HMonitorOperation : public HTemplateInstruction<1> {
2659 public:
2660 enum OperationKind {
2661 kEnter,
2662 kExit,
2663 };
2664
2665 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
2666 : HTemplateInstruction(SideEffects::None()), kind_(kind), dex_pc_(dex_pc) {
2667 SetRawInputAt(0, object);
2668 }
2669
2670 // Instruction may throw a Java exception, so we need an environment.
2671 bool NeedsEnvironment() const OVERRIDE { return true; }
2672 bool CanThrow() const OVERRIDE { return true; }
2673
2674 uint32_t GetDexPc() const { return dex_pc_; }
2675
2676 bool IsEnter() const { return kind_ == kEnter; }
2677
2678 DECLARE_INSTRUCTION(MonitorOperation);
2679
2680 protected:
2681 const OperationKind kind_;
2682 const uint32_t dex_pc_;
2683
2684 private:
2685 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
2686};
2687
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002688
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002689class MoveOperands : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002690 public:
Nicolas Geoffray740475d2014-09-29 10:33:25 +01002691 MoveOperands(Location source, Location destination, HInstruction* instruction)
2692 : source_(source), destination_(destination), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002693
2694 Location GetSource() const { return source_; }
2695 Location GetDestination() const { return destination_; }
2696
2697 void SetSource(Location value) { source_ = value; }
2698 void SetDestination(Location value) { destination_ = value; }
2699
2700 // The parallel move resolver marks moves as "in-progress" by clearing the
2701 // destination (but not the source).
2702 Location MarkPending() {
2703 DCHECK(!IsPending());
2704 Location dest = destination_;
2705 destination_ = Location::NoLocation();
2706 return dest;
2707 }
2708
2709 void ClearPending(Location dest) {
2710 DCHECK(IsPending());
2711 destination_ = dest;
2712 }
2713
2714 bool IsPending() const {
2715 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
2716 return destination_.IsInvalid() && !source_.IsInvalid();
2717 }
2718
2719 // True if this blocks a move from the given location.
2720 bool Blocks(Location loc) const {
2721 return !IsEliminated() && source_.Equals(loc);
2722 }
2723
2724 // A move is redundant if it's been eliminated, if its source and
2725 // destination are the same, or if its destination is unneeded.
2726 bool IsRedundant() const {
2727 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
2728 }
2729
2730 // We clear both operands to indicate move that's been eliminated.
2731 void Eliminate() {
2732 source_ = destination_ = Location::NoLocation();
2733 }
2734
2735 bool IsEliminated() const {
2736 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
2737 return source_.IsInvalid();
2738 }
2739
Nicolas Geoffray740475d2014-09-29 10:33:25 +01002740 HInstruction* GetInstruction() const { return instruction_; }
2741
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002742 private:
2743 Location source_;
2744 Location destination_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01002745 // The instruction this move is assocatied with. Null when this move is
2746 // for moving an input in the expected locations of user (including a phi user).
2747 // This is only used in debug mode, to ensure we do not connect interval siblings
2748 // in the same parallel move.
2749 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002750
2751 DISALLOW_COPY_AND_ASSIGN(MoveOperands);
2752};
2753
2754static constexpr size_t kDefaultNumberOfMoves = 4;
2755
2756class HParallelMove : public HTemplateInstruction<0> {
2757 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002758 explicit HParallelMove(ArenaAllocator* arena)
2759 : HTemplateInstruction(SideEffects::None()), moves_(arena, kDefaultNumberOfMoves) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002760
2761 void AddMove(MoveOperands* move) {
Nicolas Geoffray740475d2014-09-29 10:33:25 +01002762 if (kIsDebugBuild && move->GetInstruction() != nullptr) {
2763 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
2764 DCHECK_NE(moves_.Get(i)->GetInstruction(), move->GetInstruction())
2765 << "Doing parallel moves for the same instruction.";
2766 }
2767 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002768 moves_.Add(move);
2769 }
2770
2771 MoveOperands* MoveOperandsAt(size_t index) const {
2772 return moves_.Get(index);
2773 }
2774
2775 size_t NumMoves() const { return moves_.Size(); }
2776
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002777 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002778
2779 private:
2780 GrowableArray<MoveOperands*> moves_;
2781
2782 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
2783};
2784
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002785class HGraphVisitor : public ValueObject {
2786 public:
Dave Allison20dfc792014-06-16 20:44:29 -07002787 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
2788 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002789
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002790 virtual void VisitInstruction(HInstruction* instruction) { UNUSED(instruction); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002791 virtual void VisitBasicBlock(HBasicBlock* block);
2792
Roland Levillain633021e2014-10-01 14:12:25 +01002793 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002794 void VisitInsertionOrder();
2795
Roland Levillain633021e2014-10-01 14:12:25 +01002796 // Visit the graph following dominator tree reverse post-order.
2797 void VisitReversePostOrder();
2798
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002799 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002800
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002801 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002802#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002803 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
2804
2805 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
2806
2807#undef DECLARE_VISIT_INSTRUCTION
2808
2809 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07002810 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002811
2812 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
2813};
2814
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002815class HGraphDelegateVisitor : public HGraphVisitor {
2816 public:
2817 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
2818 virtual ~HGraphDelegateVisitor() {}
2819
2820 // Visit functions that delegate to to super class.
2821#define DECLARE_VISIT_INSTRUCTION(name, super) \
2822 virtual void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
2823
2824 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
2825
2826#undef DECLARE_VISIT_INSTRUCTION
2827
2828 private:
2829 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
2830};
2831
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002832class HInsertionOrderIterator : public ValueObject {
2833 public:
2834 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
2835
2836 bool Done() const { return index_ == graph_.GetBlocks().Size(); }
2837 HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); }
2838 void Advance() { ++index_; }
2839
2840 private:
2841 const HGraph& graph_;
2842 size_t index_;
2843
2844 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
2845};
2846
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002847class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002848 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002849 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002850
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002851 bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); }
2852 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002853 void Advance() { ++index_; }
2854
2855 private:
2856 const HGraph& graph_;
2857 size_t index_;
2858
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002859 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002860};
2861
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002862class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002863 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002864 explicit HPostOrderIterator(const HGraph& graph)
2865 : graph_(graph), index_(graph_.GetReversePostOrder().Size()) {}
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002866
2867 bool Done() const { return index_ == 0; }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002868 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002869 void Advance() { --index_; }
2870
2871 private:
2872 const HGraph& graph_;
2873 size_t index_;
2874
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002875 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002876};
2877
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002878} // namespace art
2879
2880#endif // ART_COMPILER_OPTIMIZING_NODES_H_