blob: 9bb91d208bdc1de3b876ebe74450450f960663d7 [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 Geoffray276d9da2015-02-02 18:24:11 +000076 bool IsEmpty() const { return first_instruction_ == nullptr; }
77 void Clear() { first_instruction_ = last_instruction_ = nullptr; }
78
79 // Update the block of all instructions to be `block`.
80 void SetBlockOfInstructions(HBasicBlock* block) const;
81
82 void AddAfter(HInstruction* cursor, const HInstructionList& instruction_list);
83 void Add(const HInstructionList& instruction_list);
84
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010085 private:
86 HInstruction* first_instruction_;
87 HInstruction* last_instruction_;
88
89 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000090 friend class HGraph;
91 friend class HInstruction;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010092 friend class HInstructionIterator;
Nicolas Geoffray804d0932014-05-02 08:46:00 +010093 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010094
95 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
96};
97
Nicolas Geoffray818f2102014-02-18 16:43:35 +000098// Control-flow graph of a method. Contains a list of basic blocks.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070099class HGraph : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000100 public:
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000101 HGraph(ArenaAllocator* arena, int start_instruction_id = 0)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000102 : arena_(arena),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000103 blocks_(arena, kDefaultNumberOfBlocks),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100104 reverse_post_order_(arena, kDefaultNumberOfBlocks),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700105 entry_block_(nullptr),
106 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100107 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100108 number_of_vregs_(0),
109 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000110 temporaries_vreg_slots_(0),
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000111 current_instruction_id_(start_instruction_id) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000112
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000113 ArenaAllocator* GetArena() const { return arena_; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100114 const GrowableArray<HBasicBlock*>& GetBlocks() const { return blocks_; }
Roland Levillain93445682014-10-06 19:24:02 +0100115 HBasicBlock* GetBlock(size_t id) const { return blocks_.Get(id); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000116
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000117 HBasicBlock* GetEntryBlock() const { return entry_block_; }
118 HBasicBlock* GetExitBlock() const { return exit_block_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000119
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000120 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
121 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000122
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000123 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100124
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000125 // Try building the SSA form of this graph, with dominance computation and loop
126 // recognition. Returns whether it was successful in doing all these steps.
127 bool TryBuildingSsa() {
128 BuildDominatorTree();
129 TransformToSsa();
130 return AnalyzeNaturalLoops();
131 }
132
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000133 void BuildDominatorTree();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000134 void TransformToSsa();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100135 void SimplifyCFG();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000136
Nicolas Geoffrayf5370122014-12-02 11:51:19 +0000137 // Analyze all natural loops in this graph. Returns false if one
138 // loop is not natural, that is the header does not dominate the
139 // back edge.
140 bool AnalyzeNaturalLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100141
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000142 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
143 void InlineInto(HGraph* outer_graph, HInvoke* invoke);
144
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100145 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
146 void SimplifyLoop(HBasicBlock* header);
147
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000148 int32_t GetNextInstructionId() {
149 DCHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000150 return current_instruction_id_++;
151 }
152
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000153 int32_t GetCurrentInstructionId() const {
154 return current_instruction_id_;
155 }
156
157 void SetCurrentInstructionId(int32_t id) {
158 current_instruction_id_ = id;
159 }
160
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100161 uint16_t GetMaximumNumberOfOutVRegs() const {
162 return maximum_number_of_out_vregs_;
163 }
164
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000165 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
166 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100167 }
168
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000169 void UpdateTemporariesVRegSlots(size_t slots) {
170 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100171 }
172
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000173 size_t GetTemporariesVRegSlots() const {
174 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100175 }
176
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100177 void SetNumberOfVRegs(uint16_t number_of_vregs) {
178 number_of_vregs_ = number_of_vregs;
179 }
180
181 uint16_t GetNumberOfVRegs() const {
182 return number_of_vregs_;
183 }
184
185 void SetNumberOfInVRegs(uint16_t value) {
186 number_of_in_vregs_ = value;
187 }
188
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100189 uint16_t GetNumberOfLocalVRegs() const {
190 return number_of_vregs_ - number_of_in_vregs_;
191 }
192
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100193 const GrowableArray<HBasicBlock*>& GetReversePostOrder() const {
194 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100195 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100196
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000197 private:
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000198 HBasicBlock* FindCommonDominator(HBasicBlock* first, HBasicBlock* second) const;
199 void VisitBlockForDominatorTree(HBasicBlock* block,
200 HBasicBlock* predecessor,
201 GrowableArray<size_t>* visits);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100202 void FindBackEdges(ArenaBitVector* visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000203 void VisitBlockForBackEdges(HBasicBlock* block,
204 ArenaBitVector* visited,
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100205 ArenaBitVector* visiting);
Roland Levillainfc600dc2014-12-02 17:16:31 +0000206 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000207 void RemoveDeadBlocks(const ArenaBitVector& visited) const;
Jean Christophe Beyler53d9da82014-12-04 13:28:25 -0800208 void RemoveBlock(HBasicBlock* block) const;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000209
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000210 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000211
212 // List of blocks in insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000213 GrowableArray<HBasicBlock*> blocks_;
214
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100215 // List of blocks to perform a reverse post order tree traversal.
216 GrowableArray<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000217
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000218 HBasicBlock* entry_block_;
219 HBasicBlock* exit_block_;
220
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100221 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100222 uint16_t maximum_number_of_out_vregs_;
223
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100224 // The number of virtual registers in this method. Contains the parameters.
225 uint16_t number_of_vregs_;
226
227 // The number of virtual registers used by parameters of this method.
228 uint16_t number_of_in_vregs_;
229
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000230 // Number of vreg size slots that the temporaries use (used in baseline compiler).
231 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100232
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000233 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000234 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000235
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000236 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000237 DISALLOW_COPY_AND_ASSIGN(HGraph);
238};
239
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700240class HLoopInformation : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000241 public:
242 HLoopInformation(HBasicBlock* header, HGraph* graph)
243 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100244 suspend_check_(nullptr),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100245 back_edges_(graph->GetArena(), kDefaultNumberOfBackEdges),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100246 // Make bit vector growable, as the number of blocks may change.
247 blocks_(graph->GetArena(), graph->GetBlocks().Size(), true) {}
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100248
249 HBasicBlock* GetHeader() const {
250 return header_;
251 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000252
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000253 void SetHeader(HBasicBlock* block) {
254 header_ = block;
255 }
256
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100257 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
258 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
259 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
260
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000261 void AddBackEdge(HBasicBlock* back_edge) {
262 back_edges_.Add(back_edge);
263 }
264
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100265 void RemoveBackEdge(HBasicBlock* back_edge) {
266 back_edges_.Delete(back_edge);
267 }
268
269 bool IsBackEdge(HBasicBlock* block) {
270 for (size_t i = 0, e = back_edges_.Size(); i < e; ++i) {
271 if (back_edges_.Get(i) == block) return true;
272 }
273 return false;
274 }
275
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000276 size_t NumberOfBackEdges() const {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000277 return back_edges_.Size();
278 }
279
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100280 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100281
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100282 const GrowableArray<HBasicBlock*>& GetBackEdges() const {
283 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100284 }
285
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100286 void ClearBackEdges() {
287 back_edges_.Reset();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100288 }
289
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100290 // Find blocks that are part of this loop. Returns whether the loop is a natural loop,
291 // that is the header dominates the back edge.
292 bool Populate();
293
294 // Returns whether this loop information contains `block`.
295 // Note that this loop information *must* be populated before entering this function.
296 bool Contains(const HBasicBlock& block) const;
297
298 // Returns whether this loop information is an inner loop of `other`.
299 // Note that `other` *must* be populated before entering this function.
300 bool IsIn(const HLoopInformation& other) const;
301
302 const ArenaBitVector& GetBlocks() const { return blocks_; }
303
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000304 void Add(HBasicBlock* block);
305
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000306 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100307 // Internal recursive implementation of `Populate`.
308 void PopulateRecursive(HBasicBlock* block);
309
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000310 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100311 HSuspendCheck* suspend_check_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000312 GrowableArray<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100313 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000314
315 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
316};
317
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100318static constexpr size_t kNoLifetime = -1;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100319static constexpr uint32_t kNoDexPc = -1;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100320
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000321// A block in a method. Contains the list of instructions represented
322// as a double linked list. Each block knows its predecessors and
323// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100324
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700325class HBasicBlock : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000326 public:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100327 explicit HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000328 : graph_(graph),
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000329 predecessors_(graph->GetArena(), kDefaultNumberOfPredecessors),
330 successors_(graph->GetArena(), kDefaultNumberOfSuccessors),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000331 loop_information_(nullptr),
332 dominator_(nullptr),
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100333 dominated_blocks_(graph->GetArena(), kDefaultNumberOfDominatedBlocks),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100334 block_id_(-1),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100335 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100336 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000337 lifetime_end_(kNoLifetime),
338 is_catch_block_(false) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000339
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100340 const GrowableArray<HBasicBlock*>& GetPredecessors() const {
341 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000342 }
343
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100344 const GrowableArray<HBasicBlock*>& GetSuccessors() const {
345 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000346 }
347
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100348 const GrowableArray<HBasicBlock*>& GetDominatedBlocks() const {
349 return dominated_blocks_;
350 }
351
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100352 bool IsEntryBlock() const {
353 return graph_->GetEntryBlock() == this;
354 }
355
356 bool IsExitBlock() const {
357 return graph_->GetExitBlock() == this;
358 }
359
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000360 void AddBackEdge(HBasicBlock* back_edge) {
361 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000362 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000363 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100364 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000365 loop_information_->AddBackEdge(back_edge);
366 }
367
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000368 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000369 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000370
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000371 int GetBlockId() const { return block_id_; }
372 void SetBlockId(int id) { block_id_ = id; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000373
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000374 HBasicBlock* GetDominator() const { return dominator_; }
375 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100376 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.Add(block); }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000377 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
378 for (size_t i = 0, e = dominated_blocks_.Size(); i < e; ++i) {
379 if (dominated_blocks_.Get(i) == existing) {
380 dominated_blocks_.Put(i, new_block);
381 return;
382 }
383 }
384 LOG(FATAL) << "Unreachable";
385 UNREACHABLE();
386 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000387
388 int NumberOfBackEdges() const {
389 return loop_information_ == nullptr
390 ? 0
391 : loop_information_->NumberOfBackEdges();
392 }
393
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100394 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
395 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100396 const HInstructionList& GetInstructions() const { return instructions_; }
397 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100398 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000399
400 void AddSuccessor(HBasicBlock* block) {
401 successors_.Add(block);
402 block->predecessors_.Add(this);
403 }
404
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100405 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
406 size_t successor_index = GetSuccessorIndexOf(existing);
407 DCHECK_NE(successor_index, static_cast<size_t>(-1));
408 existing->RemovePredecessor(this);
409 new_block->predecessors_.Add(this);
410 successors_.Put(successor_index, new_block);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000411 }
412
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000413 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
414 size_t predecessor_index = GetPredecessorIndexOf(existing);
415 DCHECK_NE(predecessor_index, static_cast<size_t>(-1));
416 existing->RemoveSuccessor(this);
417 new_block->successors_.Add(this);
418 predecessors_.Put(predecessor_index, new_block);
419 }
420
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100421 void RemovePredecessor(HBasicBlock* block) {
422 predecessors_.Delete(block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100423 }
424
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000425 void RemoveSuccessor(HBasicBlock* block) {
426 successors_.Delete(block);
427 }
428
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100429 void ClearAllPredecessors() {
430 predecessors_.Reset();
431 }
432
433 void AddPredecessor(HBasicBlock* block) {
434 predecessors_.Add(block);
435 block->successors_.Add(this);
436 }
437
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100438 void SwapPredecessors() {
Nicolas Geoffrayc83d4412014-09-18 16:46:20 +0100439 DCHECK_EQ(predecessors_.Size(), 2u);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100440 HBasicBlock* temp = predecessors_.Get(0);
441 predecessors_.Put(0, predecessors_.Get(1));
442 predecessors_.Put(1, temp);
443 }
444
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100445 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) {
446 for (size_t i = 0, e = predecessors_.Size(); i < e; ++i) {
447 if (predecessors_.Get(i) == predecessor) {
448 return i;
449 }
450 }
451 return -1;
452 }
453
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100454 size_t GetSuccessorIndexOf(HBasicBlock* successor) {
455 for (size_t i = 0, e = successors_.Size(); i < e; ++i) {
456 if (successors_.Get(i) == successor) {
457 return i;
458 }
459 }
460 return -1;
461 }
462
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000463 // Split the block into two blocks just after `cursor`. Returns the newly
464 // created block. Note that this method just updates raw block information,
465 // like predecessors, successors, dominators, and instruction list. It does not
466 // update the graph, reverse post order, loop information, nor make sure the
467 // blocks are consistent (for example ending with a control flow instruction).
468 HBasicBlock* SplitAfter(HInstruction* cursor);
469
470 // Merge `other` at the end of `this`. Successors and dominated blocks of
471 // `other` are changed to be successors and dominated blocks of `this`. Note
472 // that this method does not update the graph, reverse post order, loop
473 // information, nor make sure the blocks are consistent (for example ending
474 // with a control flow instruction).
475 void MergeWith(HBasicBlock* other);
476
477 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
478 // of `this` are moved to `other`.
479 // Note that this method does not update the graph, reverse post order, loop
480 // information, nor make sure the blocks are consistent (for example ending
481 void ReplaceWith(HBasicBlock* other);
482
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000483 void AddInstruction(HInstruction* instruction);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100484 void RemoveInstruction(HInstruction* instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100485 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +0100486 // Replace instruction `initial` with `replacement` within this block.
487 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
488 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100489 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100490 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100491 void RemovePhi(HPhi* phi);
492
493 bool IsLoopHeader() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100494 return (loop_information_ != nullptr) && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100495 }
496
Roland Levillain6b879dd2014-09-22 17:13:44 +0100497 bool IsLoopPreHeaderFirstPredecessor() const {
498 DCHECK(IsLoopHeader());
499 DCHECK(!GetPredecessors().IsEmpty());
500 return GetPredecessors().Get(0) == GetLoopInformation()->GetPreHeader();
501 }
502
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100503 HLoopInformation* GetLoopInformation() const {
504 return loop_information_;
505 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000506
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000507 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100508 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000509 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100510 void SetInLoop(HLoopInformation* info) {
511 if (IsLoopHeader()) {
512 // Nothing to do. This just means `info` is an outer loop.
513 } else if (loop_information_ == nullptr) {
514 loop_information_ = info;
515 } else if (loop_information_->Contains(*info->GetHeader())) {
516 // Block is currently part of an outer loop. Make it part of this inner loop.
517 // Note that a non loop header having a loop information means this loop information
518 // has already been populated
519 loop_information_ = info;
520 } else {
521 // Block is part of an inner loop. Do not update the loop information.
522 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
523 // at this point, because this method is being called while populating `info`.
524 }
525 }
526
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000527 // Raw update of the loop information.
528 void SetLoopInformation(HLoopInformation* info) {
529 loop_information_ = info;
530 }
531
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100532 bool IsInLoop() const { return loop_information_ != nullptr; }
533
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100534 // Returns wheter this block dominates the blocked passed as parameter.
535 bool Dominates(HBasicBlock* block) const;
536
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100537 size_t GetLifetimeStart() const { return lifetime_start_; }
538 size_t GetLifetimeEnd() const { return lifetime_end_; }
539
540 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
541 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
542
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100543 uint32_t GetDexPc() const { return dex_pc_; }
544
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000545 bool IsCatchBlock() const { return is_catch_block_; }
546 void SetIsCatchBlock() { is_catch_block_ = true; }
547
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000548 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000549 HGraph* graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000550 GrowableArray<HBasicBlock*> predecessors_;
551 GrowableArray<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100552 HInstructionList instructions_;
553 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000554 HLoopInformation* loop_information_;
555 HBasicBlock* dominator_;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100556 GrowableArray<HBasicBlock*> dominated_blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000557 int block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100558 // The dex program counter of the first instruction of this block.
559 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100560 size_t lifetime_start_;
561 size_t lifetime_end_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000562 bool is_catch_block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000563
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000564 friend class HGraph;
565 friend class HInstruction;
566
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000567 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
568};
569
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100570#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
571 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000572 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000573 M(ArrayGet, Instruction) \
574 M(ArrayLength, Instruction) \
575 M(ArraySet, Instruction) \
576 M(BoundsCheck, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000577 M(CheckCast, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100578 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000579 M(Compare, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100580 M(Condition, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000581 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +0000582 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000583 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100584 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000585 M(Exit, Instruction) \
586 M(FloatConstant, Constant) \
587 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100588 M(GreaterThan, Condition) \
589 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100590 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000591 M(InstanceFieldGet, Instruction) \
592 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000593 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100594 M(IntConstant, Constant) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +0000595 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000596 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100597 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000598 M(LessThan, Condition) \
599 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000600 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000601 M(LoadException, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100602 M(LoadLocal, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000603 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100604 M(Local, Instruction) \
605 M(LongConstant, Constant) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +0000606 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000607 M(Mul, BinaryOperation) \
608 M(Neg, UnaryOperation) \
609 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100610 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +0100611 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000612 M(NotEqual, Condition) \
613 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000614 M(Or, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100615 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000616 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100617 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000618 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100619 M(Return, Instruction) \
620 M(ReturnVoid, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000621 M(Shl, BinaryOperation) \
622 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100623 M(StaticFieldGet, Instruction) \
624 M(StaticFieldSet, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100625 M(StoreLocal, Instruction) \
626 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100627 M(SuspendCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000628 M(Temporary, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000629 M(Throw, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +0000630 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000631 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000632 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000633
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100634#define FOR_EACH_INSTRUCTION(M) \
635 FOR_EACH_CONCRETE_INSTRUCTION(M) \
636 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +0100637 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +0100638 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100639 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -0700640
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100641#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000642FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
643#undef FORWARD_DECLARATION
644
Roland Levillainccc07a92014-09-16 14:48:16 +0100645#define DECLARE_INSTRUCTION(type) \
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100646 virtual InstructionKind GetKind() const { return k##type; } \
Roland Levillainccc07a92014-09-16 14:48:16 +0100647 virtual const char* DebugName() const { return #type; } \
648 virtual const H##type* As##type() const OVERRIDE { return this; } \
649 virtual H##type* As##type() OVERRIDE { return this; } \
650 virtual bool InstructionTypeEquals(HInstruction* other) const { \
651 return other->Is##type(); \
652 } \
653 virtual void Accept(HGraphVisitor* visitor)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000654
David Brazdiled596192015-01-23 10:39:45 +0000655template <typename T> class HUseList;
656
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100657template <typename T>
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700658class HUseListNode : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000659 public:
David Brazdiled596192015-01-23 10:39:45 +0000660 HUseListNode* GetPrevious() const { return prev_; }
661 HUseListNode* GetNext() const { return next_; }
662 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100663 size_t GetIndex() const { return index_; }
664
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000665 private:
David Brazdiled596192015-01-23 10:39:45 +0000666 HUseListNode(T user, size_t index)
667 : user_(user), index_(index), prev_(nullptr), next_(nullptr) {}
668
669 T const user_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100670 const size_t index_;
David Brazdiled596192015-01-23 10:39:45 +0000671 HUseListNode<T>* prev_;
672 HUseListNode<T>* next_;
673
674 friend class HUseList<T>;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000675
676 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
677};
678
David Brazdiled596192015-01-23 10:39:45 +0000679template <typename T>
680class HUseList : public ValueObject {
681 public:
682 HUseList() : first_(nullptr) {}
683
684 void Clear() {
685 first_ = nullptr;
686 }
687
688 // Adds a new entry at the beginning of the use list and returns
689 // the newly created node.
690 HUseListNode<T>* AddUse(T user, size_t index, ArenaAllocator* arena) {
David Brazdilea55b932015-01-27 17:12:29 +0000691 HUseListNode<T>* new_node = new (arena) HUseListNode<T>(user, index);
David Brazdiled596192015-01-23 10:39:45 +0000692 if (IsEmpty()) {
693 first_ = new_node;
694 } else {
695 first_->prev_ = new_node;
696 new_node->next_ = first_;
697 first_ = new_node;
698 }
699 return new_node;
700 }
701
702 HUseListNode<T>* GetFirst() const {
703 return first_;
704 }
705
706 void Remove(HUseListNode<T>* node) {
707 if (node->prev_ != nullptr) {
708 node->prev_->next_ = node->next_;
709 }
710 if (node->next_ != nullptr) {
711 node->next_->prev_ = node->prev_;
712 }
713 if (node == first_) {
714 first_ = node->next_;
715 }
716 }
717
718 bool IsEmpty() const {
719 return first_ == nullptr;
720 }
721
722 bool HasOnlyOneUse() const {
723 return first_ != nullptr && first_->next_ == nullptr;
724 }
725
726 private:
727 HUseListNode<T>* first_;
728};
729
730template<typename T>
731class HUseIterator : public ValueObject {
732 public:
733 explicit HUseIterator(const HUseList<T>& uses) : current_(uses.GetFirst()) {}
734
735 bool Done() const { return current_ == nullptr; }
736
737 void Advance() {
738 DCHECK(!Done());
739 current_ = current_->GetNext();
740 }
741
742 HUseListNode<T>* Current() const {
743 DCHECK(!Done());
744 return current_;
745 }
746
747 private:
748 HUseListNode<T>* current_;
749
750 friend class HValue;
751};
752
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100753// Represents the side effects an instruction may have.
754class SideEffects : public ValueObject {
755 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100756 SideEffects() : flags_(0) {}
757
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100758 static SideEffects None() {
759 return SideEffects(0);
760 }
761
762 static SideEffects All() {
763 return SideEffects(ChangesSomething().flags_ | DependsOnSomething().flags_);
764 }
765
766 static SideEffects ChangesSomething() {
767 return SideEffects((1 << kFlagChangesCount) - 1);
768 }
769
770 static SideEffects DependsOnSomething() {
771 int count = kFlagDependsOnCount - kFlagChangesCount;
772 return SideEffects(((1 << count) - 1) << kFlagChangesCount);
773 }
774
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100775 SideEffects Union(SideEffects other) const {
776 return SideEffects(flags_ | other.flags_);
777 }
778
Roland Levillain72bceff2014-09-15 18:29:00 +0100779 bool HasSideEffects() const {
780 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
781 return (flags_ & all_bits_set) != 0;
782 }
783
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100784 bool HasAllSideEffects() const {
785 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
786 return all_bits_set == (flags_ & all_bits_set);
787 }
788
789 bool DependsOn(SideEffects other) const {
790 size_t depends_flags = other.ComputeDependsFlags();
791 return (flags_ & depends_flags) != 0;
792 }
793
794 bool HasDependencies() const {
795 int count = kFlagDependsOnCount - kFlagChangesCount;
796 size_t all_bits_set = (1 << count) - 1;
797 return ((flags_ >> kFlagChangesCount) & all_bits_set) != 0;
798 }
799
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100800 private:
801 static constexpr int kFlagChangesSomething = 0;
802 static constexpr int kFlagChangesCount = kFlagChangesSomething + 1;
803
804 static constexpr int kFlagDependsOnSomething = kFlagChangesCount;
805 static constexpr int kFlagDependsOnCount = kFlagDependsOnSomething + 1;
806
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100807 explicit SideEffects(size_t flags) : flags_(flags) {}
808
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100809 size_t ComputeDependsFlags() const {
810 return flags_ << kFlagChangesCount;
811 }
812
813 size_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100814};
815
David Brazdiled596192015-01-23 10:39:45 +0000816// A HEnvironment object contains the values of virtual registers at a given location.
817class HEnvironment : public ArenaObject<kArenaAllocMisc> {
818 public:
819 HEnvironment(ArenaAllocator* arena, size_t number_of_vregs)
820 : vregs_(arena, number_of_vregs) {
821 vregs_.SetSize(number_of_vregs);
822 for (size_t i = 0; i < number_of_vregs; i++) {
823 vregs_.Put(i, VRegInfo(nullptr, nullptr));
824 }
825 }
826
827 void CopyFrom(HEnvironment* env);
828
829 void SetRawEnvAt(size_t index, HInstruction* instruction) {
830 vregs_.Put(index, VRegInfo(instruction, nullptr));
831 }
832
833 // Record instructions' use entries of this environment for constant-time removal.
834 void RecordEnvUse(HUseListNode<HEnvironment*>* env_use) {
835 DCHECK(env_use->GetUser() == this);
836 size_t index = env_use->GetIndex();
837 VRegInfo info = vregs_.Get(index);
838 DCHECK(info.vreg_ != nullptr);
839 DCHECK(info.node_ == nullptr);
840 vregs_.Put(index, VRegInfo(info.vreg_, env_use));
841 }
842
843 HInstruction* GetInstructionAt(size_t index) const {
844 return vregs_.Get(index).vreg_;
845 }
846
847 HUseListNode<HEnvironment*>* GetInstructionEnvUseAt(size_t index) const {
848 return vregs_.Get(index).node_;
849 }
850
851 size_t Size() const { return vregs_.Size(); }
852
853 private:
854 struct VRegInfo {
855 HInstruction* vreg_;
856 HUseListNode<HEnvironment*>* node_;
857
858 VRegInfo(HInstruction* instruction, HUseListNode<HEnvironment*>* env_use)
859 : vreg_(instruction), node_(env_use) {}
860 };
861
862 GrowableArray<VRegInfo> vregs_;
863
864 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
865};
866
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700867class HInstruction : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000868 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100869 explicit HInstruction(SideEffects side_effects)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000870 : previous_(nullptr),
871 next_(nullptr),
872 block_(nullptr),
873 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100874 ssa_index_(-1),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100875 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100876 locations_(nullptr),
877 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100878 lifetime_position_(kNoLifetime),
879 side_effects_(side_effects) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000880
Dave Allison20dfc792014-06-16 20:44:29 -0700881 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000882
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100883#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100884 enum InstructionKind {
885 FOR_EACH_INSTRUCTION(DECLARE_KIND)
886 };
887#undef DECLARE_KIND
888
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000889 HInstruction* GetNext() const { return next_; }
890 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000891
Calin Juravle77520bc2015-01-12 18:45:46 +0000892 HInstruction* GetNextDisregardingMoves() const;
893 HInstruction* GetPreviousDisregardingMoves() const;
894
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000895 HBasicBlock* GetBlock() const { return block_; }
896 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100897 bool IsInBlock() const { return block_ != nullptr; }
898 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +0100899 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000900
Roland Levillain6b879dd2014-09-22 17:13:44 +0100901 virtual size_t InputCount() const = 0;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100902 virtual HInstruction* InputAt(size_t i) const = 0;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000903
904 virtual void Accept(HGraphVisitor* visitor) = 0;
905 virtual const char* DebugName() const = 0;
906
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100907 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100908 virtual void SetRawInputAt(size_t index, HInstruction* input) = 0;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100909
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100910 virtual bool NeedsEnvironment() const { return false; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100911 virtual bool IsControlFlow() const { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +0100912 virtual bool CanThrow() const { return false; }
Roland Levillain72bceff2014-09-15 18:29:00 +0100913 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100914
Calin Juravle10e244f2015-01-26 18:54:32 +0000915 // Does not apply for all instructions, but having this at top level greatly
916 // simplifies the null check elimination.
917 virtual bool CanBeNull() const { return true; }
918
Calin Juravle77520bc2015-01-12 18:45:46 +0000919 virtual bool CanDoImplicitNullCheck() const { return false; }
920
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100921 void AddUseAt(HInstruction* user, size_t index) {
David Brazdiled596192015-01-23 10:39:45 +0000922 uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000923 }
924
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100925 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +0100926 DCHECK(user != nullptr);
David Brazdiled596192015-01-23 10:39:45 +0000927 HUseListNode<HEnvironment*>* env_use =
928 env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
929 user->RecordEnvUse(env_use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100930 }
931
932 void RemoveUser(HInstruction* user, size_t index);
David Brazdiled596192015-01-23 10:39:45 +0000933 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100934
David Brazdilea55b932015-01-27 17:12:29 +0000935 const HUseList<HInstruction*>& GetUses() { return uses_; }
936 const HUseList<HEnvironment*>& GetEnvUses() { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000937
David Brazdiled596192015-01-23 10:39:45 +0000938 bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); }
939 bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000940
Roland Levillain6c82d402014-10-13 16:10:27 +0100941 // Does this instruction strictly dominate `other_instruction`?
942 // Returns false if this instruction and `other_instruction` are the same.
943 // Aborts if this instruction and `other_instruction` are both phis.
944 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +0100945
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000946 int GetId() const { return id_; }
947 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000948
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100949 int GetSsaIndex() const { return ssa_index_; }
950 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
951 bool HasSsaIndex() const { return ssa_index_ != -1; }
952
953 bool HasEnvironment() const { return environment_ != nullptr; }
954 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100955 void SetEnvironment(HEnvironment* environment) { environment_ = environment; }
956
Nicolas Geoffray39468442014-09-02 15:17:15 +0100957 // Returns the number of entries in the environment. Typically, that is the
958 // number of dex registers in a method. It could be more in case of inlining.
959 size_t EnvironmentSize() const;
960
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000961 LocationSummary* GetLocations() const { return locations_; }
962 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000963
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100964 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100965 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100966
Nicolas Geoffray82091da2015-01-26 10:02:45 +0000967 // Move `this` instruction before `cursor`.
968 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000969
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100970#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +0100971 bool Is##type() const { return (As##type() != nullptr); } \
972 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000973 virtual H##type* As##type() { return nullptr; }
974
975 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
976#undef INSTRUCTION_TYPE_CHECK
977
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100978 // Returns whether the instruction can be moved within the graph.
979 virtual bool CanBeMoved() const { return false; }
980
981 // Returns whether the two instructions are of the same kind.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700982 virtual bool InstructionTypeEquals(HInstruction* other) const {
983 UNUSED(other);
984 return false;
985 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100986
987 // Returns whether any data encoded in the two instructions is equal.
988 // This method does not look at the inputs. Both instructions must be
989 // of the same type, otherwise the method has undefined behavior.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700990 virtual bool InstructionDataEquals(HInstruction* other) const {
991 UNUSED(other);
992 return false;
993 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100994
995 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +0000996 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100997 // 2) Their inputs are identical.
998 bool Equals(HInstruction* other) const;
999
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001000 virtual InstructionKind GetKind() const = 0;
1001
1002 virtual size_t ComputeHashCode() const {
1003 size_t result = GetKind();
1004 for (size_t i = 0, e = InputCount(); i < e; ++i) {
1005 result = (result * 31) + InputAt(i)->GetId();
1006 }
1007 return result;
1008 }
1009
1010 SideEffects GetSideEffects() const { return side_effects_; }
1011
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001012 size_t GetLifetimePosition() const { return lifetime_position_; }
1013 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
1014 LiveInterval* GetLiveInterval() const { return live_interval_; }
1015 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
1016 bool HasLiveInterval() const { return live_interval_ != nullptr; }
1017
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001018 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
1019
1020 // Returns whether the code generation of the instruction will require to have access
1021 // to the current method. Such instructions are:
1022 // (1): Instructions that require an environment, as calling the runtime requires
1023 // to walk the stack and have the current method stored at a specific stack address.
1024 // (2): Object literals like classes and strings, that are loaded from the dex cache
1025 // fields of the current method.
1026 bool NeedsCurrentMethod() const {
1027 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
1028 }
1029
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001030 private:
1031 HInstruction* previous_;
1032 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001033 HBasicBlock* block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001034
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001035 // An instruction gets an id when it is added to the graph.
1036 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01001037 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001038 int id_;
1039
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001040 // When doing liveness analysis, instructions that have uses get an SSA index.
1041 int ssa_index_;
1042
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001043 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00001044 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001045
1046 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00001047 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001048
Nicolas Geoffray39468442014-09-02 15:17:15 +01001049 // The environment associated with this instruction. Not null if the instruction
1050 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001051 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001052
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001053 // Set by the code generator.
1054 LocationSummary* locations_;
1055
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001056 // Set by the liveness analysis.
1057 LiveInterval* live_interval_;
1058
1059 // Set by the liveness analysis, this is the position in a linear
1060 // order of blocks where this instruction's live interval start.
1061 size_t lifetime_position_;
1062
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001063 const SideEffects side_effects_;
1064
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001065 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001066 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001067 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001068
1069 DISALLOW_COPY_AND_ASSIGN(HInstruction);
1070};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001071std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001072
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001073class HInputIterator : public ValueObject {
1074 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001075 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001076
1077 bool Done() const { return index_ == instruction_->InputCount(); }
1078 HInstruction* Current() const { return instruction_->InputAt(index_); }
1079 void Advance() { index_++; }
1080
1081 private:
1082 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001083 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001084
1085 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
1086};
1087
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001088class HInstructionIterator : public ValueObject {
1089 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001090 explicit HInstructionIterator(const HInstructionList& instructions)
1091 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001092 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001093 }
1094
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001095 bool Done() const { return instruction_ == nullptr; }
1096 HInstruction* Current() const { return instruction_; }
1097 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001098 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001099 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001100 }
1101
1102 private:
1103 HInstruction* instruction_;
1104 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001105
1106 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001107};
1108
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001109class HBackwardInstructionIterator : public ValueObject {
1110 public:
1111 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
1112 : instruction_(instructions.last_instruction_) {
1113 next_ = Done() ? nullptr : instruction_->GetPrevious();
1114 }
1115
1116 bool Done() const { return instruction_ == nullptr; }
1117 HInstruction* Current() const { return instruction_; }
1118 void Advance() {
1119 instruction_ = next_;
1120 next_ = Done() ? nullptr : instruction_->GetPrevious();
1121 }
1122
1123 private:
1124 HInstruction* instruction_;
1125 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001126
1127 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001128};
1129
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001130// An embedded container with N elements of type T. Used (with partial
1131// specialization for N=0) because embedded arrays cannot have size 0.
1132template<typename T, intptr_t N>
1133class EmbeddedArray {
1134 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001135 EmbeddedArray() : elements_() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001136
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001137 intptr_t GetLength() const { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001138
1139 const T& operator[](intptr_t i) const {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001140 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001141 return elements_[i];
1142 }
1143
1144 T& operator[](intptr_t i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001145 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001146 return elements_[i];
1147 }
1148
1149 const T& At(intptr_t i) const {
1150 return (*this)[i];
1151 }
1152
1153 void SetAt(intptr_t i, const T& val) {
1154 (*this)[i] = val;
1155 }
1156
1157 private:
1158 T elements_[N];
1159};
1160
1161template<typename T>
1162class EmbeddedArray<T, 0> {
1163 public:
1164 intptr_t length() const { return 0; }
1165 const T& operator[](intptr_t i) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001166 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001167 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001168 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001169 }
1170 T& operator[](intptr_t i) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001171 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001172 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001173 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001174 }
1175};
1176
1177template<intptr_t N>
1178class HTemplateInstruction: public HInstruction {
1179 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001180 HTemplateInstruction<N>(SideEffects side_effects)
1181 : HInstruction(side_effects), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07001182 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001183
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001184 virtual size_t InputCount() const { return N; }
1185 virtual HInstruction* InputAt(size_t i) const { return inputs_[i]; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001186
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001187 protected:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001188 virtual void SetRawInputAt(size_t i, HInstruction* instruction) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001189 inputs_[i] = instruction;
1190 }
1191
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001192 private:
1193 EmbeddedArray<HInstruction*, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001194
1195 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001196};
1197
Dave Allison20dfc792014-06-16 20:44:29 -07001198template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001199class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07001200 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001201 HExpression<N>(Primitive::Type type, SideEffects side_effects)
1202 : HTemplateInstruction<N>(side_effects), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07001203 virtual ~HExpression() {}
1204
1205 virtual Primitive::Type GetType() const { return type_; }
1206
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001207 protected:
1208 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07001209};
1210
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001211// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
1212// instruction that branches to the exit block.
1213class HReturnVoid : public HTemplateInstruction<0> {
1214 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001215 HReturnVoid() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001216
1217 virtual bool IsControlFlow() const { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001218
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001219 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001220
1221 private:
1222 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
1223};
1224
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001225// Represents dex's RETURN opcodes. A HReturn is a control flow
1226// instruction that branches to the exit block.
1227class HReturn : public HTemplateInstruction<1> {
1228 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001229 explicit HReturn(HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001230 SetRawInputAt(0, value);
1231 }
1232
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001233 virtual bool IsControlFlow() const { return true; }
1234
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001235 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001236
1237 private:
1238 DISALLOW_COPY_AND_ASSIGN(HReturn);
1239};
1240
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001241// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001242// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001243// exit block.
1244class HExit : public HTemplateInstruction<0> {
1245 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001246 HExit() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001247
1248 virtual bool IsControlFlow() const { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001249
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001250 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001251
1252 private:
1253 DISALLOW_COPY_AND_ASSIGN(HExit);
1254};
1255
1256// Jumps from one block to another.
1257class HGoto : public HTemplateInstruction<0> {
1258 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001259 HGoto() : HTemplateInstruction(SideEffects::None()) {}
1260
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001261 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001262
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001263 HBasicBlock* GetSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001264 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001265 }
1266
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001267 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001268
1269 private:
1270 DISALLOW_COPY_AND_ASSIGN(HGoto);
1271};
1272
Dave Allison20dfc792014-06-16 20:44:29 -07001273
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001274// Conditional branch. A block ending with an HIf instruction must have
1275// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001276class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001277 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001278 explicit HIf(HInstruction* input) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001279 SetRawInputAt(0, input);
1280 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001281
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001282 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001283
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001284 HBasicBlock* IfTrueSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001285 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001286 }
1287
1288 HBasicBlock* IfFalseSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001289 return GetBlock()->GetSuccessors().Get(1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001290 }
1291
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001292 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001293
Dave Allison20dfc792014-06-16 20:44:29 -07001294 virtual bool IsIfInstruction() const { return true; }
1295
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001296 private:
1297 DISALLOW_COPY_AND_ASSIGN(HIf);
1298};
1299
Roland Levillain88cb1752014-10-20 16:36:47 +01001300class HUnaryOperation : public HExpression<1> {
1301 public:
1302 HUnaryOperation(Primitive::Type result_type, HInstruction* input)
1303 : HExpression(result_type, SideEffects::None()) {
1304 SetRawInputAt(0, input);
1305 }
1306
1307 HInstruction* GetInput() const { return InputAt(0); }
1308 Primitive::Type GetResultType() const { return GetType(); }
1309
1310 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001311 virtual bool InstructionDataEquals(HInstruction* other) const {
1312 UNUSED(other);
1313 return true;
1314 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001315
Roland Levillain9240d6a2014-10-20 16:47:04 +01001316 // Try to statically evaluate `operation` and return a HConstant
1317 // containing the result of this evaluation. If `operation` cannot
1318 // be evaluated as a constant, return nullptr.
1319 HConstant* TryStaticEvaluation() const;
1320
1321 // Apply this operation to `x`.
1322 virtual int32_t Evaluate(int32_t x) const = 0;
1323 virtual int64_t Evaluate(int64_t x) const = 0;
1324
Roland Levillain88cb1752014-10-20 16:36:47 +01001325 DECLARE_INSTRUCTION(UnaryOperation);
1326
1327 private:
1328 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
1329};
1330
Dave Allison20dfc792014-06-16 20:44:29 -07001331class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001332 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001333 HBinaryOperation(Primitive::Type result_type,
1334 HInstruction* left,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001335 HInstruction* right) : HExpression(result_type, SideEffects::None()) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001336 SetRawInputAt(0, left);
1337 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001338 }
1339
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001340 HInstruction* GetLeft() const { return InputAt(0); }
1341 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07001342 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001343
1344 virtual bool IsCommutative() { return false; }
1345
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001346 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001347 virtual bool InstructionDataEquals(HInstruction* other) const {
1348 UNUSED(other);
1349 return true;
1350 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001351
Roland Levillain9240d6a2014-10-20 16:47:04 +01001352 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01001353 // containing the result of this evaluation. If `operation` cannot
1354 // be evaluated as a constant, return nullptr.
Roland Levillain9240d6a2014-10-20 16:47:04 +01001355 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01001356
1357 // Apply this operation to `x` and `y`.
1358 virtual int32_t Evaluate(int32_t x, int32_t y) const = 0;
1359 virtual int64_t Evaluate(int64_t x, int64_t y) const = 0;
1360
Roland Levillainccc07a92014-09-16 14:48:16 +01001361 DECLARE_INSTRUCTION(BinaryOperation);
1362
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001363 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001364 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
1365};
1366
Dave Allison20dfc792014-06-16 20:44:29 -07001367class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001368 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001369 HCondition(HInstruction* first, HInstruction* second)
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001370 : HBinaryOperation(Primitive::kPrimBoolean, first, second),
1371 needs_materialization_(true) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001372
1373 virtual bool IsCommutative() { return true; }
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001374
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001375 bool NeedsMaterialization() const { return needs_materialization_; }
1376 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001377
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001378 // For code generation purposes, returns whether this instruction is just before
1379 // `if_`, and disregard moves in between.
1380 bool IsBeforeWhenDisregardMoves(HIf* if_) const;
1381
Dave Allison20dfc792014-06-16 20:44:29 -07001382 DECLARE_INSTRUCTION(Condition);
1383
1384 virtual IfCondition GetCondition() const = 0;
1385
1386 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001387 // For register allocation purposes, returns whether this instruction needs to be
1388 // materialized (that is, not just be in the processor flags).
1389 bool needs_materialization_;
1390
Dave Allison20dfc792014-06-16 20:44:29 -07001391 DISALLOW_COPY_AND_ASSIGN(HCondition);
1392};
1393
1394// Instruction to check if two inputs are equal to each other.
1395class HEqual : public HCondition {
1396 public:
1397 HEqual(HInstruction* first, HInstruction* second)
1398 : HCondition(first, second) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001399
Roland Levillain93445682014-10-06 19:24:02 +01001400 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1401 return x == y ? 1 : 0;
1402 }
1403 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1404 return x == y ? 1 : 0;
1405 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001406
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001407 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001408
Dave Allison20dfc792014-06-16 20:44:29 -07001409 virtual IfCondition GetCondition() const {
1410 return kCondEQ;
1411 }
1412
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001413 private:
1414 DISALLOW_COPY_AND_ASSIGN(HEqual);
1415};
1416
Dave Allison20dfc792014-06-16 20:44:29 -07001417class HNotEqual : public HCondition {
1418 public:
1419 HNotEqual(HInstruction* first, HInstruction* second)
1420 : HCondition(first, second) {}
1421
Roland Levillain93445682014-10-06 19:24:02 +01001422 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1423 return x != y ? 1 : 0;
1424 }
1425 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1426 return x != y ? 1 : 0;
1427 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001428
Dave Allison20dfc792014-06-16 20:44:29 -07001429 DECLARE_INSTRUCTION(NotEqual);
1430
1431 virtual IfCondition GetCondition() const {
1432 return kCondNE;
1433 }
1434
1435 private:
1436 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
1437};
1438
1439class HLessThan : public HCondition {
1440 public:
1441 HLessThan(HInstruction* first, HInstruction* second)
1442 : HCondition(first, second) {}
1443
Roland Levillain93445682014-10-06 19:24:02 +01001444 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1445 return x < y ? 1 : 0;
1446 }
1447 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1448 return x < y ? 1 : 0;
1449 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001450
Dave Allison20dfc792014-06-16 20:44:29 -07001451 DECLARE_INSTRUCTION(LessThan);
1452
1453 virtual IfCondition GetCondition() const {
1454 return kCondLT;
1455 }
1456
1457 private:
1458 DISALLOW_COPY_AND_ASSIGN(HLessThan);
1459};
1460
1461class HLessThanOrEqual : public HCondition {
1462 public:
1463 HLessThanOrEqual(HInstruction* first, HInstruction* second)
1464 : HCondition(first, second) {}
1465
Roland Levillain93445682014-10-06 19:24:02 +01001466 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1467 return x <= y ? 1 : 0;
1468 }
1469 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1470 return x <= y ? 1 : 0;
1471 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001472
Dave Allison20dfc792014-06-16 20:44:29 -07001473 DECLARE_INSTRUCTION(LessThanOrEqual);
1474
1475 virtual IfCondition GetCondition() const {
1476 return kCondLE;
1477 }
1478
1479 private:
1480 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
1481};
1482
1483class HGreaterThan : public HCondition {
1484 public:
1485 HGreaterThan(HInstruction* first, HInstruction* second)
1486 : HCondition(first, second) {}
1487
Roland Levillain93445682014-10-06 19:24:02 +01001488 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1489 return x > y ? 1 : 0;
1490 }
1491 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1492 return x > y ? 1 : 0;
1493 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001494
Dave Allison20dfc792014-06-16 20:44:29 -07001495 DECLARE_INSTRUCTION(GreaterThan);
1496
1497 virtual IfCondition GetCondition() const {
1498 return kCondGT;
1499 }
1500
1501 private:
1502 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
1503};
1504
1505class HGreaterThanOrEqual : public HCondition {
1506 public:
1507 HGreaterThanOrEqual(HInstruction* first, HInstruction* second)
1508 : HCondition(first, second) {}
1509
Roland Levillain93445682014-10-06 19:24:02 +01001510 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1511 return x >= y ? 1 : 0;
1512 }
1513 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1514 return x >= y ? 1 : 0;
1515 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001516
Dave Allison20dfc792014-06-16 20:44:29 -07001517 DECLARE_INSTRUCTION(GreaterThanOrEqual);
1518
1519 virtual IfCondition GetCondition() const {
1520 return kCondGE;
1521 }
1522
1523 private:
1524 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
1525};
1526
1527
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001528// Instruction to check how two inputs compare to each other.
1529// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
1530class HCompare : public HBinaryOperation {
1531 public:
Calin Juravleddb7df22014-11-25 20:56:51 +00001532 // The bias applies for floating point operations and indicates how NaN
1533 // comparisons are treated:
1534 enum Bias {
1535 kNoBias, // bias is not applicable (i.e. for long operation)
1536 kGtBias, // return 1 for NaN comparisons
1537 kLtBias, // return -1 for NaN comparisons
1538 };
1539
1540 HCompare(Primitive::Type type, HInstruction* first, HInstruction* second, Bias bias)
1541 : HBinaryOperation(Primitive::kPrimInt, first, second), bias_(bias) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001542 DCHECK_EQ(type, first->GetType());
1543 DCHECK_EQ(type, second->GetType());
1544 }
1545
Calin Juravleddb7df22014-11-25 20:56:51 +00001546 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01001547 return
1548 x == y ? 0 :
1549 x > y ? 1 :
1550 -1;
1551 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001552
1553 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01001554 return
1555 x == y ? 0 :
1556 x > y ? 1 :
1557 -1;
1558 }
1559
Calin Juravleddb7df22014-11-25 20:56:51 +00001560 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
1561 return bias_ == other->AsCompare()->bias_;
1562 }
1563
1564 bool IsGtBias() { return bias_ == kGtBias; }
1565
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001566 DECLARE_INSTRUCTION(Compare);
1567
1568 private:
Calin Juravleddb7df22014-11-25 20:56:51 +00001569 const Bias bias_;
1570
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001571 DISALLOW_COPY_AND_ASSIGN(HCompare);
1572};
1573
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001574// A local in the graph. Corresponds to a Dex register.
1575class HLocal : public HTemplateInstruction<0> {
1576 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001577 explicit HLocal(uint16_t reg_number)
1578 : HTemplateInstruction(SideEffects::None()), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001579
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001580 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001581
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001582 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001583
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001584 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001585 // The Dex register number.
1586 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001587
1588 DISALLOW_COPY_AND_ASSIGN(HLocal);
1589};
1590
1591// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07001592class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001593 public:
Roland Levillain5799fc02014-09-25 12:15:20 +01001594 HLoadLocal(HLocal* local, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001595 : HExpression(type, SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001596 SetRawInputAt(0, local);
1597 }
1598
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001599 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1600
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001601 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001602
1603 private:
1604 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
1605};
1606
1607// Store a value in a given local. This instruction has two inputs: the value
1608// and the local.
1609class HStoreLocal : public HTemplateInstruction<2> {
1610 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001611 HStoreLocal(HLocal* local, HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001612 SetRawInputAt(0, local);
1613 SetRawInputAt(1, value);
1614 }
1615
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001616 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1617
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001618 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001619
1620 private:
1621 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
1622};
1623
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001624class HConstant : public HExpression<0> {
1625 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001626 explicit HConstant(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
1627
1628 virtual bool CanBeMoved() const { return true; }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001629
1630 DECLARE_INSTRUCTION(Constant);
1631
1632 private:
1633 DISALLOW_COPY_AND_ASSIGN(HConstant);
1634};
1635
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001636class HFloatConstant : public HConstant {
1637 public:
1638 explicit HFloatConstant(float value) : HConstant(Primitive::kPrimFloat), value_(value) {}
1639
1640 float GetValue() const { return value_; }
1641
1642 virtual bool InstructionDataEquals(HInstruction* other) const {
1643 return bit_cast<float, int32_t>(other->AsFloatConstant()->value_) ==
1644 bit_cast<float, int32_t>(value_);
1645 }
1646
1647 virtual size_t ComputeHashCode() const { return static_cast<size_t>(GetValue()); }
1648
1649 DECLARE_INSTRUCTION(FloatConstant);
1650
1651 private:
1652 const float value_;
1653
1654 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
1655};
1656
1657class HDoubleConstant : public HConstant {
1658 public:
1659 explicit HDoubleConstant(double value) : HConstant(Primitive::kPrimDouble), value_(value) {}
1660
1661 double GetValue() const { return value_; }
1662
1663 virtual bool InstructionDataEquals(HInstruction* other) const {
1664 return bit_cast<double, int64_t>(other->AsDoubleConstant()->value_) ==
1665 bit_cast<double, int64_t>(value_);
1666 }
1667
1668 virtual size_t ComputeHashCode() const { return static_cast<size_t>(GetValue()); }
1669
1670 DECLARE_INSTRUCTION(DoubleConstant);
1671
1672 private:
1673 const double value_;
1674
1675 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
1676};
1677
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001678// Constants of the type int. Those can be from Dex instructions, or
1679// synthesized (for example with the if-eqz instruction).
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001680class HIntConstant : public HConstant {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001681 public:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001682 explicit HIntConstant(int32_t value) : HConstant(Primitive::kPrimInt), value_(value) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001683
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001684 int32_t GetValue() const { return value_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001685
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001686 virtual bool InstructionDataEquals(HInstruction* other) const {
1687 return other->AsIntConstant()->value_ == value_;
1688 }
1689
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001690 virtual size_t ComputeHashCode() const { return GetValue(); }
1691
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001692 DECLARE_INSTRUCTION(IntConstant);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001693
1694 private:
1695 const int32_t value_;
1696
1697 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
1698};
1699
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001700class HLongConstant : public HConstant {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001701 public:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001702 explicit HLongConstant(int64_t value) : HConstant(Primitive::kPrimLong), value_(value) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001703
1704 int64_t GetValue() const { return value_; }
1705
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001706 virtual bool InstructionDataEquals(HInstruction* other) const {
1707 return other->AsLongConstant()->value_ == value_;
1708 }
1709
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001710 virtual size_t ComputeHashCode() const { return static_cast<size_t>(GetValue()); }
1711
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001712 DECLARE_INSTRUCTION(LongConstant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001713
1714 private:
1715 const int64_t value_;
1716
1717 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
1718};
1719
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001720enum class Intrinsics {
1721#define OPTIMIZING_INTRINSICS(Name, IsStatic) k ## Name,
1722#include "intrinsics_list.h"
1723 kNone,
1724 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
1725#undef INTRINSICS_LIST
1726#undef OPTIMIZING_INTRINSICS
1727};
1728std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
1729
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001730class HInvoke : public HInstruction {
1731 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001732 virtual size_t InputCount() const { return inputs_.Size(); }
1733 virtual HInstruction* InputAt(size_t i) const { return inputs_.Get(i); }
1734
1735 // Runtime needs to walk the stack, so Dex -> Dex calls need to
1736 // know their environment.
Calin Juravle77520bc2015-01-12 18:45:46 +00001737 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001738
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001739 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001740 SetRawInputAt(index, argument);
1741 }
1742
1743 virtual void SetRawInputAt(size_t index, HInstruction* input) {
1744 inputs_.Put(index, input);
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001745 }
1746
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001747 virtual Primitive::Type GetType() const { return return_type_; }
1748
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001749 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001750
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001751 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
1752
1753 Intrinsics GetIntrinsic() {
1754 return intrinsic_;
1755 }
1756
1757 void SetIntrinsic(Intrinsics intrinsic) {
1758 intrinsic_ = intrinsic;
1759 }
1760
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001761 DECLARE_INSTRUCTION(Invoke);
1762
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001763 protected:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001764 HInvoke(ArenaAllocator* arena,
1765 uint32_t number_of_arguments,
1766 Primitive::Type return_type,
1767 uint32_t dex_pc,
1768 uint32_t dex_method_index)
1769 : HInstruction(SideEffects::All()),
1770 inputs_(arena, number_of_arguments),
1771 return_type_(return_type),
1772 dex_pc_(dex_pc),
1773 dex_method_index_(dex_method_index),
1774 intrinsic_(Intrinsics::kNone) {
1775 inputs_.SetSize(number_of_arguments);
1776 }
1777
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001778 GrowableArray<HInstruction*> inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001779 const Primitive::Type return_type_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001780 const uint32_t dex_pc_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001781 const uint32_t dex_method_index_;
1782 Intrinsics intrinsic_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001783
1784 private:
1785 DISALLOW_COPY_AND_ASSIGN(HInvoke);
1786};
1787
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001788class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001789 public:
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001790 HInvokeStaticOrDirect(ArenaAllocator* arena,
1791 uint32_t number_of_arguments,
1792 Primitive::Type return_type,
1793 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001794 uint32_t dex_method_index,
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001795 bool is_recursive,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001796 InvokeType invoke_type)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001797 : HInvoke(arena, number_of_arguments, return_type, dex_pc, dex_method_index),
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001798 invoke_type_(invoke_type),
1799 is_recursive_(is_recursive) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001800
Calin Juravle77520bc2015-01-12 18:45:46 +00001801 bool CanDoImplicitNullCheck() const OVERRIDE {
1802 // We access the method via the dex cache so we can't do an implicit null check.
1803 // TODO: for intrinsics we can generate implicit null checks.
1804 return false;
1805 }
1806
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001807 InvokeType GetInvokeType() const { return invoke_type_; }
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001808 bool IsRecursive() const { return is_recursive_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001809
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001810 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001811
1812 private:
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001813 const InvokeType invoke_type_;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001814 const bool is_recursive_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001815
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001816 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001817};
1818
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001819class HInvokeVirtual : public HInvoke {
1820 public:
1821 HInvokeVirtual(ArenaAllocator* arena,
1822 uint32_t number_of_arguments,
1823 Primitive::Type return_type,
1824 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001825 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001826 uint32_t vtable_index)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001827 : HInvoke(arena, number_of_arguments, return_type, dex_pc, dex_method_index),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001828 vtable_index_(vtable_index) {}
1829
Calin Juravle77520bc2015-01-12 18:45:46 +00001830 bool CanDoImplicitNullCheck() const OVERRIDE {
1831 // TODO: Add implicit null checks in intrinsics.
1832 return !GetLocations()->Intrinsified();
1833 }
1834
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001835 uint32_t GetVTableIndex() const { return vtable_index_; }
1836
1837 DECLARE_INSTRUCTION(InvokeVirtual);
1838
1839 private:
1840 const uint32_t vtable_index_;
1841
1842 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
1843};
1844
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001845class HInvokeInterface : public HInvoke {
1846 public:
1847 HInvokeInterface(ArenaAllocator* arena,
1848 uint32_t number_of_arguments,
1849 Primitive::Type return_type,
1850 uint32_t dex_pc,
1851 uint32_t dex_method_index,
1852 uint32_t imt_index)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001853 : HInvoke(arena, number_of_arguments, return_type, dex_pc, dex_method_index),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001854 imt_index_(imt_index) {}
1855
Calin Juravle77520bc2015-01-12 18:45:46 +00001856 bool CanDoImplicitNullCheck() const OVERRIDE {
1857 // TODO: Add implicit null checks in intrinsics.
1858 return !GetLocations()->Intrinsified();
1859 }
1860
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001861 uint32_t GetImtIndex() const { return imt_index_; }
1862 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
1863
1864 DECLARE_INSTRUCTION(InvokeInterface);
1865
1866 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001867 const uint32_t imt_index_;
1868
1869 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
1870};
1871
Dave Allison20dfc792014-06-16 20:44:29 -07001872class HNewInstance : public HExpression<0> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001873 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00001874 HNewInstance(uint32_t dex_pc, uint16_t type_index, QuickEntrypointEnum entrypoint)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001875 : HExpression(Primitive::kPrimNot, SideEffects::None()),
1876 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00001877 type_index_(type_index),
1878 entrypoint_(entrypoint) {}
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001879
1880 uint32_t GetDexPc() const { return dex_pc_; }
1881 uint16_t GetTypeIndex() const { return type_index_; }
1882
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001883 // Calls runtime so needs an environment.
Calin Juravle92a6ed22014-12-02 18:58:03 +00001884 bool NeedsEnvironment() const OVERRIDE { return true; }
1885 // It may throw when called on:
1886 // - interfaces
1887 // - abstract/innaccessible/unknown classes
1888 // TODO: optimize when possible.
1889 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001890
Calin Juravle10e244f2015-01-26 18:54:32 +00001891 bool CanBeNull() const OVERRIDE { return false; }
1892
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00001893 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
1894
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001895 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001896
1897 private:
1898 const uint32_t dex_pc_;
1899 const uint16_t type_index_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00001900 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001901
1902 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
1903};
1904
Roland Levillain88cb1752014-10-20 16:36:47 +01001905class HNeg : public HUnaryOperation {
1906 public:
1907 explicit HNeg(Primitive::Type result_type, HInstruction* input)
1908 : HUnaryOperation(result_type, input) {}
1909
Roland Levillain9240d6a2014-10-20 16:47:04 +01001910 virtual int32_t Evaluate(int32_t x) const OVERRIDE { return -x; }
1911 virtual int64_t Evaluate(int64_t x) const OVERRIDE { return -x; }
1912
Roland Levillain88cb1752014-10-20 16:36:47 +01001913 DECLARE_INSTRUCTION(Neg);
1914
1915 private:
1916 DISALLOW_COPY_AND_ASSIGN(HNeg);
1917};
1918
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001919class HNewArray : public HExpression<1> {
1920 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00001921 HNewArray(HInstruction* length,
1922 uint32_t dex_pc,
1923 uint16_t type_index,
1924 QuickEntrypointEnum entrypoint)
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001925 : HExpression(Primitive::kPrimNot, SideEffects::None()),
1926 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00001927 type_index_(type_index),
1928 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001929 SetRawInputAt(0, length);
1930 }
1931
1932 uint32_t GetDexPc() const { return dex_pc_; }
1933 uint16_t GetTypeIndex() const { return type_index_; }
1934
1935 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00001936 bool NeedsEnvironment() const OVERRIDE { return true; }
1937
1938 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001939
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00001940 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
1941
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001942 DECLARE_INSTRUCTION(NewArray);
1943
1944 private:
1945 const uint32_t dex_pc_;
1946 const uint16_t type_index_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00001947 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001948
1949 DISALLOW_COPY_AND_ASSIGN(HNewArray);
1950};
1951
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001952class HAdd : public HBinaryOperation {
1953 public:
1954 HAdd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1955 : HBinaryOperation(result_type, left, right) {}
1956
1957 virtual bool IsCommutative() { return true; }
1958
Roland Levillain93445682014-10-06 19:24:02 +01001959 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1960 return x + y;
1961 }
1962 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1963 return x + y;
1964 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001965
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001966 DECLARE_INSTRUCTION(Add);
1967
1968 private:
1969 DISALLOW_COPY_AND_ASSIGN(HAdd);
1970};
1971
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001972class HSub : public HBinaryOperation {
1973 public:
1974 HSub(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1975 : HBinaryOperation(result_type, left, right) {}
1976
Roland Levillain93445682014-10-06 19:24:02 +01001977 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1978 return x - y;
1979 }
1980 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1981 return x - y;
1982 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001983
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001984 DECLARE_INSTRUCTION(Sub);
1985
1986 private:
1987 DISALLOW_COPY_AND_ASSIGN(HSub);
1988};
1989
Calin Juravle34bacdf2014-10-07 20:23:36 +01001990class HMul : public HBinaryOperation {
1991 public:
1992 HMul(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1993 : HBinaryOperation(result_type, left, right) {}
1994
1995 virtual bool IsCommutative() { return true; }
1996
1997 virtual int32_t Evaluate(int32_t x, int32_t y) const { return x * y; }
1998 virtual int64_t Evaluate(int64_t x, int64_t y) const { return x * y; }
1999
2000 DECLARE_INSTRUCTION(Mul);
2001
2002 private:
2003 DISALLOW_COPY_AND_ASSIGN(HMul);
2004};
2005
Calin Juravle7c4954d2014-10-28 16:57:40 +00002006class HDiv : public HBinaryOperation {
2007 public:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002008 HDiv(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
2009 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00002010
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00002011 virtual int32_t Evaluate(int32_t x, int32_t y) const {
2012 // Our graph structure ensures we never have 0 for `y` during constant folding.
2013 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00002014 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00002015 return (y == -1) ? -x : x / y;
2016 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002017
2018 virtual int64_t Evaluate(int64_t x, int64_t y) const {
2019 DCHECK_NE(y, 0);
2020 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2021 return (y == -1) ? -x : x / y;
2022 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002023
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002024 uint32_t GetDexPc() const { return dex_pc_; }
2025
Calin Juravle7c4954d2014-10-28 16:57:40 +00002026 DECLARE_INSTRUCTION(Div);
2027
2028 private:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002029 const uint32_t dex_pc_;
2030
Calin Juravle7c4954d2014-10-28 16:57:40 +00002031 DISALLOW_COPY_AND_ASSIGN(HDiv);
2032};
2033
Calin Juravlebacfec32014-11-14 15:54:36 +00002034class HRem : public HBinaryOperation {
2035 public:
2036 HRem(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
2037 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
2038
2039 virtual int32_t Evaluate(int32_t x, int32_t y) const {
2040 DCHECK_NE(y, 0);
2041 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2042 return (y == -1) ? 0 : x % y;
2043 }
2044
2045 virtual int64_t Evaluate(int64_t x, int64_t y) const {
2046 DCHECK_NE(y, 0);
2047 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2048 return (y == -1) ? 0 : x % y;
2049 }
2050
2051 uint32_t GetDexPc() const { return dex_pc_; }
2052
2053 DECLARE_INSTRUCTION(Rem);
2054
2055 private:
2056 const uint32_t dex_pc_;
2057
2058 DISALLOW_COPY_AND_ASSIGN(HRem);
2059};
2060
Calin Juravled0d48522014-11-04 16:40:20 +00002061class HDivZeroCheck : public HExpression<1> {
2062 public:
2063 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
2064 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
2065 SetRawInputAt(0, value);
2066 }
2067
2068 bool CanBeMoved() const OVERRIDE { return true; }
2069
2070 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2071 UNUSED(other);
2072 return true;
2073 }
2074
2075 bool NeedsEnvironment() const OVERRIDE { return true; }
2076 bool CanThrow() const OVERRIDE { return true; }
2077
2078 uint32_t GetDexPc() const { return dex_pc_; }
2079
2080 DECLARE_INSTRUCTION(DivZeroCheck);
2081
2082 private:
2083 const uint32_t dex_pc_;
2084
2085 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
2086};
2087
Calin Juravle9aec02f2014-11-18 23:06:35 +00002088class HShl : public HBinaryOperation {
2089 public:
2090 HShl(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2091 : HBinaryOperation(result_type, left, right) {}
2092
2093 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x << (y & kMaxIntShiftValue); }
2094 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x << (y & kMaxLongShiftValue); }
2095
2096 DECLARE_INSTRUCTION(Shl);
2097
2098 private:
2099 DISALLOW_COPY_AND_ASSIGN(HShl);
2100};
2101
2102class HShr : public HBinaryOperation {
2103 public:
2104 HShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2105 : HBinaryOperation(result_type, left, right) {}
2106
2107 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x >> (y & kMaxIntShiftValue); }
2108 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x >> (y & kMaxLongShiftValue); }
2109
2110 DECLARE_INSTRUCTION(Shr);
2111
2112 private:
2113 DISALLOW_COPY_AND_ASSIGN(HShr);
2114};
2115
2116class HUShr : public HBinaryOperation {
2117 public:
2118 HUShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2119 : HBinaryOperation(result_type, left, right) {}
2120
2121 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
2122 uint32_t ux = static_cast<uint32_t>(x);
2123 uint32_t uy = static_cast<uint32_t>(y) & kMaxIntShiftValue;
2124 return static_cast<int32_t>(ux >> uy);
2125 }
2126
2127 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
2128 uint64_t ux = static_cast<uint64_t>(x);
2129 uint64_t uy = static_cast<uint64_t>(y) & kMaxLongShiftValue;
2130 return static_cast<int64_t>(ux >> uy);
2131 }
2132
2133 DECLARE_INSTRUCTION(UShr);
2134
2135 private:
2136 DISALLOW_COPY_AND_ASSIGN(HUShr);
2137};
2138
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002139class HAnd : public HBinaryOperation {
2140 public:
2141 HAnd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2142 : HBinaryOperation(result_type, left, right) {}
2143
2144 bool IsCommutative() OVERRIDE { return true; }
2145
2146 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x & y; }
2147 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x & y; }
2148
2149 DECLARE_INSTRUCTION(And);
2150
2151 private:
2152 DISALLOW_COPY_AND_ASSIGN(HAnd);
2153};
2154
2155class HOr : public HBinaryOperation {
2156 public:
2157 HOr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2158 : HBinaryOperation(result_type, left, right) {}
2159
2160 bool IsCommutative() OVERRIDE { return true; }
2161
2162 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x | y; }
2163 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x | y; }
2164
2165 DECLARE_INSTRUCTION(Or);
2166
2167 private:
2168 DISALLOW_COPY_AND_ASSIGN(HOr);
2169};
2170
2171class HXor : public HBinaryOperation {
2172 public:
2173 HXor(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2174 : HBinaryOperation(result_type, left, right) {}
2175
2176 bool IsCommutative() OVERRIDE { return true; }
2177
2178 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x ^ y; }
2179 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x ^ y; }
2180
2181 DECLARE_INSTRUCTION(Xor);
2182
2183 private:
2184 DISALLOW_COPY_AND_ASSIGN(HXor);
2185};
2186
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002187// The value of a parameter in this method. Its location depends on
2188// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07002189class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002190 public:
Calin Juravle10e244f2015-01-26 18:54:32 +00002191 HParameterValue(uint8_t index, Primitive::Type parameter_type, bool is_this = false)
2192 : HExpression(parameter_type, SideEffects::None()), index_(index), is_this_(is_this) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002193
2194 uint8_t GetIndex() const { return index_; }
2195
Calin Juravle10e244f2015-01-26 18:54:32 +00002196 bool CanBeNull() const OVERRIDE { return !is_this_; }
2197
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002198 DECLARE_INSTRUCTION(ParameterValue);
2199
2200 private:
2201 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00002202 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002203 const uint8_t index_;
2204
Calin Juravle10e244f2015-01-26 18:54:32 +00002205 // Whether or not the parameter value corresponds to 'this' argument.
2206 const bool is_this_;
2207
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002208 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
2209};
2210
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002211class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002212 public:
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002213 explicit HNot(Primitive::Type result_type, HInstruction* input)
2214 : HUnaryOperation(result_type, input) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002215
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002216 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002217 virtual bool InstructionDataEquals(HInstruction* other) const {
2218 UNUSED(other);
2219 return true;
2220 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002221
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002222 virtual int32_t Evaluate(int32_t x) const OVERRIDE { return ~x; }
2223 virtual int64_t Evaluate(int64_t x) const OVERRIDE { return ~x; }
2224
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002225 DECLARE_INSTRUCTION(Not);
2226
2227 private:
2228 DISALLOW_COPY_AND_ASSIGN(HNot);
2229};
2230
Roland Levillaindff1f282014-11-05 14:15:05 +00002231class HTypeConversion : public HExpression<1> {
2232 public:
2233 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00002234 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
2235 : HExpression(result_type, SideEffects::None()), dex_pc_(dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002236 SetRawInputAt(0, input);
2237 DCHECK_NE(input->GetType(), result_type);
2238 }
2239
2240 HInstruction* GetInput() const { return InputAt(0); }
2241 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
2242 Primitive::Type GetResultType() const { return GetType(); }
2243
Roland Levillain624279f2014-12-04 11:54:28 +00002244 // Required by the x86 and ARM code generators when producing calls
2245 // to the runtime.
2246 uint32_t GetDexPc() const { return dex_pc_; }
2247
Roland Levillaindff1f282014-11-05 14:15:05 +00002248 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00002249 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00002250
2251 DECLARE_INSTRUCTION(TypeConversion);
2252
2253 private:
Roland Levillain624279f2014-12-04 11:54:28 +00002254 const uint32_t dex_pc_;
2255
Roland Levillaindff1f282014-11-05 14:15:05 +00002256 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
2257};
2258
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00002259static constexpr uint32_t kNoRegNumber = -1;
2260
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002261class HPhi : public HInstruction {
2262 public:
2263 HPhi(ArenaAllocator* arena, uint32_t reg_number, size_t number_of_inputs, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002264 : HInstruction(SideEffects::None()),
2265 inputs_(arena, number_of_inputs),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002266 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002267 type_(type),
Calin Juravle10e244f2015-01-26 18:54:32 +00002268 is_live_(false),
2269 can_be_null_(true) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002270 inputs_.SetSize(number_of_inputs);
2271 }
2272
Calin Juravle10e244f2015-01-26 18:54:32 +00002273 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
2274 HInstruction* InputAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002275
Calin Juravle10e244f2015-01-26 18:54:32 +00002276 void SetRawInputAt(size_t index, HInstruction* input) OVERRIDE {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002277 inputs_.Put(index, input);
2278 }
2279
2280 void AddInput(HInstruction* input);
2281
Calin Juravle10e244f2015-01-26 18:54:32 +00002282 Primitive::Type GetType() const OVERRIDE { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002283 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002284
Calin Juravle10e244f2015-01-26 18:54:32 +00002285 bool CanBeNull() const OVERRIDE { return can_be_null_; }
2286 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
2287
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002288 uint32_t GetRegNumber() const { return reg_number_; }
2289
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002290 void SetDead() { is_live_ = false; }
2291 void SetLive() { is_live_ = true; }
2292 bool IsDead() const { return !is_live_; }
2293 bool IsLive() const { return is_live_; }
2294
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002295 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002296
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002297 private:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002298 GrowableArray<HInstruction*> inputs_;
2299 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002300 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002301 bool is_live_;
Calin Juravle10e244f2015-01-26 18:54:32 +00002302 bool can_be_null_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002303
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002304 DISALLOW_COPY_AND_ASSIGN(HPhi);
2305};
2306
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002307class HNullCheck : public HExpression<1> {
2308 public:
2309 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002310 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002311 SetRawInputAt(0, value);
2312 }
2313
Calin Juravle10e244f2015-01-26 18:54:32 +00002314 bool CanBeMoved() const OVERRIDE { return true; }
2315 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002316 UNUSED(other);
2317 return true;
2318 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002319
Calin Juravle10e244f2015-01-26 18:54:32 +00002320 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002321
Calin Juravle10e244f2015-01-26 18:54:32 +00002322 bool CanThrow() const OVERRIDE { return true; }
2323
2324 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01002325
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002326 uint32_t GetDexPc() const { return dex_pc_; }
2327
2328 DECLARE_INSTRUCTION(NullCheck);
2329
2330 private:
2331 const uint32_t dex_pc_;
2332
2333 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
2334};
2335
2336class FieldInfo : public ValueObject {
2337 public:
Calin Juravle52c48962014-12-16 17:02:57 +00002338 FieldInfo(MemberOffset field_offset, Primitive::Type field_type, bool is_volatile)
2339 : field_offset_(field_offset), field_type_(field_type), is_volatile_(is_volatile) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002340
2341 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002342 Primitive::Type GetFieldType() const { return field_type_; }
Calin Juravle52c48962014-12-16 17:02:57 +00002343 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002344
2345 private:
2346 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01002347 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00002348 const bool is_volatile_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002349};
2350
2351class HInstanceFieldGet : public HExpression<1> {
2352 public:
2353 HInstanceFieldGet(HInstruction* value,
2354 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00002355 MemberOffset field_offset,
2356 bool is_volatile)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002357 : HExpression(field_type, SideEffects::DependsOnSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00002358 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002359 SetRawInputAt(0, value);
2360 }
2361
Calin Juravle10c9cbe2014-12-19 10:50:19 +00002362 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002363
2364 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2365 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
2366 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002367 }
2368
Calin Juravle77520bc2015-01-12 18:45:46 +00002369 bool CanDoImplicitNullCheck() const OVERRIDE {
2370 return GetFieldOffset().Uint32Value() < kPageSize;
2371 }
2372
2373 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002374 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
2375 }
2376
Calin Juravle52c48962014-12-16 17:02:57 +00002377 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002378 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002379 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002380 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002381
2382 DECLARE_INSTRUCTION(InstanceFieldGet);
2383
2384 private:
2385 const FieldInfo field_info_;
2386
2387 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
2388};
2389
2390class HInstanceFieldSet : public HTemplateInstruction<2> {
2391 public:
2392 HInstanceFieldSet(HInstruction* object,
2393 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01002394 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00002395 MemberOffset field_offset,
2396 bool is_volatile)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002397 : HTemplateInstruction(SideEffects::ChangesSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00002398 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002399 SetRawInputAt(0, object);
2400 SetRawInputAt(1, value);
2401 }
2402
Calin Juravle77520bc2015-01-12 18:45:46 +00002403 bool CanDoImplicitNullCheck() const OVERRIDE {
2404 return GetFieldOffset().Uint32Value() < kPageSize;
2405 }
2406
Calin Juravle52c48962014-12-16 17:02:57 +00002407 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002408 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002409 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002410 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002411 HInstruction* GetValue() const { return InputAt(1); }
2412
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002413 DECLARE_INSTRUCTION(InstanceFieldSet);
2414
2415 private:
2416 const FieldInfo field_info_;
2417
2418 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
2419};
2420
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002421class HArrayGet : public HExpression<2> {
2422 public:
2423 HArrayGet(HInstruction* array, HInstruction* index, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002424 : HExpression(type, SideEffects::DependsOnSomething()) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002425 SetRawInputAt(0, array);
2426 SetRawInputAt(1, index);
2427 }
2428
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002429 bool CanBeMoved() const OVERRIDE { return true; }
2430 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002431 UNUSED(other);
2432 return true;
2433 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002434 bool CanDoImplicitNullCheck() const OVERRIDE {
2435 // TODO: We can be smarter here.
2436 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
2437 // which generates the implicit null check. There are cases when these can be removed
2438 // to produce better code. If we ever add optimizations to do so we should allow an
2439 // implicit check here (as long as the address falls in the first page).
2440 return false;
2441 }
2442
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002443 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002444
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002445 HInstruction* GetArray() const { return InputAt(0); }
2446 HInstruction* GetIndex() const { return InputAt(1); }
2447
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002448 DECLARE_INSTRUCTION(ArrayGet);
2449
2450 private:
2451 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
2452};
2453
2454class HArraySet : public HTemplateInstruction<3> {
2455 public:
2456 HArraySet(HInstruction* array,
2457 HInstruction* index,
2458 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002459 Primitive::Type expected_component_type,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002460 uint32_t dex_pc)
2461 : HTemplateInstruction(SideEffects::ChangesSomething()),
2462 dex_pc_(dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002463 expected_component_type_(expected_component_type),
2464 needs_type_check_(value->GetType() == Primitive::kPrimNot) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002465 SetRawInputAt(0, array);
2466 SetRawInputAt(1, index);
2467 SetRawInputAt(2, value);
2468 }
2469
Calin Juravle77520bc2015-01-12 18:45:46 +00002470 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002471 // We currently always call a runtime method to catch array store
2472 // exceptions.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002473 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002474 }
2475
Calin Juravle77520bc2015-01-12 18:45:46 +00002476 bool CanDoImplicitNullCheck() const OVERRIDE {
2477 // TODO: Same as for ArrayGet.
2478 return false;
2479 }
2480
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002481 void ClearNeedsTypeCheck() {
2482 needs_type_check_ = false;
2483 }
2484
2485 bool NeedsTypeCheck() const { return needs_type_check_; }
2486
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002487 uint32_t GetDexPc() const { return dex_pc_; }
2488
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002489 HInstruction* GetArray() const { return InputAt(0); }
2490 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002491 HInstruction* GetValue() const { return InputAt(2); }
2492
2493 Primitive::Type GetComponentType() const {
2494 // The Dex format does not type floating point index operations. Since the
2495 // `expected_component_type_` is set during building and can therefore not
2496 // be correct, we also check what is the value type. If it is a floating
2497 // point type, we must use that type.
2498 Primitive::Type value_type = GetValue()->GetType();
2499 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
2500 ? value_type
2501 : expected_component_type_;
2502 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002503
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002504 DECLARE_INSTRUCTION(ArraySet);
2505
2506 private:
2507 const uint32_t dex_pc_;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002508 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002509 bool needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002510
2511 DISALLOW_COPY_AND_ASSIGN(HArraySet);
2512};
2513
2514class HArrayLength : public HExpression<1> {
2515 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002516 explicit HArrayLength(HInstruction* array)
2517 : HExpression(Primitive::kPrimInt, SideEffects::None()) {
2518 // Note that arrays do not change length, so the instruction does not
2519 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002520 SetRawInputAt(0, array);
2521 }
2522
Calin Juravle77520bc2015-01-12 18:45:46 +00002523 bool CanBeMoved() const OVERRIDE { return true; }
2524 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002525 UNUSED(other);
2526 return true;
2527 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002528 bool CanDoImplicitNullCheck() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002529
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002530 DECLARE_INSTRUCTION(ArrayLength);
2531
2532 private:
2533 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
2534};
2535
2536class HBoundsCheck : public HExpression<2> {
2537 public:
2538 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002539 : HExpression(index->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002540 DCHECK(index->GetType() == Primitive::kPrimInt);
2541 SetRawInputAt(0, index);
2542 SetRawInputAt(1, length);
2543 }
2544
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002545 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002546 virtual bool InstructionDataEquals(HInstruction* other) const {
2547 UNUSED(other);
2548 return true;
2549 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002550
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002551 virtual bool NeedsEnvironment() const { return true; }
2552
Roland Levillaine161a2a2014-10-03 12:45:18 +01002553 virtual bool CanThrow() const { return true; }
2554
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002555 uint32_t GetDexPc() const { return dex_pc_; }
2556
2557 DECLARE_INSTRUCTION(BoundsCheck);
2558
2559 private:
2560 const uint32_t dex_pc_;
2561
2562 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
2563};
2564
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002565/**
2566 * Some DEX instructions are folded into multiple HInstructions that need
2567 * to stay live until the last HInstruction. This class
2568 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00002569 * HInstruction stays live. `index` represents the stack location index of the
2570 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002571 */
2572class HTemporary : public HTemplateInstruction<0> {
2573 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002574 explicit HTemporary(size_t index) : HTemplateInstruction(SideEffects::None()), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002575
2576 size_t GetIndex() const { return index_; }
2577
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00002578 Primitive::Type GetType() const OVERRIDE {
2579 // The previous instruction is the one that will be stored in the temporary location.
2580 DCHECK(GetPrevious() != nullptr);
2581 return GetPrevious()->GetType();
2582 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00002583
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002584 DECLARE_INSTRUCTION(Temporary);
2585
2586 private:
2587 const size_t index_;
2588
2589 DISALLOW_COPY_AND_ASSIGN(HTemporary);
2590};
2591
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002592class HSuspendCheck : public HTemplateInstruction<0> {
2593 public:
2594 explicit HSuspendCheck(uint32_t dex_pc)
Nicolas Geoffray9ebc72c2014-09-25 16:33:42 +01002595 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002596
2597 virtual bool NeedsEnvironment() const {
2598 return true;
2599 }
2600
2601 uint32_t GetDexPc() const { return dex_pc_; }
2602
2603 DECLARE_INSTRUCTION(SuspendCheck);
2604
2605 private:
2606 const uint32_t dex_pc_;
2607
2608 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
2609};
2610
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002611/**
2612 * Instruction to load a Class object.
2613 */
2614class HLoadClass : public HExpression<0> {
2615 public:
2616 HLoadClass(uint16_t type_index,
2617 bool is_referrers_class,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002618 uint32_t dex_pc)
2619 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2620 type_index_(type_index),
2621 is_referrers_class_(is_referrers_class),
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002622 dex_pc_(dex_pc),
2623 generate_clinit_check_(false) {}
2624
2625 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002626
2627 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2628 return other->AsLoadClass()->type_index_ == type_index_;
2629 }
2630
2631 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
2632
2633 uint32_t GetDexPc() const { return dex_pc_; }
2634 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002635 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002636
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002637 bool NeedsEnvironment() const OVERRIDE {
2638 // Will call runtime and load the class if the class is not loaded yet.
2639 // TODO: finer grain decision.
2640 return !is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002641 }
2642
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002643 bool MustGenerateClinitCheck() const {
2644 return generate_clinit_check_;
2645 }
2646
2647 void SetMustGenerateClinitCheck() {
2648 generate_clinit_check_ = true;
2649 }
2650
2651 bool CanCallRuntime() const {
2652 return MustGenerateClinitCheck() || !is_referrers_class_;
2653 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002654
Nicolas Geoffray82091da2015-01-26 10:02:45 +00002655 bool CanThrow() const OVERRIDE {
2656 // May call runtime and and therefore can throw.
2657 // TODO: finer grain decision.
2658 return !is_referrers_class_;
2659 }
2660
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002661 DECLARE_INSTRUCTION(LoadClass);
2662
2663 private:
2664 const uint16_t type_index_;
2665 const bool is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002666 const uint32_t dex_pc_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002667 // Whether this instruction must generate the initialization check.
2668 // Used for code generation.
2669 bool generate_clinit_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002670
2671 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
2672};
2673
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002674class HLoadString : public HExpression<0> {
2675 public:
2676 HLoadString(uint32_t string_index, uint32_t dex_pc)
2677 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2678 string_index_(string_index),
2679 dex_pc_(dex_pc) {}
2680
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002681 bool CanBeMoved() const OVERRIDE { return true; }
2682
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002683 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2684 return other->AsLoadString()->string_index_ == string_index_;
2685 }
2686
2687 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
2688
2689 uint32_t GetDexPc() const { return dex_pc_; }
2690 uint32_t GetStringIndex() const { return string_index_; }
2691
2692 // TODO: Can we deopt or debug when we resolve a string?
2693 bool NeedsEnvironment() const OVERRIDE { return false; }
2694
2695 DECLARE_INSTRUCTION(LoadString);
2696
2697 private:
2698 const uint32_t string_index_;
2699 const uint32_t dex_pc_;
2700
2701 DISALLOW_COPY_AND_ASSIGN(HLoadString);
2702};
2703
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002704// TODO: Pass this check to HInvokeStaticOrDirect nodes.
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002705/**
2706 * Performs an initialization check on its Class object input.
2707 */
2708class HClinitCheck : public HExpression<1> {
2709 public:
2710 explicit HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
2711 : HExpression(Primitive::kPrimNot, SideEffects::All()),
2712 dex_pc_(dex_pc) {
2713 SetRawInputAt(0, constant);
2714 }
2715
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002716 bool CanBeMoved() const OVERRIDE { return true; }
2717 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2718 UNUSED(other);
2719 return true;
2720 }
2721
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002722 bool NeedsEnvironment() const OVERRIDE {
2723 // May call runtime to initialize the class.
2724 return true;
2725 }
2726
2727 uint32_t GetDexPc() const { return dex_pc_; }
2728
2729 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
2730
2731 DECLARE_INSTRUCTION(ClinitCheck);
2732
2733 private:
2734 const uint32_t dex_pc_;
2735
2736 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
2737};
2738
2739class HStaticFieldGet : public HExpression<1> {
2740 public:
2741 HStaticFieldGet(HInstruction* cls,
2742 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00002743 MemberOffset field_offset,
2744 bool is_volatile)
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002745 : HExpression(field_type, SideEffects::DependsOnSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00002746 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002747 SetRawInputAt(0, cls);
2748 }
2749
Calin Juravle52c48962014-12-16 17:02:57 +00002750
Calin Juravle10c9cbe2014-12-19 10:50:19 +00002751 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002752
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002753 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00002754 HStaticFieldGet* other_get = other->AsStaticFieldGet();
2755 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002756 }
2757
2758 size_t ComputeHashCode() const OVERRIDE {
2759 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
2760 }
2761
Calin Juravle52c48962014-12-16 17:02:57 +00002762 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002763 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
2764 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002765 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002766
2767 DECLARE_INSTRUCTION(StaticFieldGet);
2768
2769 private:
2770 const FieldInfo field_info_;
2771
2772 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
2773};
2774
2775class HStaticFieldSet : public HTemplateInstruction<2> {
2776 public:
2777 HStaticFieldSet(HInstruction* cls,
2778 HInstruction* value,
2779 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00002780 MemberOffset field_offset,
2781 bool is_volatile)
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002782 : HTemplateInstruction(SideEffects::ChangesSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00002783 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002784 SetRawInputAt(0, cls);
2785 SetRawInputAt(1, value);
2786 }
2787
Calin Juravle52c48962014-12-16 17:02:57 +00002788 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002789 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
2790 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002791 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002792
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002793 HInstruction* GetValue() const { return InputAt(1); }
2794
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002795 DECLARE_INSTRUCTION(StaticFieldSet);
2796
2797 private:
2798 const FieldInfo field_info_;
2799
2800 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
2801};
2802
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002803// Implement the move-exception DEX instruction.
2804class HLoadException : public HExpression<0> {
2805 public:
2806 HLoadException() : HExpression(Primitive::kPrimNot, SideEffects::None()) {}
2807
2808 DECLARE_INSTRUCTION(LoadException);
2809
2810 private:
2811 DISALLOW_COPY_AND_ASSIGN(HLoadException);
2812};
2813
2814class HThrow : public HTemplateInstruction<1> {
2815 public:
2816 HThrow(HInstruction* exception, uint32_t dex_pc)
2817 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {
2818 SetRawInputAt(0, exception);
2819 }
2820
2821 bool IsControlFlow() const OVERRIDE { return true; }
2822
2823 bool NeedsEnvironment() const OVERRIDE { return true; }
2824
Nicolas Geoffray82091da2015-01-26 10:02:45 +00002825 bool CanThrow() const OVERRIDE { return true; }
2826
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002827 uint32_t GetDexPc() const { return dex_pc_; }
2828
2829 DECLARE_INSTRUCTION(Throw);
2830
2831 private:
Nicolas Geoffray82091da2015-01-26 10:02:45 +00002832 const uint32_t dex_pc_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002833
2834 DISALLOW_COPY_AND_ASSIGN(HThrow);
2835};
2836
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002837class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002838 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002839 HInstanceOf(HInstruction* object,
2840 HLoadClass* constant,
2841 bool class_is_final,
2842 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002843 : HExpression(Primitive::kPrimBoolean, SideEffects::None()),
2844 class_is_final_(class_is_final),
2845 dex_pc_(dex_pc) {
2846 SetRawInputAt(0, object);
2847 SetRawInputAt(1, constant);
2848 }
2849
2850 bool CanBeMoved() const OVERRIDE { return true; }
2851
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002852 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002853 return true;
2854 }
2855
2856 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002857 return false;
2858 }
2859
2860 uint32_t GetDexPc() const { return dex_pc_; }
2861
2862 bool IsClassFinal() const { return class_is_final_; }
2863
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002864 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002865
2866 private:
2867 const bool class_is_final_;
2868 const uint32_t dex_pc_;
2869
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002870 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
2871};
2872
2873class HCheckCast : public HTemplateInstruction<2> {
2874 public:
2875 HCheckCast(HInstruction* object,
2876 HLoadClass* constant,
2877 bool class_is_final,
2878 uint32_t dex_pc)
2879 : HTemplateInstruction(SideEffects::None()),
2880 class_is_final_(class_is_final),
2881 dex_pc_(dex_pc) {
2882 SetRawInputAt(0, object);
2883 SetRawInputAt(1, constant);
2884 }
2885
2886 bool CanBeMoved() const OVERRIDE { return true; }
2887
2888 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2889 return true;
2890 }
2891
2892 bool NeedsEnvironment() const OVERRIDE {
2893 // Instruction may throw a CheckCastError.
2894 return true;
2895 }
2896
2897 bool CanThrow() const OVERRIDE { return true; }
2898
2899 uint32_t GetDexPc() const { return dex_pc_; }
2900
2901 bool IsClassFinal() const { return class_is_final_; }
2902
2903 DECLARE_INSTRUCTION(CheckCast);
2904
2905 private:
2906 const bool class_is_final_;
2907 const uint32_t dex_pc_;
2908
2909 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002910};
2911
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00002912class HMonitorOperation : public HTemplateInstruction<1> {
2913 public:
2914 enum OperationKind {
2915 kEnter,
2916 kExit,
2917 };
2918
2919 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
2920 : HTemplateInstruction(SideEffects::None()), kind_(kind), dex_pc_(dex_pc) {
2921 SetRawInputAt(0, object);
2922 }
2923
2924 // Instruction may throw a Java exception, so we need an environment.
2925 bool NeedsEnvironment() const OVERRIDE { return true; }
2926 bool CanThrow() const OVERRIDE { return true; }
2927
2928 uint32_t GetDexPc() const { return dex_pc_; }
2929
2930 bool IsEnter() const { return kind_ == kEnter; }
2931
2932 DECLARE_INSTRUCTION(MonitorOperation);
2933
Calin Juravle52c48962014-12-16 17:02:57 +00002934 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00002935 const OperationKind kind_;
2936 const uint32_t dex_pc_;
2937
2938 private:
2939 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
2940};
2941
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002942class MoveOperands : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002943 public:
Nicolas Geoffray740475d2014-09-29 10:33:25 +01002944 MoveOperands(Location source, Location destination, HInstruction* instruction)
2945 : source_(source), destination_(destination), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002946
2947 Location GetSource() const { return source_; }
2948 Location GetDestination() const { return destination_; }
2949
2950 void SetSource(Location value) { source_ = value; }
2951 void SetDestination(Location value) { destination_ = value; }
2952
2953 // The parallel move resolver marks moves as "in-progress" by clearing the
2954 // destination (but not the source).
2955 Location MarkPending() {
2956 DCHECK(!IsPending());
2957 Location dest = destination_;
2958 destination_ = Location::NoLocation();
2959 return dest;
2960 }
2961
2962 void ClearPending(Location dest) {
2963 DCHECK(IsPending());
2964 destination_ = dest;
2965 }
2966
2967 bool IsPending() const {
2968 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
2969 return destination_.IsInvalid() && !source_.IsInvalid();
2970 }
2971
2972 // True if this blocks a move from the given location.
2973 bool Blocks(Location loc) const {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00002974 return !IsEliminated() && source_.Equals(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002975 }
2976
2977 // A move is redundant if it's been eliminated, if its source and
2978 // destination are the same, or if its destination is unneeded.
2979 bool IsRedundant() const {
2980 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
2981 }
2982
2983 // We clear both operands to indicate move that's been eliminated.
2984 void Eliminate() {
2985 source_ = destination_ = Location::NoLocation();
2986 }
2987
2988 bool IsEliminated() const {
2989 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
2990 return source_.IsInvalid();
2991 }
2992
Nicolas Geoffray740475d2014-09-29 10:33:25 +01002993 HInstruction* GetInstruction() const { return instruction_; }
2994
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002995 private:
2996 Location source_;
2997 Location destination_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01002998 // The instruction this move is assocatied with. Null when this move is
2999 // for moving an input in the expected locations of user (including a phi user).
3000 // This is only used in debug mode, to ensure we do not connect interval siblings
3001 // in the same parallel move.
3002 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003003};
3004
3005static constexpr size_t kDefaultNumberOfMoves = 4;
3006
3007class HParallelMove : public HTemplateInstruction<0> {
3008 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003009 explicit HParallelMove(ArenaAllocator* arena)
3010 : HTemplateInstruction(SideEffects::None()), moves_(arena, kDefaultNumberOfMoves) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003011
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003012 void AddMove(Location source, Location destination, HInstruction* instruction) {
3013 DCHECK(source.IsValid());
3014 DCHECK(destination.IsValid());
3015 // The parallel move resolver does not handle pairs. So we decompose the
3016 // pair locations into two moves.
3017 if (source.IsPair() && destination.IsPair()) {
3018 AddMove(source.ToLow(), destination.ToLow(), instruction);
3019 AddMove(source.ToHigh(), destination.ToHigh(), nullptr);
3020 } else if (source.IsPair()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003021 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003022 AddMove(source.ToLow(), Location::StackSlot(destination.GetStackIndex()), instruction);
3023 AddMove(source.ToHigh(), Location::StackSlot(destination.GetHighStackIndex(4)), nullptr);
3024 } else if (destination.IsPair()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003025 if (source.IsConstant()) {
3026 // We put the same constant in the move. The code generator will handle which
3027 // low or high part to use.
3028 AddMove(source, destination.ToLow(), instruction);
3029 AddMove(source, destination.ToHigh(), nullptr);
3030 } else {
3031 DCHECK(source.IsDoubleStackSlot());
3032 AddMove(Location::StackSlot(source.GetStackIndex()), destination.ToLow(), instruction);
3033 // TODO: rewrite GetHighStackIndex to not require a word size. It's supposed to
3034 // always be 4.
3035 static constexpr int kHighOffset = 4;
3036 AddMove(Location::StackSlot(source.GetHighStackIndex(kHighOffset)),
3037 destination.ToHigh(),
3038 nullptr);
3039 }
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003040 } else {
3041 if (kIsDebugBuild) {
3042 if (instruction != nullptr) {
3043 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
3044 DCHECK_NE(moves_.Get(i).GetInstruction(), instruction)
3045 << "Doing parallel moves for the same instruction.";
3046 }
3047 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00003048 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003049 DCHECK(!destination.Equals(moves_.Get(i).GetDestination()))
3050 << "Same destination for two moves in a parallel move.";
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00003051 }
3052 }
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003053 moves_.Add(MoveOperands(source, destination, instruction));
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003054 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003055 }
3056
3057 MoveOperands* MoveOperandsAt(size_t index) const {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003058 return moves_.GetRawStorage() + index;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003059 }
3060
3061 size_t NumMoves() const { return moves_.Size(); }
3062
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003063 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003064
3065 private:
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003066 GrowableArray<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003067
3068 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
3069};
3070
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003071class HGraphVisitor : public ValueObject {
3072 public:
Dave Allison20dfc792014-06-16 20:44:29 -07003073 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
3074 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003075
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003076 virtual void VisitInstruction(HInstruction* instruction) { UNUSED(instruction); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003077 virtual void VisitBasicBlock(HBasicBlock* block);
3078
Roland Levillain633021e2014-10-01 14:12:25 +01003079 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003080 void VisitInsertionOrder();
3081
Roland Levillain633021e2014-10-01 14:12:25 +01003082 // Visit the graph following dominator tree reverse post-order.
3083 void VisitReversePostOrder();
3084
Nicolas Geoffray787c3072014-03-17 10:20:19 +00003085 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00003086
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003087 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003088#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003089 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
3090
3091 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
3092
3093#undef DECLARE_VISIT_INSTRUCTION
3094
3095 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07003096 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003097
3098 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
3099};
3100
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003101class HGraphDelegateVisitor : public HGraphVisitor {
3102 public:
3103 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
3104 virtual ~HGraphDelegateVisitor() {}
3105
3106 // Visit functions that delegate to to super class.
3107#define DECLARE_VISIT_INSTRUCTION(name, super) \
3108 virtual void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
3109
3110 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
3111
3112#undef DECLARE_VISIT_INSTRUCTION
3113
3114 private:
3115 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
3116};
3117
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003118class HInsertionOrderIterator : public ValueObject {
3119 public:
3120 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
3121
3122 bool Done() const { return index_ == graph_.GetBlocks().Size(); }
3123 HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); }
3124 void Advance() { ++index_; }
3125
3126 private:
3127 const HGraph& graph_;
3128 size_t index_;
3129
3130 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
3131};
3132
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003133class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003134 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003135 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003136
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003137 bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); }
3138 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003139 void Advance() { ++index_; }
3140
3141 private:
3142 const HGraph& graph_;
3143 size_t index_;
3144
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003145 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003146};
3147
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003148class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003149 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003150 explicit HPostOrderIterator(const HGraph& graph)
3151 : graph_(graph), index_(graph_.GetReversePostOrder().Size()) {}
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003152
3153 bool Done() const { return index_ == 0; }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003154 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003155 void Advance() { --index_; }
3156
3157 private:
3158 const HGraph& graph_;
3159 size_t index_;
3160
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003161 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003162};
3163
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003164// Iterator over the blocks that art part of the loop. Includes blocks part
3165// of an inner loop. The order in which the blocks are iterated is on their
3166// block id.
3167class HBlocksInLoopIterator : public ValueObject {
3168 public:
3169 explicit HBlocksInLoopIterator(const HLoopInformation& info)
3170 : blocks_in_loop_(info.GetBlocks()),
3171 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
3172 index_(0) {
3173 if (!blocks_in_loop_.IsBitSet(index_)) {
3174 Advance();
3175 }
3176 }
3177
3178 bool Done() const { return index_ == blocks_.Size(); }
3179 HBasicBlock* Current() const { return blocks_.Get(index_); }
3180 void Advance() {
3181 ++index_;
3182 for (size_t e = blocks_.Size(); index_ < e; ++index_) {
3183 if (blocks_in_loop_.IsBitSet(index_)) {
3184 break;
3185 }
3186 }
3187 }
3188
3189 private:
3190 const BitVector& blocks_in_loop_;
3191 const GrowableArray<HBasicBlock*>& blocks_;
3192 size_t index_;
3193
3194 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
3195};
3196
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003197} // namespace art
3198
3199#endif // ART_COMPILER_OPTIMIZING_NODES_H_