blob: 601d45e56feb5bbba44afaec122c25c248cc249c [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;
Jean Christophe Beyler53d9da82014-12-04 13:28:25 -0800198 void RemoveBlock(HBasicBlock* block) const;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000199
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000200 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000201
202 // List of blocks in insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000203 GrowableArray<HBasicBlock*> blocks_;
204
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100205 // List of blocks to perform a reverse post order tree traversal.
206 GrowableArray<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000207
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000208 HBasicBlock* entry_block_;
209 HBasicBlock* exit_block_;
210
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100211 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100212 uint16_t maximum_number_of_out_vregs_;
213
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100214 // The number of virtual registers in this method. Contains the parameters.
215 uint16_t number_of_vregs_;
216
217 // The number of virtual registers used by parameters of this method.
218 uint16_t number_of_in_vregs_;
219
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000220 // Number of vreg size slots that the temporaries use (used in baseline compiler).
221 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100222
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000223 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000224 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000225
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000226 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000227 DISALLOW_COPY_AND_ASSIGN(HGraph);
228};
229
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700230class HLoopInformation : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000231 public:
232 HLoopInformation(HBasicBlock* header, HGraph* graph)
233 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100234 suspend_check_(nullptr),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100235 back_edges_(graph->GetArena(), kDefaultNumberOfBackEdges),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100236 // Make bit vector growable, as the number of blocks may change.
237 blocks_(graph->GetArena(), graph->GetBlocks().Size(), true) {}
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100238
239 HBasicBlock* GetHeader() const {
240 return header_;
241 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000242
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100243 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
244 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
245 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
246
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000247 void AddBackEdge(HBasicBlock* back_edge) {
248 back_edges_.Add(back_edge);
249 }
250
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100251 void RemoveBackEdge(HBasicBlock* back_edge) {
252 back_edges_.Delete(back_edge);
253 }
254
255 bool IsBackEdge(HBasicBlock* block) {
256 for (size_t i = 0, e = back_edges_.Size(); i < e; ++i) {
257 if (back_edges_.Get(i) == block) return true;
258 }
259 return false;
260 }
261
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000262 size_t NumberOfBackEdges() const {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000263 return back_edges_.Size();
264 }
265
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100266 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100267
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100268 const GrowableArray<HBasicBlock*>& GetBackEdges() const {
269 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100270 }
271
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100272 void ClearBackEdges() {
273 back_edges_.Reset();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100274 }
275
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100276 // Find blocks that are part of this loop. Returns whether the loop is a natural loop,
277 // that is the header dominates the back edge.
278 bool Populate();
279
280 // Returns whether this loop information contains `block`.
281 // Note that this loop information *must* be populated before entering this function.
282 bool Contains(const HBasicBlock& block) const;
283
284 // Returns whether this loop information is an inner loop of `other`.
285 // Note that `other` *must* be populated before entering this function.
286 bool IsIn(const HLoopInformation& other) const;
287
288 const ArenaBitVector& GetBlocks() const { return blocks_; }
289
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000290 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100291 // Internal recursive implementation of `Populate`.
292 void PopulateRecursive(HBasicBlock* block);
293
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000294 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100295 HSuspendCheck* suspend_check_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000296 GrowableArray<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100297 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000298
299 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
300};
301
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100302static constexpr size_t kNoLifetime = -1;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100303static constexpr uint32_t kNoDexPc = -1;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100304
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000305// A block in a method. Contains the list of instructions represented
306// as a double linked list. Each block knows its predecessors and
307// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100308
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700309class HBasicBlock : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000310 public:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100311 explicit HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000312 : graph_(graph),
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000313 predecessors_(graph->GetArena(), kDefaultNumberOfPredecessors),
314 successors_(graph->GetArena(), kDefaultNumberOfSuccessors),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000315 loop_information_(nullptr),
316 dominator_(nullptr),
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100317 dominated_blocks_(graph->GetArena(), kDefaultNumberOfDominatedBlocks),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100318 block_id_(-1),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100319 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100320 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000321 lifetime_end_(kNoLifetime),
322 is_catch_block_(false) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000323
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100324 const GrowableArray<HBasicBlock*>& GetPredecessors() const {
325 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000326 }
327
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100328 const GrowableArray<HBasicBlock*>& GetSuccessors() const {
329 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000330 }
331
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100332 const GrowableArray<HBasicBlock*>& GetDominatedBlocks() const {
333 return dominated_blocks_;
334 }
335
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100336 bool IsEntryBlock() const {
337 return graph_->GetEntryBlock() == this;
338 }
339
340 bool IsExitBlock() const {
341 return graph_->GetExitBlock() == this;
342 }
343
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000344 void AddBackEdge(HBasicBlock* back_edge) {
345 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000346 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000347 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100348 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000349 loop_information_->AddBackEdge(back_edge);
350 }
351
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000352 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000353
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000354 int GetBlockId() const { return block_id_; }
355 void SetBlockId(int id) { block_id_ = id; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000356
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000357 HBasicBlock* GetDominator() const { return dominator_; }
358 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100359 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.Add(block); }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000360
361 int NumberOfBackEdges() const {
362 return loop_information_ == nullptr
363 ? 0
364 : loop_information_->NumberOfBackEdges();
365 }
366
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100367 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
368 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100369 const HInstructionList& GetInstructions() const { return instructions_; }
370 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100371 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000372
373 void AddSuccessor(HBasicBlock* block) {
374 successors_.Add(block);
375 block->predecessors_.Add(this);
376 }
377
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100378 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
379 size_t successor_index = GetSuccessorIndexOf(existing);
380 DCHECK_NE(successor_index, static_cast<size_t>(-1));
381 existing->RemovePredecessor(this);
382 new_block->predecessors_.Add(this);
383 successors_.Put(successor_index, new_block);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000384 }
385
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100386 void RemovePredecessor(HBasicBlock* block) {
387 predecessors_.Delete(block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100388 }
389
390 void ClearAllPredecessors() {
391 predecessors_.Reset();
392 }
393
394 void AddPredecessor(HBasicBlock* block) {
395 predecessors_.Add(block);
396 block->successors_.Add(this);
397 }
398
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100399 void SwapPredecessors() {
Nicolas Geoffrayc83d4412014-09-18 16:46:20 +0100400 DCHECK_EQ(predecessors_.Size(), 2u);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100401 HBasicBlock* temp = predecessors_.Get(0);
402 predecessors_.Put(0, predecessors_.Get(1));
403 predecessors_.Put(1, temp);
404 }
405
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100406 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) {
407 for (size_t i = 0, e = predecessors_.Size(); i < e; ++i) {
408 if (predecessors_.Get(i) == predecessor) {
409 return i;
410 }
411 }
412 return -1;
413 }
414
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100415 size_t GetSuccessorIndexOf(HBasicBlock* successor) {
416 for (size_t i = 0, e = successors_.Size(); i < e; ++i) {
417 if (successors_.Get(i) == successor) {
418 return i;
419 }
420 }
421 return -1;
422 }
423
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000424 void AddInstruction(HInstruction* instruction);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100425 void RemoveInstruction(HInstruction* instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100426 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +0100427 // Replace instruction `initial` with `replacement` within this block.
428 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
429 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100430 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100431 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100432 void RemovePhi(HPhi* phi);
433
434 bool IsLoopHeader() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100435 return (loop_information_ != nullptr) && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100436 }
437
Roland Levillain6b879dd2014-09-22 17:13:44 +0100438 bool IsLoopPreHeaderFirstPredecessor() const {
439 DCHECK(IsLoopHeader());
440 DCHECK(!GetPredecessors().IsEmpty());
441 return GetPredecessors().Get(0) == GetLoopInformation()->GetPreHeader();
442 }
443
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100444 HLoopInformation* GetLoopInformation() const {
445 return loop_information_;
446 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000447
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100448 // Set the loop_information_ on this block. This method overrides the current
449 // loop_information if it is an outer loop of the passed loop information.
450 void SetInLoop(HLoopInformation* info) {
451 if (IsLoopHeader()) {
452 // Nothing to do. This just means `info` is an outer loop.
453 } else if (loop_information_ == nullptr) {
454 loop_information_ = info;
455 } else if (loop_information_->Contains(*info->GetHeader())) {
456 // Block is currently part of an outer loop. Make it part of this inner loop.
457 // Note that a non loop header having a loop information means this loop information
458 // has already been populated
459 loop_information_ = info;
460 } else {
461 // Block is part of an inner loop. Do not update the loop information.
462 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
463 // at this point, because this method is being called while populating `info`.
464 }
465 }
466
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100467 bool IsInLoop() const { return loop_information_ != nullptr; }
468
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100469 // Returns wheter this block dominates the blocked passed as parameter.
470 bool Dominates(HBasicBlock* block) const;
471
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100472 size_t GetLifetimeStart() const { return lifetime_start_; }
473 size_t GetLifetimeEnd() const { return lifetime_end_; }
474
475 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
476 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
477
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100478 uint32_t GetDexPc() const { return dex_pc_; }
479
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000480 bool IsCatchBlock() const { return is_catch_block_; }
481 void SetIsCatchBlock() { is_catch_block_ = true; }
482
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000483 private:
484 HGraph* const graph_;
485 GrowableArray<HBasicBlock*> predecessors_;
486 GrowableArray<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100487 HInstructionList instructions_;
488 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000489 HLoopInformation* loop_information_;
490 HBasicBlock* dominator_;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100491 GrowableArray<HBasicBlock*> dominated_blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000492 int block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100493 // The dex program counter of the first instruction of this block.
494 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100495 size_t lifetime_start_;
496 size_t lifetime_end_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000497 bool is_catch_block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000498
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000499 friend class HGraph;
500 friend class HInstruction;
501
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000502 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
503};
504
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100505#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
506 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000507 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000508 M(ArrayGet, Instruction) \
509 M(ArrayLength, Instruction) \
510 M(ArraySet, Instruction) \
511 M(BoundsCheck, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000512 M(CheckCast, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100513 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000514 M(Compare, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100515 M(Condition, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000516 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +0000517 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000518 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100519 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000520 M(Exit, Instruction) \
521 M(FloatConstant, Constant) \
522 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100523 M(GreaterThan, Condition) \
524 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100525 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000526 M(InstanceFieldGet, Instruction) \
527 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000528 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100529 M(IntConstant, Constant) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +0000530 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000531 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100532 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000533 M(LessThan, Condition) \
534 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000535 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000536 M(LoadException, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100537 M(LoadLocal, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000538 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100539 M(Local, Instruction) \
540 M(LongConstant, Constant) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +0000541 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000542 M(Mul, BinaryOperation) \
543 M(Neg, UnaryOperation) \
544 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100545 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +0100546 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000547 M(NotEqual, Condition) \
548 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000549 M(Or, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100550 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000551 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100552 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000553 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100554 M(Return, Instruction) \
555 M(ReturnVoid, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000556 M(Shl, BinaryOperation) \
557 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100558 M(StaticFieldGet, Instruction) \
559 M(StaticFieldSet, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100560 M(StoreLocal, Instruction) \
561 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100562 M(SuspendCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000563 M(Temporary, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000564 M(Throw, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +0000565 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000566 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000567 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000568
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100569#define FOR_EACH_INSTRUCTION(M) \
570 FOR_EACH_CONCRETE_INSTRUCTION(M) \
571 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +0100572 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +0100573 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100574 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -0700575
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100576#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000577FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
578#undef FORWARD_DECLARATION
579
Roland Levillainccc07a92014-09-16 14:48:16 +0100580#define DECLARE_INSTRUCTION(type) \
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100581 virtual InstructionKind GetKind() const { return k##type; } \
Roland Levillainccc07a92014-09-16 14:48:16 +0100582 virtual const char* DebugName() const { return #type; } \
583 virtual const H##type* As##type() const OVERRIDE { return this; } \
584 virtual H##type* As##type() OVERRIDE { return this; } \
585 virtual bool InstructionTypeEquals(HInstruction* other) const { \
586 return other->Is##type(); \
587 } \
588 virtual void Accept(HGraphVisitor* visitor)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000589
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100590template <typename T>
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700591class HUseListNode : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000592 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100593 HUseListNode(T* user, size_t index, HUseListNode* tail)
Dave Allison20dfc792014-06-16 20:44:29 -0700594 : user_(user), index_(index), tail_(tail) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000595
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000596 HUseListNode* GetTail() const { return tail_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100597 T* GetUser() const { return user_; }
598 size_t GetIndex() const { return index_; }
599
600 void SetTail(HUseListNode<T>* node) { tail_ = node; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000601
602 private:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100603 T* const user_;
604 const size_t index_;
605 HUseListNode<T>* tail_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000606
607 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
608};
609
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100610// Represents the side effects an instruction may have.
611class SideEffects : public ValueObject {
612 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100613 SideEffects() : flags_(0) {}
614
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100615 static SideEffects None() {
616 return SideEffects(0);
617 }
618
619 static SideEffects All() {
620 return SideEffects(ChangesSomething().flags_ | DependsOnSomething().flags_);
621 }
622
623 static SideEffects ChangesSomething() {
624 return SideEffects((1 << kFlagChangesCount) - 1);
625 }
626
627 static SideEffects DependsOnSomething() {
628 int count = kFlagDependsOnCount - kFlagChangesCount;
629 return SideEffects(((1 << count) - 1) << kFlagChangesCount);
630 }
631
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100632 SideEffects Union(SideEffects other) const {
633 return SideEffects(flags_ | other.flags_);
634 }
635
Roland Levillain72bceff2014-09-15 18:29:00 +0100636 bool HasSideEffects() const {
637 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
638 return (flags_ & all_bits_set) != 0;
639 }
640
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100641 bool HasAllSideEffects() const {
642 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
643 return all_bits_set == (flags_ & all_bits_set);
644 }
645
646 bool DependsOn(SideEffects other) const {
647 size_t depends_flags = other.ComputeDependsFlags();
648 return (flags_ & depends_flags) != 0;
649 }
650
651 bool HasDependencies() const {
652 int count = kFlagDependsOnCount - kFlagChangesCount;
653 size_t all_bits_set = (1 << count) - 1;
654 return ((flags_ >> kFlagChangesCount) & all_bits_set) != 0;
655 }
656
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100657 private:
658 static constexpr int kFlagChangesSomething = 0;
659 static constexpr int kFlagChangesCount = kFlagChangesSomething + 1;
660
661 static constexpr int kFlagDependsOnSomething = kFlagChangesCount;
662 static constexpr int kFlagDependsOnCount = kFlagDependsOnSomething + 1;
663
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100664 explicit SideEffects(size_t flags) : flags_(flags) {}
665
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100666 size_t ComputeDependsFlags() const {
667 return flags_ << kFlagChangesCount;
668 }
669
670 size_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100671};
672
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700673class HInstruction : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000674 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100675 explicit HInstruction(SideEffects side_effects)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000676 : previous_(nullptr),
677 next_(nullptr),
678 block_(nullptr),
679 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100680 ssa_index_(-1),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000681 uses_(nullptr),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100682 env_uses_(nullptr),
683 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100684 locations_(nullptr),
685 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100686 lifetime_position_(kNoLifetime),
687 side_effects_(side_effects) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000688
Dave Allison20dfc792014-06-16 20:44:29 -0700689 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000690
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100691#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100692 enum InstructionKind {
693 FOR_EACH_INSTRUCTION(DECLARE_KIND)
694 };
695#undef DECLARE_KIND
696
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000697 HInstruction* GetNext() const { return next_; }
698 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000699
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000700 HBasicBlock* GetBlock() const { return block_; }
701 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100702 bool IsInBlock() const { return block_ != nullptr; }
703 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +0100704 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000705
Roland Levillain6b879dd2014-09-22 17:13:44 +0100706 virtual size_t InputCount() const = 0;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100707 virtual HInstruction* InputAt(size_t i) const = 0;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000708
709 virtual void Accept(HGraphVisitor* visitor) = 0;
710 virtual const char* DebugName() const = 0;
711
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100712 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100713 virtual void SetRawInputAt(size_t index, HInstruction* input) = 0;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100714
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100715 virtual bool NeedsEnvironment() const { return false; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100716 virtual bool IsControlFlow() const { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +0100717 virtual bool CanThrow() const { return false; }
Roland Levillain72bceff2014-09-15 18:29:00 +0100718 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100719
720 void AddUseAt(HInstruction* user, size_t index) {
721 uses_ = new (block_->GetGraph()->GetArena()) HUseListNode<HInstruction>(user, index, uses_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000722 }
723
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100724 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +0100725 DCHECK(user != nullptr);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100726 env_uses_ = new (block_->GetGraph()->GetArena()) HUseListNode<HEnvironment>(
727 user, index, env_uses_);
728 }
729
730 void RemoveUser(HInstruction* user, size_t index);
Nicolas Geoffray724c9632014-09-22 12:27:27 +0100731 void RemoveEnvironmentUser(HEnvironment* user, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100732
733 HUseListNode<HInstruction>* GetUses() const { return uses_; }
734 HUseListNode<HEnvironment>* GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000735
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100736 bool HasUses() const { return uses_ != nullptr || env_uses_ != nullptr; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100737 bool HasEnvironmentUses() const { return env_uses_ != nullptr; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000738
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100739 size_t NumberOfUses() const {
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100740 // TODO: Optimize this method if it is used outside of the HGraphVisualizer.
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100741 size_t result = 0;
742 HUseListNode<HInstruction>* current = uses_;
743 while (current != nullptr) {
744 current = current->GetTail();
745 ++result;
746 }
747 return result;
748 }
749
Roland Levillain6c82d402014-10-13 16:10:27 +0100750 // Does this instruction strictly dominate `other_instruction`?
751 // Returns false if this instruction and `other_instruction` are the same.
752 // Aborts if this instruction and `other_instruction` are both phis.
753 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +0100754
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000755 int GetId() const { return id_; }
756 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000757
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100758 int GetSsaIndex() const { return ssa_index_; }
759 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
760 bool HasSsaIndex() const { return ssa_index_ != -1; }
761
762 bool HasEnvironment() const { return environment_ != nullptr; }
763 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100764 void SetEnvironment(HEnvironment* environment) { environment_ = environment; }
765
Nicolas Geoffray39468442014-09-02 15:17:15 +0100766 // Returns the number of entries in the environment. Typically, that is the
767 // number of dex registers in a method. It could be more in case of inlining.
768 size_t EnvironmentSize() const;
769
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000770 LocationSummary* GetLocations() const { return locations_; }
771 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000772
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100773 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100774 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100775
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000776 // Insert `this` instruction in `cursor`'s graph, just before `cursor`.
777 void InsertBefore(HInstruction* cursor);
778
Dave Allison20dfc792014-06-16 20:44:29 -0700779 bool HasOnlyOneUse() const {
780 return uses_ != nullptr && uses_->GetTail() == nullptr;
781 }
782
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100783#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +0100784 bool Is##type() const { return (As##type() != nullptr); } \
785 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000786 virtual H##type* As##type() { return nullptr; }
787
788 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
789#undef INSTRUCTION_TYPE_CHECK
790
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100791 // Returns whether the instruction can be moved within the graph.
792 virtual bool CanBeMoved() const { return false; }
793
794 // Returns whether the two instructions are of the same kind.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700795 virtual bool InstructionTypeEquals(HInstruction* other) const {
796 UNUSED(other);
797 return false;
798 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100799
800 // Returns whether any data encoded in the two instructions is equal.
801 // This method does not look at the inputs. Both instructions must be
802 // of the same type, otherwise the method has undefined behavior.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700803 virtual bool InstructionDataEquals(HInstruction* other) const {
804 UNUSED(other);
805 return false;
806 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100807
808 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +0000809 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100810 // 2) Their inputs are identical.
811 bool Equals(HInstruction* other) const;
812
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100813 virtual InstructionKind GetKind() const = 0;
814
815 virtual size_t ComputeHashCode() const {
816 size_t result = GetKind();
817 for (size_t i = 0, e = InputCount(); i < e; ++i) {
818 result = (result * 31) + InputAt(i)->GetId();
819 }
820 return result;
821 }
822
823 SideEffects GetSideEffects() const { return side_effects_; }
824
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100825 size_t GetLifetimePosition() const { return lifetime_position_; }
826 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
827 LiveInterval* GetLiveInterval() const { return live_interval_; }
828 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
829 bool HasLiveInterval() const { return live_interval_ != nullptr; }
830
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000831 private:
832 HInstruction* previous_;
833 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000834 HBasicBlock* block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000835
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000836 // An instruction gets an id when it is added to the graph.
837 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +0100838 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000839 int id_;
840
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100841 // When doing liveness analysis, instructions that have uses get an SSA index.
842 int ssa_index_;
843
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100844 // List of instructions that have this instruction as input.
845 HUseListNode<HInstruction>* uses_;
846
847 // List of environments that contain this instruction.
848 HUseListNode<HEnvironment>* env_uses_;
849
Nicolas Geoffray39468442014-09-02 15:17:15 +0100850 // The environment associated with this instruction. Not null if the instruction
851 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100852 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000853
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000854 // Set by the code generator.
855 LocationSummary* locations_;
856
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100857 // Set by the liveness analysis.
858 LiveInterval* live_interval_;
859
860 // Set by the liveness analysis, this is the position in a linear
861 // order of blocks where this instruction's live interval start.
862 size_t lifetime_position_;
863
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100864 const SideEffects side_effects_;
865
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000866 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000867 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100868 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000869
870 DISALLOW_COPY_AND_ASSIGN(HInstruction);
871};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700872std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000873
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100874template<typename T>
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000875class HUseIterator : public ValueObject {
876 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100877 explicit HUseIterator(HUseListNode<T>* uses) : current_(uses) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000878
879 bool Done() const { return current_ == nullptr; }
880
881 void Advance() {
882 DCHECK(!Done());
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000883 current_ = current_->GetTail();
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000884 }
885
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100886 HUseListNode<T>* Current() const {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000887 DCHECK(!Done());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100888 return current_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000889 }
890
891 private:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100892 HUseListNode<T>* current_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000893
894 friend class HValue;
895};
896
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100897// A HEnvironment object contains the values of virtual registers at a given location.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700898class HEnvironment : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100899 public:
900 HEnvironment(ArenaAllocator* arena, size_t number_of_vregs) : vregs_(arena, number_of_vregs) {
901 vregs_.SetSize(number_of_vregs);
902 for (size_t i = 0; i < number_of_vregs; i++) {
903 vregs_.Put(i, nullptr);
904 }
905 }
906
907 void Populate(const GrowableArray<HInstruction*>& env) {
908 for (size_t i = 0; i < env.Size(); i++) {
909 HInstruction* instruction = env.Get(i);
910 vregs_.Put(i, instruction);
911 if (instruction != nullptr) {
912 instruction->AddEnvUseAt(this, i);
913 }
914 }
915 }
916
917 void SetRawEnvAt(size_t index, HInstruction* instruction) {
918 vregs_.Put(index, instruction);
919 }
920
Nicolas Geoffray39468442014-09-02 15:17:15 +0100921 HInstruction* GetInstructionAt(size_t index) const {
922 return vregs_.Get(index);
923 }
924
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100925 GrowableArray<HInstruction*>* GetVRegs() {
926 return &vregs_;
927 }
928
Nicolas Geoffray39468442014-09-02 15:17:15 +0100929 size_t Size() const { return vregs_.Size(); }
930
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100931 private:
932 GrowableArray<HInstruction*> vregs_;
933
934 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
935};
936
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000937class HInputIterator : public ValueObject {
938 public:
Dave Allison20dfc792014-06-16 20:44:29 -0700939 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000940
941 bool Done() const { return index_ == instruction_->InputCount(); }
942 HInstruction* Current() const { return instruction_->InputAt(index_); }
943 void Advance() { index_++; }
944
945 private:
946 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100947 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000948
949 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
950};
951
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000952class HInstructionIterator : public ValueObject {
953 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100954 explicit HInstructionIterator(const HInstructionList& instructions)
955 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000956 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000957 }
958
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000959 bool Done() const { return instruction_ == nullptr; }
960 HInstruction* Current() const { return instruction_; }
961 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000962 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000963 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000964 }
965
966 private:
967 HInstruction* instruction_;
968 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100969
970 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000971};
972
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100973class HBackwardInstructionIterator : public ValueObject {
974 public:
975 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
976 : instruction_(instructions.last_instruction_) {
977 next_ = Done() ? nullptr : instruction_->GetPrevious();
978 }
979
980 bool Done() const { return instruction_ == nullptr; }
981 HInstruction* Current() const { return instruction_; }
982 void Advance() {
983 instruction_ = next_;
984 next_ = Done() ? nullptr : instruction_->GetPrevious();
985 }
986
987 private:
988 HInstruction* instruction_;
989 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100990
991 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100992};
993
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000994// An embedded container with N elements of type T. Used (with partial
995// specialization for N=0) because embedded arrays cannot have size 0.
996template<typename T, intptr_t N>
997class EmbeddedArray {
998 public:
Dave Allison20dfc792014-06-16 20:44:29 -0700999 EmbeddedArray() : elements_() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001000
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001001 intptr_t GetLength() const { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001002
1003 const T& operator[](intptr_t i) const {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001004 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001005 return elements_[i];
1006 }
1007
1008 T& operator[](intptr_t i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001009 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001010 return elements_[i];
1011 }
1012
1013 const T& At(intptr_t i) const {
1014 return (*this)[i];
1015 }
1016
1017 void SetAt(intptr_t i, const T& val) {
1018 (*this)[i] = val;
1019 }
1020
1021 private:
1022 T elements_[N];
1023};
1024
1025template<typename T>
1026class EmbeddedArray<T, 0> {
1027 public:
1028 intptr_t length() const { return 0; }
1029 const T& operator[](intptr_t i) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001030 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001031 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001032 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001033 }
1034 T& operator[](intptr_t i) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001035 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001036 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001037 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001038 }
1039};
1040
1041template<intptr_t N>
1042class HTemplateInstruction: public HInstruction {
1043 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001044 HTemplateInstruction<N>(SideEffects side_effects)
1045 : HInstruction(side_effects), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07001046 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001047
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001048 virtual size_t InputCount() const { return N; }
1049 virtual HInstruction* InputAt(size_t i) const { return inputs_[i]; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001050
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001051 protected:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001052 virtual void SetRawInputAt(size_t i, HInstruction* instruction) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001053 inputs_[i] = instruction;
1054 }
1055
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001056 private:
1057 EmbeddedArray<HInstruction*, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001058
1059 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001060};
1061
Dave Allison20dfc792014-06-16 20:44:29 -07001062template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001063class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07001064 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001065 HExpression<N>(Primitive::Type type, SideEffects side_effects)
1066 : HTemplateInstruction<N>(side_effects), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07001067 virtual ~HExpression() {}
1068
1069 virtual Primitive::Type GetType() const { return type_; }
1070
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001071 protected:
1072 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07001073};
1074
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001075// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
1076// instruction that branches to the exit block.
1077class HReturnVoid : public HTemplateInstruction<0> {
1078 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001079 HReturnVoid() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001080
1081 virtual bool IsControlFlow() const { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001082
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001083 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001084
1085 private:
1086 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
1087};
1088
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001089// Represents dex's RETURN opcodes. A HReturn is a control flow
1090// instruction that branches to the exit block.
1091class HReturn : public HTemplateInstruction<1> {
1092 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001093 explicit HReturn(HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001094 SetRawInputAt(0, value);
1095 }
1096
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001097 virtual bool IsControlFlow() const { return true; }
1098
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001099 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001100
1101 private:
1102 DISALLOW_COPY_AND_ASSIGN(HReturn);
1103};
1104
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001105// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001106// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001107// exit block.
1108class HExit : public HTemplateInstruction<0> {
1109 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001110 HExit() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001111
1112 virtual bool IsControlFlow() const { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001113
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001114 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001115
1116 private:
1117 DISALLOW_COPY_AND_ASSIGN(HExit);
1118};
1119
1120// Jumps from one block to another.
1121class HGoto : public HTemplateInstruction<0> {
1122 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001123 HGoto() : HTemplateInstruction(SideEffects::None()) {}
1124
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001125 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001126
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001127 HBasicBlock* GetSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001128 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001129 }
1130
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001131 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001132
1133 private:
1134 DISALLOW_COPY_AND_ASSIGN(HGoto);
1135};
1136
Dave Allison20dfc792014-06-16 20:44:29 -07001137
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001138// Conditional branch. A block ending with an HIf instruction must have
1139// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001140class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001141 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001142 explicit HIf(HInstruction* input) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001143 SetRawInputAt(0, input);
1144 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001145
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001146 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001147
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001148 HBasicBlock* IfTrueSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001149 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001150 }
1151
1152 HBasicBlock* IfFalseSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001153 return GetBlock()->GetSuccessors().Get(1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001154 }
1155
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001156 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001157
Dave Allison20dfc792014-06-16 20:44:29 -07001158 virtual bool IsIfInstruction() const { return true; }
1159
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001160 private:
1161 DISALLOW_COPY_AND_ASSIGN(HIf);
1162};
1163
Roland Levillain88cb1752014-10-20 16:36:47 +01001164class HUnaryOperation : public HExpression<1> {
1165 public:
1166 HUnaryOperation(Primitive::Type result_type, HInstruction* input)
1167 : HExpression(result_type, SideEffects::None()) {
1168 SetRawInputAt(0, input);
1169 }
1170
1171 HInstruction* GetInput() const { return InputAt(0); }
1172 Primitive::Type GetResultType() const { return GetType(); }
1173
1174 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001175 virtual bool InstructionDataEquals(HInstruction* other) const {
1176 UNUSED(other);
1177 return true;
1178 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001179
Roland Levillain9240d6a2014-10-20 16:47:04 +01001180 // Try to statically evaluate `operation` and return a HConstant
1181 // containing the result of this evaluation. If `operation` cannot
1182 // be evaluated as a constant, return nullptr.
1183 HConstant* TryStaticEvaluation() const;
1184
1185 // Apply this operation to `x`.
1186 virtual int32_t Evaluate(int32_t x) const = 0;
1187 virtual int64_t Evaluate(int64_t x) const = 0;
1188
Roland Levillain88cb1752014-10-20 16:36:47 +01001189 DECLARE_INSTRUCTION(UnaryOperation);
1190
1191 private:
1192 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
1193};
1194
Dave Allison20dfc792014-06-16 20:44:29 -07001195class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001196 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001197 HBinaryOperation(Primitive::Type result_type,
1198 HInstruction* left,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001199 HInstruction* right) : HExpression(result_type, SideEffects::None()) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001200 SetRawInputAt(0, left);
1201 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001202 }
1203
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001204 HInstruction* GetLeft() const { return InputAt(0); }
1205 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07001206 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001207
1208 virtual bool IsCommutative() { return false; }
1209
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001210 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001211 virtual bool InstructionDataEquals(HInstruction* other) const {
1212 UNUSED(other);
1213 return true;
1214 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001215
Roland Levillain9240d6a2014-10-20 16:47:04 +01001216 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01001217 // containing the result of this evaluation. If `operation` cannot
1218 // be evaluated as a constant, return nullptr.
Roland Levillain9240d6a2014-10-20 16:47:04 +01001219 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01001220
1221 // Apply this operation to `x` and `y`.
1222 virtual int32_t Evaluate(int32_t x, int32_t y) const = 0;
1223 virtual int64_t Evaluate(int64_t x, int64_t y) const = 0;
1224
Roland Levillainccc07a92014-09-16 14:48:16 +01001225 DECLARE_INSTRUCTION(BinaryOperation);
1226
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001227 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001228 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
1229};
1230
Dave Allison20dfc792014-06-16 20:44:29 -07001231class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001232 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001233 HCondition(HInstruction* first, HInstruction* second)
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001234 : HBinaryOperation(Primitive::kPrimBoolean, first, second),
1235 needs_materialization_(true) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001236
1237 virtual bool IsCommutative() { return true; }
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001238
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001239 bool NeedsMaterialization() const { return needs_materialization_; }
1240 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001241
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001242 // For code generation purposes, returns whether this instruction is just before
1243 // `if_`, and disregard moves in between.
1244 bool IsBeforeWhenDisregardMoves(HIf* if_) const;
1245
Dave Allison20dfc792014-06-16 20:44:29 -07001246 DECLARE_INSTRUCTION(Condition);
1247
1248 virtual IfCondition GetCondition() const = 0;
1249
1250 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001251 // For register allocation purposes, returns whether this instruction needs to be
1252 // materialized (that is, not just be in the processor flags).
1253 bool needs_materialization_;
1254
Dave Allison20dfc792014-06-16 20:44:29 -07001255 DISALLOW_COPY_AND_ASSIGN(HCondition);
1256};
1257
1258// Instruction to check if two inputs are equal to each other.
1259class HEqual : public HCondition {
1260 public:
1261 HEqual(HInstruction* first, HInstruction* second)
1262 : HCondition(first, second) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001263
Roland Levillain93445682014-10-06 19:24:02 +01001264 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1265 return x == y ? 1 : 0;
1266 }
1267 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1268 return x == y ? 1 : 0;
1269 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001270
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001271 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001272
Dave Allison20dfc792014-06-16 20:44:29 -07001273 virtual IfCondition GetCondition() const {
1274 return kCondEQ;
1275 }
1276
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001277 private:
1278 DISALLOW_COPY_AND_ASSIGN(HEqual);
1279};
1280
Dave Allison20dfc792014-06-16 20:44:29 -07001281class HNotEqual : public HCondition {
1282 public:
1283 HNotEqual(HInstruction* first, HInstruction* second)
1284 : HCondition(first, second) {}
1285
Roland Levillain93445682014-10-06 19:24:02 +01001286 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1287 return x != y ? 1 : 0;
1288 }
1289 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1290 return x != y ? 1 : 0;
1291 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001292
Dave Allison20dfc792014-06-16 20:44:29 -07001293 DECLARE_INSTRUCTION(NotEqual);
1294
1295 virtual IfCondition GetCondition() const {
1296 return kCondNE;
1297 }
1298
1299 private:
1300 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
1301};
1302
1303class HLessThan : public HCondition {
1304 public:
1305 HLessThan(HInstruction* first, HInstruction* second)
1306 : HCondition(first, second) {}
1307
Roland Levillain93445682014-10-06 19:24:02 +01001308 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1309 return x < y ? 1 : 0;
1310 }
1311 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1312 return x < y ? 1 : 0;
1313 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001314
Dave Allison20dfc792014-06-16 20:44:29 -07001315 DECLARE_INSTRUCTION(LessThan);
1316
1317 virtual IfCondition GetCondition() const {
1318 return kCondLT;
1319 }
1320
1321 private:
1322 DISALLOW_COPY_AND_ASSIGN(HLessThan);
1323};
1324
1325class HLessThanOrEqual : public HCondition {
1326 public:
1327 HLessThanOrEqual(HInstruction* first, HInstruction* second)
1328 : HCondition(first, second) {}
1329
Roland Levillain93445682014-10-06 19:24:02 +01001330 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1331 return x <= y ? 1 : 0;
1332 }
1333 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1334 return x <= y ? 1 : 0;
1335 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001336
Dave Allison20dfc792014-06-16 20:44:29 -07001337 DECLARE_INSTRUCTION(LessThanOrEqual);
1338
1339 virtual IfCondition GetCondition() const {
1340 return kCondLE;
1341 }
1342
1343 private:
1344 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
1345};
1346
1347class HGreaterThan : public HCondition {
1348 public:
1349 HGreaterThan(HInstruction* first, HInstruction* second)
1350 : HCondition(first, second) {}
1351
Roland Levillain93445682014-10-06 19:24:02 +01001352 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1353 return x > y ? 1 : 0;
1354 }
1355 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1356 return x > y ? 1 : 0;
1357 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001358
Dave Allison20dfc792014-06-16 20:44:29 -07001359 DECLARE_INSTRUCTION(GreaterThan);
1360
1361 virtual IfCondition GetCondition() const {
1362 return kCondGT;
1363 }
1364
1365 private:
1366 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
1367};
1368
1369class HGreaterThanOrEqual : public HCondition {
1370 public:
1371 HGreaterThanOrEqual(HInstruction* first, HInstruction* second)
1372 : HCondition(first, second) {}
1373
Roland Levillain93445682014-10-06 19:24:02 +01001374 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1375 return x >= y ? 1 : 0;
1376 }
1377 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1378 return x >= y ? 1 : 0;
1379 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001380
Dave Allison20dfc792014-06-16 20:44:29 -07001381 DECLARE_INSTRUCTION(GreaterThanOrEqual);
1382
1383 virtual IfCondition GetCondition() const {
1384 return kCondGE;
1385 }
1386
1387 private:
1388 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
1389};
1390
1391
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001392// Instruction to check how two inputs compare to each other.
1393// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
1394class HCompare : public HBinaryOperation {
1395 public:
Calin Juravleddb7df22014-11-25 20:56:51 +00001396 // The bias applies for floating point operations and indicates how NaN
1397 // comparisons are treated:
1398 enum Bias {
1399 kNoBias, // bias is not applicable (i.e. for long operation)
1400 kGtBias, // return 1 for NaN comparisons
1401 kLtBias, // return -1 for NaN comparisons
1402 };
1403
1404 HCompare(Primitive::Type type, HInstruction* first, HInstruction* second, Bias bias)
1405 : HBinaryOperation(Primitive::kPrimInt, first, second), bias_(bias) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001406 DCHECK_EQ(type, first->GetType());
1407 DCHECK_EQ(type, second->GetType());
1408 }
1409
Calin Juravleddb7df22014-11-25 20:56:51 +00001410 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01001411 return
1412 x == y ? 0 :
1413 x > y ? 1 :
1414 -1;
1415 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001416
1417 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01001418 return
1419 x == y ? 0 :
1420 x > y ? 1 :
1421 -1;
1422 }
1423
Calin Juravleddb7df22014-11-25 20:56:51 +00001424 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
1425 return bias_ == other->AsCompare()->bias_;
1426 }
1427
1428 bool IsGtBias() { return bias_ == kGtBias; }
1429
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001430 DECLARE_INSTRUCTION(Compare);
1431
1432 private:
Calin Juravleddb7df22014-11-25 20:56:51 +00001433 const Bias bias_;
1434
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001435 DISALLOW_COPY_AND_ASSIGN(HCompare);
1436};
1437
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001438// A local in the graph. Corresponds to a Dex register.
1439class HLocal : public HTemplateInstruction<0> {
1440 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001441 explicit HLocal(uint16_t reg_number)
1442 : HTemplateInstruction(SideEffects::None()), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001443
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001444 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001445
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001446 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001447
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001448 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001449 // The Dex register number.
1450 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001451
1452 DISALLOW_COPY_AND_ASSIGN(HLocal);
1453};
1454
1455// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07001456class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001457 public:
Roland Levillain5799fc02014-09-25 12:15:20 +01001458 HLoadLocal(HLocal* local, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001459 : HExpression(type, SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001460 SetRawInputAt(0, local);
1461 }
1462
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001463 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1464
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001465 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001466
1467 private:
1468 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
1469};
1470
1471// Store a value in a given local. This instruction has two inputs: the value
1472// and the local.
1473class HStoreLocal : public HTemplateInstruction<2> {
1474 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001475 HStoreLocal(HLocal* local, HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001476 SetRawInputAt(0, local);
1477 SetRawInputAt(1, value);
1478 }
1479
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001480 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1481
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001482 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001483
1484 private:
1485 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
1486};
1487
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001488class HConstant : public HExpression<0> {
1489 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001490 explicit HConstant(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
1491
1492 virtual bool CanBeMoved() const { return true; }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001493
1494 DECLARE_INSTRUCTION(Constant);
1495
1496 private:
1497 DISALLOW_COPY_AND_ASSIGN(HConstant);
1498};
1499
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001500class HFloatConstant : public HConstant {
1501 public:
1502 explicit HFloatConstant(float value) : HConstant(Primitive::kPrimFloat), value_(value) {}
1503
1504 float GetValue() const { return value_; }
1505
1506 virtual bool InstructionDataEquals(HInstruction* other) const {
1507 return bit_cast<float, int32_t>(other->AsFloatConstant()->value_) ==
1508 bit_cast<float, int32_t>(value_);
1509 }
1510
1511 virtual size_t ComputeHashCode() const { return static_cast<size_t>(GetValue()); }
1512
1513 DECLARE_INSTRUCTION(FloatConstant);
1514
1515 private:
1516 const float value_;
1517
1518 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
1519};
1520
1521class HDoubleConstant : public HConstant {
1522 public:
1523 explicit HDoubleConstant(double value) : HConstant(Primitive::kPrimDouble), value_(value) {}
1524
1525 double GetValue() const { return value_; }
1526
1527 virtual bool InstructionDataEquals(HInstruction* other) const {
1528 return bit_cast<double, int64_t>(other->AsDoubleConstant()->value_) ==
1529 bit_cast<double, int64_t>(value_);
1530 }
1531
1532 virtual size_t ComputeHashCode() const { return static_cast<size_t>(GetValue()); }
1533
1534 DECLARE_INSTRUCTION(DoubleConstant);
1535
1536 private:
1537 const double value_;
1538
1539 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
1540};
1541
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001542// Constants of the type int. Those can be from Dex instructions, or
1543// synthesized (for example with the if-eqz instruction).
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001544class HIntConstant : public HConstant {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001545 public:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001546 explicit HIntConstant(int32_t value) : HConstant(Primitive::kPrimInt), value_(value) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001547
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001548 int32_t GetValue() const { return value_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001549
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001550 virtual bool InstructionDataEquals(HInstruction* other) const {
1551 return other->AsIntConstant()->value_ == value_;
1552 }
1553
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001554 virtual size_t ComputeHashCode() const { return GetValue(); }
1555
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001556 DECLARE_INSTRUCTION(IntConstant);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001557
1558 private:
1559 const int32_t value_;
1560
1561 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
1562};
1563
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001564class HLongConstant : public HConstant {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001565 public:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001566 explicit HLongConstant(int64_t value) : HConstant(Primitive::kPrimLong), value_(value) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001567
1568 int64_t GetValue() const { return value_; }
1569
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001570 virtual bool InstructionDataEquals(HInstruction* other) const {
1571 return other->AsLongConstant()->value_ == value_;
1572 }
1573
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001574 virtual size_t ComputeHashCode() const { return static_cast<size_t>(GetValue()); }
1575
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001576 DECLARE_INSTRUCTION(LongConstant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001577
1578 private:
1579 const int64_t value_;
1580
1581 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
1582};
1583
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001584class HInvoke : public HInstruction {
1585 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001586 HInvoke(ArenaAllocator* arena,
1587 uint32_t number_of_arguments,
1588 Primitive::Type return_type,
1589 uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001590 : HInstruction(SideEffects::All()),
1591 inputs_(arena, number_of_arguments),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001592 return_type_(return_type),
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001593 dex_pc_(dex_pc) {
1594 inputs_.SetSize(number_of_arguments);
1595 }
1596
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001597 virtual size_t InputCount() const { return inputs_.Size(); }
1598 virtual HInstruction* InputAt(size_t i) const { return inputs_.Get(i); }
1599
1600 // Runtime needs to walk the stack, so Dex -> Dex calls need to
1601 // know their environment.
1602 virtual bool NeedsEnvironment() const { return true; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001603
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001604 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001605 SetRawInputAt(index, argument);
1606 }
1607
1608 virtual void SetRawInputAt(size_t index, HInstruction* input) {
1609 inputs_.Put(index, input);
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001610 }
1611
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001612 virtual Primitive::Type GetType() const { return return_type_; }
1613
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001614 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001615
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001616 DECLARE_INSTRUCTION(Invoke);
1617
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001618 protected:
1619 GrowableArray<HInstruction*> inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001620 const Primitive::Type return_type_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001621 const uint32_t dex_pc_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001622
1623 private:
1624 DISALLOW_COPY_AND_ASSIGN(HInvoke);
1625};
1626
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001627class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001628 public:
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001629 HInvokeStaticOrDirect(ArenaAllocator* arena,
1630 uint32_t number_of_arguments,
1631 Primitive::Type return_type,
1632 uint32_t dex_pc,
1633 uint32_t index_in_dex_cache,
1634 InvokeType invoke_type)
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001635 : HInvoke(arena, number_of_arguments, return_type, dex_pc),
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001636 index_in_dex_cache_(index_in_dex_cache),
1637 invoke_type_(invoke_type) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001638
1639 uint32_t GetIndexInDexCache() const { return index_in_dex_cache_; }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001640 InvokeType GetInvokeType() const { return invoke_type_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001641
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001642 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001643
1644 private:
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001645 const uint32_t index_in_dex_cache_;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001646 const InvokeType invoke_type_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001647
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001648 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001649};
1650
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001651class HInvokeVirtual : public HInvoke {
1652 public:
1653 HInvokeVirtual(ArenaAllocator* arena,
1654 uint32_t number_of_arguments,
1655 Primitive::Type return_type,
1656 uint32_t dex_pc,
1657 uint32_t vtable_index)
1658 : HInvoke(arena, number_of_arguments, return_type, dex_pc),
1659 vtable_index_(vtable_index) {}
1660
1661 uint32_t GetVTableIndex() const { return vtable_index_; }
1662
1663 DECLARE_INSTRUCTION(InvokeVirtual);
1664
1665 private:
1666 const uint32_t vtable_index_;
1667
1668 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
1669};
1670
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001671class HInvokeInterface : public HInvoke {
1672 public:
1673 HInvokeInterface(ArenaAllocator* arena,
1674 uint32_t number_of_arguments,
1675 Primitive::Type return_type,
1676 uint32_t dex_pc,
1677 uint32_t dex_method_index,
1678 uint32_t imt_index)
1679 : HInvoke(arena, number_of_arguments, return_type, dex_pc),
1680 dex_method_index_(dex_method_index),
1681 imt_index_(imt_index) {}
1682
1683 uint32_t GetImtIndex() const { return imt_index_; }
1684 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
1685
1686 DECLARE_INSTRUCTION(InvokeInterface);
1687
1688 private:
1689 const uint32_t dex_method_index_;
1690 const uint32_t imt_index_;
1691
1692 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
1693};
1694
Dave Allison20dfc792014-06-16 20:44:29 -07001695class HNewInstance : public HExpression<0> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001696 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001697 HNewInstance(uint32_t dex_pc, uint16_t type_index)
1698 : HExpression(Primitive::kPrimNot, SideEffects::None()),
1699 dex_pc_(dex_pc),
1700 type_index_(type_index) {}
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001701
1702 uint32_t GetDexPc() const { return dex_pc_; }
1703 uint16_t GetTypeIndex() const { return type_index_; }
1704
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001705 // Calls runtime so needs an environment.
Calin Juravle92a6ed22014-12-02 18:58:03 +00001706 bool NeedsEnvironment() const OVERRIDE { return true; }
1707 // It may throw when called on:
1708 // - interfaces
1709 // - abstract/innaccessible/unknown classes
1710 // TODO: optimize when possible.
1711 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001712
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001713 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001714
1715 private:
1716 const uint32_t dex_pc_;
1717 const uint16_t type_index_;
1718
1719 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
1720};
1721
Roland Levillain88cb1752014-10-20 16:36:47 +01001722class HNeg : public HUnaryOperation {
1723 public:
1724 explicit HNeg(Primitive::Type result_type, HInstruction* input)
1725 : HUnaryOperation(result_type, input) {}
1726
Roland Levillain9240d6a2014-10-20 16:47:04 +01001727 virtual int32_t Evaluate(int32_t x) const OVERRIDE { return -x; }
1728 virtual int64_t Evaluate(int64_t x) const OVERRIDE { return -x; }
1729
Roland Levillain88cb1752014-10-20 16:36:47 +01001730 DECLARE_INSTRUCTION(Neg);
1731
1732 private:
1733 DISALLOW_COPY_AND_ASSIGN(HNeg);
1734};
1735
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001736class HNewArray : public HExpression<1> {
1737 public:
1738 HNewArray(HInstruction* length, uint32_t dex_pc, uint16_t type_index)
1739 : HExpression(Primitive::kPrimNot, SideEffects::None()),
1740 dex_pc_(dex_pc),
1741 type_index_(type_index) {
1742 SetRawInputAt(0, length);
1743 }
1744
1745 uint32_t GetDexPc() const { return dex_pc_; }
1746 uint16_t GetTypeIndex() const { return type_index_; }
1747
1748 // Calls runtime so needs an environment.
1749 virtual bool NeedsEnvironment() const { return true; }
1750
1751 DECLARE_INSTRUCTION(NewArray);
1752
1753 private:
1754 const uint32_t dex_pc_;
1755 const uint16_t type_index_;
1756
1757 DISALLOW_COPY_AND_ASSIGN(HNewArray);
1758};
1759
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001760class HAdd : public HBinaryOperation {
1761 public:
1762 HAdd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1763 : HBinaryOperation(result_type, left, right) {}
1764
1765 virtual bool IsCommutative() { return true; }
1766
Roland Levillain93445682014-10-06 19:24:02 +01001767 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1768 return x + y;
1769 }
1770 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1771 return x + y;
1772 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001773
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001774 DECLARE_INSTRUCTION(Add);
1775
1776 private:
1777 DISALLOW_COPY_AND_ASSIGN(HAdd);
1778};
1779
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001780class HSub : public HBinaryOperation {
1781 public:
1782 HSub(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1783 : HBinaryOperation(result_type, left, right) {}
1784
Roland Levillain93445682014-10-06 19:24:02 +01001785 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1786 return x - y;
1787 }
1788 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1789 return x - y;
1790 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001791
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001792 DECLARE_INSTRUCTION(Sub);
1793
1794 private:
1795 DISALLOW_COPY_AND_ASSIGN(HSub);
1796};
1797
Calin Juravle34bacdf2014-10-07 20:23:36 +01001798class HMul : public HBinaryOperation {
1799 public:
1800 HMul(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1801 : HBinaryOperation(result_type, left, right) {}
1802
1803 virtual bool IsCommutative() { return true; }
1804
1805 virtual int32_t Evaluate(int32_t x, int32_t y) const { return x * y; }
1806 virtual int64_t Evaluate(int64_t x, int64_t y) const { return x * y; }
1807
1808 DECLARE_INSTRUCTION(Mul);
1809
1810 private:
1811 DISALLOW_COPY_AND_ASSIGN(HMul);
1812};
1813
Calin Juravle7c4954d2014-10-28 16:57:40 +00001814class HDiv : public HBinaryOperation {
1815 public:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001816 HDiv(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
1817 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00001818
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00001819 virtual int32_t Evaluate(int32_t x, int32_t y) const {
1820 // Our graph structure ensures we never have 0 for `y` during constant folding.
1821 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00001822 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00001823 return (y == -1) ? -x : x / y;
1824 }
Calin Juravlebacfec32014-11-14 15:54:36 +00001825
1826 virtual int64_t Evaluate(int64_t x, int64_t y) const {
1827 DCHECK_NE(y, 0);
1828 // Special case -1 to avoid getting a SIGFPE on x86(_64).
1829 return (y == -1) ? -x : x / y;
1830 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00001831
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001832 uint32_t GetDexPc() const { return dex_pc_; }
1833
Calin Juravle7c4954d2014-10-28 16:57:40 +00001834 DECLARE_INSTRUCTION(Div);
1835
1836 private:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001837 const uint32_t dex_pc_;
1838
Calin Juravle7c4954d2014-10-28 16:57:40 +00001839 DISALLOW_COPY_AND_ASSIGN(HDiv);
1840};
1841
Calin Juravlebacfec32014-11-14 15:54:36 +00001842class HRem : public HBinaryOperation {
1843 public:
1844 HRem(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
1845 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
1846
1847 virtual int32_t Evaluate(int32_t x, int32_t y) const {
1848 DCHECK_NE(y, 0);
1849 // Special case -1 to avoid getting a SIGFPE on x86(_64).
1850 return (y == -1) ? 0 : x % y;
1851 }
1852
1853 virtual int64_t Evaluate(int64_t x, int64_t y) const {
1854 DCHECK_NE(y, 0);
1855 // Special case -1 to avoid getting a SIGFPE on x86(_64).
1856 return (y == -1) ? 0 : x % y;
1857 }
1858
1859 uint32_t GetDexPc() const { return dex_pc_; }
1860
1861 DECLARE_INSTRUCTION(Rem);
1862
1863 private:
1864 const uint32_t dex_pc_;
1865
1866 DISALLOW_COPY_AND_ASSIGN(HRem);
1867};
1868
Calin Juravled0d48522014-11-04 16:40:20 +00001869class HDivZeroCheck : public HExpression<1> {
1870 public:
1871 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
1872 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
1873 SetRawInputAt(0, value);
1874 }
1875
1876 bool CanBeMoved() const OVERRIDE { return true; }
1877
1878 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
1879 UNUSED(other);
1880 return true;
1881 }
1882
1883 bool NeedsEnvironment() const OVERRIDE { return true; }
1884 bool CanThrow() const OVERRIDE { return true; }
1885
1886 uint32_t GetDexPc() const { return dex_pc_; }
1887
1888 DECLARE_INSTRUCTION(DivZeroCheck);
1889
1890 private:
1891 const uint32_t dex_pc_;
1892
1893 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
1894};
1895
Calin Juravle9aec02f2014-11-18 23:06:35 +00001896class HShl : public HBinaryOperation {
1897 public:
1898 HShl(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1899 : HBinaryOperation(result_type, left, right) {}
1900
1901 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x << (y & kMaxIntShiftValue); }
1902 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x << (y & kMaxLongShiftValue); }
1903
1904 DECLARE_INSTRUCTION(Shl);
1905
1906 private:
1907 DISALLOW_COPY_AND_ASSIGN(HShl);
1908};
1909
1910class HShr : public HBinaryOperation {
1911 public:
1912 HShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1913 : HBinaryOperation(result_type, left, right) {}
1914
1915 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x >> (y & kMaxIntShiftValue); }
1916 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x >> (y & kMaxLongShiftValue); }
1917
1918 DECLARE_INSTRUCTION(Shr);
1919
1920 private:
1921 DISALLOW_COPY_AND_ASSIGN(HShr);
1922};
1923
1924class HUShr : public HBinaryOperation {
1925 public:
1926 HUShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1927 : HBinaryOperation(result_type, left, right) {}
1928
1929 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1930 uint32_t ux = static_cast<uint32_t>(x);
1931 uint32_t uy = static_cast<uint32_t>(y) & kMaxIntShiftValue;
1932 return static_cast<int32_t>(ux >> uy);
1933 }
1934
1935 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1936 uint64_t ux = static_cast<uint64_t>(x);
1937 uint64_t uy = static_cast<uint64_t>(y) & kMaxLongShiftValue;
1938 return static_cast<int64_t>(ux >> uy);
1939 }
1940
1941 DECLARE_INSTRUCTION(UShr);
1942
1943 private:
1944 DISALLOW_COPY_AND_ASSIGN(HUShr);
1945};
1946
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001947class HAnd : public HBinaryOperation {
1948 public:
1949 HAnd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1950 : HBinaryOperation(result_type, left, right) {}
1951
1952 bool IsCommutative() OVERRIDE { return true; }
1953
1954 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x & y; }
1955 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x & y; }
1956
1957 DECLARE_INSTRUCTION(And);
1958
1959 private:
1960 DISALLOW_COPY_AND_ASSIGN(HAnd);
1961};
1962
1963class HOr : public HBinaryOperation {
1964 public:
1965 HOr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1966 : HBinaryOperation(result_type, left, right) {}
1967
1968 bool IsCommutative() OVERRIDE { return true; }
1969
1970 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x | y; }
1971 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x | y; }
1972
1973 DECLARE_INSTRUCTION(Or);
1974
1975 private:
1976 DISALLOW_COPY_AND_ASSIGN(HOr);
1977};
1978
1979class HXor : public HBinaryOperation {
1980 public:
1981 HXor(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1982 : HBinaryOperation(result_type, left, right) {}
1983
1984 bool IsCommutative() OVERRIDE { return true; }
1985
1986 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x ^ y; }
1987 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x ^ y; }
1988
1989 DECLARE_INSTRUCTION(Xor);
1990
1991 private:
1992 DISALLOW_COPY_AND_ASSIGN(HXor);
1993};
1994
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001995// The value of a parameter in this method. Its location depends on
1996// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07001997class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001998 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001999 HParameterValue(uint8_t index, Primitive::Type parameter_type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002000 : HExpression(parameter_type, SideEffects::None()), index_(index) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002001
2002 uint8_t GetIndex() const { return index_; }
2003
2004 DECLARE_INSTRUCTION(ParameterValue);
2005
2006 private:
2007 // The index of this parameter in the parameters list. Must be less
2008 // than HGraph::number_of_in_vregs_;
2009 const uint8_t index_;
2010
2011 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
2012};
2013
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002014class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002015 public:
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002016 explicit HNot(Primitive::Type result_type, HInstruction* input)
2017 : HUnaryOperation(result_type, input) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002018
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002019 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002020 virtual bool InstructionDataEquals(HInstruction* other) const {
2021 UNUSED(other);
2022 return true;
2023 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002024
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002025 virtual int32_t Evaluate(int32_t x) const OVERRIDE { return ~x; }
2026 virtual int64_t Evaluate(int64_t x) const OVERRIDE { return ~x; }
2027
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002028 DECLARE_INSTRUCTION(Not);
2029
2030 private:
2031 DISALLOW_COPY_AND_ASSIGN(HNot);
2032};
2033
Roland Levillaindff1f282014-11-05 14:15:05 +00002034class HTypeConversion : public HExpression<1> {
2035 public:
2036 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00002037 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
2038 : HExpression(result_type, SideEffects::None()), dex_pc_(dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002039 SetRawInputAt(0, input);
2040 DCHECK_NE(input->GetType(), result_type);
2041 }
2042
2043 HInstruction* GetInput() const { return InputAt(0); }
2044 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
2045 Primitive::Type GetResultType() const { return GetType(); }
2046
Roland Levillain624279f2014-12-04 11:54:28 +00002047 // Required by the x86 and ARM code generators when producing calls
2048 // to the runtime.
2049 uint32_t GetDexPc() const { return dex_pc_; }
2050
Roland Levillaindff1f282014-11-05 14:15:05 +00002051 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00002052 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00002053
2054 DECLARE_INSTRUCTION(TypeConversion);
2055
2056 private:
Roland Levillain624279f2014-12-04 11:54:28 +00002057 const uint32_t dex_pc_;
2058
Roland Levillaindff1f282014-11-05 14:15:05 +00002059 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
2060};
2061
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002062class HPhi : public HInstruction {
2063 public:
2064 HPhi(ArenaAllocator* arena, uint32_t reg_number, size_t number_of_inputs, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002065 : HInstruction(SideEffects::None()),
2066 inputs_(arena, number_of_inputs),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002067 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002068 type_(type),
2069 is_live_(false) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002070 inputs_.SetSize(number_of_inputs);
2071 }
2072
2073 virtual size_t InputCount() const { return inputs_.Size(); }
2074 virtual HInstruction* InputAt(size_t i) const { return inputs_.Get(i); }
2075
2076 virtual void SetRawInputAt(size_t index, HInstruction* input) {
2077 inputs_.Put(index, input);
2078 }
2079
2080 void AddInput(HInstruction* input);
2081
2082 virtual Primitive::Type GetType() const { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002083 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002084
2085 uint32_t GetRegNumber() const { return reg_number_; }
2086
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002087 void SetDead() { is_live_ = false; }
2088 void SetLive() { is_live_ = true; }
2089 bool IsDead() const { return !is_live_; }
2090 bool IsLive() const { return is_live_; }
2091
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002092 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002093
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002094 private:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002095 GrowableArray<HInstruction*> inputs_;
2096 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002097 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002098 bool is_live_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002099
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002100 DISALLOW_COPY_AND_ASSIGN(HPhi);
2101};
2102
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002103class HNullCheck : public HExpression<1> {
2104 public:
2105 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002106 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002107 SetRawInputAt(0, value);
2108 }
2109
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002110 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002111 virtual bool InstructionDataEquals(HInstruction* other) const {
2112 UNUSED(other);
2113 return true;
2114 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002115
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002116 virtual bool NeedsEnvironment() const { return true; }
2117
Roland Levillaine161a2a2014-10-03 12:45:18 +01002118 virtual bool CanThrow() const { return true; }
2119
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002120 uint32_t GetDexPc() const { return dex_pc_; }
2121
2122 DECLARE_INSTRUCTION(NullCheck);
2123
2124 private:
2125 const uint32_t dex_pc_;
2126
2127 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
2128};
2129
2130class FieldInfo : public ValueObject {
2131 public:
Roland Levillain5799fc02014-09-25 12:15:20 +01002132 FieldInfo(MemberOffset field_offset, Primitive::Type field_type)
Nicolas Geoffray39468442014-09-02 15:17:15 +01002133 : field_offset_(field_offset), field_type_(field_type) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002134
2135 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002136 Primitive::Type GetFieldType() const { return field_type_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002137
2138 private:
2139 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01002140 const Primitive::Type field_type_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002141};
2142
2143class HInstanceFieldGet : public HExpression<1> {
2144 public:
2145 HInstanceFieldGet(HInstruction* value,
2146 Primitive::Type field_type,
2147 MemberOffset field_offset)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002148 : HExpression(field_type, SideEffects::DependsOnSomething()),
2149 field_info_(field_offset, field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002150 SetRawInputAt(0, value);
2151 }
2152
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002153 virtual bool CanBeMoved() const { return true; }
2154 virtual bool InstructionDataEquals(HInstruction* other) const {
2155 size_t other_offset = other->AsInstanceFieldGet()->GetFieldOffset().SizeValue();
2156 return other_offset == GetFieldOffset().SizeValue();
2157 }
2158
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002159 virtual size_t ComputeHashCode() const {
2160 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
2161 }
2162
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002163 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002164 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002165
2166 DECLARE_INSTRUCTION(InstanceFieldGet);
2167
2168 private:
2169 const FieldInfo field_info_;
2170
2171 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
2172};
2173
2174class HInstanceFieldSet : public HTemplateInstruction<2> {
2175 public:
2176 HInstanceFieldSet(HInstruction* object,
2177 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01002178 Primitive::Type field_type,
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002179 MemberOffset field_offset)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002180 : HTemplateInstruction(SideEffects::ChangesSomething()),
2181 field_info_(field_offset, field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002182 SetRawInputAt(0, object);
2183 SetRawInputAt(1, value);
2184 }
2185
2186 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002187 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002188
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002189 HInstruction* GetValue() const { return InputAt(1); }
2190
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002191 DECLARE_INSTRUCTION(InstanceFieldSet);
2192
2193 private:
2194 const FieldInfo field_info_;
2195
2196 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
2197};
2198
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002199class HArrayGet : public HExpression<2> {
2200 public:
2201 HArrayGet(HInstruction* array, HInstruction* index, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002202 : HExpression(type, SideEffects::DependsOnSomething()) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002203 SetRawInputAt(0, array);
2204 SetRawInputAt(1, index);
2205 }
2206
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002207 bool CanBeMoved() const OVERRIDE { return true; }
2208 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002209 UNUSED(other);
2210 return true;
2211 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002212 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002213
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002214 HInstruction* GetArray() const { return InputAt(0); }
2215 HInstruction* GetIndex() const { return InputAt(1); }
2216
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002217 DECLARE_INSTRUCTION(ArrayGet);
2218
2219 private:
2220 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
2221};
2222
2223class HArraySet : public HTemplateInstruction<3> {
2224 public:
2225 HArraySet(HInstruction* array,
2226 HInstruction* index,
2227 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002228 Primitive::Type expected_component_type,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002229 uint32_t dex_pc)
2230 : HTemplateInstruction(SideEffects::ChangesSomething()),
2231 dex_pc_(dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002232 expected_component_type_(expected_component_type),
2233 needs_type_check_(value->GetType() == Primitive::kPrimNot) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002234 SetRawInputAt(0, array);
2235 SetRawInputAt(1, index);
2236 SetRawInputAt(2, value);
2237 }
2238
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002239 bool NeedsEnvironment() const {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002240 // We currently always call a runtime method to catch array store
2241 // exceptions.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002242 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002243 }
2244
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002245 void ClearNeedsTypeCheck() {
2246 needs_type_check_ = false;
2247 }
2248
2249 bool NeedsTypeCheck() const { return needs_type_check_; }
2250
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002251 uint32_t GetDexPc() const { return dex_pc_; }
2252
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002253 HInstruction* GetArray() const { return InputAt(0); }
2254 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002255 HInstruction* GetValue() const { return InputAt(2); }
2256
2257 Primitive::Type GetComponentType() const {
2258 // The Dex format does not type floating point index operations. Since the
2259 // `expected_component_type_` is set during building and can therefore not
2260 // be correct, we also check what is the value type. If it is a floating
2261 // point type, we must use that type.
2262 Primitive::Type value_type = GetValue()->GetType();
2263 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
2264 ? value_type
2265 : expected_component_type_;
2266 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002267
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002268 DECLARE_INSTRUCTION(ArraySet);
2269
2270 private:
2271 const uint32_t dex_pc_;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002272 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002273 bool needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002274
2275 DISALLOW_COPY_AND_ASSIGN(HArraySet);
2276};
2277
2278class HArrayLength : public HExpression<1> {
2279 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002280 explicit HArrayLength(HInstruction* array)
2281 : HExpression(Primitive::kPrimInt, SideEffects::None()) {
2282 // Note that arrays do not change length, so the instruction does not
2283 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002284 SetRawInputAt(0, array);
2285 }
2286
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002287 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002288 virtual bool InstructionDataEquals(HInstruction* other) const {
2289 UNUSED(other);
2290 return true;
2291 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002292
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002293 DECLARE_INSTRUCTION(ArrayLength);
2294
2295 private:
2296 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
2297};
2298
2299class HBoundsCheck : public HExpression<2> {
2300 public:
2301 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002302 : HExpression(index->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002303 DCHECK(index->GetType() == Primitive::kPrimInt);
2304 SetRawInputAt(0, index);
2305 SetRawInputAt(1, length);
2306 }
2307
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002308 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002309 virtual bool InstructionDataEquals(HInstruction* other) const {
2310 UNUSED(other);
2311 return true;
2312 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002313
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002314 virtual bool NeedsEnvironment() const { return true; }
2315
Roland Levillaine161a2a2014-10-03 12:45:18 +01002316 virtual bool CanThrow() const { return true; }
2317
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002318 uint32_t GetDexPc() const { return dex_pc_; }
2319
2320 DECLARE_INSTRUCTION(BoundsCheck);
2321
2322 private:
2323 const uint32_t dex_pc_;
2324
2325 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
2326};
2327
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002328/**
2329 * Some DEX instructions are folded into multiple HInstructions that need
2330 * to stay live until the last HInstruction. This class
2331 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00002332 * HInstruction stays live. `index` represents the stack location index of the
2333 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002334 */
2335class HTemporary : public HTemplateInstruction<0> {
2336 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002337 explicit HTemporary(size_t index) : HTemplateInstruction(SideEffects::None()), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002338
2339 size_t GetIndex() const { return index_; }
2340
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00002341 Primitive::Type GetType() const OVERRIDE {
2342 // The previous instruction is the one that will be stored in the temporary location.
2343 DCHECK(GetPrevious() != nullptr);
2344 return GetPrevious()->GetType();
2345 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00002346
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002347 DECLARE_INSTRUCTION(Temporary);
2348
2349 private:
2350 const size_t index_;
2351
2352 DISALLOW_COPY_AND_ASSIGN(HTemporary);
2353};
2354
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002355class HSuspendCheck : public HTemplateInstruction<0> {
2356 public:
2357 explicit HSuspendCheck(uint32_t dex_pc)
Nicolas Geoffray9ebc72c2014-09-25 16:33:42 +01002358 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002359
2360 virtual bool NeedsEnvironment() const {
2361 return true;
2362 }
2363
2364 uint32_t GetDexPc() const { return dex_pc_; }
2365
2366 DECLARE_INSTRUCTION(SuspendCheck);
2367
2368 private:
2369 const uint32_t dex_pc_;
2370
2371 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
2372};
2373
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002374/**
2375 * Instruction to load a Class object.
2376 */
2377class HLoadClass : public HExpression<0> {
2378 public:
2379 HLoadClass(uint16_t type_index,
2380 bool is_referrers_class,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002381 uint32_t dex_pc)
2382 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2383 type_index_(type_index),
2384 is_referrers_class_(is_referrers_class),
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002385 dex_pc_(dex_pc),
2386 generate_clinit_check_(false) {}
2387
2388 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002389
2390 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2391 return other->AsLoadClass()->type_index_ == type_index_;
2392 }
2393
2394 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
2395
2396 uint32_t GetDexPc() const { return dex_pc_; }
2397 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002398 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002399
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002400 bool NeedsEnvironment() const OVERRIDE {
2401 // Will call runtime and load the class if the class is not loaded yet.
2402 // TODO: finer grain decision.
2403 return !is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002404 }
2405
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002406 bool MustGenerateClinitCheck() const {
2407 return generate_clinit_check_;
2408 }
2409
2410 void SetMustGenerateClinitCheck() {
2411 generate_clinit_check_ = true;
2412 }
2413
2414 bool CanCallRuntime() const {
2415 return MustGenerateClinitCheck() || !is_referrers_class_;
2416 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002417
2418 DECLARE_INSTRUCTION(LoadClass);
2419
2420 private:
2421 const uint16_t type_index_;
2422 const bool is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002423 const uint32_t dex_pc_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002424 // Whether this instruction must generate the initialization check.
2425 // Used for code generation.
2426 bool generate_clinit_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002427
2428 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
2429};
2430
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002431class HLoadString : public HExpression<0> {
2432 public:
2433 HLoadString(uint32_t string_index, uint32_t dex_pc)
2434 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2435 string_index_(string_index),
2436 dex_pc_(dex_pc) {}
2437
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002438 bool CanBeMoved() const OVERRIDE { return true; }
2439
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002440 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2441 return other->AsLoadString()->string_index_ == string_index_;
2442 }
2443
2444 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
2445
2446 uint32_t GetDexPc() const { return dex_pc_; }
2447 uint32_t GetStringIndex() const { return string_index_; }
2448
2449 // TODO: Can we deopt or debug when we resolve a string?
2450 bool NeedsEnvironment() const OVERRIDE { return false; }
2451
2452 DECLARE_INSTRUCTION(LoadString);
2453
2454 private:
2455 const uint32_t string_index_;
2456 const uint32_t dex_pc_;
2457
2458 DISALLOW_COPY_AND_ASSIGN(HLoadString);
2459};
2460
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002461// TODO: Pass this check to HInvokeStaticOrDirect nodes.
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002462/**
2463 * Performs an initialization check on its Class object input.
2464 */
2465class HClinitCheck : public HExpression<1> {
2466 public:
2467 explicit HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
2468 : HExpression(Primitive::kPrimNot, SideEffects::All()),
2469 dex_pc_(dex_pc) {
2470 SetRawInputAt(0, constant);
2471 }
2472
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002473 bool CanBeMoved() const OVERRIDE { return true; }
2474 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2475 UNUSED(other);
2476 return true;
2477 }
2478
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002479 bool NeedsEnvironment() const OVERRIDE {
2480 // May call runtime to initialize the class.
2481 return true;
2482 }
2483
2484 uint32_t GetDexPc() const { return dex_pc_; }
2485
2486 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
2487
2488 DECLARE_INSTRUCTION(ClinitCheck);
2489
2490 private:
2491 const uint32_t dex_pc_;
2492
2493 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
2494};
2495
2496class HStaticFieldGet : public HExpression<1> {
2497 public:
2498 HStaticFieldGet(HInstruction* cls,
2499 Primitive::Type field_type,
2500 MemberOffset field_offset)
2501 : HExpression(field_type, SideEffects::DependsOnSomething()),
2502 field_info_(field_offset, field_type) {
2503 SetRawInputAt(0, cls);
2504 }
2505
2506 bool CanBeMoved() const OVERRIDE { return true; }
2507 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2508 size_t other_offset = other->AsStaticFieldGet()->GetFieldOffset().SizeValue();
2509 return other_offset == GetFieldOffset().SizeValue();
2510 }
2511
2512 size_t ComputeHashCode() const OVERRIDE {
2513 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
2514 }
2515
2516 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
2517 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
2518
2519 DECLARE_INSTRUCTION(StaticFieldGet);
2520
2521 private:
2522 const FieldInfo field_info_;
2523
2524 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
2525};
2526
2527class HStaticFieldSet : public HTemplateInstruction<2> {
2528 public:
2529 HStaticFieldSet(HInstruction* cls,
2530 HInstruction* value,
2531 Primitive::Type field_type,
2532 MemberOffset field_offset)
2533 : HTemplateInstruction(SideEffects::ChangesSomething()),
2534 field_info_(field_offset, field_type) {
2535 SetRawInputAt(0, cls);
2536 SetRawInputAt(1, value);
2537 }
2538
2539 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
2540 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
2541
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002542 HInstruction* GetValue() const { return InputAt(1); }
2543
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002544 DECLARE_INSTRUCTION(StaticFieldSet);
2545
2546 private:
2547 const FieldInfo field_info_;
2548
2549 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
2550};
2551
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002552// Implement the move-exception DEX instruction.
2553class HLoadException : public HExpression<0> {
2554 public:
2555 HLoadException() : HExpression(Primitive::kPrimNot, SideEffects::None()) {}
2556
2557 DECLARE_INSTRUCTION(LoadException);
2558
2559 private:
2560 DISALLOW_COPY_AND_ASSIGN(HLoadException);
2561};
2562
2563class HThrow : public HTemplateInstruction<1> {
2564 public:
2565 HThrow(HInstruction* exception, uint32_t dex_pc)
2566 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {
2567 SetRawInputAt(0, exception);
2568 }
2569
2570 bool IsControlFlow() const OVERRIDE { return true; }
2571
2572 bool NeedsEnvironment() const OVERRIDE { return true; }
2573
2574 uint32_t GetDexPc() const { return dex_pc_; }
2575
2576 DECLARE_INSTRUCTION(Throw);
2577
2578 private:
2579 uint32_t dex_pc_;
2580
2581 DISALLOW_COPY_AND_ASSIGN(HThrow);
2582};
2583
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002584class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002585 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002586 HInstanceOf(HInstruction* object,
2587 HLoadClass* constant,
2588 bool class_is_final,
2589 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002590 : HExpression(Primitive::kPrimBoolean, SideEffects::None()),
2591 class_is_final_(class_is_final),
2592 dex_pc_(dex_pc) {
2593 SetRawInputAt(0, object);
2594 SetRawInputAt(1, constant);
2595 }
2596
2597 bool CanBeMoved() const OVERRIDE { return true; }
2598
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002599 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002600 return true;
2601 }
2602
2603 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002604 return false;
2605 }
2606
2607 uint32_t GetDexPc() const { return dex_pc_; }
2608
2609 bool IsClassFinal() const { return class_is_final_; }
2610
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002611 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002612
2613 private:
2614 const bool class_is_final_;
2615 const uint32_t dex_pc_;
2616
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002617 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
2618};
2619
2620class HCheckCast : public HTemplateInstruction<2> {
2621 public:
2622 HCheckCast(HInstruction* object,
2623 HLoadClass* constant,
2624 bool class_is_final,
2625 uint32_t dex_pc)
2626 : HTemplateInstruction(SideEffects::None()),
2627 class_is_final_(class_is_final),
2628 dex_pc_(dex_pc) {
2629 SetRawInputAt(0, object);
2630 SetRawInputAt(1, constant);
2631 }
2632
2633 bool CanBeMoved() const OVERRIDE { return true; }
2634
2635 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2636 return true;
2637 }
2638
2639 bool NeedsEnvironment() const OVERRIDE {
2640 // Instruction may throw a CheckCastError.
2641 return true;
2642 }
2643
2644 bool CanThrow() const OVERRIDE { return true; }
2645
2646 uint32_t GetDexPc() const { return dex_pc_; }
2647
2648 bool IsClassFinal() const { return class_is_final_; }
2649
2650 DECLARE_INSTRUCTION(CheckCast);
2651
2652 private:
2653 const bool class_is_final_;
2654 const uint32_t dex_pc_;
2655
2656 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002657};
2658
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00002659class HMonitorOperation : public HTemplateInstruction<1> {
2660 public:
2661 enum OperationKind {
2662 kEnter,
2663 kExit,
2664 };
2665
2666 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
2667 : HTemplateInstruction(SideEffects::None()), kind_(kind), dex_pc_(dex_pc) {
2668 SetRawInputAt(0, object);
2669 }
2670
2671 // Instruction may throw a Java exception, so we need an environment.
2672 bool NeedsEnvironment() const OVERRIDE { return true; }
2673 bool CanThrow() const OVERRIDE { return true; }
2674
2675 uint32_t GetDexPc() const { return dex_pc_; }
2676
2677 bool IsEnter() const { return kind_ == kEnter; }
2678
2679 DECLARE_INSTRUCTION(MonitorOperation);
2680
2681 protected:
2682 const OperationKind kind_;
2683 const uint32_t dex_pc_;
2684
2685 private:
2686 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
2687};
2688
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002689
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002690class MoveOperands : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002691 public:
Nicolas Geoffray740475d2014-09-29 10:33:25 +01002692 MoveOperands(Location source, Location destination, HInstruction* instruction)
2693 : source_(source), destination_(destination), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002694
2695 Location GetSource() const { return source_; }
2696 Location GetDestination() const { return destination_; }
2697
2698 void SetSource(Location value) { source_ = value; }
2699 void SetDestination(Location value) { destination_ = value; }
2700
2701 // The parallel move resolver marks moves as "in-progress" by clearing the
2702 // destination (but not the source).
2703 Location MarkPending() {
2704 DCHECK(!IsPending());
2705 Location dest = destination_;
2706 destination_ = Location::NoLocation();
2707 return dest;
2708 }
2709
2710 void ClearPending(Location dest) {
2711 DCHECK(IsPending());
2712 destination_ = dest;
2713 }
2714
2715 bool IsPending() const {
2716 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
2717 return destination_.IsInvalid() && !source_.IsInvalid();
2718 }
2719
2720 // True if this blocks a move from the given location.
2721 bool Blocks(Location loc) const {
2722 return !IsEliminated() && source_.Equals(loc);
2723 }
2724
2725 // A move is redundant if it's been eliminated, if its source and
2726 // destination are the same, or if its destination is unneeded.
2727 bool IsRedundant() const {
2728 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
2729 }
2730
2731 // We clear both operands to indicate move that's been eliminated.
2732 void Eliminate() {
2733 source_ = destination_ = Location::NoLocation();
2734 }
2735
2736 bool IsEliminated() const {
2737 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
2738 return source_.IsInvalid();
2739 }
2740
Nicolas Geoffray740475d2014-09-29 10:33:25 +01002741 HInstruction* GetInstruction() const { return instruction_; }
2742
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002743 private:
2744 Location source_;
2745 Location destination_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01002746 // The instruction this move is assocatied with. Null when this move is
2747 // for moving an input in the expected locations of user (including a phi user).
2748 // This is only used in debug mode, to ensure we do not connect interval siblings
2749 // in the same parallel move.
2750 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002751
2752 DISALLOW_COPY_AND_ASSIGN(MoveOperands);
2753};
2754
2755static constexpr size_t kDefaultNumberOfMoves = 4;
2756
2757class HParallelMove : public HTemplateInstruction<0> {
2758 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002759 explicit HParallelMove(ArenaAllocator* arena)
2760 : HTemplateInstruction(SideEffects::None()), moves_(arena, kDefaultNumberOfMoves) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002761
2762 void AddMove(MoveOperands* move) {
Nicolas Geoffray740475d2014-09-29 10:33:25 +01002763 if (kIsDebugBuild && move->GetInstruction() != nullptr) {
2764 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
2765 DCHECK_NE(moves_.Get(i)->GetInstruction(), move->GetInstruction())
2766 << "Doing parallel moves for the same instruction.";
2767 }
2768 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002769 moves_.Add(move);
2770 }
2771
2772 MoveOperands* MoveOperandsAt(size_t index) const {
2773 return moves_.Get(index);
2774 }
2775
2776 size_t NumMoves() const { return moves_.Size(); }
2777
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002778 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002779
2780 private:
2781 GrowableArray<MoveOperands*> moves_;
2782
2783 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
2784};
2785
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002786class HGraphVisitor : public ValueObject {
2787 public:
Dave Allison20dfc792014-06-16 20:44:29 -07002788 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
2789 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002790
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002791 virtual void VisitInstruction(HInstruction* instruction) { UNUSED(instruction); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002792 virtual void VisitBasicBlock(HBasicBlock* block);
2793
Roland Levillain633021e2014-10-01 14:12:25 +01002794 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002795 void VisitInsertionOrder();
2796
Roland Levillain633021e2014-10-01 14:12:25 +01002797 // Visit the graph following dominator tree reverse post-order.
2798 void VisitReversePostOrder();
2799
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002800 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002801
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002802 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002803#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002804 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
2805
2806 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
2807
2808#undef DECLARE_VISIT_INSTRUCTION
2809
2810 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07002811 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002812
2813 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
2814};
2815
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002816class HGraphDelegateVisitor : public HGraphVisitor {
2817 public:
2818 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
2819 virtual ~HGraphDelegateVisitor() {}
2820
2821 // Visit functions that delegate to to super class.
2822#define DECLARE_VISIT_INSTRUCTION(name, super) \
2823 virtual void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
2824
2825 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
2826
2827#undef DECLARE_VISIT_INSTRUCTION
2828
2829 private:
2830 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
2831};
2832
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002833class HInsertionOrderIterator : public ValueObject {
2834 public:
2835 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
2836
2837 bool Done() const { return index_ == graph_.GetBlocks().Size(); }
2838 HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); }
2839 void Advance() { ++index_; }
2840
2841 private:
2842 const HGraph& graph_;
2843 size_t index_;
2844
2845 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
2846};
2847
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002848class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002849 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002850 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002851
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002852 bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); }
2853 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002854 void Advance() { ++index_; }
2855
2856 private:
2857 const HGraph& graph_;
2858 size_t index_;
2859
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002860 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002861};
2862
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002863class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002864 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002865 explicit HPostOrderIterator(const HGraph& graph)
2866 : graph_(graph), index_(graph_.GetReversePostOrder().Size()) {}
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002867
2868 bool Done() const { return index_ == 0; }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002869 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002870 void Advance() { --index_; }
2871
2872 private:
2873 const HGraph& graph_;
2874 size_t index_;
2875
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002876 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002877};
2878
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002879} // namespace art
2880
2881#endif // ART_COMPILER_OPTIMIZING_NODES_H_