blob: 6f7bc0c76e500e918187dce0dd0f4fbe780dbc63 [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 Geoffraycb1b00a2015-01-28 14:50:01 +000020#include "entrypoints/quick/quick_entrypoints_enum.h"
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000021#include "invoke_type.h"
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +010022#include "locations.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010023#include "offsets.h"
Ian Rogerse63db272014-07-15 15:36:11 -070024#include "primitive.h"
Ian Rogers0279ebb2014-10-08 17:27:48 -070025#include "utils/arena_object.h"
Nicolas Geoffray0e336432014-02-26 18:24:38 +000026#include "utils/arena_bit_vector.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000027#include "utils/growable_array.h"
28
29namespace art {
30
31class HBasicBlock;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010032class HEnvironment;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000033class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000034class HIntConstant;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000035class HInvoke;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000036class HGraphVisitor;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010037class HPhi;
Nicolas Geoffray3c049742014-09-24 18:10:46 +010038class HSuspendCheck;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010039class LiveInterval;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000040class LocationSummary;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000041
42static const int kDefaultNumberOfBlocks = 8;
43static const int kDefaultNumberOfSuccessors = 2;
44static const int kDefaultNumberOfPredecessors = 2;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010045static const int kDefaultNumberOfDominatedBlocks = 1;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000046static const int kDefaultNumberOfBackEdges = 1;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000047
Calin Juravle9aec02f2014-11-18 23:06:35 +000048static constexpr uint32_t kMaxIntShiftValue = 0x1f;
49static constexpr uint64_t kMaxLongShiftValue = 0x3f;
50
Dave Allison20dfc792014-06-16 20:44:29 -070051enum IfCondition {
52 kCondEQ,
53 kCondNE,
54 kCondLT,
55 kCondLE,
56 kCondGT,
57 kCondGE,
58};
59
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010060class HInstructionList {
61 public:
62 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
63
64 void AddInstruction(HInstruction* instruction);
65 void RemoveInstruction(HInstruction* instruction);
66
Roland Levillain6b469232014-09-25 10:10:38 +010067 // Return true if this list contains `instruction`.
68 bool Contains(HInstruction* instruction) const;
69
Roland Levillainccc07a92014-09-16 14:48:16 +010070 // Return true if `instruction1` is found before `instruction2` in
71 // this instruction list and false otherwise. Abort if none
72 // of these instructions is found.
73 bool FoundBefore(const HInstruction* instruction1,
74 const HInstruction* instruction2) const;
75
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010076 private:
77 HInstruction* first_instruction_;
78 HInstruction* last_instruction_;
79
80 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000081 friend class HGraph;
82 friend class HInstruction;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010083 friend class HInstructionIterator;
Nicolas Geoffray804d0932014-05-02 08:46:00 +010084 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010085
86 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
87};
88
Nicolas Geoffray818f2102014-02-18 16:43:35 +000089// Control-flow graph of a method. Contains a list of basic blocks.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070090class HGraph : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +000091 public:
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000092 HGraph(ArenaAllocator* arena, int start_instruction_id = 0)
Nicolas Geoffray818f2102014-02-18 16:43:35 +000093 : arena_(arena),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000094 blocks_(arena, kDefaultNumberOfBlocks),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +010095 reverse_post_order_(arena, kDefaultNumberOfBlocks),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070096 entry_block_(nullptr),
97 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +010098 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +010099 number_of_vregs_(0),
100 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000101 temporaries_vreg_slots_(0),
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000102 current_instruction_id_(start_instruction_id) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000103
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000104 ArenaAllocator* GetArena() const { return arena_; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100105 const GrowableArray<HBasicBlock*>& GetBlocks() const { return blocks_; }
Roland Levillain93445682014-10-06 19:24:02 +0100106 HBasicBlock* GetBlock(size_t id) const { return blocks_.Get(id); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000107
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000108 HBasicBlock* GetEntryBlock() const { return entry_block_; }
109 HBasicBlock* GetExitBlock() const { return exit_block_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000110
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000111 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
112 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000113
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000114 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100115
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000116 // Try building the SSA form of this graph, with dominance computation and loop
117 // recognition. Returns whether it was successful in doing all these steps.
118 bool TryBuildingSsa() {
119 BuildDominatorTree();
120 TransformToSsa();
121 return AnalyzeNaturalLoops();
122 }
123
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000124 void BuildDominatorTree();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000125 void TransformToSsa();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100126 void SimplifyCFG();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000127
Nicolas Geoffrayf5370122014-12-02 11:51:19 +0000128 // Analyze all natural loops in this graph. Returns false if one
129 // loop is not natural, that is the header does not dominate the
130 // back edge.
131 bool AnalyzeNaturalLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100132
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000133 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
134 void InlineInto(HGraph* outer_graph, HInvoke* invoke);
135
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100136 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
137 void SimplifyLoop(HBasicBlock* header);
138
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000139 int32_t GetNextInstructionId() {
140 DCHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000141 return current_instruction_id_++;
142 }
143
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000144 int32_t GetCurrentInstructionId() const {
145 return current_instruction_id_;
146 }
147
148 void SetCurrentInstructionId(int32_t id) {
149 current_instruction_id_ = id;
150 }
151
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100152 uint16_t GetMaximumNumberOfOutVRegs() const {
153 return maximum_number_of_out_vregs_;
154 }
155
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000156 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
157 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100158 }
159
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000160 void UpdateTemporariesVRegSlots(size_t slots) {
161 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100162 }
163
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000164 size_t GetTemporariesVRegSlots() const {
165 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100166 }
167
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100168 void SetNumberOfVRegs(uint16_t number_of_vregs) {
169 number_of_vregs_ = number_of_vregs;
170 }
171
172 uint16_t GetNumberOfVRegs() const {
173 return number_of_vregs_;
174 }
175
176 void SetNumberOfInVRegs(uint16_t value) {
177 number_of_in_vregs_ = value;
178 }
179
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100180 uint16_t GetNumberOfLocalVRegs() const {
181 return number_of_vregs_ - number_of_in_vregs_;
182 }
183
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100184 const GrowableArray<HBasicBlock*>& GetReversePostOrder() const {
185 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100186 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100187
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000188 private:
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000189 HBasicBlock* FindCommonDominator(HBasicBlock* first, HBasicBlock* second) const;
190 void VisitBlockForDominatorTree(HBasicBlock* block,
191 HBasicBlock* predecessor,
192 GrowableArray<size_t>* visits);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100193 void FindBackEdges(ArenaBitVector* visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000194 void VisitBlockForBackEdges(HBasicBlock* block,
195 ArenaBitVector* visited,
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100196 ArenaBitVector* visiting);
Roland Levillainfc600dc2014-12-02 17:16:31 +0000197 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000198 void RemoveDeadBlocks(const ArenaBitVector& visited) const;
Jean Christophe Beyler53d9da82014-12-04 13:28:25 -0800199 void RemoveBlock(HBasicBlock* block) const;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000200
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000201 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000202
203 // List of blocks in insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000204 GrowableArray<HBasicBlock*> blocks_;
205
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100206 // List of blocks to perform a reverse post order tree traversal.
207 GrowableArray<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000208
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000209 HBasicBlock* entry_block_;
210 HBasicBlock* exit_block_;
211
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100212 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100213 uint16_t maximum_number_of_out_vregs_;
214
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100215 // The number of virtual registers in this method. Contains the parameters.
216 uint16_t number_of_vregs_;
217
218 // The number of virtual registers used by parameters of this method.
219 uint16_t number_of_in_vregs_;
220
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000221 // Number of vreg size slots that the temporaries use (used in baseline compiler).
222 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100223
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000224 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000225 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000226
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000227 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000228 DISALLOW_COPY_AND_ASSIGN(HGraph);
229};
230
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700231class HLoopInformation : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000232 public:
233 HLoopInformation(HBasicBlock* header, HGraph* graph)
234 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100235 suspend_check_(nullptr),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100236 back_edges_(graph->GetArena(), kDefaultNumberOfBackEdges),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100237 // Make bit vector growable, as the number of blocks may change.
238 blocks_(graph->GetArena(), graph->GetBlocks().Size(), true) {}
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100239
240 HBasicBlock* GetHeader() const {
241 return header_;
242 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000243
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100244 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
245 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
246 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
247
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000248 void AddBackEdge(HBasicBlock* back_edge) {
249 back_edges_.Add(back_edge);
250 }
251
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100252 void RemoveBackEdge(HBasicBlock* back_edge) {
253 back_edges_.Delete(back_edge);
254 }
255
256 bool IsBackEdge(HBasicBlock* block) {
257 for (size_t i = 0, e = back_edges_.Size(); i < e; ++i) {
258 if (back_edges_.Get(i) == block) return true;
259 }
260 return false;
261 }
262
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000263 size_t NumberOfBackEdges() const {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000264 return back_edges_.Size();
265 }
266
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100267 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100268
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100269 const GrowableArray<HBasicBlock*>& GetBackEdges() const {
270 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100271 }
272
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100273 void ClearBackEdges() {
274 back_edges_.Reset();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100275 }
276
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100277 // Find blocks that are part of this loop. Returns whether the loop is a natural loop,
278 // that is the header dominates the back edge.
279 bool Populate();
280
281 // Returns whether this loop information contains `block`.
282 // Note that this loop information *must* be populated before entering this function.
283 bool Contains(const HBasicBlock& block) const;
284
285 // Returns whether this loop information is an inner loop of `other`.
286 // Note that `other` *must* be populated before entering this function.
287 bool IsIn(const HLoopInformation& other) const;
288
289 const ArenaBitVector& GetBlocks() const { return blocks_; }
290
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000291 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100292 // Internal recursive implementation of `Populate`.
293 void PopulateRecursive(HBasicBlock* block);
294
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000295 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100296 HSuspendCheck* suspend_check_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000297 GrowableArray<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100298 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000299
300 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
301};
302
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100303static constexpr size_t kNoLifetime = -1;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100304static constexpr uint32_t kNoDexPc = -1;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100305
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000306// A block in a method. Contains the list of instructions represented
307// as a double linked list. Each block knows its predecessors and
308// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100309
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700310class HBasicBlock : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000311 public:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100312 explicit HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000313 : graph_(graph),
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000314 predecessors_(graph->GetArena(), kDefaultNumberOfPredecessors),
315 successors_(graph->GetArena(), kDefaultNumberOfSuccessors),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000316 loop_information_(nullptr),
317 dominator_(nullptr),
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100318 dominated_blocks_(graph->GetArena(), kDefaultNumberOfDominatedBlocks),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100319 block_id_(-1),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100320 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100321 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000322 lifetime_end_(kNoLifetime),
323 is_catch_block_(false) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000324
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100325 const GrowableArray<HBasicBlock*>& GetPredecessors() const {
326 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000327 }
328
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100329 const GrowableArray<HBasicBlock*>& GetSuccessors() const {
330 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000331 }
332
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100333 const GrowableArray<HBasicBlock*>& GetDominatedBlocks() const {
334 return dominated_blocks_;
335 }
336
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100337 bool IsEntryBlock() const {
338 return graph_->GetEntryBlock() == this;
339 }
340
341 bool IsExitBlock() const {
342 return graph_->GetExitBlock() == this;
343 }
344
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000345 void AddBackEdge(HBasicBlock* back_edge) {
346 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000347 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000348 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100349 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000350 loop_information_->AddBackEdge(back_edge);
351 }
352
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000353 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000354
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000355 int GetBlockId() const { return block_id_; }
356 void SetBlockId(int id) { block_id_ = id; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000357
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000358 HBasicBlock* GetDominator() const { return dominator_; }
359 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100360 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.Add(block); }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000361
362 int NumberOfBackEdges() const {
363 return loop_information_ == nullptr
364 ? 0
365 : loop_information_->NumberOfBackEdges();
366 }
367
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100368 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
369 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100370 const HInstructionList& GetInstructions() const { return instructions_; }
371 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100372 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000373
374 void AddSuccessor(HBasicBlock* block) {
375 successors_.Add(block);
376 block->predecessors_.Add(this);
377 }
378
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100379 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
380 size_t successor_index = GetSuccessorIndexOf(existing);
381 DCHECK_NE(successor_index, static_cast<size_t>(-1));
382 existing->RemovePredecessor(this);
383 new_block->predecessors_.Add(this);
384 successors_.Put(successor_index, new_block);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000385 }
386
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100387 void RemovePredecessor(HBasicBlock* block) {
388 predecessors_.Delete(block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100389 }
390
391 void ClearAllPredecessors() {
392 predecessors_.Reset();
393 }
394
395 void AddPredecessor(HBasicBlock* block) {
396 predecessors_.Add(block);
397 block->successors_.Add(this);
398 }
399
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100400 void SwapPredecessors() {
Nicolas Geoffrayc83d4412014-09-18 16:46:20 +0100401 DCHECK_EQ(predecessors_.Size(), 2u);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100402 HBasicBlock* temp = predecessors_.Get(0);
403 predecessors_.Put(0, predecessors_.Get(1));
404 predecessors_.Put(1, temp);
405 }
406
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100407 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) {
408 for (size_t i = 0, e = predecessors_.Size(); i < e; ++i) {
409 if (predecessors_.Get(i) == predecessor) {
410 return i;
411 }
412 }
413 return -1;
414 }
415
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100416 size_t GetSuccessorIndexOf(HBasicBlock* successor) {
417 for (size_t i = 0, e = successors_.Size(); i < e; ++i) {
418 if (successors_.Get(i) == successor) {
419 return i;
420 }
421 }
422 return -1;
423 }
424
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000425 void AddInstruction(HInstruction* instruction);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100426 void RemoveInstruction(HInstruction* instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100427 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +0100428 // Replace instruction `initial` with `replacement` within this block.
429 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
430 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100431 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100432 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100433 void RemovePhi(HPhi* phi);
434
435 bool IsLoopHeader() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100436 return (loop_information_ != nullptr) && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100437 }
438
Roland Levillain6b879dd2014-09-22 17:13:44 +0100439 bool IsLoopPreHeaderFirstPredecessor() const {
440 DCHECK(IsLoopHeader());
441 DCHECK(!GetPredecessors().IsEmpty());
442 return GetPredecessors().Get(0) == GetLoopInformation()->GetPreHeader();
443 }
444
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100445 HLoopInformation* GetLoopInformation() const {
446 return loop_information_;
447 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000448
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100449 // Set the loop_information_ on this block. This method overrides the current
450 // loop_information if it is an outer loop of the passed loop information.
451 void SetInLoop(HLoopInformation* info) {
452 if (IsLoopHeader()) {
453 // Nothing to do. This just means `info` is an outer loop.
454 } else if (loop_information_ == nullptr) {
455 loop_information_ = info;
456 } else if (loop_information_->Contains(*info->GetHeader())) {
457 // Block is currently part of an outer loop. Make it part of this inner loop.
458 // Note that a non loop header having a loop information means this loop information
459 // has already been populated
460 loop_information_ = info;
461 } else {
462 // Block is part of an inner loop. Do not update the loop information.
463 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
464 // at this point, because this method is being called while populating `info`.
465 }
466 }
467
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100468 bool IsInLoop() const { return loop_information_ != nullptr; }
469
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100470 // Returns wheter this block dominates the blocked passed as parameter.
471 bool Dominates(HBasicBlock* block) const;
472
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100473 size_t GetLifetimeStart() const { return lifetime_start_; }
474 size_t GetLifetimeEnd() const { return lifetime_end_; }
475
476 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
477 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
478
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100479 uint32_t GetDexPc() const { return dex_pc_; }
480
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000481 bool IsCatchBlock() const { return is_catch_block_; }
482 void SetIsCatchBlock() { is_catch_block_ = true; }
483
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000484 private:
485 HGraph* const graph_;
486 GrowableArray<HBasicBlock*> predecessors_;
487 GrowableArray<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100488 HInstructionList instructions_;
489 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000490 HLoopInformation* loop_information_;
491 HBasicBlock* dominator_;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100492 GrowableArray<HBasicBlock*> dominated_blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000493 int block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100494 // The dex program counter of the first instruction of this block.
495 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100496 size_t lifetime_start_;
497 size_t lifetime_end_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000498 bool is_catch_block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000499
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000500 friend class HGraph;
501 friend class HInstruction;
502
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000503 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
504};
505
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100506#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
507 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000508 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000509 M(ArrayGet, Instruction) \
510 M(ArrayLength, Instruction) \
511 M(ArraySet, Instruction) \
512 M(BoundsCheck, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000513 M(CheckCast, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100514 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000515 M(Compare, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100516 M(Condition, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000517 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +0000518 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000519 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100520 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000521 M(Exit, Instruction) \
522 M(FloatConstant, Constant) \
523 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100524 M(GreaterThan, Condition) \
525 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100526 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000527 M(InstanceFieldGet, Instruction) \
528 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000529 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100530 M(IntConstant, Constant) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +0000531 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000532 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100533 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000534 M(LessThan, Condition) \
535 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000536 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000537 M(LoadException, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100538 M(LoadLocal, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000539 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100540 M(Local, Instruction) \
541 M(LongConstant, Constant) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +0000542 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000543 M(Mul, BinaryOperation) \
544 M(Neg, UnaryOperation) \
545 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100546 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +0100547 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000548 M(NotEqual, Condition) \
549 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000550 M(Or, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100551 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000552 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100553 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000554 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100555 M(Return, Instruction) \
556 M(ReturnVoid, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000557 M(Shl, BinaryOperation) \
558 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100559 M(StaticFieldGet, Instruction) \
560 M(StaticFieldSet, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100561 M(StoreLocal, Instruction) \
562 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100563 M(SuspendCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000564 M(Temporary, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000565 M(Throw, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +0000566 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000567 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000568 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000569
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100570#define FOR_EACH_INSTRUCTION(M) \
571 FOR_EACH_CONCRETE_INSTRUCTION(M) \
572 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +0100573 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +0100574 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100575 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -0700576
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100577#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000578FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
579#undef FORWARD_DECLARATION
580
Roland Levillainccc07a92014-09-16 14:48:16 +0100581#define DECLARE_INSTRUCTION(type) \
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100582 virtual InstructionKind GetKind() const { return k##type; } \
Roland Levillainccc07a92014-09-16 14:48:16 +0100583 virtual const char* DebugName() const { return #type; } \
584 virtual const H##type* As##type() const OVERRIDE { return this; } \
585 virtual H##type* As##type() OVERRIDE { return this; } \
586 virtual bool InstructionTypeEquals(HInstruction* other) const { \
587 return other->Is##type(); \
588 } \
589 virtual void Accept(HGraphVisitor* visitor)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000590
David Brazdiled596192015-01-23 10:39:45 +0000591template <typename T> class HUseList;
592
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100593template <typename T>
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700594class HUseListNode : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000595 public:
David Brazdiled596192015-01-23 10:39:45 +0000596 HUseListNode* GetPrevious() const { return prev_; }
597 HUseListNode* GetNext() const { return next_; }
598 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100599 size_t GetIndex() const { return index_; }
600
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000601 private:
David Brazdiled596192015-01-23 10:39:45 +0000602 HUseListNode(T user, size_t index)
603 : user_(user), index_(index), prev_(nullptr), next_(nullptr) {}
604
605 T const user_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100606 const size_t index_;
David Brazdiled596192015-01-23 10:39:45 +0000607 HUseListNode<T>* prev_;
608 HUseListNode<T>* next_;
609
610 friend class HUseList<T>;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000611
612 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
613};
614
David Brazdiled596192015-01-23 10:39:45 +0000615template <typename T>
616class HUseList : public ValueObject {
617 public:
618 HUseList() : first_(nullptr) {}
619
620 void Clear() {
621 first_ = nullptr;
622 }
623
624 // Adds a new entry at the beginning of the use list and returns
625 // the newly created node.
626 HUseListNode<T>* AddUse(T user, size_t index, ArenaAllocator* arena) {
David Brazdilea55b932015-01-27 17:12:29 +0000627 HUseListNode<T>* new_node = new (arena) HUseListNode<T>(user, index);
David Brazdiled596192015-01-23 10:39:45 +0000628 if (IsEmpty()) {
629 first_ = new_node;
630 } else {
631 first_->prev_ = new_node;
632 new_node->next_ = first_;
633 first_ = new_node;
634 }
635 return new_node;
636 }
637
638 HUseListNode<T>* GetFirst() const {
639 return first_;
640 }
641
642 void Remove(HUseListNode<T>* node) {
643 if (node->prev_ != nullptr) {
644 node->prev_->next_ = node->next_;
645 }
646 if (node->next_ != nullptr) {
647 node->next_->prev_ = node->prev_;
648 }
649 if (node == first_) {
650 first_ = node->next_;
651 }
652 }
653
654 bool IsEmpty() const {
655 return first_ == nullptr;
656 }
657
658 bool HasOnlyOneUse() const {
659 return first_ != nullptr && first_->next_ == nullptr;
660 }
661
662 private:
663 HUseListNode<T>* first_;
664};
665
666template<typename T>
667class HUseIterator : public ValueObject {
668 public:
669 explicit HUseIterator(const HUseList<T>& uses) : current_(uses.GetFirst()) {}
670
671 bool Done() const { return current_ == nullptr; }
672
673 void Advance() {
674 DCHECK(!Done());
675 current_ = current_->GetNext();
676 }
677
678 HUseListNode<T>* Current() const {
679 DCHECK(!Done());
680 return current_;
681 }
682
683 private:
684 HUseListNode<T>* current_;
685
686 friend class HValue;
687};
688
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100689// Represents the side effects an instruction may have.
690class SideEffects : public ValueObject {
691 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100692 SideEffects() : flags_(0) {}
693
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100694 static SideEffects None() {
695 return SideEffects(0);
696 }
697
698 static SideEffects All() {
699 return SideEffects(ChangesSomething().flags_ | DependsOnSomething().flags_);
700 }
701
702 static SideEffects ChangesSomething() {
703 return SideEffects((1 << kFlagChangesCount) - 1);
704 }
705
706 static SideEffects DependsOnSomething() {
707 int count = kFlagDependsOnCount - kFlagChangesCount;
708 return SideEffects(((1 << count) - 1) << kFlagChangesCount);
709 }
710
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100711 SideEffects Union(SideEffects other) const {
712 return SideEffects(flags_ | other.flags_);
713 }
714
Roland Levillain72bceff2014-09-15 18:29:00 +0100715 bool HasSideEffects() const {
716 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
717 return (flags_ & all_bits_set) != 0;
718 }
719
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100720 bool HasAllSideEffects() const {
721 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
722 return all_bits_set == (flags_ & all_bits_set);
723 }
724
725 bool DependsOn(SideEffects other) const {
726 size_t depends_flags = other.ComputeDependsFlags();
727 return (flags_ & depends_flags) != 0;
728 }
729
730 bool HasDependencies() const {
731 int count = kFlagDependsOnCount - kFlagChangesCount;
732 size_t all_bits_set = (1 << count) - 1;
733 return ((flags_ >> kFlagChangesCount) & all_bits_set) != 0;
734 }
735
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100736 private:
737 static constexpr int kFlagChangesSomething = 0;
738 static constexpr int kFlagChangesCount = kFlagChangesSomething + 1;
739
740 static constexpr int kFlagDependsOnSomething = kFlagChangesCount;
741 static constexpr int kFlagDependsOnCount = kFlagDependsOnSomething + 1;
742
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100743 explicit SideEffects(size_t flags) : flags_(flags) {}
744
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100745 size_t ComputeDependsFlags() const {
746 return flags_ << kFlagChangesCount;
747 }
748
749 size_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100750};
751
David Brazdiled596192015-01-23 10:39:45 +0000752// A HEnvironment object contains the values of virtual registers at a given location.
753class HEnvironment : public ArenaObject<kArenaAllocMisc> {
754 public:
755 HEnvironment(ArenaAllocator* arena, size_t number_of_vregs)
756 : vregs_(arena, number_of_vregs) {
757 vregs_.SetSize(number_of_vregs);
758 for (size_t i = 0; i < number_of_vregs; i++) {
759 vregs_.Put(i, VRegInfo(nullptr, nullptr));
760 }
761 }
762
763 void CopyFrom(HEnvironment* env);
764
765 void SetRawEnvAt(size_t index, HInstruction* instruction) {
766 vregs_.Put(index, VRegInfo(instruction, nullptr));
767 }
768
769 // Record instructions' use entries of this environment for constant-time removal.
770 void RecordEnvUse(HUseListNode<HEnvironment*>* env_use) {
771 DCHECK(env_use->GetUser() == this);
772 size_t index = env_use->GetIndex();
773 VRegInfo info = vregs_.Get(index);
774 DCHECK(info.vreg_ != nullptr);
775 DCHECK(info.node_ == nullptr);
776 vregs_.Put(index, VRegInfo(info.vreg_, env_use));
777 }
778
779 HInstruction* GetInstructionAt(size_t index) const {
780 return vregs_.Get(index).vreg_;
781 }
782
783 HUseListNode<HEnvironment*>* GetInstructionEnvUseAt(size_t index) const {
784 return vregs_.Get(index).node_;
785 }
786
787 size_t Size() const { return vregs_.Size(); }
788
789 private:
790 struct VRegInfo {
791 HInstruction* vreg_;
792 HUseListNode<HEnvironment*>* node_;
793
794 VRegInfo(HInstruction* instruction, HUseListNode<HEnvironment*>* env_use)
795 : vreg_(instruction), node_(env_use) {}
796 };
797
798 GrowableArray<VRegInfo> vregs_;
799
800 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
801};
802
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700803class HInstruction : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000804 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100805 explicit HInstruction(SideEffects side_effects)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000806 : previous_(nullptr),
807 next_(nullptr),
808 block_(nullptr),
809 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100810 ssa_index_(-1),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100811 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100812 locations_(nullptr),
813 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100814 lifetime_position_(kNoLifetime),
815 side_effects_(side_effects) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000816
Dave Allison20dfc792014-06-16 20:44:29 -0700817 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000818
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100819#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100820 enum InstructionKind {
821 FOR_EACH_INSTRUCTION(DECLARE_KIND)
822 };
823#undef DECLARE_KIND
824
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000825 HInstruction* GetNext() const { return next_; }
826 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000827
Calin Juravle77520bc2015-01-12 18:45:46 +0000828 HInstruction* GetNextDisregardingMoves() const;
829 HInstruction* GetPreviousDisregardingMoves() const;
830
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000831 HBasicBlock* GetBlock() const { return block_; }
832 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100833 bool IsInBlock() const { return block_ != nullptr; }
834 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +0100835 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000836
Roland Levillain6b879dd2014-09-22 17:13:44 +0100837 virtual size_t InputCount() const = 0;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100838 virtual HInstruction* InputAt(size_t i) const = 0;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000839
840 virtual void Accept(HGraphVisitor* visitor) = 0;
841 virtual const char* DebugName() const = 0;
842
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100843 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100844 virtual void SetRawInputAt(size_t index, HInstruction* input) = 0;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100845
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100846 virtual bool NeedsEnvironment() const { return false; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100847 virtual bool IsControlFlow() const { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +0100848 virtual bool CanThrow() const { return false; }
Roland Levillain72bceff2014-09-15 18:29:00 +0100849 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100850
Calin Juravle10e244f2015-01-26 18:54:32 +0000851 // Does not apply for all instructions, but having this at top level greatly
852 // simplifies the null check elimination.
853 virtual bool CanBeNull() const { return true; }
854
Calin Juravle77520bc2015-01-12 18:45:46 +0000855 virtual bool CanDoImplicitNullCheck() const { return false; }
856
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100857 void AddUseAt(HInstruction* user, size_t index) {
David Brazdiled596192015-01-23 10:39:45 +0000858 uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000859 }
860
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100861 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +0100862 DCHECK(user != nullptr);
David Brazdiled596192015-01-23 10:39:45 +0000863 HUseListNode<HEnvironment*>* env_use =
864 env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
865 user->RecordEnvUse(env_use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100866 }
867
868 void RemoveUser(HInstruction* user, size_t index);
David Brazdiled596192015-01-23 10:39:45 +0000869 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100870
David Brazdilea55b932015-01-27 17:12:29 +0000871 const HUseList<HInstruction*>& GetUses() { return uses_; }
872 const HUseList<HEnvironment*>& GetEnvUses() { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000873
David Brazdiled596192015-01-23 10:39:45 +0000874 bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); }
875 bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000876
Roland Levillain6c82d402014-10-13 16:10:27 +0100877 // Does this instruction strictly dominate `other_instruction`?
878 // Returns false if this instruction and `other_instruction` are the same.
879 // Aborts if this instruction and `other_instruction` are both phis.
880 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +0100881
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000882 int GetId() const { return id_; }
883 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000884
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100885 int GetSsaIndex() const { return ssa_index_; }
886 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
887 bool HasSsaIndex() const { return ssa_index_ != -1; }
888
889 bool HasEnvironment() const { return environment_ != nullptr; }
890 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100891 void SetEnvironment(HEnvironment* environment) { environment_ = environment; }
892
Nicolas Geoffray39468442014-09-02 15:17:15 +0100893 // Returns the number of entries in the environment. Typically, that is the
894 // number of dex registers in a method. It could be more in case of inlining.
895 size_t EnvironmentSize() const;
896
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000897 LocationSummary* GetLocations() const { return locations_; }
898 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000899
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100900 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100901 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100902
Nicolas Geoffray82091da2015-01-26 10:02:45 +0000903 // Move `this` instruction before `cursor`.
904 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000905
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100906#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +0100907 bool Is##type() const { return (As##type() != nullptr); } \
908 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000909 virtual H##type* As##type() { return nullptr; }
910
911 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
912#undef INSTRUCTION_TYPE_CHECK
913
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100914 // Returns whether the instruction can be moved within the graph.
915 virtual bool CanBeMoved() const { return false; }
916
917 // Returns whether the two instructions are of the same kind.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700918 virtual bool InstructionTypeEquals(HInstruction* other) const {
919 UNUSED(other);
920 return false;
921 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100922
923 // Returns whether any data encoded in the two instructions is equal.
924 // This method does not look at the inputs. Both instructions must be
925 // of the same type, otherwise the method has undefined behavior.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700926 virtual bool InstructionDataEquals(HInstruction* other) const {
927 UNUSED(other);
928 return false;
929 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100930
931 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +0000932 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100933 // 2) Their inputs are identical.
934 bool Equals(HInstruction* other) const;
935
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100936 virtual InstructionKind GetKind() const = 0;
937
938 virtual size_t ComputeHashCode() const {
939 size_t result = GetKind();
940 for (size_t i = 0, e = InputCount(); i < e; ++i) {
941 result = (result * 31) + InputAt(i)->GetId();
942 }
943 return result;
944 }
945
946 SideEffects GetSideEffects() const { return side_effects_; }
947
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100948 size_t GetLifetimePosition() const { return lifetime_position_; }
949 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
950 LiveInterval* GetLiveInterval() const { return live_interval_; }
951 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
952 bool HasLiveInterval() const { return live_interval_ != nullptr; }
953
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000954 private:
955 HInstruction* previous_;
956 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000957 HBasicBlock* block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000958
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000959 // An instruction gets an id when it is added to the graph.
960 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +0100961 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000962 int id_;
963
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100964 // When doing liveness analysis, instructions that have uses get an SSA index.
965 int ssa_index_;
966
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100967 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +0000968 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100969
970 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +0000971 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100972
Nicolas Geoffray39468442014-09-02 15:17:15 +0100973 // The environment associated with this instruction. Not null if the instruction
974 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100975 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000976
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000977 // Set by the code generator.
978 LocationSummary* locations_;
979
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100980 // Set by the liveness analysis.
981 LiveInterval* live_interval_;
982
983 // Set by the liveness analysis, this is the position in a linear
984 // order of blocks where this instruction's live interval start.
985 size_t lifetime_position_;
986
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100987 const SideEffects side_effects_;
988
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000989 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000990 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100991 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000992
993 DISALLOW_COPY_AND_ASSIGN(HInstruction);
994};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700995std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000996
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000997class HInputIterator : public ValueObject {
998 public:
Dave Allison20dfc792014-06-16 20:44:29 -0700999 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001000
1001 bool Done() const { return index_ == instruction_->InputCount(); }
1002 HInstruction* Current() const { return instruction_->InputAt(index_); }
1003 void Advance() { index_++; }
1004
1005 private:
1006 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001007 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001008
1009 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
1010};
1011
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001012class HInstructionIterator : public ValueObject {
1013 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001014 explicit HInstructionIterator(const HInstructionList& instructions)
1015 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001016 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001017 }
1018
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001019 bool Done() const { return instruction_ == nullptr; }
1020 HInstruction* Current() const { return instruction_; }
1021 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001022 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001023 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001024 }
1025
1026 private:
1027 HInstruction* instruction_;
1028 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001029
1030 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001031};
1032
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001033class HBackwardInstructionIterator : public ValueObject {
1034 public:
1035 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
1036 : instruction_(instructions.last_instruction_) {
1037 next_ = Done() ? nullptr : instruction_->GetPrevious();
1038 }
1039
1040 bool Done() const { return instruction_ == nullptr; }
1041 HInstruction* Current() const { return instruction_; }
1042 void Advance() {
1043 instruction_ = next_;
1044 next_ = Done() ? nullptr : instruction_->GetPrevious();
1045 }
1046
1047 private:
1048 HInstruction* instruction_;
1049 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001050
1051 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001052};
1053
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001054// An embedded container with N elements of type T. Used (with partial
1055// specialization for N=0) because embedded arrays cannot have size 0.
1056template<typename T, intptr_t N>
1057class EmbeddedArray {
1058 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001059 EmbeddedArray() : elements_() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001060
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001061 intptr_t GetLength() const { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001062
1063 const T& operator[](intptr_t i) const {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001064 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001065 return elements_[i];
1066 }
1067
1068 T& operator[](intptr_t i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001069 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001070 return elements_[i];
1071 }
1072
1073 const T& At(intptr_t i) const {
1074 return (*this)[i];
1075 }
1076
1077 void SetAt(intptr_t i, const T& val) {
1078 (*this)[i] = val;
1079 }
1080
1081 private:
1082 T elements_[N];
1083};
1084
1085template<typename T>
1086class EmbeddedArray<T, 0> {
1087 public:
1088 intptr_t length() const { return 0; }
1089 const T& operator[](intptr_t i) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001090 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001091 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001092 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001093 }
1094 T& operator[](intptr_t i) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001095 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001096 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001097 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001098 }
1099};
1100
1101template<intptr_t N>
1102class HTemplateInstruction: public HInstruction {
1103 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001104 HTemplateInstruction<N>(SideEffects side_effects)
1105 : HInstruction(side_effects), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07001106 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001107
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001108 virtual size_t InputCount() const { return N; }
1109 virtual HInstruction* InputAt(size_t i) const { return inputs_[i]; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001110
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001111 protected:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001112 virtual void SetRawInputAt(size_t i, HInstruction* instruction) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001113 inputs_[i] = instruction;
1114 }
1115
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001116 private:
1117 EmbeddedArray<HInstruction*, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001118
1119 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001120};
1121
Dave Allison20dfc792014-06-16 20:44:29 -07001122template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001123class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07001124 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001125 HExpression<N>(Primitive::Type type, SideEffects side_effects)
1126 : HTemplateInstruction<N>(side_effects), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07001127 virtual ~HExpression() {}
1128
1129 virtual Primitive::Type GetType() const { return type_; }
1130
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001131 protected:
1132 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07001133};
1134
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001135// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
1136// instruction that branches to the exit block.
1137class HReturnVoid : public HTemplateInstruction<0> {
1138 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001139 HReturnVoid() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001140
1141 virtual bool IsControlFlow() const { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001142
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001143 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001144
1145 private:
1146 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
1147};
1148
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001149// Represents dex's RETURN opcodes. A HReturn is a control flow
1150// instruction that branches to the exit block.
1151class HReturn : public HTemplateInstruction<1> {
1152 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001153 explicit HReturn(HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001154 SetRawInputAt(0, value);
1155 }
1156
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001157 virtual bool IsControlFlow() const { return true; }
1158
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001159 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001160
1161 private:
1162 DISALLOW_COPY_AND_ASSIGN(HReturn);
1163};
1164
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001165// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001166// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001167// exit block.
1168class HExit : public HTemplateInstruction<0> {
1169 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001170 HExit() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001171
1172 virtual bool IsControlFlow() const { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001173
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001174 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001175
1176 private:
1177 DISALLOW_COPY_AND_ASSIGN(HExit);
1178};
1179
1180// Jumps from one block to another.
1181class HGoto : public HTemplateInstruction<0> {
1182 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001183 HGoto() : HTemplateInstruction(SideEffects::None()) {}
1184
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001185 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001186
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001187 HBasicBlock* GetSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001188 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001189 }
1190
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001191 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001192
1193 private:
1194 DISALLOW_COPY_AND_ASSIGN(HGoto);
1195};
1196
Dave Allison20dfc792014-06-16 20:44:29 -07001197
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001198// Conditional branch. A block ending with an HIf instruction must have
1199// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001200class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001201 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001202 explicit HIf(HInstruction* input) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001203 SetRawInputAt(0, input);
1204 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001205
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001206 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001207
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001208 HBasicBlock* IfTrueSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001209 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001210 }
1211
1212 HBasicBlock* IfFalseSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001213 return GetBlock()->GetSuccessors().Get(1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001214 }
1215
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001216 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001217
Dave Allison20dfc792014-06-16 20:44:29 -07001218 virtual bool IsIfInstruction() const { return true; }
1219
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001220 private:
1221 DISALLOW_COPY_AND_ASSIGN(HIf);
1222};
1223
Roland Levillain88cb1752014-10-20 16:36:47 +01001224class HUnaryOperation : public HExpression<1> {
1225 public:
1226 HUnaryOperation(Primitive::Type result_type, HInstruction* input)
1227 : HExpression(result_type, SideEffects::None()) {
1228 SetRawInputAt(0, input);
1229 }
1230
1231 HInstruction* GetInput() const { return InputAt(0); }
1232 Primitive::Type GetResultType() const { return GetType(); }
1233
1234 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001235 virtual bool InstructionDataEquals(HInstruction* other) const {
1236 UNUSED(other);
1237 return true;
1238 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001239
Roland Levillain9240d6a2014-10-20 16:47:04 +01001240 // Try to statically evaluate `operation` and return a HConstant
1241 // containing the result of this evaluation. If `operation` cannot
1242 // be evaluated as a constant, return nullptr.
1243 HConstant* TryStaticEvaluation() const;
1244
1245 // Apply this operation to `x`.
1246 virtual int32_t Evaluate(int32_t x) const = 0;
1247 virtual int64_t Evaluate(int64_t x) const = 0;
1248
Roland Levillain88cb1752014-10-20 16:36:47 +01001249 DECLARE_INSTRUCTION(UnaryOperation);
1250
1251 private:
1252 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
1253};
1254
Dave Allison20dfc792014-06-16 20:44:29 -07001255class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001256 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001257 HBinaryOperation(Primitive::Type result_type,
1258 HInstruction* left,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001259 HInstruction* right) : HExpression(result_type, SideEffects::None()) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001260 SetRawInputAt(0, left);
1261 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001262 }
1263
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001264 HInstruction* GetLeft() const { return InputAt(0); }
1265 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07001266 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001267
1268 virtual bool IsCommutative() { return false; }
1269
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001270 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001271 virtual bool InstructionDataEquals(HInstruction* other) const {
1272 UNUSED(other);
1273 return true;
1274 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001275
Roland Levillain9240d6a2014-10-20 16:47:04 +01001276 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01001277 // containing the result of this evaluation. If `operation` cannot
1278 // be evaluated as a constant, return nullptr.
Roland Levillain9240d6a2014-10-20 16:47:04 +01001279 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01001280
1281 // Apply this operation to `x` and `y`.
1282 virtual int32_t Evaluate(int32_t x, int32_t y) const = 0;
1283 virtual int64_t Evaluate(int64_t x, int64_t y) const = 0;
1284
Roland Levillainccc07a92014-09-16 14:48:16 +01001285 DECLARE_INSTRUCTION(BinaryOperation);
1286
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001287 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001288 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
1289};
1290
Dave Allison20dfc792014-06-16 20:44:29 -07001291class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001292 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001293 HCondition(HInstruction* first, HInstruction* second)
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001294 : HBinaryOperation(Primitive::kPrimBoolean, first, second),
1295 needs_materialization_(true) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001296
1297 virtual bool IsCommutative() { return true; }
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001298
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001299 bool NeedsMaterialization() const { return needs_materialization_; }
1300 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001301
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001302 // For code generation purposes, returns whether this instruction is just before
1303 // `if_`, and disregard moves in between.
1304 bool IsBeforeWhenDisregardMoves(HIf* if_) const;
1305
Dave Allison20dfc792014-06-16 20:44:29 -07001306 DECLARE_INSTRUCTION(Condition);
1307
1308 virtual IfCondition GetCondition() const = 0;
1309
1310 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001311 // For register allocation purposes, returns whether this instruction needs to be
1312 // materialized (that is, not just be in the processor flags).
1313 bool needs_materialization_;
1314
Dave Allison20dfc792014-06-16 20:44:29 -07001315 DISALLOW_COPY_AND_ASSIGN(HCondition);
1316};
1317
1318// Instruction to check if two inputs are equal to each other.
1319class HEqual : public HCondition {
1320 public:
1321 HEqual(HInstruction* first, HInstruction* second)
1322 : HCondition(first, second) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001323
Roland Levillain93445682014-10-06 19:24:02 +01001324 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1325 return x == y ? 1 : 0;
1326 }
1327 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1328 return x == y ? 1 : 0;
1329 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001330
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001331 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001332
Dave Allison20dfc792014-06-16 20:44:29 -07001333 virtual IfCondition GetCondition() const {
1334 return kCondEQ;
1335 }
1336
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001337 private:
1338 DISALLOW_COPY_AND_ASSIGN(HEqual);
1339};
1340
Dave Allison20dfc792014-06-16 20:44:29 -07001341class HNotEqual : public HCondition {
1342 public:
1343 HNotEqual(HInstruction* first, HInstruction* second)
1344 : HCondition(first, second) {}
1345
Roland Levillain93445682014-10-06 19:24:02 +01001346 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1347 return x != y ? 1 : 0;
1348 }
1349 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1350 return x != y ? 1 : 0;
1351 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001352
Dave Allison20dfc792014-06-16 20:44:29 -07001353 DECLARE_INSTRUCTION(NotEqual);
1354
1355 virtual IfCondition GetCondition() const {
1356 return kCondNE;
1357 }
1358
1359 private:
1360 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
1361};
1362
1363class HLessThan : public HCondition {
1364 public:
1365 HLessThan(HInstruction* first, HInstruction* second)
1366 : HCondition(first, second) {}
1367
Roland Levillain93445682014-10-06 19:24:02 +01001368 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1369 return x < y ? 1 : 0;
1370 }
1371 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1372 return x < y ? 1 : 0;
1373 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001374
Dave Allison20dfc792014-06-16 20:44:29 -07001375 DECLARE_INSTRUCTION(LessThan);
1376
1377 virtual IfCondition GetCondition() const {
1378 return kCondLT;
1379 }
1380
1381 private:
1382 DISALLOW_COPY_AND_ASSIGN(HLessThan);
1383};
1384
1385class HLessThanOrEqual : public HCondition {
1386 public:
1387 HLessThanOrEqual(HInstruction* first, HInstruction* second)
1388 : HCondition(first, second) {}
1389
Roland Levillain93445682014-10-06 19:24:02 +01001390 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1391 return x <= y ? 1 : 0;
1392 }
1393 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1394 return x <= y ? 1 : 0;
1395 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001396
Dave Allison20dfc792014-06-16 20:44:29 -07001397 DECLARE_INSTRUCTION(LessThanOrEqual);
1398
1399 virtual IfCondition GetCondition() const {
1400 return kCondLE;
1401 }
1402
1403 private:
1404 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
1405};
1406
1407class HGreaterThan : public HCondition {
1408 public:
1409 HGreaterThan(HInstruction* first, HInstruction* second)
1410 : HCondition(first, second) {}
1411
Roland Levillain93445682014-10-06 19:24:02 +01001412 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1413 return x > y ? 1 : 0;
1414 }
1415 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1416 return x > y ? 1 : 0;
1417 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001418
Dave Allison20dfc792014-06-16 20:44:29 -07001419 DECLARE_INSTRUCTION(GreaterThan);
1420
1421 virtual IfCondition GetCondition() const {
1422 return kCondGT;
1423 }
1424
1425 private:
1426 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
1427};
1428
1429class HGreaterThanOrEqual : public HCondition {
1430 public:
1431 HGreaterThanOrEqual(HInstruction* first, HInstruction* second)
1432 : HCondition(first, second) {}
1433
Roland Levillain93445682014-10-06 19:24:02 +01001434 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1435 return x >= y ? 1 : 0;
1436 }
1437 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1438 return x >= y ? 1 : 0;
1439 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001440
Dave Allison20dfc792014-06-16 20:44:29 -07001441 DECLARE_INSTRUCTION(GreaterThanOrEqual);
1442
1443 virtual IfCondition GetCondition() const {
1444 return kCondGE;
1445 }
1446
1447 private:
1448 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
1449};
1450
1451
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001452// Instruction to check how two inputs compare to each other.
1453// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
1454class HCompare : public HBinaryOperation {
1455 public:
Calin Juravleddb7df22014-11-25 20:56:51 +00001456 // The bias applies for floating point operations and indicates how NaN
1457 // comparisons are treated:
1458 enum Bias {
1459 kNoBias, // bias is not applicable (i.e. for long operation)
1460 kGtBias, // return 1 for NaN comparisons
1461 kLtBias, // return -1 for NaN comparisons
1462 };
1463
1464 HCompare(Primitive::Type type, HInstruction* first, HInstruction* second, Bias bias)
1465 : HBinaryOperation(Primitive::kPrimInt, first, second), bias_(bias) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001466 DCHECK_EQ(type, first->GetType());
1467 DCHECK_EQ(type, second->GetType());
1468 }
1469
Calin Juravleddb7df22014-11-25 20:56:51 +00001470 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01001471 return
1472 x == y ? 0 :
1473 x > y ? 1 :
1474 -1;
1475 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001476
1477 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01001478 return
1479 x == y ? 0 :
1480 x > y ? 1 :
1481 -1;
1482 }
1483
Calin Juravleddb7df22014-11-25 20:56:51 +00001484 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
1485 return bias_ == other->AsCompare()->bias_;
1486 }
1487
1488 bool IsGtBias() { return bias_ == kGtBias; }
1489
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001490 DECLARE_INSTRUCTION(Compare);
1491
1492 private:
Calin Juravleddb7df22014-11-25 20:56:51 +00001493 const Bias bias_;
1494
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001495 DISALLOW_COPY_AND_ASSIGN(HCompare);
1496};
1497
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001498// A local in the graph. Corresponds to a Dex register.
1499class HLocal : public HTemplateInstruction<0> {
1500 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001501 explicit HLocal(uint16_t reg_number)
1502 : HTemplateInstruction(SideEffects::None()), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001503
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001504 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001505
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001506 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001507
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001508 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001509 // The Dex register number.
1510 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001511
1512 DISALLOW_COPY_AND_ASSIGN(HLocal);
1513};
1514
1515// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07001516class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001517 public:
Roland Levillain5799fc02014-09-25 12:15:20 +01001518 HLoadLocal(HLocal* local, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001519 : HExpression(type, SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001520 SetRawInputAt(0, local);
1521 }
1522
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001523 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1524
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001525 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001526
1527 private:
1528 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
1529};
1530
1531// Store a value in a given local. This instruction has two inputs: the value
1532// and the local.
1533class HStoreLocal : public HTemplateInstruction<2> {
1534 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001535 HStoreLocal(HLocal* local, HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001536 SetRawInputAt(0, local);
1537 SetRawInputAt(1, value);
1538 }
1539
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001540 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1541
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001542 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001543
1544 private:
1545 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
1546};
1547
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001548class HConstant : public HExpression<0> {
1549 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001550 explicit HConstant(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
1551
1552 virtual bool CanBeMoved() const { return true; }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001553
1554 DECLARE_INSTRUCTION(Constant);
1555
1556 private:
1557 DISALLOW_COPY_AND_ASSIGN(HConstant);
1558};
1559
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001560class HFloatConstant : public HConstant {
1561 public:
1562 explicit HFloatConstant(float value) : HConstant(Primitive::kPrimFloat), value_(value) {}
1563
1564 float GetValue() const { return value_; }
1565
1566 virtual bool InstructionDataEquals(HInstruction* other) const {
1567 return bit_cast<float, int32_t>(other->AsFloatConstant()->value_) ==
1568 bit_cast<float, int32_t>(value_);
1569 }
1570
1571 virtual size_t ComputeHashCode() const { return static_cast<size_t>(GetValue()); }
1572
1573 DECLARE_INSTRUCTION(FloatConstant);
1574
1575 private:
1576 const float value_;
1577
1578 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
1579};
1580
1581class HDoubleConstant : public HConstant {
1582 public:
1583 explicit HDoubleConstant(double value) : HConstant(Primitive::kPrimDouble), value_(value) {}
1584
1585 double GetValue() const { return value_; }
1586
1587 virtual bool InstructionDataEquals(HInstruction* other) const {
1588 return bit_cast<double, int64_t>(other->AsDoubleConstant()->value_) ==
1589 bit_cast<double, int64_t>(value_);
1590 }
1591
1592 virtual size_t ComputeHashCode() const { return static_cast<size_t>(GetValue()); }
1593
1594 DECLARE_INSTRUCTION(DoubleConstant);
1595
1596 private:
1597 const double value_;
1598
1599 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
1600};
1601
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001602// Constants of the type int. Those can be from Dex instructions, or
1603// synthesized (for example with the if-eqz instruction).
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001604class HIntConstant : public HConstant {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001605 public:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001606 explicit HIntConstant(int32_t value) : HConstant(Primitive::kPrimInt), value_(value) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001607
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001608 int32_t GetValue() const { return value_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001609
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001610 virtual bool InstructionDataEquals(HInstruction* other) const {
1611 return other->AsIntConstant()->value_ == value_;
1612 }
1613
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001614 virtual size_t ComputeHashCode() const { return GetValue(); }
1615
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001616 DECLARE_INSTRUCTION(IntConstant);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001617
1618 private:
1619 const int32_t value_;
1620
1621 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
1622};
1623
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001624class HLongConstant : public HConstant {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001625 public:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001626 explicit HLongConstant(int64_t value) : HConstant(Primitive::kPrimLong), value_(value) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001627
1628 int64_t GetValue() const { return value_; }
1629
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001630 virtual bool InstructionDataEquals(HInstruction* other) const {
1631 return other->AsLongConstant()->value_ == value_;
1632 }
1633
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001634 virtual size_t ComputeHashCode() const { return static_cast<size_t>(GetValue()); }
1635
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001636 DECLARE_INSTRUCTION(LongConstant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001637
1638 private:
1639 const int64_t value_;
1640
1641 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
1642};
1643
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001644enum class Intrinsics {
1645#define OPTIMIZING_INTRINSICS(Name, IsStatic) k ## Name,
1646#include "intrinsics_list.h"
1647 kNone,
1648 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
1649#undef INTRINSICS_LIST
1650#undef OPTIMIZING_INTRINSICS
1651};
1652std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
1653
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001654class HInvoke : public HInstruction {
1655 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001656 virtual size_t InputCount() const { return inputs_.Size(); }
1657 virtual HInstruction* InputAt(size_t i) const { return inputs_.Get(i); }
1658
1659 // Runtime needs to walk the stack, so Dex -> Dex calls need to
1660 // know their environment.
Calin Juravle77520bc2015-01-12 18:45:46 +00001661 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001662
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001663 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001664 SetRawInputAt(index, argument);
1665 }
1666
1667 virtual void SetRawInputAt(size_t index, HInstruction* input) {
1668 inputs_.Put(index, input);
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001669 }
1670
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001671 virtual Primitive::Type GetType() const { return return_type_; }
1672
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001673 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001674
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001675 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
1676
1677 Intrinsics GetIntrinsic() {
1678 return intrinsic_;
1679 }
1680
1681 void SetIntrinsic(Intrinsics intrinsic) {
1682 intrinsic_ = intrinsic;
1683 }
1684
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001685 DECLARE_INSTRUCTION(Invoke);
1686
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001687 protected:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001688 HInvoke(ArenaAllocator* arena,
1689 uint32_t number_of_arguments,
1690 Primitive::Type return_type,
1691 uint32_t dex_pc,
1692 uint32_t dex_method_index)
1693 : HInstruction(SideEffects::All()),
1694 inputs_(arena, number_of_arguments),
1695 return_type_(return_type),
1696 dex_pc_(dex_pc),
1697 dex_method_index_(dex_method_index),
1698 intrinsic_(Intrinsics::kNone) {
1699 inputs_.SetSize(number_of_arguments);
1700 }
1701
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001702 GrowableArray<HInstruction*> inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001703 const Primitive::Type return_type_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001704 const uint32_t dex_pc_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001705 const uint32_t dex_method_index_;
1706 Intrinsics intrinsic_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001707
1708 private:
1709 DISALLOW_COPY_AND_ASSIGN(HInvoke);
1710};
1711
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001712class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001713 public:
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001714 HInvokeStaticOrDirect(ArenaAllocator* arena,
1715 uint32_t number_of_arguments,
1716 Primitive::Type return_type,
1717 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001718 uint32_t dex_method_index,
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001719 bool is_recursive,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001720 InvokeType invoke_type)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001721 : HInvoke(arena, number_of_arguments, return_type, dex_pc, dex_method_index),
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001722 invoke_type_(invoke_type),
1723 is_recursive_(is_recursive) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001724
Calin Juravle77520bc2015-01-12 18:45:46 +00001725 bool CanDoImplicitNullCheck() const OVERRIDE {
1726 // We access the method via the dex cache so we can't do an implicit null check.
1727 // TODO: for intrinsics we can generate implicit null checks.
1728 return false;
1729 }
1730
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001731 InvokeType GetInvokeType() const { return invoke_type_; }
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001732 bool IsRecursive() const { return is_recursive_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001733
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001734 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001735
1736 private:
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001737 const InvokeType invoke_type_;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001738 const bool is_recursive_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001739
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001740 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001741};
1742
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001743class HInvokeVirtual : public HInvoke {
1744 public:
1745 HInvokeVirtual(ArenaAllocator* arena,
1746 uint32_t number_of_arguments,
1747 Primitive::Type return_type,
1748 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001749 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001750 uint32_t vtable_index)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001751 : HInvoke(arena, number_of_arguments, return_type, dex_pc, dex_method_index),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001752 vtable_index_(vtable_index) {}
1753
Calin Juravle77520bc2015-01-12 18:45:46 +00001754 bool CanDoImplicitNullCheck() const OVERRIDE {
1755 // TODO: Add implicit null checks in intrinsics.
1756 return !GetLocations()->Intrinsified();
1757 }
1758
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001759 uint32_t GetVTableIndex() const { return vtable_index_; }
1760
1761 DECLARE_INSTRUCTION(InvokeVirtual);
1762
1763 private:
1764 const uint32_t vtable_index_;
1765
1766 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
1767};
1768
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001769class HInvokeInterface : public HInvoke {
1770 public:
1771 HInvokeInterface(ArenaAllocator* arena,
1772 uint32_t number_of_arguments,
1773 Primitive::Type return_type,
1774 uint32_t dex_pc,
1775 uint32_t dex_method_index,
1776 uint32_t imt_index)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001777 : HInvoke(arena, number_of_arguments, return_type, dex_pc, dex_method_index),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001778 imt_index_(imt_index) {}
1779
Calin Juravle77520bc2015-01-12 18:45:46 +00001780 bool CanDoImplicitNullCheck() const OVERRIDE {
1781 // TODO: Add implicit null checks in intrinsics.
1782 return !GetLocations()->Intrinsified();
1783 }
1784
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001785 uint32_t GetImtIndex() const { return imt_index_; }
1786 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
1787
1788 DECLARE_INSTRUCTION(InvokeInterface);
1789
1790 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001791 const uint32_t imt_index_;
1792
1793 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
1794};
1795
Dave Allison20dfc792014-06-16 20:44:29 -07001796class HNewInstance : public HExpression<0> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001797 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00001798 HNewInstance(uint32_t dex_pc, uint16_t type_index, QuickEntrypointEnum entrypoint)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001799 : HExpression(Primitive::kPrimNot, SideEffects::None()),
1800 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00001801 type_index_(type_index),
1802 entrypoint_(entrypoint) {}
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001803
1804 uint32_t GetDexPc() const { return dex_pc_; }
1805 uint16_t GetTypeIndex() const { return type_index_; }
1806
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001807 // Calls runtime so needs an environment.
Calin Juravle92a6ed22014-12-02 18:58:03 +00001808 bool NeedsEnvironment() const OVERRIDE { return true; }
1809 // It may throw when called on:
1810 // - interfaces
1811 // - abstract/innaccessible/unknown classes
1812 // TODO: optimize when possible.
1813 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001814
Calin Juravle10e244f2015-01-26 18:54:32 +00001815 bool CanBeNull() const OVERRIDE { return false; }
1816
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00001817 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
1818
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001819 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001820
1821 private:
1822 const uint32_t dex_pc_;
1823 const uint16_t type_index_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00001824 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001825
1826 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
1827};
1828
Roland Levillain88cb1752014-10-20 16:36:47 +01001829class HNeg : public HUnaryOperation {
1830 public:
1831 explicit HNeg(Primitive::Type result_type, HInstruction* input)
1832 : HUnaryOperation(result_type, input) {}
1833
Roland Levillain9240d6a2014-10-20 16:47:04 +01001834 virtual int32_t Evaluate(int32_t x) const OVERRIDE { return -x; }
1835 virtual int64_t Evaluate(int64_t x) const OVERRIDE { return -x; }
1836
Roland Levillain88cb1752014-10-20 16:36:47 +01001837 DECLARE_INSTRUCTION(Neg);
1838
1839 private:
1840 DISALLOW_COPY_AND_ASSIGN(HNeg);
1841};
1842
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001843class HNewArray : public HExpression<1> {
1844 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00001845 HNewArray(HInstruction* length,
1846 uint32_t dex_pc,
1847 uint16_t type_index,
1848 QuickEntrypointEnum entrypoint)
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001849 : HExpression(Primitive::kPrimNot, SideEffects::None()),
1850 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00001851 type_index_(type_index),
1852 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001853 SetRawInputAt(0, length);
1854 }
1855
1856 uint32_t GetDexPc() const { return dex_pc_; }
1857 uint16_t GetTypeIndex() const { return type_index_; }
1858
1859 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00001860 bool NeedsEnvironment() const OVERRIDE { return true; }
1861
1862 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001863
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00001864 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
1865
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001866 DECLARE_INSTRUCTION(NewArray);
1867
1868 private:
1869 const uint32_t dex_pc_;
1870 const uint16_t type_index_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00001871 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001872
1873 DISALLOW_COPY_AND_ASSIGN(HNewArray);
1874};
1875
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001876class HAdd : public HBinaryOperation {
1877 public:
1878 HAdd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1879 : HBinaryOperation(result_type, left, right) {}
1880
1881 virtual bool IsCommutative() { return true; }
1882
Roland Levillain93445682014-10-06 19:24:02 +01001883 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1884 return x + y;
1885 }
1886 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1887 return x + y;
1888 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001889
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001890 DECLARE_INSTRUCTION(Add);
1891
1892 private:
1893 DISALLOW_COPY_AND_ASSIGN(HAdd);
1894};
1895
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001896class HSub : public HBinaryOperation {
1897 public:
1898 HSub(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1899 : HBinaryOperation(result_type, left, right) {}
1900
Roland Levillain93445682014-10-06 19:24:02 +01001901 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1902 return x - y;
1903 }
1904 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1905 return x - y;
1906 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001907
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001908 DECLARE_INSTRUCTION(Sub);
1909
1910 private:
1911 DISALLOW_COPY_AND_ASSIGN(HSub);
1912};
1913
Calin Juravle34bacdf2014-10-07 20:23:36 +01001914class HMul : public HBinaryOperation {
1915 public:
1916 HMul(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1917 : HBinaryOperation(result_type, left, right) {}
1918
1919 virtual bool IsCommutative() { return true; }
1920
1921 virtual int32_t Evaluate(int32_t x, int32_t y) const { return x * y; }
1922 virtual int64_t Evaluate(int64_t x, int64_t y) const { return x * y; }
1923
1924 DECLARE_INSTRUCTION(Mul);
1925
1926 private:
1927 DISALLOW_COPY_AND_ASSIGN(HMul);
1928};
1929
Calin Juravle7c4954d2014-10-28 16:57:40 +00001930class HDiv : public HBinaryOperation {
1931 public:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001932 HDiv(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
1933 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00001934
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00001935 virtual int32_t Evaluate(int32_t x, int32_t y) const {
1936 // Our graph structure ensures we never have 0 for `y` during constant folding.
1937 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00001938 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00001939 return (y == -1) ? -x : x / y;
1940 }
Calin Juravlebacfec32014-11-14 15:54:36 +00001941
1942 virtual int64_t Evaluate(int64_t x, int64_t y) const {
1943 DCHECK_NE(y, 0);
1944 // Special case -1 to avoid getting a SIGFPE on x86(_64).
1945 return (y == -1) ? -x : x / y;
1946 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00001947
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001948 uint32_t GetDexPc() const { return dex_pc_; }
1949
Calin Juravle7c4954d2014-10-28 16:57:40 +00001950 DECLARE_INSTRUCTION(Div);
1951
1952 private:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001953 const uint32_t dex_pc_;
1954
Calin Juravle7c4954d2014-10-28 16:57:40 +00001955 DISALLOW_COPY_AND_ASSIGN(HDiv);
1956};
1957
Calin Juravlebacfec32014-11-14 15:54:36 +00001958class HRem : public HBinaryOperation {
1959 public:
1960 HRem(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
1961 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
1962
1963 virtual int32_t Evaluate(int32_t x, int32_t y) const {
1964 DCHECK_NE(y, 0);
1965 // Special case -1 to avoid getting a SIGFPE on x86(_64).
1966 return (y == -1) ? 0 : x % y;
1967 }
1968
1969 virtual int64_t Evaluate(int64_t x, int64_t y) const {
1970 DCHECK_NE(y, 0);
1971 // Special case -1 to avoid getting a SIGFPE on x86(_64).
1972 return (y == -1) ? 0 : x % y;
1973 }
1974
1975 uint32_t GetDexPc() const { return dex_pc_; }
1976
1977 DECLARE_INSTRUCTION(Rem);
1978
1979 private:
1980 const uint32_t dex_pc_;
1981
1982 DISALLOW_COPY_AND_ASSIGN(HRem);
1983};
1984
Calin Juravled0d48522014-11-04 16:40:20 +00001985class HDivZeroCheck : public HExpression<1> {
1986 public:
1987 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
1988 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
1989 SetRawInputAt(0, value);
1990 }
1991
1992 bool CanBeMoved() const OVERRIDE { return true; }
1993
1994 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
1995 UNUSED(other);
1996 return true;
1997 }
1998
1999 bool NeedsEnvironment() const OVERRIDE { return true; }
2000 bool CanThrow() const OVERRIDE { return true; }
2001
2002 uint32_t GetDexPc() const { return dex_pc_; }
2003
2004 DECLARE_INSTRUCTION(DivZeroCheck);
2005
2006 private:
2007 const uint32_t dex_pc_;
2008
2009 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
2010};
2011
Calin Juravle9aec02f2014-11-18 23:06:35 +00002012class HShl : public HBinaryOperation {
2013 public:
2014 HShl(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2015 : HBinaryOperation(result_type, left, right) {}
2016
2017 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x << (y & kMaxIntShiftValue); }
2018 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x << (y & kMaxLongShiftValue); }
2019
2020 DECLARE_INSTRUCTION(Shl);
2021
2022 private:
2023 DISALLOW_COPY_AND_ASSIGN(HShl);
2024};
2025
2026class HShr : public HBinaryOperation {
2027 public:
2028 HShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2029 : HBinaryOperation(result_type, left, right) {}
2030
2031 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x >> (y & kMaxIntShiftValue); }
2032 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x >> (y & kMaxLongShiftValue); }
2033
2034 DECLARE_INSTRUCTION(Shr);
2035
2036 private:
2037 DISALLOW_COPY_AND_ASSIGN(HShr);
2038};
2039
2040class HUShr : public HBinaryOperation {
2041 public:
2042 HUShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2043 : HBinaryOperation(result_type, left, right) {}
2044
2045 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
2046 uint32_t ux = static_cast<uint32_t>(x);
2047 uint32_t uy = static_cast<uint32_t>(y) & kMaxIntShiftValue;
2048 return static_cast<int32_t>(ux >> uy);
2049 }
2050
2051 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
2052 uint64_t ux = static_cast<uint64_t>(x);
2053 uint64_t uy = static_cast<uint64_t>(y) & kMaxLongShiftValue;
2054 return static_cast<int64_t>(ux >> uy);
2055 }
2056
2057 DECLARE_INSTRUCTION(UShr);
2058
2059 private:
2060 DISALLOW_COPY_AND_ASSIGN(HUShr);
2061};
2062
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002063class HAnd : public HBinaryOperation {
2064 public:
2065 HAnd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2066 : HBinaryOperation(result_type, left, right) {}
2067
2068 bool IsCommutative() OVERRIDE { return true; }
2069
2070 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x & y; }
2071 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x & y; }
2072
2073 DECLARE_INSTRUCTION(And);
2074
2075 private:
2076 DISALLOW_COPY_AND_ASSIGN(HAnd);
2077};
2078
2079class HOr : public HBinaryOperation {
2080 public:
2081 HOr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2082 : HBinaryOperation(result_type, left, right) {}
2083
2084 bool IsCommutative() OVERRIDE { return true; }
2085
2086 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x | y; }
2087 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x | y; }
2088
2089 DECLARE_INSTRUCTION(Or);
2090
2091 private:
2092 DISALLOW_COPY_AND_ASSIGN(HOr);
2093};
2094
2095class HXor : public HBinaryOperation {
2096 public:
2097 HXor(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2098 : HBinaryOperation(result_type, left, right) {}
2099
2100 bool IsCommutative() OVERRIDE { return true; }
2101
2102 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x ^ y; }
2103 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x ^ y; }
2104
2105 DECLARE_INSTRUCTION(Xor);
2106
2107 private:
2108 DISALLOW_COPY_AND_ASSIGN(HXor);
2109};
2110
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002111// The value of a parameter in this method. Its location depends on
2112// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07002113class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002114 public:
Calin Juravle10e244f2015-01-26 18:54:32 +00002115 HParameterValue(uint8_t index, Primitive::Type parameter_type, bool is_this = false)
2116 : HExpression(parameter_type, SideEffects::None()), index_(index), is_this_(is_this) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002117
2118 uint8_t GetIndex() const { return index_; }
2119
Calin Juravle10e244f2015-01-26 18:54:32 +00002120 bool CanBeNull() const OVERRIDE { return !is_this_; }
2121
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002122 DECLARE_INSTRUCTION(ParameterValue);
2123
2124 private:
2125 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00002126 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002127 const uint8_t index_;
2128
Calin Juravle10e244f2015-01-26 18:54:32 +00002129 // Whether or not the parameter value corresponds to 'this' argument.
2130 const bool is_this_;
2131
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002132 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
2133};
2134
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002135class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002136 public:
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002137 explicit HNot(Primitive::Type result_type, HInstruction* input)
2138 : HUnaryOperation(result_type, input) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002139
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002140 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002141 virtual bool InstructionDataEquals(HInstruction* other) const {
2142 UNUSED(other);
2143 return true;
2144 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002145
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002146 virtual int32_t Evaluate(int32_t x) const OVERRIDE { return ~x; }
2147 virtual int64_t Evaluate(int64_t x) const OVERRIDE { return ~x; }
2148
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002149 DECLARE_INSTRUCTION(Not);
2150
2151 private:
2152 DISALLOW_COPY_AND_ASSIGN(HNot);
2153};
2154
Roland Levillaindff1f282014-11-05 14:15:05 +00002155class HTypeConversion : public HExpression<1> {
2156 public:
2157 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00002158 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
2159 : HExpression(result_type, SideEffects::None()), dex_pc_(dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002160 SetRawInputAt(0, input);
2161 DCHECK_NE(input->GetType(), result_type);
2162 }
2163
2164 HInstruction* GetInput() const { return InputAt(0); }
2165 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
2166 Primitive::Type GetResultType() const { return GetType(); }
2167
Roland Levillain624279f2014-12-04 11:54:28 +00002168 // Required by the x86 and ARM code generators when producing calls
2169 // to the runtime.
2170 uint32_t GetDexPc() const { return dex_pc_; }
2171
Roland Levillaindff1f282014-11-05 14:15:05 +00002172 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00002173 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00002174
2175 DECLARE_INSTRUCTION(TypeConversion);
2176
2177 private:
Roland Levillain624279f2014-12-04 11:54:28 +00002178 const uint32_t dex_pc_;
2179
Roland Levillaindff1f282014-11-05 14:15:05 +00002180 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
2181};
2182
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002183class HPhi : public HInstruction {
2184 public:
2185 HPhi(ArenaAllocator* arena, uint32_t reg_number, size_t number_of_inputs, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002186 : HInstruction(SideEffects::None()),
2187 inputs_(arena, number_of_inputs),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002188 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002189 type_(type),
Calin Juravle10e244f2015-01-26 18:54:32 +00002190 is_live_(false),
2191 can_be_null_(true) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002192 inputs_.SetSize(number_of_inputs);
2193 }
2194
Calin Juravle10e244f2015-01-26 18:54:32 +00002195 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
2196 HInstruction* InputAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002197
Calin Juravle10e244f2015-01-26 18:54:32 +00002198 void SetRawInputAt(size_t index, HInstruction* input) OVERRIDE {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002199 inputs_.Put(index, input);
2200 }
2201
2202 void AddInput(HInstruction* input);
2203
Calin Juravle10e244f2015-01-26 18:54:32 +00002204 Primitive::Type GetType() const OVERRIDE { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002205 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002206
Calin Juravle10e244f2015-01-26 18:54:32 +00002207 bool CanBeNull() const OVERRIDE { return can_be_null_; }
2208 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
2209
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002210 uint32_t GetRegNumber() const { return reg_number_; }
2211
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002212 void SetDead() { is_live_ = false; }
2213 void SetLive() { is_live_ = true; }
2214 bool IsDead() const { return !is_live_; }
2215 bool IsLive() const { return is_live_; }
2216
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002217 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002218
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002219 private:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002220 GrowableArray<HInstruction*> inputs_;
2221 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002222 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002223 bool is_live_;
Calin Juravle10e244f2015-01-26 18:54:32 +00002224 bool can_be_null_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002225
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002226 DISALLOW_COPY_AND_ASSIGN(HPhi);
2227};
2228
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002229class HNullCheck : public HExpression<1> {
2230 public:
2231 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002232 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002233 SetRawInputAt(0, value);
2234 }
2235
Calin Juravle10e244f2015-01-26 18:54:32 +00002236 bool CanBeMoved() const OVERRIDE { return true; }
2237 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002238 UNUSED(other);
2239 return true;
2240 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002241
Calin Juravle10e244f2015-01-26 18:54:32 +00002242 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002243
Calin Juravle10e244f2015-01-26 18:54:32 +00002244 bool CanThrow() const OVERRIDE { return true; }
2245
2246 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01002247
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002248 uint32_t GetDexPc() const { return dex_pc_; }
2249
2250 DECLARE_INSTRUCTION(NullCheck);
2251
2252 private:
2253 const uint32_t dex_pc_;
2254
2255 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
2256};
2257
2258class FieldInfo : public ValueObject {
2259 public:
Calin Juravle52c48962014-12-16 17:02:57 +00002260 FieldInfo(MemberOffset field_offset, Primitive::Type field_type, bool is_volatile)
2261 : field_offset_(field_offset), field_type_(field_type), is_volatile_(is_volatile) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002262
2263 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002264 Primitive::Type GetFieldType() const { return field_type_; }
Calin Juravle52c48962014-12-16 17:02:57 +00002265 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002266
2267 private:
2268 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01002269 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00002270 const bool is_volatile_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002271};
2272
2273class HInstanceFieldGet : public HExpression<1> {
2274 public:
2275 HInstanceFieldGet(HInstruction* value,
2276 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00002277 MemberOffset field_offset,
2278 bool is_volatile)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002279 : HExpression(field_type, SideEffects::DependsOnSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00002280 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002281 SetRawInputAt(0, value);
2282 }
2283
Calin Juravle10c9cbe2014-12-19 10:50:19 +00002284 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002285
2286 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2287 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
2288 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002289 }
2290
Calin Juravle77520bc2015-01-12 18:45:46 +00002291 bool CanDoImplicitNullCheck() const OVERRIDE {
2292 return GetFieldOffset().Uint32Value() < kPageSize;
2293 }
2294
2295 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002296 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
2297 }
2298
Calin Juravle52c48962014-12-16 17:02:57 +00002299 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002300 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002301 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002302 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002303
2304 DECLARE_INSTRUCTION(InstanceFieldGet);
2305
2306 private:
2307 const FieldInfo field_info_;
2308
2309 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
2310};
2311
2312class HInstanceFieldSet : public HTemplateInstruction<2> {
2313 public:
2314 HInstanceFieldSet(HInstruction* object,
2315 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01002316 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00002317 MemberOffset field_offset,
2318 bool is_volatile)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002319 : HTemplateInstruction(SideEffects::ChangesSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00002320 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002321 SetRawInputAt(0, object);
2322 SetRawInputAt(1, value);
2323 }
2324
Calin Juravle77520bc2015-01-12 18:45:46 +00002325 bool CanDoImplicitNullCheck() const OVERRIDE {
2326 return GetFieldOffset().Uint32Value() < kPageSize;
2327 }
2328
Calin Juravle52c48962014-12-16 17:02:57 +00002329 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002330 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002331 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002332 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002333 HInstruction* GetValue() const { return InputAt(1); }
2334
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002335 DECLARE_INSTRUCTION(InstanceFieldSet);
2336
2337 private:
2338 const FieldInfo field_info_;
2339
2340 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
2341};
2342
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002343class HArrayGet : public HExpression<2> {
2344 public:
2345 HArrayGet(HInstruction* array, HInstruction* index, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002346 : HExpression(type, SideEffects::DependsOnSomething()) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002347 SetRawInputAt(0, array);
2348 SetRawInputAt(1, index);
2349 }
2350
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002351 bool CanBeMoved() const OVERRIDE { return true; }
2352 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002353 UNUSED(other);
2354 return true;
2355 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002356 bool CanDoImplicitNullCheck() const OVERRIDE {
2357 // TODO: We can be smarter here.
2358 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
2359 // which generates the implicit null check. There are cases when these can be removed
2360 // to produce better code. If we ever add optimizations to do so we should allow an
2361 // implicit check here (as long as the address falls in the first page).
2362 return false;
2363 }
2364
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002365 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002366
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002367 HInstruction* GetArray() const { return InputAt(0); }
2368 HInstruction* GetIndex() const { return InputAt(1); }
2369
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002370 DECLARE_INSTRUCTION(ArrayGet);
2371
2372 private:
2373 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
2374};
2375
2376class HArraySet : public HTemplateInstruction<3> {
2377 public:
2378 HArraySet(HInstruction* array,
2379 HInstruction* index,
2380 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002381 Primitive::Type expected_component_type,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002382 uint32_t dex_pc)
2383 : HTemplateInstruction(SideEffects::ChangesSomething()),
2384 dex_pc_(dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002385 expected_component_type_(expected_component_type),
2386 needs_type_check_(value->GetType() == Primitive::kPrimNot) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002387 SetRawInputAt(0, array);
2388 SetRawInputAt(1, index);
2389 SetRawInputAt(2, value);
2390 }
2391
Calin Juravle77520bc2015-01-12 18:45:46 +00002392 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002393 // We currently always call a runtime method to catch array store
2394 // exceptions.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002395 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002396 }
2397
Calin Juravle77520bc2015-01-12 18:45:46 +00002398 bool CanDoImplicitNullCheck() const OVERRIDE {
2399 // TODO: Same as for ArrayGet.
2400 return false;
2401 }
2402
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002403 void ClearNeedsTypeCheck() {
2404 needs_type_check_ = false;
2405 }
2406
2407 bool NeedsTypeCheck() const { return needs_type_check_; }
2408
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002409 uint32_t GetDexPc() const { return dex_pc_; }
2410
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002411 HInstruction* GetArray() const { return InputAt(0); }
2412 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002413 HInstruction* GetValue() const { return InputAt(2); }
2414
2415 Primitive::Type GetComponentType() const {
2416 // The Dex format does not type floating point index operations. Since the
2417 // `expected_component_type_` is set during building and can therefore not
2418 // be correct, we also check what is the value type. If it is a floating
2419 // point type, we must use that type.
2420 Primitive::Type value_type = GetValue()->GetType();
2421 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
2422 ? value_type
2423 : expected_component_type_;
2424 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002425
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002426 DECLARE_INSTRUCTION(ArraySet);
2427
2428 private:
2429 const uint32_t dex_pc_;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002430 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002431 bool needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002432
2433 DISALLOW_COPY_AND_ASSIGN(HArraySet);
2434};
2435
2436class HArrayLength : public HExpression<1> {
2437 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002438 explicit HArrayLength(HInstruction* array)
2439 : HExpression(Primitive::kPrimInt, SideEffects::None()) {
2440 // Note that arrays do not change length, so the instruction does not
2441 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002442 SetRawInputAt(0, array);
2443 }
2444
Calin Juravle77520bc2015-01-12 18:45:46 +00002445 bool CanBeMoved() const OVERRIDE { return true; }
2446 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002447 UNUSED(other);
2448 return true;
2449 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002450 bool CanDoImplicitNullCheck() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002451
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002452 DECLARE_INSTRUCTION(ArrayLength);
2453
2454 private:
2455 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
2456};
2457
2458class HBoundsCheck : public HExpression<2> {
2459 public:
2460 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002461 : HExpression(index->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002462 DCHECK(index->GetType() == Primitive::kPrimInt);
2463 SetRawInputAt(0, index);
2464 SetRawInputAt(1, length);
2465 }
2466
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002467 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002468 virtual bool InstructionDataEquals(HInstruction* other) const {
2469 UNUSED(other);
2470 return true;
2471 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002472
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002473 virtual bool NeedsEnvironment() const { return true; }
2474
Roland Levillaine161a2a2014-10-03 12:45:18 +01002475 virtual bool CanThrow() const { return true; }
2476
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002477 uint32_t GetDexPc() const { return dex_pc_; }
2478
2479 DECLARE_INSTRUCTION(BoundsCheck);
2480
2481 private:
2482 const uint32_t dex_pc_;
2483
2484 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
2485};
2486
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002487/**
2488 * Some DEX instructions are folded into multiple HInstructions that need
2489 * to stay live until the last HInstruction. This class
2490 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00002491 * HInstruction stays live. `index` represents the stack location index of the
2492 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002493 */
2494class HTemporary : public HTemplateInstruction<0> {
2495 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002496 explicit HTemporary(size_t index) : HTemplateInstruction(SideEffects::None()), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002497
2498 size_t GetIndex() const { return index_; }
2499
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00002500 Primitive::Type GetType() const OVERRIDE {
2501 // The previous instruction is the one that will be stored in the temporary location.
2502 DCHECK(GetPrevious() != nullptr);
2503 return GetPrevious()->GetType();
2504 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00002505
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002506 DECLARE_INSTRUCTION(Temporary);
2507
2508 private:
2509 const size_t index_;
2510
2511 DISALLOW_COPY_AND_ASSIGN(HTemporary);
2512};
2513
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002514class HSuspendCheck : public HTemplateInstruction<0> {
2515 public:
2516 explicit HSuspendCheck(uint32_t dex_pc)
Nicolas Geoffray9ebc72c2014-09-25 16:33:42 +01002517 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002518
2519 virtual bool NeedsEnvironment() const {
2520 return true;
2521 }
2522
2523 uint32_t GetDexPc() const { return dex_pc_; }
2524
2525 DECLARE_INSTRUCTION(SuspendCheck);
2526
2527 private:
2528 const uint32_t dex_pc_;
2529
2530 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
2531};
2532
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002533/**
2534 * Instruction to load a Class object.
2535 */
2536class HLoadClass : public HExpression<0> {
2537 public:
2538 HLoadClass(uint16_t type_index,
2539 bool is_referrers_class,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002540 uint32_t dex_pc)
2541 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2542 type_index_(type_index),
2543 is_referrers_class_(is_referrers_class),
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002544 dex_pc_(dex_pc),
2545 generate_clinit_check_(false) {}
2546
2547 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002548
2549 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2550 return other->AsLoadClass()->type_index_ == type_index_;
2551 }
2552
2553 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
2554
2555 uint32_t GetDexPc() const { return dex_pc_; }
2556 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002557 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002558
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002559 bool NeedsEnvironment() const OVERRIDE {
2560 // Will call runtime and load the class if the class is not loaded yet.
2561 // TODO: finer grain decision.
2562 return !is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002563 }
2564
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002565 bool MustGenerateClinitCheck() const {
2566 return generate_clinit_check_;
2567 }
2568
2569 void SetMustGenerateClinitCheck() {
2570 generate_clinit_check_ = true;
2571 }
2572
2573 bool CanCallRuntime() const {
2574 return MustGenerateClinitCheck() || !is_referrers_class_;
2575 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002576
Nicolas Geoffray82091da2015-01-26 10:02:45 +00002577 bool CanThrow() const OVERRIDE {
2578 // May call runtime and and therefore can throw.
2579 // TODO: finer grain decision.
2580 return !is_referrers_class_;
2581 }
2582
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002583 DECLARE_INSTRUCTION(LoadClass);
2584
2585 private:
2586 const uint16_t type_index_;
2587 const bool is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002588 const uint32_t dex_pc_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002589 // Whether this instruction must generate the initialization check.
2590 // Used for code generation.
2591 bool generate_clinit_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002592
2593 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
2594};
2595
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002596class HLoadString : public HExpression<0> {
2597 public:
2598 HLoadString(uint32_t string_index, uint32_t dex_pc)
2599 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2600 string_index_(string_index),
2601 dex_pc_(dex_pc) {}
2602
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002603 bool CanBeMoved() const OVERRIDE { return true; }
2604
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002605 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2606 return other->AsLoadString()->string_index_ == string_index_;
2607 }
2608
2609 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
2610
2611 uint32_t GetDexPc() const { return dex_pc_; }
2612 uint32_t GetStringIndex() const { return string_index_; }
2613
2614 // TODO: Can we deopt or debug when we resolve a string?
2615 bool NeedsEnvironment() const OVERRIDE { return false; }
2616
2617 DECLARE_INSTRUCTION(LoadString);
2618
2619 private:
2620 const uint32_t string_index_;
2621 const uint32_t dex_pc_;
2622
2623 DISALLOW_COPY_AND_ASSIGN(HLoadString);
2624};
2625
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002626// TODO: Pass this check to HInvokeStaticOrDirect nodes.
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002627/**
2628 * Performs an initialization check on its Class object input.
2629 */
2630class HClinitCheck : public HExpression<1> {
2631 public:
2632 explicit HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
2633 : HExpression(Primitive::kPrimNot, SideEffects::All()),
2634 dex_pc_(dex_pc) {
2635 SetRawInputAt(0, constant);
2636 }
2637
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002638 bool CanBeMoved() const OVERRIDE { return true; }
2639 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2640 UNUSED(other);
2641 return true;
2642 }
2643
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002644 bool NeedsEnvironment() const OVERRIDE {
2645 // May call runtime to initialize the class.
2646 return true;
2647 }
2648
2649 uint32_t GetDexPc() const { return dex_pc_; }
2650
2651 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
2652
2653 DECLARE_INSTRUCTION(ClinitCheck);
2654
2655 private:
2656 const uint32_t dex_pc_;
2657
2658 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
2659};
2660
2661class HStaticFieldGet : public HExpression<1> {
2662 public:
2663 HStaticFieldGet(HInstruction* cls,
2664 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00002665 MemberOffset field_offset,
2666 bool is_volatile)
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002667 : HExpression(field_type, SideEffects::DependsOnSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00002668 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002669 SetRawInputAt(0, cls);
2670 }
2671
Calin Juravle52c48962014-12-16 17:02:57 +00002672
Calin Juravle10c9cbe2014-12-19 10:50:19 +00002673 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002674
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002675 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00002676 HStaticFieldGet* other_get = other->AsStaticFieldGet();
2677 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002678 }
2679
2680 size_t ComputeHashCode() const OVERRIDE {
2681 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
2682 }
2683
Calin Juravle52c48962014-12-16 17:02:57 +00002684 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002685 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
2686 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002687 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002688
2689 DECLARE_INSTRUCTION(StaticFieldGet);
2690
2691 private:
2692 const FieldInfo field_info_;
2693
2694 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
2695};
2696
2697class HStaticFieldSet : public HTemplateInstruction<2> {
2698 public:
2699 HStaticFieldSet(HInstruction* cls,
2700 HInstruction* value,
2701 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00002702 MemberOffset field_offset,
2703 bool is_volatile)
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002704 : HTemplateInstruction(SideEffects::ChangesSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00002705 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002706 SetRawInputAt(0, cls);
2707 SetRawInputAt(1, value);
2708 }
2709
Calin Juravle52c48962014-12-16 17:02:57 +00002710 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002711 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
2712 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002713 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002714
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002715 HInstruction* GetValue() const { return InputAt(1); }
2716
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002717 DECLARE_INSTRUCTION(StaticFieldSet);
2718
2719 private:
2720 const FieldInfo field_info_;
2721
2722 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
2723};
2724
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002725// Implement the move-exception DEX instruction.
2726class HLoadException : public HExpression<0> {
2727 public:
2728 HLoadException() : HExpression(Primitive::kPrimNot, SideEffects::None()) {}
2729
2730 DECLARE_INSTRUCTION(LoadException);
2731
2732 private:
2733 DISALLOW_COPY_AND_ASSIGN(HLoadException);
2734};
2735
2736class HThrow : public HTemplateInstruction<1> {
2737 public:
2738 HThrow(HInstruction* exception, uint32_t dex_pc)
2739 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {
2740 SetRawInputAt(0, exception);
2741 }
2742
2743 bool IsControlFlow() const OVERRIDE { return true; }
2744
2745 bool NeedsEnvironment() const OVERRIDE { return true; }
2746
Nicolas Geoffray82091da2015-01-26 10:02:45 +00002747 bool CanThrow() const OVERRIDE { return true; }
2748
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002749 uint32_t GetDexPc() const { return dex_pc_; }
2750
2751 DECLARE_INSTRUCTION(Throw);
2752
2753 private:
Nicolas Geoffray82091da2015-01-26 10:02:45 +00002754 const uint32_t dex_pc_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002755
2756 DISALLOW_COPY_AND_ASSIGN(HThrow);
2757};
2758
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002759class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002760 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002761 HInstanceOf(HInstruction* object,
2762 HLoadClass* constant,
2763 bool class_is_final,
2764 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002765 : HExpression(Primitive::kPrimBoolean, SideEffects::None()),
2766 class_is_final_(class_is_final),
2767 dex_pc_(dex_pc) {
2768 SetRawInputAt(0, object);
2769 SetRawInputAt(1, constant);
2770 }
2771
2772 bool CanBeMoved() const OVERRIDE { return true; }
2773
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002774 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002775 return true;
2776 }
2777
2778 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002779 return false;
2780 }
2781
2782 uint32_t GetDexPc() const { return dex_pc_; }
2783
2784 bool IsClassFinal() const { return class_is_final_; }
2785
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002786 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002787
2788 private:
2789 const bool class_is_final_;
2790 const uint32_t dex_pc_;
2791
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002792 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
2793};
2794
2795class HCheckCast : public HTemplateInstruction<2> {
2796 public:
2797 HCheckCast(HInstruction* object,
2798 HLoadClass* constant,
2799 bool class_is_final,
2800 uint32_t dex_pc)
2801 : HTemplateInstruction(SideEffects::None()),
2802 class_is_final_(class_is_final),
2803 dex_pc_(dex_pc) {
2804 SetRawInputAt(0, object);
2805 SetRawInputAt(1, constant);
2806 }
2807
2808 bool CanBeMoved() const OVERRIDE { return true; }
2809
2810 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2811 return true;
2812 }
2813
2814 bool NeedsEnvironment() const OVERRIDE {
2815 // Instruction may throw a CheckCastError.
2816 return true;
2817 }
2818
2819 bool CanThrow() const OVERRIDE { return true; }
2820
2821 uint32_t GetDexPc() const { return dex_pc_; }
2822
2823 bool IsClassFinal() const { return class_is_final_; }
2824
2825 DECLARE_INSTRUCTION(CheckCast);
2826
2827 private:
2828 const bool class_is_final_;
2829 const uint32_t dex_pc_;
2830
2831 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002832};
2833
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00002834class HMonitorOperation : public HTemplateInstruction<1> {
2835 public:
2836 enum OperationKind {
2837 kEnter,
2838 kExit,
2839 };
2840
2841 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
2842 : HTemplateInstruction(SideEffects::None()), kind_(kind), dex_pc_(dex_pc) {
2843 SetRawInputAt(0, object);
2844 }
2845
2846 // Instruction may throw a Java exception, so we need an environment.
2847 bool NeedsEnvironment() const OVERRIDE { return true; }
2848 bool CanThrow() const OVERRIDE { return true; }
2849
2850 uint32_t GetDexPc() const { return dex_pc_; }
2851
2852 bool IsEnter() const { return kind_ == kEnter; }
2853
2854 DECLARE_INSTRUCTION(MonitorOperation);
2855
Calin Juravle52c48962014-12-16 17:02:57 +00002856 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00002857 const OperationKind kind_;
2858 const uint32_t dex_pc_;
2859
2860 private:
2861 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
2862};
2863
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002864class MoveOperands : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002865 public:
Nicolas Geoffray740475d2014-09-29 10:33:25 +01002866 MoveOperands(Location source, Location destination, HInstruction* instruction)
2867 : source_(source), destination_(destination), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002868
2869 Location GetSource() const { return source_; }
2870 Location GetDestination() const { return destination_; }
2871
2872 void SetSource(Location value) { source_ = value; }
2873 void SetDestination(Location value) { destination_ = value; }
2874
2875 // The parallel move resolver marks moves as "in-progress" by clearing the
2876 // destination (but not the source).
2877 Location MarkPending() {
2878 DCHECK(!IsPending());
2879 Location dest = destination_;
2880 destination_ = Location::NoLocation();
2881 return dest;
2882 }
2883
2884 void ClearPending(Location dest) {
2885 DCHECK(IsPending());
2886 destination_ = dest;
2887 }
2888
2889 bool IsPending() const {
2890 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
2891 return destination_.IsInvalid() && !source_.IsInvalid();
2892 }
2893
2894 // True if this blocks a move from the given location.
2895 bool Blocks(Location loc) const {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00002896 return !IsEliminated() && source_.Equals(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002897 }
2898
2899 // A move is redundant if it's been eliminated, if its source and
2900 // destination are the same, or if its destination is unneeded.
2901 bool IsRedundant() const {
2902 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
2903 }
2904
2905 // We clear both operands to indicate move that's been eliminated.
2906 void Eliminate() {
2907 source_ = destination_ = Location::NoLocation();
2908 }
2909
2910 bool IsEliminated() const {
2911 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
2912 return source_.IsInvalid();
2913 }
2914
Nicolas Geoffray740475d2014-09-29 10:33:25 +01002915 HInstruction* GetInstruction() const { return instruction_; }
2916
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002917 private:
2918 Location source_;
2919 Location destination_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01002920 // The instruction this move is assocatied with. Null when this move is
2921 // for moving an input in the expected locations of user (including a phi user).
2922 // This is only used in debug mode, to ensure we do not connect interval siblings
2923 // in the same parallel move.
2924 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002925};
2926
2927static constexpr size_t kDefaultNumberOfMoves = 4;
2928
2929class HParallelMove : public HTemplateInstruction<0> {
2930 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002931 explicit HParallelMove(ArenaAllocator* arena)
2932 : HTemplateInstruction(SideEffects::None()), moves_(arena, kDefaultNumberOfMoves) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002933
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00002934 void AddMove(Location source, Location destination, HInstruction* instruction) {
2935 DCHECK(source.IsValid());
2936 DCHECK(destination.IsValid());
2937 // The parallel move resolver does not handle pairs. So we decompose the
2938 // pair locations into two moves.
2939 if (source.IsPair() && destination.IsPair()) {
2940 AddMove(source.ToLow(), destination.ToLow(), instruction);
2941 AddMove(source.ToHigh(), destination.ToHigh(), nullptr);
2942 } else if (source.IsPair()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002943 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00002944 AddMove(source.ToLow(), Location::StackSlot(destination.GetStackIndex()), instruction);
2945 AddMove(source.ToHigh(), Location::StackSlot(destination.GetHighStackIndex(4)), nullptr);
2946 } else if (destination.IsPair()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002947 if (source.IsConstant()) {
2948 // We put the same constant in the move. The code generator will handle which
2949 // low or high part to use.
2950 AddMove(source, destination.ToLow(), instruction);
2951 AddMove(source, destination.ToHigh(), nullptr);
2952 } else {
2953 DCHECK(source.IsDoubleStackSlot());
2954 AddMove(Location::StackSlot(source.GetStackIndex()), destination.ToLow(), instruction);
2955 // TODO: rewrite GetHighStackIndex to not require a word size. It's supposed to
2956 // always be 4.
2957 static constexpr int kHighOffset = 4;
2958 AddMove(Location::StackSlot(source.GetHighStackIndex(kHighOffset)),
2959 destination.ToHigh(),
2960 nullptr);
2961 }
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00002962 } else {
2963 if (kIsDebugBuild) {
2964 if (instruction != nullptr) {
2965 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
2966 DCHECK_NE(moves_.Get(i).GetInstruction(), instruction)
2967 << "Doing parallel moves for the same instruction.";
2968 }
2969 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00002970 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00002971 DCHECK(!destination.Equals(moves_.Get(i).GetDestination()))
2972 << "Same destination for two moves in a parallel move.";
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00002973 }
2974 }
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00002975 moves_.Add(MoveOperands(source, destination, instruction));
Nicolas Geoffray740475d2014-09-29 10:33:25 +01002976 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002977 }
2978
2979 MoveOperands* MoveOperandsAt(size_t index) const {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00002980 return moves_.GetRawStorage() + index;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002981 }
2982
2983 size_t NumMoves() const { return moves_.Size(); }
2984
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002985 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002986
2987 private:
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00002988 GrowableArray<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002989
2990 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
2991};
2992
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002993class HGraphVisitor : public ValueObject {
2994 public:
Dave Allison20dfc792014-06-16 20:44:29 -07002995 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
2996 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002997
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002998 virtual void VisitInstruction(HInstruction* instruction) { UNUSED(instruction); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002999 virtual void VisitBasicBlock(HBasicBlock* block);
3000
Roland Levillain633021e2014-10-01 14:12:25 +01003001 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003002 void VisitInsertionOrder();
3003
Roland Levillain633021e2014-10-01 14:12:25 +01003004 // Visit the graph following dominator tree reverse post-order.
3005 void VisitReversePostOrder();
3006
Nicolas Geoffray787c3072014-03-17 10:20:19 +00003007 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00003008
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003009 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003010#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003011 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
3012
3013 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
3014
3015#undef DECLARE_VISIT_INSTRUCTION
3016
3017 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07003018 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003019
3020 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
3021};
3022
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003023class HGraphDelegateVisitor : public HGraphVisitor {
3024 public:
3025 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
3026 virtual ~HGraphDelegateVisitor() {}
3027
3028 // Visit functions that delegate to to super class.
3029#define DECLARE_VISIT_INSTRUCTION(name, super) \
3030 virtual void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
3031
3032 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
3033
3034#undef DECLARE_VISIT_INSTRUCTION
3035
3036 private:
3037 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
3038};
3039
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003040class HInsertionOrderIterator : public ValueObject {
3041 public:
3042 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
3043
3044 bool Done() const { return index_ == graph_.GetBlocks().Size(); }
3045 HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); }
3046 void Advance() { ++index_; }
3047
3048 private:
3049 const HGraph& graph_;
3050 size_t index_;
3051
3052 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
3053};
3054
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003055class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003056 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003057 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003058
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003059 bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); }
3060 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003061 void Advance() { ++index_; }
3062
3063 private:
3064 const HGraph& graph_;
3065 size_t index_;
3066
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003067 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003068};
3069
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003070class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003071 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003072 explicit HPostOrderIterator(const HGraph& graph)
3073 : graph_(graph), index_(graph_.GetReversePostOrder().Size()) {}
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003074
3075 bool Done() const { return index_ == 0; }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003076 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003077 void Advance() { --index_; }
3078
3079 private:
3080 const HGraph& graph_;
3081 size_t index_;
3082
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003083 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003084};
3085
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003086// Iterator over the blocks that art part of the loop. Includes blocks part
3087// of an inner loop. The order in which the blocks are iterated is on their
3088// block id.
3089class HBlocksInLoopIterator : public ValueObject {
3090 public:
3091 explicit HBlocksInLoopIterator(const HLoopInformation& info)
3092 : blocks_in_loop_(info.GetBlocks()),
3093 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
3094 index_(0) {
3095 if (!blocks_in_loop_.IsBitSet(index_)) {
3096 Advance();
3097 }
3098 }
3099
3100 bool Done() const { return index_ == blocks_.Size(); }
3101 HBasicBlock* Current() const { return blocks_.Get(index_); }
3102 void Advance() {
3103 ++index_;
3104 for (size_t e = blocks_.Size(); index_ < e; ++index_) {
3105 if (blocks_in_loop_.IsBitSet(index_)) {
3106 break;
3107 }
3108 }
3109 }
3110
3111 private:
3112 const BitVector& blocks_in_loop_;
3113 const GrowableArray<HBasicBlock*>& blocks_;
3114 size_t index_;
3115
3116 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
3117};
3118
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003119} // namespace art
3120
3121#endif // ART_COMPILER_OPTIMIZING_NODES_H_