blob: ee7701b5ab08adc3401e09c465c3d77e7c2e6677 [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 Geoffray818f2102014-02-18 16:43:35 +00001018 private:
1019 HInstruction* previous_;
1020 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001021 HBasicBlock* block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001022
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001023 // An instruction gets an id when it is added to the graph.
1024 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01001025 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001026 int id_;
1027
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001028 // When doing liveness analysis, instructions that have uses get an SSA index.
1029 int ssa_index_;
1030
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001031 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00001032 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001033
1034 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00001035 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001036
Nicolas Geoffray39468442014-09-02 15:17:15 +01001037 // The environment associated with this instruction. Not null if the instruction
1038 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001039 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001040
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001041 // Set by the code generator.
1042 LocationSummary* locations_;
1043
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001044 // Set by the liveness analysis.
1045 LiveInterval* live_interval_;
1046
1047 // Set by the liveness analysis, this is the position in a linear
1048 // order of blocks where this instruction's live interval start.
1049 size_t lifetime_position_;
1050
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001051 const SideEffects side_effects_;
1052
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001053 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001054 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001055 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001056
1057 DISALLOW_COPY_AND_ASSIGN(HInstruction);
1058};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001059std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001060
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001061class HInputIterator : public ValueObject {
1062 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001063 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001064
1065 bool Done() const { return index_ == instruction_->InputCount(); }
1066 HInstruction* Current() const { return instruction_->InputAt(index_); }
1067 void Advance() { index_++; }
1068
1069 private:
1070 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001071 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001072
1073 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
1074};
1075
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001076class HInstructionIterator : public ValueObject {
1077 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001078 explicit HInstructionIterator(const HInstructionList& instructions)
1079 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001080 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001081 }
1082
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001083 bool Done() const { return instruction_ == nullptr; }
1084 HInstruction* Current() const { return instruction_; }
1085 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001086 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001087 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001088 }
1089
1090 private:
1091 HInstruction* instruction_;
1092 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001093
1094 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001095};
1096
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001097class HBackwardInstructionIterator : public ValueObject {
1098 public:
1099 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
1100 : instruction_(instructions.last_instruction_) {
1101 next_ = Done() ? nullptr : instruction_->GetPrevious();
1102 }
1103
1104 bool Done() const { return instruction_ == nullptr; }
1105 HInstruction* Current() const { return instruction_; }
1106 void Advance() {
1107 instruction_ = next_;
1108 next_ = Done() ? nullptr : instruction_->GetPrevious();
1109 }
1110
1111 private:
1112 HInstruction* instruction_;
1113 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001114
1115 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001116};
1117
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001118// An embedded container with N elements of type T. Used (with partial
1119// specialization for N=0) because embedded arrays cannot have size 0.
1120template<typename T, intptr_t N>
1121class EmbeddedArray {
1122 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001123 EmbeddedArray() : elements_() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001124
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001125 intptr_t GetLength() const { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001126
1127 const T& operator[](intptr_t i) const {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001128 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001129 return elements_[i];
1130 }
1131
1132 T& operator[](intptr_t i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001133 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001134 return elements_[i];
1135 }
1136
1137 const T& At(intptr_t i) const {
1138 return (*this)[i];
1139 }
1140
1141 void SetAt(intptr_t i, const T& val) {
1142 (*this)[i] = val;
1143 }
1144
1145 private:
1146 T elements_[N];
1147};
1148
1149template<typename T>
1150class EmbeddedArray<T, 0> {
1151 public:
1152 intptr_t length() const { return 0; }
1153 const T& operator[](intptr_t i) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001154 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001155 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001156 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001157 }
1158 T& operator[](intptr_t i) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001159 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001160 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001161 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001162 }
1163};
1164
1165template<intptr_t N>
1166class HTemplateInstruction: public HInstruction {
1167 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001168 HTemplateInstruction<N>(SideEffects side_effects)
1169 : HInstruction(side_effects), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07001170 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001171
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001172 virtual size_t InputCount() const { return N; }
1173 virtual HInstruction* InputAt(size_t i) const { return inputs_[i]; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001174
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001175 protected:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001176 virtual void SetRawInputAt(size_t i, HInstruction* instruction) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001177 inputs_[i] = instruction;
1178 }
1179
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001180 private:
1181 EmbeddedArray<HInstruction*, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001182
1183 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001184};
1185
Dave Allison20dfc792014-06-16 20:44:29 -07001186template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001187class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07001188 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001189 HExpression<N>(Primitive::Type type, SideEffects side_effects)
1190 : HTemplateInstruction<N>(side_effects), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07001191 virtual ~HExpression() {}
1192
1193 virtual Primitive::Type GetType() const { return type_; }
1194
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001195 protected:
1196 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07001197};
1198
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001199// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
1200// instruction that branches to the exit block.
1201class HReturnVoid : public HTemplateInstruction<0> {
1202 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001203 HReturnVoid() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001204
1205 virtual bool IsControlFlow() const { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001206
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001207 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001208
1209 private:
1210 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
1211};
1212
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001213// Represents dex's RETURN opcodes. A HReturn is a control flow
1214// instruction that branches to the exit block.
1215class HReturn : public HTemplateInstruction<1> {
1216 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001217 explicit HReturn(HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001218 SetRawInputAt(0, value);
1219 }
1220
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001221 virtual bool IsControlFlow() const { return true; }
1222
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001223 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001224
1225 private:
1226 DISALLOW_COPY_AND_ASSIGN(HReturn);
1227};
1228
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001229// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001230// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001231// exit block.
1232class HExit : public HTemplateInstruction<0> {
1233 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001234 HExit() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001235
1236 virtual bool IsControlFlow() const { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001237
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001238 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001239
1240 private:
1241 DISALLOW_COPY_AND_ASSIGN(HExit);
1242};
1243
1244// Jumps from one block to another.
1245class HGoto : public HTemplateInstruction<0> {
1246 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001247 HGoto() : HTemplateInstruction(SideEffects::None()) {}
1248
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001249 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001250
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001251 HBasicBlock* GetSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001252 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001253 }
1254
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001255 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001256
1257 private:
1258 DISALLOW_COPY_AND_ASSIGN(HGoto);
1259};
1260
Dave Allison20dfc792014-06-16 20:44:29 -07001261
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001262// Conditional branch. A block ending with an HIf instruction must have
1263// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001264class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001265 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001266 explicit HIf(HInstruction* input) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001267 SetRawInputAt(0, input);
1268 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001269
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001270 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001271
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001272 HBasicBlock* IfTrueSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001273 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001274 }
1275
1276 HBasicBlock* IfFalseSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001277 return GetBlock()->GetSuccessors().Get(1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001278 }
1279
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001280 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001281
Dave Allison20dfc792014-06-16 20:44:29 -07001282 virtual bool IsIfInstruction() const { return true; }
1283
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001284 private:
1285 DISALLOW_COPY_AND_ASSIGN(HIf);
1286};
1287
Roland Levillain88cb1752014-10-20 16:36:47 +01001288class HUnaryOperation : public HExpression<1> {
1289 public:
1290 HUnaryOperation(Primitive::Type result_type, HInstruction* input)
1291 : HExpression(result_type, SideEffects::None()) {
1292 SetRawInputAt(0, input);
1293 }
1294
1295 HInstruction* GetInput() const { return InputAt(0); }
1296 Primitive::Type GetResultType() const { return GetType(); }
1297
1298 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001299 virtual bool InstructionDataEquals(HInstruction* other) const {
1300 UNUSED(other);
1301 return true;
1302 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001303
Roland Levillain9240d6a2014-10-20 16:47:04 +01001304 // Try to statically evaluate `operation` and return a HConstant
1305 // containing the result of this evaluation. If `operation` cannot
1306 // be evaluated as a constant, return nullptr.
1307 HConstant* TryStaticEvaluation() const;
1308
1309 // Apply this operation to `x`.
1310 virtual int32_t Evaluate(int32_t x) const = 0;
1311 virtual int64_t Evaluate(int64_t x) const = 0;
1312
Roland Levillain88cb1752014-10-20 16:36:47 +01001313 DECLARE_INSTRUCTION(UnaryOperation);
1314
1315 private:
1316 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
1317};
1318
Dave Allison20dfc792014-06-16 20:44:29 -07001319class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001320 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001321 HBinaryOperation(Primitive::Type result_type,
1322 HInstruction* left,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001323 HInstruction* right) : HExpression(result_type, SideEffects::None()) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001324 SetRawInputAt(0, left);
1325 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001326 }
1327
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001328 HInstruction* GetLeft() const { return InputAt(0); }
1329 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07001330 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001331
1332 virtual bool IsCommutative() { return false; }
1333
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001334 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001335 virtual bool InstructionDataEquals(HInstruction* other) const {
1336 UNUSED(other);
1337 return true;
1338 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001339
Roland Levillain9240d6a2014-10-20 16:47:04 +01001340 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01001341 // containing the result of this evaluation. If `operation` cannot
1342 // be evaluated as a constant, return nullptr.
Roland Levillain9240d6a2014-10-20 16:47:04 +01001343 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01001344
1345 // Apply this operation to `x` and `y`.
1346 virtual int32_t Evaluate(int32_t x, int32_t y) const = 0;
1347 virtual int64_t Evaluate(int64_t x, int64_t y) const = 0;
1348
Roland Levillainccc07a92014-09-16 14:48:16 +01001349 DECLARE_INSTRUCTION(BinaryOperation);
1350
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001351 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001352 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
1353};
1354
Dave Allison20dfc792014-06-16 20:44:29 -07001355class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001356 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001357 HCondition(HInstruction* first, HInstruction* second)
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001358 : HBinaryOperation(Primitive::kPrimBoolean, first, second),
1359 needs_materialization_(true) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001360
1361 virtual bool IsCommutative() { return true; }
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001362
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001363 bool NeedsMaterialization() const { return needs_materialization_; }
1364 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001365
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001366 // For code generation purposes, returns whether this instruction is just before
1367 // `if_`, and disregard moves in between.
1368 bool IsBeforeWhenDisregardMoves(HIf* if_) const;
1369
Dave Allison20dfc792014-06-16 20:44:29 -07001370 DECLARE_INSTRUCTION(Condition);
1371
1372 virtual IfCondition GetCondition() const = 0;
1373
1374 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001375 // For register allocation purposes, returns whether this instruction needs to be
1376 // materialized (that is, not just be in the processor flags).
1377 bool needs_materialization_;
1378
Dave Allison20dfc792014-06-16 20:44:29 -07001379 DISALLOW_COPY_AND_ASSIGN(HCondition);
1380};
1381
1382// Instruction to check if two inputs are equal to each other.
1383class HEqual : public HCondition {
1384 public:
1385 HEqual(HInstruction* first, HInstruction* second)
1386 : HCondition(first, second) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001387
Roland Levillain93445682014-10-06 19:24:02 +01001388 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1389 return x == y ? 1 : 0;
1390 }
1391 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1392 return x == y ? 1 : 0;
1393 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001394
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001395 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001396
Dave Allison20dfc792014-06-16 20:44:29 -07001397 virtual IfCondition GetCondition() const {
1398 return kCondEQ;
1399 }
1400
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001401 private:
1402 DISALLOW_COPY_AND_ASSIGN(HEqual);
1403};
1404
Dave Allison20dfc792014-06-16 20:44:29 -07001405class HNotEqual : public HCondition {
1406 public:
1407 HNotEqual(HInstruction* first, HInstruction* second)
1408 : HCondition(first, second) {}
1409
Roland Levillain93445682014-10-06 19:24:02 +01001410 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1411 return x != y ? 1 : 0;
1412 }
1413 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1414 return x != y ? 1 : 0;
1415 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001416
Dave Allison20dfc792014-06-16 20:44:29 -07001417 DECLARE_INSTRUCTION(NotEqual);
1418
1419 virtual IfCondition GetCondition() const {
1420 return kCondNE;
1421 }
1422
1423 private:
1424 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
1425};
1426
1427class HLessThan : public HCondition {
1428 public:
1429 HLessThan(HInstruction* first, HInstruction* second)
1430 : HCondition(first, second) {}
1431
Roland Levillain93445682014-10-06 19:24:02 +01001432 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1433 return x < y ? 1 : 0;
1434 }
1435 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1436 return x < y ? 1 : 0;
1437 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001438
Dave Allison20dfc792014-06-16 20:44:29 -07001439 DECLARE_INSTRUCTION(LessThan);
1440
1441 virtual IfCondition GetCondition() const {
1442 return kCondLT;
1443 }
1444
1445 private:
1446 DISALLOW_COPY_AND_ASSIGN(HLessThan);
1447};
1448
1449class HLessThanOrEqual : public HCondition {
1450 public:
1451 HLessThanOrEqual(HInstruction* first, HInstruction* second)
1452 : HCondition(first, second) {}
1453
Roland Levillain93445682014-10-06 19:24:02 +01001454 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1455 return x <= y ? 1 : 0;
1456 }
1457 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1458 return x <= y ? 1 : 0;
1459 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001460
Dave Allison20dfc792014-06-16 20:44:29 -07001461 DECLARE_INSTRUCTION(LessThanOrEqual);
1462
1463 virtual IfCondition GetCondition() const {
1464 return kCondLE;
1465 }
1466
1467 private:
1468 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
1469};
1470
1471class HGreaterThan : public HCondition {
1472 public:
1473 HGreaterThan(HInstruction* first, HInstruction* second)
1474 : HCondition(first, second) {}
1475
Roland Levillain93445682014-10-06 19:24:02 +01001476 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1477 return x > y ? 1 : 0;
1478 }
1479 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1480 return x > y ? 1 : 0;
1481 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001482
Dave Allison20dfc792014-06-16 20:44:29 -07001483 DECLARE_INSTRUCTION(GreaterThan);
1484
1485 virtual IfCondition GetCondition() const {
1486 return kCondGT;
1487 }
1488
1489 private:
1490 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
1491};
1492
1493class HGreaterThanOrEqual : public HCondition {
1494 public:
1495 HGreaterThanOrEqual(HInstruction* first, HInstruction* second)
1496 : HCondition(first, second) {}
1497
Roland Levillain93445682014-10-06 19:24:02 +01001498 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1499 return x >= y ? 1 : 0;
1500 }
1501 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1502 return x >= y ? 1 : 0;
1503 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001504
Dave Allison20dfc792014-06-16 20:44:29 -07001505 DECLARE_INSTRUCTION(GreaterThanOrEqual);
1506
1507 virtual IfCondition GetCondition() const {
1508 return kCondGE;
1509 }
1510
1511 private:
1512 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
1513};
1514
1515
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001516// Instruction to check how two inputs compare to each other.
1517// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
1518class HCompare : public HBinaryOperation {
1519 public:
Calin Juravleddb7df22014-11-25 20:56:51 +00001520 // The bias applies for floating point operations and indicates how NaN
1521 // comparisons are treated:
1522 enum Bias {
1523 kNoBias, // bias is not applicable (i.e. for long operation)
1524 kGtBias, // return 1 for NaN comparisons
1525 kLtBias, // return -1 for NaN comparisons
1526 };
1527
1528 HCompare(Primitive::Type type, HInstruction* first, HInstruction* second, Bias bias)
1529 : HBinaryOperation(Primitive::kPrimInt, first, second), bias_(bias) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001530 DCHECK_EQ(type, first->GetType());
1531 DCHECK_EQ(type, second->GetType());
1532 }
1533
Calin Juravleddb7df22014-11-25 20:56:51 +00001534 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01001535 return
1536 x == y ? 0 :
1537 x > y ? 1 :
1538 -1;
1539 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001540
1541 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01001542 return
1543 x == y ? 0 :
1544 x > y ? 1 :
1545 -1;
1546 }
1547
Calin Juravleddb7df22014-11-25 20:56:51 +00001548 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
1549 return bias_ == other->AsCompare()->bias_;
1550 }
1551
1552 bool IsGtBias() { return bias_ == kGtBias; }
1553
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001554 DECLARE_INSTRUCTION(Compare);
1555
1556 private:
Calin Juravleddb7df22014-11-25 20:56:51 +00001557 const Bias bias_;
1558
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001559 DISALLOW_COPY_AND_ASSIGN(HCompare);
1560};
1561
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001562// A local in the graph. Corresponds to a Dex register.
1563class HLocal : public HTemplateInstruction<0> {
1564 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001565 explicit HLocal(uint16_t reg_number)
1566 : HTemplateInstruction(SideEffects::None()), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001567
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001568 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001569
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001570 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001571
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001572 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001573 // The Dex register number.
1574 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001575
1576 DISALLOW_COPY_AND_ASSIGN(HLocal);
1577};
1578
1579// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07001580class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001581 public:
Roland Levillain5799fc02014-09-25 12:15:20 +01001582 HLoadLocal(HLocal* local, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001583 : HExpression(type, SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001584 SetRawInputAt(0, local);
1585 }
1586
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001587 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1588
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001589 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001590
1591 private:
1592 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
1593};
1594
1595// Store a value in a given local. This instruction has two inputs: the value
1596// and the local.
1597class HStoreLocal : public HTemplateInstruction<2> {
1598 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001599 HStoreLocal(HLocal* local, HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001600 SetRawInputAt(0, local);
1601 SetRawInputAt(1, value);
1602 }
1603
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001604 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1605
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001606 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001607
1608 private:
1609 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
1610};
1611
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001612class HConstant : public HExpression<0> {
1613 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001614 explicit HConstant(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
1615
1616 virtual bool CanBeMoved() const { return true; }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001617
1618 DECLARE_INSTRUCTION(Constant);
1619
1620 private:
1621 DISALLOW_COPY_AND_ASSIGN(HConstant);
1622};
1623
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001624class HFloatConstant : public HConstant {
1625 public:
1626 explicit HFloatConstant(float value) : HConstant(Primitive::kPrimFloat), value_(value) {}
1627
1628 float GetValue() const { return value_; }
1629
1630 virtual bool InstructionDataEquals(HInstruction* other) const {
1631 return bit_cast<float, int32_t>(other->AsFloatConstant()->value_) ==
1632 bit_cast<float, int32_t>(value_);
1633 }
1634
1635 virtual size_t ComputeHashCode() const { return static_cast<size_t>(GetValue()); }
1636
1637 DECLARE_INSTRUCTION(FloatConstant);
1638
1639 private:
1640 const float value_;
1641
1642 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
1643};
1644
1645class HDoubleConstant : public HConstant {
1646 public:
1647 explicit HDoubleConstant(double value) : HConstant(Primitive::kPrimDouble), value_(value) {}
1648
1649 double GetValue() const { return value_; }
1650
1651 virtual bool InstructionDataEquals(HInstruction* other) const {
1652 return bit_cast<double, int64_t>(other->AsDoubleConstant()->value_) ==
1653 bit_cast<double, int64_t>(value_);
1654 }
1655
1656 virtual size_t ComputeHashCode() const { return static_cast<size_t>(GetValue()); }
1657
1658 DECLARE_INSTRUCTION(DoubleConstant);
1659
1660 private:
1661 const double value_;
1662
1663 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
1664};
1665
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001666// Constants of the type int. Those can be from Dex instructions, or
1667// synthesized (for example with the if-eqz instruction).
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001668class HIntConstant : public HConstant {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001669 public:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001670 explicit HIntConstant(int32_t value) : HConstant(Primitive::kPrimInt), value_(value) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001671
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001672 int32_t GetValue() const { return value_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001673
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001674 virtual bool InstructionDataEquals(HInstruction* other) const {
1675 return other->AsIntConstant()->value_ == value_;
1676 }
1677
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001678 virtual size_t ComputeHashCode() const { return GetValue(); }
1679
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001680 DECLARE_INSTRUCTION(IntConstant);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001681
1682 private:
1683 const int32_t value_;
1684
1685 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
1686};
1687
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001688class HLongConstant : public HConstant {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001689 public:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001690 explicit HLongConstant(int64_t value) : HConstant(Primitive::kPrimLong), value_(value) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001691
1692 int64_t GetValue() const { return value_; }
1693
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001694 virtual bool InstructionDataEquals(HInstruction* other) const {
1695 return other->AsLongConstant()->value_ == value_;
1696 }
1697
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001698 virtual size_t ComputeHashCode() const { return static_cast<size_t>(GetValue()); }
1699
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001700 DECLARE_INSTRUCTION(LongConstant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001701
1702 private:
1703 const int64_t value_;
1704
1705 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
1706};
1707
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001708enum class Intrinsics {
1709#define OPTIMIZING_INTRINSICS(Name, IsStatic) k ## Name,
1710#include "intrinsics_list.h"
1711 kNone,
1712 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
1713#undef INTRINSICS_LIST
1714#undef OPTIMIZING_INTRINSICS
1715};
1716std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
1717
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001718class HInvoke : public HInstruction {
1719 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001720 virtual size_t InputCount() const { return inputs_.Size(); }
1721 virtual HInstruction* InputAt(size_t i) const { return inputs_.Get(i); }
1722
1723 // Runtime needs to walk the stack, so Dex -> Dex calls need to
1724 // know their environment.
Calin Juravle77520bc2015-01-12 18:45:46 +00001725 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001726
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001727 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001728 SetRawInputAt(index, argument);
1729 }
1730
1731 virtual void SetRawInputAt(size_t index, HInstruction* input) {
1732 inputs_.Put(index, input);
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001733 }
1734
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001735 virtual Primitive::Type GetType() const { return return_type_; }
1736
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001737 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001738
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001739 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
1740
1741 Intrinsics GetIntrinsic() {
1742 return intrinsic_;
1743 }
1744
1745 void SetIntrinsic(Intrinsics intrinsic) {
1746 intrinsic_ = intrinsic;
1747 }
1748
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001749 DECLARE_INSTRUCTION(Invoke);
1750
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001751 protected:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001752 HInvoke(ArenaAllocator* arena,
1753 uint32_t number_of_arguments,
1754 Primitive::Type return_type,
1755 uint32_t dex_pc,
1756 uint32_t dex_method_index)
1757 : HInstruction(SideEffects::All()),
1758 inputs_(arena, number_of_arguments),
1759 return_type_(return_type),
1760 dex_pc_(dex_pc),
1761 dex_method_index_(dex_method_index),
1762 intrinsic_(Intrinsics::kNone) {
1763 inputs_.SetSize(number_of_arguments);
1764 }
1765
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001766 GrowableArray<HInstruction*> inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001767 const Primitive::Type return_type_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001768 const uint32_t dex_pc_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001769 const uint32_t dex_method_index_;
1770 Intrinsics intrinsic_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001771
1772 private:
1773 DISALLOW_COPY_AND_ASSIGN(HInvoke);
1774};
1775
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001776class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001777 public:
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001778 HInvokeStaticOrDirect(ArenaAllocator* arena,
1779 uint32_t number_of_arguments,
1780 Primitive::Type return_type,
1781 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001782 uint32_t dex_method_index,
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001783 bool is_recursive,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001784 InvokeType invoke_type)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001785 : HInvoke(arena, number_of_arguments, return_type, dex_pc, dex_method_index),
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001786 invoke_type_(invoke_type),
1787 is_recursive_(is_recursive) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001788
Calin Juravle77520bc2015-01-12 18:45:46 +00001789 bool CanDoImplicitNullCheck() const OVERRIDE {
1790 // We access the method via the dex cache so we can't do an implicit null check.
1791 // TODO: for intrinsics we can generate implicit null checks.
1792 return false;
1793 }
1794
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001795 InvokeType GetInvokeType() const { return invoke_type_; }
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001796 bool IsRecursive() const { return is_recursive_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001797
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001798 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001799
1800 private:
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001801 const InvokeType invoke_type_;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001802 const bool is_recursive_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001803
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001804 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001805};
1806
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001807class HInvokeVirtual : public HInvoke {
1808 public:
1809 HInvokeVirtual(ArenaAllocator* arena,
1810 uint32_t number_of_arguments,
1811 Primitive::Type return_type,
1812 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001813 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001814 uint32_t vtable_index)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001815 : HInvoke(arena, number_of_arguments, return_type, dex_pc, dex_method_index),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001816 vtable_index_(vtable_index) {}
1817
Calin Juravle77520bc2015-01-12 18:45:46 +00001818 bool CanDoImplicitNullCheck() const OVERRIDE {
1819 // TODO: Add implicit null checks in intrinsics.
1820 return !GetLocations()->Intrinsified();
1821 }
1822
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001823 uint32_t GetVTableIndex() const { return vtable_index_; }
1824
1825 DECLARE_INSTRUCTION(InvokeVirtual);
1826
1827 private:
1828 const uint32_t vtable_index_;
1829
1830 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
1831};
1832
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001833class HInvokeInterface : public HInvoke {
1834 public:
1835 HInvokeInterface(ArenaAllocator* arena,
1836 uint32_t number_of_arguments,
1837 Primitive::Type return_type,
1838 uint32_t dex_pc,
1839 uint32_t dex_method_index,
1840 uint32_t imt_index)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001841 : HInvoke(arena, number_of_arguments, return_type, dex_pc, dex_method_index),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001842 imt_index_(imt_index) {}
1843
Calin Juravle77520bc2015-01-12 18:45:46 +00001844 bool CanDoImplicitNullCheck() const OVERRIDE {
1845 // TODO: Add implicit null checks in intrinsics.
1846 return !GetLocations()->Intrinsified();
1847 }
1848
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001849 uint32_t GetImtIndex() const { return imt_index_; }
1850 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
1851
1852 DECLARE_INSTRUCTION(InvokeInterface);
1853
1854 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001855 const uint32_t imt_index_;
1856
1857 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
1858};
1859
Dave Allison20dfc792014-06-16 20:44:29 -07001860class HNewInstance : public HExpression<0> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001861 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00001862 HNewInstance(uint32_t dex_pc, uint16_t type_index, QuickEntrypointEnum entrypoint)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001863 : HExpression(Primitive::kPrimNot, SideEffects::None()),
1864 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00001865 type_index_(type_index),
1866 entrypoint_(entrypoint) {}
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001867
1868 uint32_t GetDexPc() const { return dex_pc_; }
1869 uint16_t GetTypeIndex() const { return type_index_; }
1870
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001871 // Calls runtime so needs an environment.
Calin Juravle92a6ed22014-12-02 18:58:03 +00001872 bool NeedsEnvironment() const OVERRIDE { return true; }
1873 // It may throw when called on:
1874 // - interfaces
1875 // - abstract/innaccessible/unknown classes
1876 // TODO: optimize when possible.
1877 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001878
Calin Juravle10e244f2015-01-26 18:54:32 +00001879 bool CanBeNull() const OVERRIDE { return false; }
1880
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00001881 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
1882
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001883 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001884
1885 private:
1886 const uint32_t dex_pc_;
1887 const uint16_t type_index_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00001888 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001889
1890 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
1891};
1892
Roland Levillain88cb1752014-10-20 16:36:47 +01001893class HNeg : public HUnaryOperation {
1894 public:
1895 explicit HNeg(Primitive::Type result_type, HInstruction* input)
1896 : HUnaryOperation(result_type, input) {}
1897
Roland Levillain9240d6a2014-10-20 16:47:04 +01001898 virtual int32_t Evaluate(int32_t x) const OVERRIDE { return -x; }
1899 virtual int64_t Evaluate(int64_t x) const OVERRIDE { return -x; }
1900
Roland Levillain88cb1752014-10-20 16:36:47 +01001901 DECLARE_INSTRUCTION(Neg);
1902
1903 private:
1904 DISALLOW_COPY_AND_ASSIGN(HNeg);
1905};
1906
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001907class HNewArray : public HExpression<1> {
1908 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00001909 HNewArray(HInstruction* length,
1910 uint32_t dex_pc,
1911 uint16_t type_index,
1912 QuickEntrypointEnum entrypoint)
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001913 : HExpression(Primitive::kPrimNot, SideEffects::None()),
1914 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00001915 type_index_(type_index),
1916 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001917 SetRawInputAt(0, length);
1918 }
1919
1920 uint32_t GetDexPc() const { return dex_pc_; }
1921 uint16_t GetTypeIndex() const { return type_index_; }
1922
1923 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00001924 bool NeedsEnvironment() const OVERRIDE { return true; }
1925
1926 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001927
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00001928 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
1929
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001930 DECLARE_INSTRUCTION(NewArray);
1931
1932 private:
1933 const uint32_t dex_pc_;
1934 const uint16_t type_index_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00001935 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001936
1937 DISALLOW_COPY_AND_ASSIGN(HNewArray);
1938};
1939
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001940class HAdd : public HBinaryOperation {
1941 public:
1942 HAdd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1943 : HBinaryOperation(result_type, left, right) {}
1944
1945 virtual bool IsCommutative() { return true; }
1946
Roland Levillain93445682014-10-06 19:24:02 +01001947 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1948 return x + y;
1949 }
1950 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1951 return x + y;
1952 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001953
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001954 DECLARE_INSTRUCTION(Add);
1955
1956 private:
1957 DISALLOW_COPY_AND_ASSIGN(HAdd);
1958};
1959
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001960class HSub : public HBinaryOperation {
1961 public:
1962 HSub(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1963 : HBinaryOperation(result_type, left, right) {}
1964
Roland Levillain93445682014-10-06 19:24:02 +01001965 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1966 return x - y;
1967 }
1968 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1969 return x - y;
1970 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001971
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001972 DECLARE_INSTRUCTION(Sub);
1973
1974 private:
1975 DISALLOW_COPY_AND_ASSIGN(HSub);
1976};
1977
Calin Juravle34bacdf2014-10-07 20:23:36 +01001978class HMul : public HBinaryOperation {
1979 public:
1980 HMul(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1981 : HBinaryOperation(result_type, left, right) {}
1982
1983 virtual bool IsCommutative() { return true; }
1984
1985 virtual int32_t Evaluate(int32_t x, int32_t y) const { return x * y; }
1986 virtual int64_t Evaluate(int64_t x, int64_t y) const { return x * y; }
1987
1988 DECLARE_INSTRUCTION(Mul);
1989
1990 private:
1991 DISALLOW_COPY_AND_ASSIGN(HMul);
1992};
1993
Calin Juravle7c4954d2014-10-28 16:57:40 +00001994class HDiv : public HBinaryOperation {
1995 public:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001996 HDiv(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
1997 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00001998
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00001999 virtual int32_t Evaluate(int32_t x, int32_t y) const {
2000 // Our graph structure ensures we never have 0 for `y` during constant folding.
2001 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00002002 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00002003 return (y == -1) ? -x : x / y;
2004 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002005
2006 virtual int64_t Evaluate(int64_t x, int64_t y) const {
2007 DCHECK_NE(y, 0);
2008 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2009 return (y == -1) ? -x : x / y;
2010 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002011
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002012 uint32_t GetDexPc() const { return dex_pc_; }
2013
Calin Juravle7c4954d2014-10-28 16:57:40 +00002014 DECLARE_INSTRUCTION(Div);
2015
2016 private:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002017 const uint32_t dex_pc_;
2018
Calin Juravle7c4954d2014-10-28 16:57:40 +00002019 DISALLOW_COPY_AND_ASSIGN(HDiv);
2020};
2021
Calin Juravlebacfec32014-11-14 15:54:36 +00002022class HRem : public HBinaryOperation {
2023 public:
2024 HRem(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
2025 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
2026
2027 virtual int32_t Evaluate(int32_t x, int32_t y) const {
2028 DCHECK_NE(y, 0);
2029 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2030 return (y == -1) ? 0 : x % y;
2031 }
2032
2033 virtual int64_t Evaluate(int64_t x, int64_t y) const {
2034 DCHECK_NE(y, 0);
2035 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2036 return (y == -1) ? 0 : x % y;
2037 }
2038
2039 uint32_t GetDexPc() const { return dex_pc_; }
2040
2041 DECLARE_INSTRUCTION(Rem);
2042
2043 private:
2044 const uint32_t dex_pc_;
2045
2046 DISALLOW_COPY_AND_ASSIGN(HRem);
2047};
2048
Calin Juravled0d48522014-11-04 16:40:20 +00002049class HDivZeroCheck : public HExpression<1> {
2050 public:
2051 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
2052 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
2053 SetRawInputAt(0, value);
2054 }
2055
2056 bool CanBeMoved() const OVERRIDE { return true; }
2057
2058 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2059 UNUSED(other);
2060 return true;
2061 }
2062
2063 bool NeedsEnvironment() const OVERRIDE { return true; }
2064 bool CanThrow() const OVERRIDE { return true; }
2065
2066 uint32_t GetDexPc() const { return dex_pc_; }
2067
2068 DECLARE_INSTRUCTION(DivZeroCheck);
2069
2070 private:
2071 const uint32_t dex_pc_;
2072
2073 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
2074};
2075
Calin Juravle9aec02f2014-11-18 23:06:35 +00002076class HShl : public HBinaryOperation {
2077 public:
2078 HShl(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2079 : HBinaryOperation(result_type, left, right) {}
2080
2081 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x << (y & kMaxIntShiftValue); }
2082 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x << (y & kMaxLongShiftValue); }
2083
2084 DECLARE_INSTRUCTION(Shl);
2085
2086 private:
2087 DISALLOW_COPY_AND_ASSIGN(HShl);
2088};
2089
2090class HShr : public HBinaryOperation {
2091 public:
2092 HShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2093 : HBinaryOperation(result_type, left, right) {}
2094
2095 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x >> (y & kMaxIntShiftValue); }
2096 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x >> (y & kMaxLongShiftValue); }
2097
2098 DECLARE_INSTRUCTION(Shr);
2099
2100 private:
2101 DISALLOW_COPY_AND_ASSIGN(HShr);
2102};
2103
2104class HUShr : public HBinaryOperation {
2105 public:
2106 HUShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2107 : HBinaryOperation(result_type, left, right) {}
2108
2109 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
2110 uint32_t ux = static_cast<uint32_t>(x);
2111 uint32_t uy = static_cast<uint32_t>(y) & kMaxIntShiftValue;
2112 return static_cast<int32_t>(ux >> uy);
2113 }
2114
2115 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
2116 uint64_t ux = static_cast<uint64_t>(x);
2117 uint64_t uy = static_cast<uint64_t>(y) & kMaxLongShiftValue;
2118 return static_cast<int64_t>(ux >> uy);
2119 }
2120
2121 DECLARE_INSTRUCTION(UShr);
2122
2123 private:
2124 DISALLOW_COPY_AND_ASSIGN(HUShr);
2125};
2126
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002127class HAnd : public HBinaryOperation {
2128 public:
2129 HAnd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2130 : HBinaryOperation(result_type, left, right) {}
2131
2132 bool IsCommutative() OVERRIDE { return true; }
2133
2134 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x & y; }
2135 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x & y; }
2136
2137 DECLARE_INSTRUCTION(And);
2138
2139 private:
2140 DISALLOW_COPY_AND_ASSIGN(HAnd);
2141};
2142
2143class HOr : public HBinaryOperation {
2144 public:
2145 HOr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2146 : HBinaryOperation(result_type, left, right) {}
2147
2148 bool IsCommutative() OVERRIDE { return true; }
2149
2150 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x | y; }
2151 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x | y; }
2152
2153 DECLARE_INSTRUCTION(Or);
2154
2155 private:
2156 DISALLOW_COPY_AND_ASSIGN(HOr);
2157};
2158
2159class HXor : public HBinaryOperation {
2160 public:
2161 HXor(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2162 : HBinaryOperation(result_type, left, right) {}
2163
2164 bool IsCommutative() OVERRIDE { return true; }
2165
2166 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x ^ y; }
2167 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x ^ y; }
2168
2169 DECLARE_INSTRUCTION(Xor);
2170
2171 private:
2172 DISALLOW_COPY_AND_ASSIGN(HXor);
2173};
2174
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002175// The value of a parameter in this method. Its location depends on
2176// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07002177class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002178 public:
Calin Juravle10e244f2015-01-26 18:54:32 +00002179 HParameterValue(uint8_t index, Primitive::Type parameter_type, bool is_this = false)
2180 : HExpression(parameter_type, SideEffects::None()), index_(index), is_this_(is_this) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002181
2182 uint8_t GetIndex() const { return index_; }
2183
Calin Juravle10e244f2015-01-26 18:54:32 +00002184 bool CanBeNull() const OVERRIDE { return !is_this_; }
2185
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002186 DECLARE_INSTRUCTION(ParameterValue);
2187
2188 private:
2189 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00002190 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002191 const uint8_t index_;
2192
Calin Juravle10e244f2015-01-26 18:54:32 +00002193 // Whether or not the parameter value corresponds to 'this' argument.
2194 const bool is_this_;
2195
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002196 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
2197};
2198
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002199class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002200 public:
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002201 explicit HNot(Primitive::Type result_type, HInstruction* input)
2202 : HUnaryOperation(result_type, input) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002203
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002204 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002205 virtual bool InstructionDataEquals(HInstruction* other) const {
2206 UNUSED(other);
2207 return true;
2208 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002209
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002210 virtual int32_t Evaluate(int32_t x) const OVERRIDE { return ~x; }
2211 virtual int64_t Evaluate(int64_t x) const OVERRIDE { return ~x; }
2212
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002213 DECLARE_INSTRUCTION(Not);
2214
2215 private:
2216 DISALLOW_COPY_AND_ASSIGN(HNot);
2217};
2218
Roland Levillaindff1f282014-11-05 14:15:05 +00002219class HTypeConversion : public HExpression<1> {
2220 public:
2221 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00002222 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
2223 : HExpression(result_type, SideEffects::None()), dex_pc_(dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002224 SetRawInputAt(0, input);
2225 DCHECK_NE(input->GetType(), result_type);
2226 }
2227
2228 HInstruction* GetInput() const { return InputAt(0); }
2229 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
2230 Primitive::Type GetResultType() const { return GetType(); }
2231
Roland Levillain624279f2014-12-04 11:54:28 +00002232 // Required by the x86 and ARM code generators when producing calls
2233 // to the runtime.
2234 uint32_t GetDexPc() const { return dex_pc_; }
2235
Roland Levillaindff1f282014-11-05 14:15:05 +00002236 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00002237 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00002238
2239 DECLARE_INSTRUCTION(TypeConversion);
2240
2241 private:
Roland Levillain624279f2014-12-04 11:54:28 +00002242 const uint32_t dex_pc_;
2243
Roland Levillaindff1f282014-11-05 14:15:05 +00002244 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
2245};
2246
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00002247static constexpr uint32_t kNoRegNumber = -1;
2248
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002249class HPhi : public HInstruction {
2250 public:
2251 HPhi(ArenaAllocator* arena, uint32_t reg_number, size_t number_of_inputs, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002252 : HInstruction(SideEffects::None()),
2253 inputs_(arena, number_of_inputs),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002254 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002255 type_(type),
Calin Juravle10e244f2015-01-26 18:54:32 +00002256 is_live_(false),
2257 can_be_null_(true) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002258 inputs_.SetSize(number_of_inputs);
2259 }
2260
Calin Juravle10e244f2015-01-26 18:54:32 +00002261 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
2262 HInstruction* InputAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002263
Calin Juravle10e244f2015-01-26 18:54:32 +00002264 void SetRawInputAt(size_t index, HInstruction* input) OVERRIDE {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002265 inputs_.Put(index, input);
2266 }
2267
2268 void AddInput(HInstruction* input);
2269
Calin Juravle10e244f2015-01-26 18:54:32 +00002270 Primitive::Type GetType() const OVERRIDE { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002271 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002272
Calin Juravle10e244f2015-01-26 18:54:32 +00002273 bool CanBeNull() const OVERRIDE { return can_be_null_; }
2274 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
2275
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002276 uint32_t GetRegNumber() const { return reg_number_; }
2277
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002278 void SetDead() { is_live_ = false; }
2279 void SetLive() { is_live_ = true; }
2280 bool IsDead() const { return !is_live_; }
2281 bool IsLive() const { return is_live_; }
2282
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002283 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002284
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002285 private:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002286 GrowableArray<HInstruction*> inputs_;
2287 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002288 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002289 bool is_live_;
Calin Juravle10e244f2015-01-26 18:54:32 +00002290 bool can_be_null_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002291
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002292 DISALLOW_COPY_AND_ASSIGN(HPhi);
2293};
2294
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002295class HNullCheck : public HExpression<1> {
2296 public:
2297 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002298 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002299 SetRawInputAt(0, value);
2300 }
2301
Calin Juravle10e244f2015-01-26 18:54:32 +00002302 bool CanBeMoved() const OVERRIDE { return true; }
2303 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002304 UNUSED(other);
2305 return true;
2306 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002307
Calin Juravle10e244f2015-01-26 18:54:32 +00002308 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002309
Calin Juravle10e244f2015-01-26 18:54:32 +00002310 bool CanThrow() const OVERRIDE { return true; }
2311
2312 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01002313
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002314 uint32_t GetDexPc() const { return dex_pc_; }
2315
2316 DECLARE_INSTRUCTION(NullCheck);
2317
2318 private:
2319 const uint32_t dex_pc_;
2320
2321 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
2322};
2323
2324class FieldInfo : public ValueObject {
2325 public:
Calin Juravle52c48962014-12-16 17:02:57 +00002326 FieldInfo(MemberOffset field_offset, Primitive::Type field_type, bool is_volatile)
2327 : field_offset_(field_offset), field_type_(field_type), is_volatile_(is_volatile) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002328
2329 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002330 Primitive::Type GetFieldType() const { return field_type_; }
Calin Juravle52c48962014-12-16 17:02:57 +00002331 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002332
2333 private:
2334 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01002335 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00002336 const bool is_volatile_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002337};
2338
2339class HInstanceFieldGet : public HExpression<1> {
2340 public:
2341 HInstanceFieldGet(HInstruction* value,
2342 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00002343 MemberOffset field_offset,
2344 bool is_volatile)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002345 : HExpression(field_type, SideEffects::DependsOnSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00002346 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002347 SetRawInputAt(0, value);
2348 }
2349
Calin Juravle10c9cbe2014-12-19 10:50:19 +00002350 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002351
2352 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2353 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
2354 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002355 }
2356
Calin Juravle77520bc2015-01-12 18:45:46 +00002357 bool CanDoImplicitNullCheck() const OVERRIDE {
2358 return GetFieldOffset().Uint32Value() < kPageSize;
2359 }
2360
2361 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002362 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
2363 }
2364
Calin Juravle52c48962014-12-16 17:02:57 +00002365 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002366 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002367 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002368 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002369
2370 DECLARE_INSTRUCTION(InstanceFieldGet);
2371
2372 private:
2373 const FieldInfo field_info_;
2374
2375 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
2376};
2377
2378class HInstanceFieldSet : public HTemplateInstruction<2> {
2379 public:
2380 HInstanceFieldSet(HInstruction* object,
2381 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01002382 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00002383 MemberOffset field_offset,
2384 bool is_volatile)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002385 : HTemplateInstruction(SideEffects::ChangesSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00002386 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002387 SetRawInputAt(0, object);
2388 SetRawInputAt(1, value);
2389 }
2390
Calin Juravle77520bc2015-01-12 18:45:46 +00002391 bool CanDoImplicitNullCheck() const OVERRIDE {
2392 return GetFieldOffset().Uint32Value() < kPageSize;
2393 }
2394
Calin Juravle52c48962014-12-16 17:02:57 +00002395 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002396 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002397 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002398 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002399 HInstruction* GetValue() const { return InputAt(1); }
2400
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002401 DECLARE_INSTRUCTION(InstanceFieldSet);
2402
2403 private:
2404 const FieldInfo field_info_;
2405
2406 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
2407};
2408
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002409class HArrayGet : public HExpression<2> {
2410 public:
2411 HArrayGet(HInstruction* array, HInstruction* index, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002412 : HExpression(type, SideEffects::DependsOnSomething()) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002413 SetRawInputAt(0, array);
2414 SetRawInputAt(1, index);
2415 }
2416
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002417 bool CanBeMoved() const OVERRIDE { return true; }
2418 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002419 UNUSED(other);
2420 return true;
2421 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002422 bool CanDoImplicitNullCheck() const OVERRIDE {
2423 // TODO: We can be smarter here.
2424 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
2425 // which generates the implicit null check. There are cases when these can be removed
2426 // to produce better code. If we ever add optimizations to do so we should allow an
2427 // implicit check here (as long as the address falls in the first page).
2428 return false;
2429 }
2430
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002431 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002432
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002433 HInstruction* GetArray() const { return InputAt(0); }
2434 HInstruction* GetIndex() const { return InputAt(1); }
2435
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002436 DECLARE_INSTRUCTION(ArrayGet);
2437
2438 private:
2439 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
2440};
2441
2442class HArraySet : public HTemplateInstruction<3> {
2443 public:
2444 HArraySet(HInstruction* array,
2445 HInstruction* index,
2446 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002447 Primitive::Type expected_component_type,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002448 uint32_t dex_pc)
2449 : HTemplateInstruction(SideEffects::ChangesSomething()),
2450 dex_pc_(dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002451 expected_component_type_(expected_component_type),
2452 needs_type_check_(value->GetType() == Primitive::kPrimNot) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002453 SetRawInputAt(0, array);
2454 SetRawInputAt(1, index);
2455 SetRawInputAt(2, value);
2456 }
2457
Calin Juravle77520bc2015-01-12 18:45:46 +00002458 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002459 // We currently always call a runtime method to catch array store
2460 // exceptions.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002461 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002462 }
2463
Calin Juravle77520bc2015-01-12 18:45:46 +00002464 bool CanDoImplicitNullCheck() const OVERRIDE {
2465 // TODO: Same as for ArrayGet.
2466 return false;
2467 }
2468
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002469 void ClearNeedsTypeCheck() {
2470 needs_type_check_ = false;
2471 }
2472
2473 bool NeedsTypeCheck() const { return needs_type_check_; }
2474
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002475 uint32_t GetDexPc() const { return dex_pc_; }
2476
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002477 HInstruction* GetArray() const { return InputAt(0); }
2478 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002479 HInstruction* GetValue() const { return InputAt(2); }
2480
2481 Primitive::Type GetComponentType() const {
2482 // The Dex format does not type floating point index operations. Since the
2483 // `expected_component_type_` is set during building and can therefore not
2484 // be correct, we also check what is the value type. If it is a floating
2485 // point type, we must use that type.
2486 Primitive::Type value_type = GetValue()->GetType();
2487 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
2488 ? value_type
2489 : expected_component_type_;
2490 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002491
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002492 DECLARE_INSTRUCTION(ArraySet);
2493
2494 private:
2495 const uint32_t dex_pc_;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002496 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002497 bool needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002498
2499 DISALLOW_COPY_AND_ASSIGN(HArraySet);
2500};
2501
2502class HArrayLength : public HExpression<1> {
2503 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002504 explicit HArrayLength(HInstruction* array)
2505 : HExpression(Primitive::kPrimInt, SideEffects::None()) {
2506 // Note that arrays do not change length, so the instruction does not
2507 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002508 SetRawInputAt(0, array);
2509 }
2510
Calin Juravle77520bc2015-01-12 18:45:46 +00002511 bool CanBeMoved() const OVERRIDE { return true; }
2512 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002513 UNUSED(other);
2514 return true;
2515 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002516 bool CanDoImplicitNullCheck() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002517
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002518 DECLARE_INSTRUCTION(ArrayLength);
2519
2520 private:
2521 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
2522};
2523
2524class HBoundsCheck : public HExpression<2> {
2525 public:
2526 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002527 : HExpression(index->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002528 DCHECK(index->GetType() == Primitive::kPrimInt);
2529 SetRawInputAt(0, index);
2530 SetRawInputAt(1, length);
2531 }
2532
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002533 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002534 virtual bool InstructionDataEquals(HInstruction* other) const {
2535 UNUSED(other);
2536 return true;
2537 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002538
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002539 virtual bool NeedsEnvironment() const { return true; }
2540
Roland Levillaine161a2a2014-10-03 12:45:18 +01002541 virtual bool CanThrow() const { return true; }
2542
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002543 uint32_t GetDexPc() const { return dex_pc_; }
2544
2545 DECLARE_INSTRUCTION(BoundsCheck);
2546
2547 private:
2548 const uint32_t dex_pc_;
2549
2550 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
2551};
2552
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002553/**
2554 * Some DEX instructions are folded into multiple HInstructions that need
2555 * to stay live until the last HInstruction. This class
2556 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00002557 * HInstruction stays live. `index` represents the stack location index of the
2558 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002559 */
2560class HTemporary : public HTemplateInstruction<0> {
2561 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002562 explicit HTemporary(size_t index) : HTemplateInstruction(SideEffects::None()), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002563
2564 size_t GetIndex() const { return index_; }
2565
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00002566 Primitive::Type GetType() const OVERRIDE {
2567 // The previous instruction is the one that will be stored in the temporary location.
2568 DCHECK(GetPrevious() != nullptr);
2569 return GetPrevious()->GetType();
2570 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00002571
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002572 DECLARE_INSTRUCTION(Temporary);
2573
2574 private:
2575 const size_t index_;
2576
2577 DISALLOW_COPY_AND_ASSIGN(HTemporary);
2578};
2579
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002580class HSuspendCheck : public HTemplateInstruction<0> {
2581 public:
2582 explicit HSuspendCheck(uint32_t dex_pc)
Nicolas Geoffray9ebc72c2014-09-25 16:33:42 +01002583 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002584
2585 virtual bool NeedsEnvironment() const {
2586 return true;
2587 }
2588
2589 uint32_t GetDexPc() const { return dex_pc_; }
2590
2591 DECLARE_INSTRUCTION(SuspendCheck);
2592
2593 private:
2594 const uint32_t dex_pc_;
2595
2596 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
2597};
2598
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002599/**
2600 * Instruction to load a Class object.
2601 */
2602class HLoadClass : public HExpression<0> {
2603 public:
2604 HLoadClass(uint16_t type_index,
2605 bool is_referrers_class,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002606 uint32_t dex_pc)
2607 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2608 type_index_(type_index),
2609 is_referrers_class_(is_referrers_class),
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002610 dex_pc_(dex_pc),
2611 generate_clinit_check_(false) {}
2612
2613 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002614
2615 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2616 return other->AsLoadClass()->type_index_ == type_index_;
2617 }
2618
2619 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
2620
2621 uint32_t GetDexPc() const { return dex_pc_; }
2622 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002623 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002624
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002625 bool NeedsEnvironment() const OVERRIDE {
2626 // Will call runtime and load the class if the class is not loaded yet.
2627 // TODO: finer grain decision.
2628 return !is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002629 }
2630
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002631 bool MustGenerateClinitCheck() const {
2632 return generate_clinit_check_;
2633 }
2634
2635 void SetMustGenerateClinitCheck() {
2636 generate_clinit_check_ = true;
2637 }
2638
2639 bool CanCallRuntime() const {
2640 return MustGenerateClinitCheck() || !is_referrers_class_;
2641 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002642
Nicolas Geoffray82091da2015-01-26 10:02:45 +00002643 bool CanThrow() const OVERRIDE {
2644 // May call runtime and and therefore can throw.
2645 // TODO: finer grain decision.
2646 return !is_referrers_class_;
2647 }
2648
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002649 DECLARE_INSTRUCTION(LoadClass);
2650
2651 private:
2652 const uint16_t type_index_;
2653 const bool is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002654 const uint32_t dex_pc_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002655 // Whether this instruction must generate the initialization check.
2656 // Used for code generation.
2657 bool generate_clinit_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002658
2659 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
2660};
2661
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002662class HLoadString : public HExpression<0> {
2663 public:
2664 HLoadString(uint32_t string_index, uint32_t dex_pc)
2665 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2666 string_index_(string_index),
2667 dex_pc_(dex_pc) {}
2668
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002669 bool CanBeMoved() const OVERRIDE { return true; }
2670
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002671 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2672 return other->AsLoadString()->string_index_ == string_index_;
2673 }
2674
2675 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
2676
2677 uint32_t GetDexPc() const { return dex_pc_; }
2678 uint32_t GetStringIndex() const { return string_index_; }
2679
2680 // TODO: Can we deopt or debug when we resolve a string?
2681 bool NeedsEnvironment() const OVERRIDE { return false; }
2682
2683 DECLARE_INSTRUCTION(LoadString);
2684
2685 private:
2686 const uint32_t string_index_;
2687 const uint32_t dex_pc_;
2688
2689 DISALLOW_COPY_AND_ASSIGN(HLoadString);
2690};
2691
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002692// TODO: Pass this check to HInvokeStaticOrDirect nodes.
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002693/**
2694 * Performs an initialization check on its Class object input.
2695 */
2696class HClinitCheck : public HExpression<1> {
2697 public:
2698 explicit HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
2699 : HExpression(Primitive::kPrimNot, SideEffects::All()),
2700 dex_pc_(dex_pc) {
2701 SetRawInputAt(0, constant);
2702 }
2703
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002704 bool CanBeMoved() const OVERRIDE { return true; }
2705 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2706 UNUSED(other);
2707 return true;
2708 }
2709
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002710 bool NeedsEnvironment() const OVERRIDE {
2711 // May call runtime to initialize the class.
2712 return true;
2713 }
2714
2715 uint32_t GetDexPc() const { return dex_pc_; }
2716
2717 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
2718
2719 DECLARE_INSTRUCTION(ClinitCheck);
2720
2721 private:
2722 const uint32_t dex_pc_;
2723
2724 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
2725};
2726
2727class HStaticFieldGet : public HExpression<1> {
2728 public:
2729 HStaticFieldGet(HInstruction* cls,
2730 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00002731 MemberOffset field_offset,
2732 bool is_volatile)
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002733 : HExpression(field_type, SideEffects::DependsOnSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00002734 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002735 SetRawInputAt(0, cls);
2736 }
2737
Calin Juravle52c48962014-12-16 17:02:57 +00002738
Calin Juravle10c9cbe2014-12-19 10:50:19 +00002739 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002740
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002741 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00002742 HStaticFieldGet* other_get = other->AsStaticFieldGet();
2743 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002744 }
2745
2746 size_t ComputeHashCode() const OVERRIDE {
2747 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
2748 }
2749
Calin Juravle52c48962014-12-16 17:02:57 +00002750 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002751 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
2752 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002753 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002754
2755 DECLARE_INSTRUCTION(StaticFieldGet);
2756
2757 private:
2758 const FieldInfo field_info_;
2759
2760 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
2761};
2762
2763class HStaticFieldSet : public HTemplateInstruction<2> {
2764 public:
2765 HStaticFieldSet(HInstruction* cls,
2766 HInstruction* value,
2767 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00002768 MemberOffset field_offset,
2769 bool is_volatile)
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002770 : HTemplateInstruction(SideEffects::ChangesSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00002771 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002772 SetRawInputAt(0, cls);
2773 SetRawInputAt(1, value);
2774 }
2775
Calin Juravle52c48962014-12-16 17:02:57 +00002776 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002777 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
2778 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002779 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002780
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002781 HInstruction* GetValue() const { return InputAt(1); }
2782
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002783 DECLARE_INSTRUCTION(StaticFieldSet);
2784
2785 private:
2786 const FieldInfo field_info_;
2787
2788 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
2789};
2790
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002791// Implement the move-exception DEX instruction.
2792class HLoadException : public HExpression<0> {
2793 public:
2794 HLoadException() : HExpression(Primitive::kPrimNot, SideEffects::None()) {}
2795
2796 DECLARE_INSTRUCTION(LoadException);
2797
2798 private:
2799 DISALLOW_COPY_AND_ASSIGN(HLoadException);
2800};
2801
2802class HThrow : public HTemplateInstruction<1> {
2803 public:
2804 HThrow(HInstruction* exception, uint32_t dex_pc)
2805 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {
2806 SetRawInputAt(0, exception);
2807 }
2808
2809 bool IsControlFlow() const OVERRIDE { return true; }
2810
2811 bool NeedsEnvironment() const OVERRIDE { return true; }
2812
Nicolas Geoffray82091da2015-01-26 10:02:45 +00002813 bool CanThrow() const OVERRIDE { return true; }
2814
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002815 uint32_t GetDexPc() const { return dex_pc_; }
2816
2817 DECLARE_INSTRUCTION(Throw);
2818
2819 private:
Nicolas Geoffray82091da2015-01-26 10:02:45 +00002820 const uint32_t dex_pc_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002821
2822 DISALLOW_COPY_AND_ASSIGN(HThrow);
2823};
2824
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002825class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002826 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002827 HInstanceOf(HInstruction* object,
2828 HLoadClass* constant,
2829 bool class_is_final,
2830 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002831 : HExpression(Primitive::kPrimBoolean, SideEffects::None()),
2832 class_is_final_(class_is_final),
2833 dex_pc_(dex_pc) {
2834 SetRawInputAt(0, object);
2835 SetRawInputAt(1, constant);
2836 }
2837
2838 bool CanBeMoved() const OVERRIDE { return true; }
2839
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002840 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002841 return true;
2842 }
2843
2844 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002845 return false;
2846 }
2847
2848 uint32_t GetDexPc() const { return dex_pc_; }
2849
2850 bool IsClassFinal() const { return class_is_final_; }
2851
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002852 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002853
2854 private:
2855 const bool class_is_final_;
2856 const uint32_t dex_pc_;
2857
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002858 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
2859};
2860
2861class HCheckCast : public HTemplateInstruction<2> {
2862 public:
2863 HCheckCast(HInstruction* object,
2864 HLoadClass* constant,
2865 bool class_is_final,
2866 uint32_t dex_pc)
2867 : HTemplateInstruction(SideEffects::None()),
2868 class_is_final_(class_is_final),
2869 dex_pc_(dex_pc) {
2870 SetRawInputAt(0, object);
2871 SetRawInputAt(1, constant);
2872 }
2873
2874 bool CanBeMoved() const OVERRIDE { return true; }
2875
2876 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2877 return true;
2878 }
2879
2880 bool NeedsEnvironment() const OVERRIDE {
2881 // Instruction may throw a CheckCastError.
2882 return true;
2883 }
2884
2885 bool CanThrow() const OVERRIDE { return true; }
2886
2887 uint32_t GetDexPc() const { return dex_pc_; }
2888
2889 bool IsClassFinal() const { return class_is_final_; }
2890
2891 DECLARE_INSTRUCTION(CheckCast);
2892
2893 private:
2894 const bool class_is_final_;
2895 const uint32_t dex_pc_;
2896
2897 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002898};
2899
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00002900class HMonitorOperation : public HTemplateInstruction<1> {
2901 public:
2902 enum OperationKind {
2903 kEnter,
2904 kExit,
2905 };
2906
2907 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
2908 : HTemplateInstruction(SideEffects::None()), kind_(kind), dex_pc_(dex_pc) {
2909 SetRawInputAt(0, object);
2910 }
2911
2912 // Instruction may throw a Java exception, so we need an environment.
2913 bool NeedsEnvironment() const OVERRIDE { return true; }
2914 bool CanThrow() const OVERRIDE { return true; }
2915
2916 uint32_t GetDexPc() const { return dex_pc_; }
2917
2918 bool IsEnter() const { return kind_ == kEnter; }
2919
2920 DECLARE_INSTRUCTION(MonitorOperation);
2921
Calin Juravle52c48962014-12-16 17:02:57 +00002922 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00002923 const OperationKind kind_;
2924 const uint32_t dex_pc_;
2925
2926 private:
2927 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
2928};
2929
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002930class MoveOperands : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002931 public:
Nicolas Geoffray740475d2014-09-29 10:33:25 +01002932 MoveOperands(Location source, Location destination, HInstruction* instruction)
2933 : source_(source), destination_(destination), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002934
2935 Location GetSource() const { return source_; }
2936 Location GetDestination() const { return destination_; }
2937
2938 void SetSource(Location value) { source_ = value; }
2939 void SetDestination(Location value) { destination_ = value; }
2940
2941 // The parallel move resolver marks moves as "in-progress" by clearing the
2942 // destination (but not the source).
2943 Location MarkPending() {
2944 DCHECK(!IsPending());
2945 Location dest = destination_;
2946 destination_ = Location::NoLocation();
2947 return dest;
2948 }
2949
2950 void ClearPending(Location dest) {
2951 DCHECK(IsPending());
2952 destination_ = dest;
2953 }
2954
2955 bool IsPending() const {
2956 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
2957 return destination_.IsInvalid() && !source_.IsInvalid();
2958 }
2959
2960 // True if this blocks a move from the given location.
2961 bool Blocks(Location loc) const {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00002962 return !IsEliminated() && (source_.Contains(loc) || loc.Contains(source_));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002963 }
2964
2965 // A move is redundant if it's been eliminated, if its source and
2966 // destination are the same, or if its destination is unneeded.
2967 bool IsRedundant() const {
2968 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
2969 }
2970
2971 // We clear both operands to indicate move that's been eliminated.
2972 void Eliminate() {
2973 source_ = destination_ = Location::NoLocation();
2974 }
2975
2976 bool IsEliminated() const {
2977 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
2978 return source_.IsInvalid();
2979 }
2980
Nicolas Geoffray740475d2014-09-29 10:33:25 +01002981 HInstruction* GetInstruction() const { return instruction_; }
2982
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002983 private:
2984 Location source_;
2985 Location destination_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01002986 // The instruction this move is assocatied with. Null when this move is
2987 // for moving an input in the expected locations of user (including a phi user).
2988 // This is only used in debug mode, to ensure we do not connect interval siblings
2989 // in the same parallel move.
2990 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002991};
2992
2993static constexpr size_t kDefaultNumberOfMoves = 4;
2994
2995class HParallelMove : public HTemplateInstruction<0> {
2996 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002997 explicit HParallelMove(ArenaAllocator* arena)
2998 : HTemplateInstruction(SideEffects::None()), moves_(arena, kDefaultNumberOfMoves) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002999
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003000 void AddMove(Location source, Location destination, HInstruction* instruction) {
3001 DCHECK(source.IsValid());
3002 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003003 if (kIsDebugBuild) {
3004 if (instruction != nullptr) {
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00003005 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003006 DCHECK_NE(moves_.Get(i).GetInstruction(), instruction)
3007 << "Doing parallel moves for the same instruction.";
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00003008 }
3009 }
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003010 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
3011 DCHECK(!destination.Equals(moves_.Get(i).GetDestination()))
3012 << "Same destination for two moves in a parallel move.";
3013 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003014 }
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003015 moves_.Add(MoveOperands(source, destination, instruction));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003016 }
3017
3018 MoveOperands* MoveOperandsAt(size_t index) const {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003019 return moves_.GetRawStorage() + index;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003020 }
3021
3022 size_t NumMoves() const { return moves_.Size(); }
3023
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003024 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003025
3026 private:
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003027 GrowableArray<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003028
3029 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
3030};
3031
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003032class HGraphVisitor : public ValueObject {
3033 public:
Dave Allison20dfc792014-06-16 20:44:29 -07003034 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
3035 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003036
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003037 virtual void VisitInstruction(HInstruction* instruction) { UNUSED(instruction); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003038 virtual void VisitBasicBlock(HBasicBlock* block);
3039
Roland Levillain633021e2014-10-01 14:12:25 +01003040 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003041 void VisitInsertionOrder();
3042
Roland Levillain633021e2014-10-01 14:12:25 +01003043 // Visit the graph following dominator tree reverse post-order.
3044 void VisitReversePostOrder();
3045
Nicolas Geoffray787c3072014-03-17 10:20:19 +00003046 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00003047
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003048 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003049#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003050 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
3051
3052 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
3053
3054#undef DECLARE_VISIT_INSTRUCTION
3055
3056 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07003057 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003058
3059 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
3060};
3061
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003062class HGraphDelegateVisitor : public HGraphVisitor {
3063 public:
3064 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
3065 virtual ~HGraphDelegateVisitor() {}
3066
3067 // Visit functions that delegate to to super class.
3068#define DECLARE_VISIT_INSTRUCTION(name, super) \
3069 virtual void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
3070
3071 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
3072
3073#undef DECLARE_VISIT_INSTRUCTION
3074
3075 private:
3076 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
3077};
3078
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003079class HInsertionOrderIterator : public ValueObject {
3080 public:
3081 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
3082
3083 bool Done() const { return index_ == graph_.GetBlocks().Size(); }
3084 HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); }
3085 void Advance() { ++index_; }
3086
3087 private:
3088 const HGraph& graph_;
3089 size_t index_;
3090
3091 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
3092};
3093
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003094class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003095 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003096 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003097
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003098 bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); }
3099 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003100 void Advance() { ++index_; }
3101
3102 private:
3103 const HGraph& graph_;
3104 size_t index_;
3105
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003106 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003107};
3108
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003109class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003110 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003111 explicit HPostOrderIterator(const HGraph& graph)
3112 : graph_(graph), index_(graph_.GetReversePostOrder().Size()) {}
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003113
3114 bool Done() const { return index_ == 0; }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003115 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003116 void Advance() { --index_; }
3117
3118 private:
3119 const HGraph& graph_;
3120 size_t index_;
3121
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003122 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003123};
3124
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003125// Iterator over the blocks that art part of the loop. Includes blocks part
3126// of an inner loop. The order in which the blocks are iterated is on their
3127// block id.
3128class HBlocksInLoopIterator : public ValueObject {
3129 public:
3130 explicit HBlocksInLoopIterator(const HLoopInformation& info)
3131 : blocks_in_loop_(info.GetBlocks()),
3132 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
3133 index_(0) {
3134 if (!blocks_in_loop_.IsBitSet(index_)) {
3135 Advance();
3136 }
3137 }
3138
3139 bool Done() const { return index_ == blocks_.Size(); }
3140 HBasicBlock* Current() const { return blocks_.Get(index_); }
3141 void Advance() {
3142 ++index_;
3143 for (size_t e = blocks_.Size(); index_ < e; ++index_) {
3144 if (blocks_in_loop_.IsBitSet(index_)) {
3145 break;
3146 }
3147 }
3148 }
3149
3150 private:
3151 const BitVector& blocks_in_loop_;
3152 const GrowableArray<HBasicBlock*>& blocks_;
3153 size_t index_;
3154
3155 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
3156};
3157
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003158} // namespace art
3159
3160#endif // ART_COMPILER_OPTIMIZING_NODES_H_