blob: c7b8343ea505ee954e322a8957bd8358e7879305 [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"
Calin Juravleacf735c2015-02-12 15:25:22 +000021#include "handle.h"
22#include "handle_scope.h"
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000023#include "invoke_type.h"
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +010024#include "locations.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000025#include "mirror/class.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010026#include "offsets.h"
Ian Rogerse63db272014-07-15 15:36:11 -070027#include "primitive.h"
Ian Rogers0279ebb2014-10-08 17:27:48 -070028#include "utils/arena_object.h"
Nicolas Geoffray0e336432014-02-26 18:24:38 +000029#include "utils/arena_bit_vector.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000030#include "utils/growable_array.h"
31
32namespace art {
33
34class HBasicBlock;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010035class HEnvironment;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000036class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000037class HIntConstant;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000038class HInvoke;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000039class HGraphVisitor;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +000040class HNullConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010041class HPhi;
Nicolas Geoffray3c049742014-09-24 18:10:46 +010042class HSuspendCheck;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010043class LiveInterval;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000044class LocationSummary;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000045
46static const int kDefaultNumberOfBlocks = 8;
47static const int kDefaultNumberOfSuccessors = 2;
48static const int kDefaultNumberOfPredecessors = 2;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010049static const int kDefaultNumberOfDominatedBlocks = 1;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000050static const int kDefaultNumberOfBackEdges = 1;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000051
Calin Juravle9aec02f2014-11-18 23:06:35 +000052static constexpr uint32_t kMaxIntShiftValue = 0x1f;
53static constexpr uint64_t kMaxLongShiftValue = 0x3f;
54
Dave Allison20dfc792014-06-16 20:44:29 -070055enum IfCondition {
56 kCondEQ,
57 kCondNE,
58 kCondLT,
59 kCondLE,
60 kCondGT,
61 kCondGE,
62};
63
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010064class HInstructionList {
65 public:
66 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
67
68 void AddInstruction(HInstruction* instruction);
69 void RemoveInstruction(HInstruction* instruction);
70
Roland Levillain6b469232014-09-25 10:10:38 +010071 // Return true if this list contains `instruction`.
72 bool Contains(HInstruction* instruction) const;
73
Roland Levillainccc07a92014-09-16 14:48:16 +010074 // Return true if `instruction1` is found before `instruction2` in
75 // this instruction list and false otherwise. Abort if none
76 // of these instructions is found.
77 bool FoundBefore(const HInstruction* instruction1,
78 const HInstruction* instruction2) const;
79
Nicolas Geoffray276d9da2015-02-02 18:24:11 +000080 bool IsEmpty() const { return first_instruction_ == nullptr; }
81 void Clear() { first_instruction_ = last_instruction_ = nullptr; }
82
83 // Update the block of all instructions to be `block`.
84 void SetBlockOfInstructions(HBasicBlock* block) const;
85
86 void AddAfter(HInstruction* cursor, const HInstructionList& instruction_list);
87 void Add(const HInstructionList& instruction_list);
88
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010089 private:
90 HInstruction* first_instruction_;
91 HInstruction* last_instruction_;
92
93 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000094 friend class HGraph;
95 friend class HInstruction;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010096 friend class HInstructionIterator;
Nicolas Geoffray804d0932014-05-02 08:46:00 +010097 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010098
99 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
100};
101
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000102// Control-flow graph of a method. Contains a list of basic blocks.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700103class HGraph : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000104 public:
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000105 HGraph(ArenaAllocator* arena, int start_instruction_id = 0)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000106 : arena_(arena),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000107 blocks_(arena, kDefaultNumberOfBlocks),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100108 reverse_post_order_(arena, kDefaultNumberOfBlocks),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700109 entry_block_(nullptr),
110 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100111 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100112 number_of_vregs_(0),
113 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000114 temporaries_vreg_slots_(0),
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000115 current_instruction_id_(start_instruction_id) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000116
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000117 ArenaAllocator* GetArena() const { return arena_; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100118 const GrowableArray<HBasicBlock*>& GetBlocks() const { return blocks_; }
Roland Levillain93445682014-10-06 19:24:02 +0100119 HBasicBlock* GetBlock(size_t id) const { return blocks_.Get(id); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000120
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000121 HBasicBlock* GetEntryBlock() const { return entry_block_; }
122 HBasicBlock* GetExitBlock() const { return exit_block_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000123
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000124 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
125 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000126
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000127 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100128
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000129 // Try building the SSA form of this graph, with dominance computation and loop
130 // recognition. Returns whether it was successful in doing all these steps.
131 bool TryBuildingSsa() {
132 BuildDominatorTree();
133 TransformToSsa();
134 return AnalyzeNaturalLoops();
135 }
136
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000137 void BuildDominatorTree();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000138 void TransformToSsa();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100139 void SimplifyCFG();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000140
Nicolas Geoffrayf5370122014-12-02 11:51:19 +0000141 // Analyze all natural loops in this graph. Returns false if one
142 // loop is not natural, that is the header does not dominate the
143 // back edge.
144 bool AnalyzeNaturalLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100145
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000146 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
147 void InlineInto(HGraph* outer_graph, HInvoke* invoke);
148
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100149 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
150 void SimplifyLoop(HBasicBlock* header);
151
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000152 int32_t GetNextInstructionId() {
153 DCHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000154 return current_instruction_id_++;
155 }
156
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000157 int32_t GetCurrentInstructionId() const {
158 return current_instruction_id_;
159 }
160
161 void SetCurrentInstructionId(int32_t id) {
162 current_instruction_id_ = id;
163 }
164
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100165 uint16_t GetMaximumNumberOfOutVRegs() const {
166 return maximum_number_of_out_vregs_;
167 }
168
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000169 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
170 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100171 }
172
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000173 void UpdateTemporariesVRegSlots(size_t slots) {
174 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100175 }
176
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000177 size_t GetTemporariesVRegSlots() const {
178 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100179 }
180
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100181 void SetNumberOfVRegs(uint16_t number_of_vregs) {
182 number_of_vregs_ = number_of_vregs;
183 }
184
185 uint16_t GetNumberOfVRegs() const {
186 return number_of_vregs_;
187 }
188
189 void SetNumberOfInVRegs(uint16_t value) {
190 number_of_in_vregs_ = value;
191 }
192
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100193 uint16_t GetNumberOfLocalVRegs() const {
194 return number_of_vregs_ - number_of_in_vregs_;
195 }
196
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100197 const GrowableArray<HBasicBlock*>& GetReversePostOrder() const {
198 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100199 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100200
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000201 HNullConstant* GetNullConstant();
202
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000203 private:
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000204 HBasicBlock* FindCommonDominator(HBasicBlock* first, HBasicBlock* second) const;
205 void VisitBlockForDominatorTree(HBasicBlock* block,
206 HBasicBlock* predecessor,
207 GrowableArray<size_t>* visits);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100208 void FindBackEdges(ArenaBitVector* visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000209 void VisitBlockForBackEdges(HBasicBlock* block,
210 ArenaBitVector* visited,
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100211 ArenaBitVector* visiting);
Roland Levillainfc600dc2014-12-02 17:16:31 +0000212 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000213 void RemoveDeadBlocks(const ArenaBitVector& visited) const;
Jean Christophe Beyler53d9da82014-12-04 13:28:25 -0800214 void RemoveBlock(HBasicBlock* block) const;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000215
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000216 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000217
218 // List of blocks in insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000219 GrowableArray<HBasicBlock*> blocks_;
220
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100221 // List of blocks to perform a reverse post order tree traversal.
222 GrowableArray<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000223
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000224 HBasicBlock* entry_block_;
225 HBasicBlock* exit_block_;
226
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100227 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100228 uint16_t maximum_number_of_out_vregs_;
229
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100230 // The number of virtual registers in this method. Contains the parameters.
231 uint16_t number_of_vregs_;
232
233 // The number of virtual registers used by parameters of this method.
234 uint16_t number_of_in_vregs_;
235
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000236 // Number of vreg size slots that the temporaries use (used in baseline compiler).
237 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100238
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000239 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000240 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000241
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000242 // Cached null constant that might be created when building SSA form.
243 HNullConstant* cached_null_constant_;
244
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000245 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000246 DISALLOW_COPY_AND_ASSIGN(HGraph);
247};
248
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700249class HLoopInformation : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000250 public:
251 HLoopInformation(HBasicBlock* header, HGraph* graph)
252 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100253 suspend_check_(nullptr),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100254 back_edges_(graph->GetArena(), kDefaultNumberOfBackEdges),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100255 // Make bit vector growable, as the number of blocks may change.
256 blocks_(graph->GetArena(), graph->GetBlocks().Size(), true) {}
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100257
258 HBasicBlock* GetHeader() const {
259 return header_;
260 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000261
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000262 void SetHeader(HBasicBlock* block) {
263 header_ = block;
264 }
265
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100266 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
267 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
268 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
269
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000270 void AddBackEdge(HBasicBlock* back_edge) {
271 back_edges_.Add(back_edge);
272 }
273
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100274 void RemoveBackEdge(HBasicBlock* back_edge) {
275 back_edges_.Delete(back_edge);
276 }
277
278 bool IsBackEdge(HBasicBlock* block) {
279 for (size_t i = 0, e = back_edges_.Size(); i < e; ++i) {
280 if (back_edges_.Get(i) == block) return true;
281 }
282 return false;
283 }
284
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000285 size_t NumberOfBackEdges() const {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000286 return back_edges_.Size();
287 }
288
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100289 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100290
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100291 const GrowableArray<HBasicBlock*>& GetBackEdges() const {
292 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100293 }
294
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100295 void ClearBackEdges() {
296 back_edges_.Reset();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100297 }
298
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100299 // Find blocks that are part of this loop. Returns whether the loop is a natural loop,
300 // that is the header dominates the back edge.
301 bool Populate();
302
303 // Returns whether this loop information contains `block`.
304 // Note that this loop information *must* be populated before entering this function.
305 bool Contains(const HBasicBlock& block) const;
306
307 // Returns whether this loop information is an inner loop of `other`.
308 // Note that `other` *must* be populated before entering this function.
309 bool IsIn(const HLoopInformation& other) const;
310
311 const ArenaBitVector& GetBlocks() const { return blocks_; }
312
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000313 void Add(HBasicBlock* block);
314
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000315 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100316 // Internal recursive implementation of `Populate`.
317 void PopulateRecursive(HBasicBlock* block);
318
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000319 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100320 HSuspendCheck* suspend_check_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000321 GrowableArray<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100322 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000323
324 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
325};
326
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100327static constexpr size_t kNoLifetime = -1;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100328static constexpr uint32_t kNoDexPc = -1;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100329
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000330// A block in a method. Contains the list of instructions represented
331// as a double linked list. Each block knows its predecessors and
332// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100333
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700334class HBasicBlock : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000335 public:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100336 explicit HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000337 : graph_(graph),
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000338 predecessors_(graph->GetArena(), kDefaultNumberOfPredecessors),
339 successors_(graph->GetArena(), kDefaultNumberOfSuccessors),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000340 loop_information_(nullptr),
341 dominator_(nullptr),
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100342 dominated_blocks_(graph->GetArena(), kDefaultNumberOfDominatedBlocks),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100343 block_id_(-1),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100344 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100345 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000346 lifetime_end_(kNoLifetime),
347 is_catch_block_(false) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000348
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100349 const GrowableArray<HBasicBlock*>& GetPredecessors() const {
350 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000351 }
352
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100353 const GrowableArray<HBasicBlock*>& GetSuccessors() const {
354 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000355 }
356
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100357 const GrowableArray<HBasicBlock*>& GetDominatedBlocks() const {
358 return dominated_blocks_;
359 }
360
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100361 bool IsEntryBlock() const {
362 return graph_->GetEntryBlock() == this;
363 }
364
365 bool IsExitBlock() const {
366 return graph_->GetExitBlock() == this;
367 }
368
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000369 void AddBackEdge(HBasicBlock* back_edge) {
370 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000371 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000372 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100373 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000374 loop_information_->AddBackEdge(back_edge);
375 }
376
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000377 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000378 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000379
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000380 int GetBlockId() const { return block_id_; }
381 void SetBlockId(int id) { block_id_ = id; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000382
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000383 HBasicBlock* GetDominator() const { return dominator_; }
384 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100385 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.Add(block); }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000386 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
387 for (size_t i = 0, e = dominated_blocks_.Size(); i < e; ++i) {
388 if (dominated_blocks_.Get(i) == existing) {
389 dominated_blocks_.Put(i, new_block);
390 return;
391 }
392 }
393 LOG(FATAL) << "Unreachable";
394 UNREACHABLE();
395 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000396
397 int NumberOfBackEdges() const {
398 return loop_information_ == nullptr
399 ? 0
400 : loop_information_->NumberOfBackEdges();
401 }
402
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100403 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
404 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100405 const HInstructionList& GetInstructions() const { return instructions_; }
406 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100407 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000408
409 void AddSuccessor(HBasicBlock* block) {
410 successors_.Add(block);
411 block->predecessors_.Add(this);
412 }
413
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100414 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
415 size_t successor_index = GetSuccessorIndexOf(existing);
416 DCHECK_NE(successor_index, static_cast<size_t>(-1));
417 existing->RemovePredecessor(this);
418 new_block->predecessors_.Add(this);
419 successors_.Put(successor_index, new_block);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000420 }
421
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000422 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
423 size_t predecessor_index = GetPredecessorIndexOf(existing);
424 DCHECK_NE(predecessor_index, static_cast<size_t>(-1));
425 existing->RemoveSuccessor(this);
426 new_block->successors_.Add(this);
427 predecessors_.Put(predecessor_index, new_block);
428 }
429
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100430 void RemovePredecessor(HBasicBlock* block) {
431 predecessors_.Delete(block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100432 }
433
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000434 void RemoveSuccessor(HBasicBlock* block) {
435 successors_.Delete(block);
436 }
437
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100438 void ClearAllPredecessors() {
439 predecessors_.Reset();
440 }
441
442 void AddPredecessor(HBasicBlock* block) {
443 predecessors_.Add(block);
444 block->successors_.Add(this);
445 }
446
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100447 void SwapPredecessors() {
Nicolas Geoffrayc83d4412014-09-18 16:46:20 +0100448 DCHECK_EQ(predecessors_.Size(), 2u);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100449 HBasicBlock* temp = predecessors_.Get(0);
450 predecessors_.Put(0, predecessors_.Get(1));
451 predecessors_.Put(1, temp);
452 }
453
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100454 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) {
455 for (size_t i = 0, e = predecessors_.Size(); i < e; ++i) {
456 if (predecessors_.Get(i) == predecessor) {
457 return i;
458 }
459 }
460 return -1;
461 }
462
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100463 size_t GetSuccessorIndexOf(HBasicBlock* successor) {
464 for (size_t i = 0, e = successors_.Size(); i < e; ++i) {
465 if (successors_.Get(i) == successor) {
466 return i;
467 }
468 }
469 return -1;
470 }
471
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000472 // Split the block into two blocks just after `cursor`. Returns the newly
473 // created block. Note that this method just updates raw block information,
474 // like predecessors, successors, dominators, and instruction list. It does not
475 // update the graph, reverse post order, loop information, nor make sure the
476 // blocks are consistent (for example ending with a control flow instruction).
477 HBasicBlock* SplitAfter(HInstruction* cursor);
478
479 // Merge `other` at the end of `this`. Successors and dominated blocks of
480 // `other` are changed to be successors and dominated blocks of `this`. Note
481 // that this method does not update the graph, reverse post order, loop
482 // information, nor make sure the blocks are consistent (for example ending
483 // with a control flow instruction).
484 void MergeWith(HBasicBlock* other);
485
486 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
487 // of `this` are moved to `other`.
488 // Note that this method does not update the graph, reverse post order, loop
489 // information, nor make sure the blocks are consistent (for example ending
490 void ReplaceWith(HBasicBlock* other);
491
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000492 void AddInstruction(HInstruction* instruction);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100493 void RemoveInstruction(HInstruction* instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100494 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +0100495 // Replace instruction `initial` with `replacement` within this block.
496 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
497 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100498 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100499 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100500 void RemovePhi(HPhi* phi);
501
502 bool IsLoopHeader() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100503 return (loop_information_ != nullptr) && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100504 }
505
Roland Levillain6b879dd2014-09-22 17:13:44 +0100506 bool IsLoopPreHeaderFirstPredecessor() const {
507 DCHECK(IsLoopHeader());
508 DCHECK(!GetPredecessors().IsEmpty());
509 return GetPredecessors().Get(0) == GetLoopInformation()->GetPreHeader();
510 }
511
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100512 HLoopInformation* GetLoopInformation() const {
513 return loop_information_;
514 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000515
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000516 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100517 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000518 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100519 void SetInLoop(HLoopInformation* info) {
520 if (IsLoopHeader()) {
521 // Nothing to do. This just means `info` is an outer loop.
522 } else if (loop_information_ == nullptr) {
523 loop_information_ = info;
524 } else if (loop_information_->Contains(*info->GetHeader())) {
525 // Block is currently part of an outer loop. Make it part of this inner loop.
526 // Note that a non loop header having a loop information means this loop information
527 // has already been populated
528 loop_information_ = info;
529 } else {
530 // Block is part of an inner loop. Do not update the loop information.
531 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
532 // at this point, because this method is being called while populating `info`.
533 }
534 }
535
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000536 // Raw update of the loop information.
537 void SetLoopInformation(HLoopInformation* info) {
538 loop_information_ = info;
539 }
540
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100541 bool IsInLoop() const { return loop_information_ != nullptr; }
542
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100543 // Returns wheter this block dominates the blocked passed as parameter.
544 bool Dominates(HBasicBlock* block) const;
545
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100546 size_t GetLifetimeStart() const { return lifetime_start_; }
547 size_t GetLifetimeEnd() const { return lifetime_end_; }
548
549 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
550 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
551
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100552 uint32_t GetDexPc() const { return dex_pc_; }
553
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000554 bool IsCatchBlock() const { return is_catch_block_; }
555 void SetIsCatchBlock() { is_catch_block_ = true; }
556
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000557 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000558 HGraph* graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000559 GrowableArray<HBasicBlock*> predecessors_;
560 GrowableArray<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100561 HInstructionList instructions_;
562 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000563 HLoopInformation* loop_information_;
564 HBasicBlock* dominator_;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100565 GrowableArray<HBasicBlock*> dominated_blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000566 int block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100567 // The dex program counter of the first instruction of this block.
568 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100569 size_t lifetime_start_;
570 size_t lifetime_end_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000571 bool is_catch_block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000572
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000573 friend class HGraph;
574 friend class HInstruction;
575
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000576 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
577};
578
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100579#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
580 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000581 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000582 M(ArrayGet, Instruction) \
583 M(ArrayLength, Instruction) \
584 M(ArraySet, Instruction) \
585 M(BoundsCheck, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000586 M(CheckCast, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100587 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000588 M(Compare, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100589 M(Condition, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000590 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +0000591 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000592 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100593 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000594 M(Exit, Instruction) \
595 M(FloatConstant, Constant) \
596 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100597 M(GreaterThan, Condition) \
598 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100599 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000600 M(InstanceFieldGet, Instruction) \
601 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000602 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100603 M(IntConstant, Constant) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +0000604 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000605 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100606 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000607 M(LessThan, Condition) \
608 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000609 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000610 M(LoadException, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100611 M(LoadLocal, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000612 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100613 M(Local, Instruction) \
614 M(LongConstant, Constant) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +0000615 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000616 M(Mul, BinaryOperation) \
617 M(Neg, UnaryOperation) \
618 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100619 M(NewInstance, Instruction) \
Roland Levillain1cc5f252014-10-22 18:06:21 +0100620 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000621 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000622 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000623 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000624 M(Or, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100625 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000626 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100627 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000628 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100629 M(Return, Instruction) \
630 M(ReturnVoid, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000631 M(Shl, BinaryOperation) \
632 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100633 M(StaticFieldGet, Instruction) \
634 M(StaticFieldSet, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100635 M(StoreLocal, Instruction) \
636 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100637 M(SuspendCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000638 M(Temporary, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000639 M(Throw, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +0000640 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000641 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000642 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000643
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100644#define FOR_EACH_INSTRUCTION(M) \
645 FOR_EACH_CONCRETE_INSTRUCTION(M) \
646 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +0100647 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +0100648 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100649 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -0700650
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100651#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000652FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
653#undef FORWARD_DECLARATION
654
Roland Levillainccc07a92014-09-16 14:48:16 +0100655#define DECLARE_INSTRUCTION(type) \
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100656 virtual InstructionKind GetKind() const { return k##type; } \
Roland Levillainccc07a92014-09-16 14:48:16 +0100657 virtual const char* DebugName() const { return #type; } \
658 virtual const H##type* As##type() const OVERRIDE { return this; } \
659 virtual H##type* As##type() OVERRIDE { return this; } \
660 virtual bool InstructionTypeEquals(HInstruction* other) const { \
661 return other->Is##type(); \
662 } \
663 virtual void Accept(HGraphVisitor* visitor)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000664
David Brazdiled596192015-01-23 10:39:45 +0000665template <typename T> class HUseList;
666
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100667template <typename T>
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700668class HUseListNode : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000669 public:
David Brazdiled596192015-01-23 10:39:45 +0000670 HUseListNode* GetPrevious() const { return prev_; }
671 HUseListNode* GetNext() const { return next_; }
672 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100673 size_t GetIndex() const { return index_; }
674
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000675 private:
David Brazdiled596192015-01-23 10:39:45 +0000676 HUseListNode(T user, size_t index)
677 : user_(user), index_(index), prev_(nullptr), next_(nullptr) {}
678
679 T const user_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100680 const size_t index_;
David Brazdiled596192015-01-23 10:39:45 +0000681 HUseListNode<T>* prev_;
682 HUseListNode<T>* next_;
683
684 friend class HUseList<T>;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000685
686 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
687};
688
David Brazdiled596192015-01-23 10:39:45 +0000689template <typename T>
690class HUseList : public ValueObject {
691 public:
692 HUseList() : first_(nullptr) {}
693
694 void Clear() {
695 first_ = nullptr;
696 }
697
698 // Adds a new entry at the beginning of the use list and returns
699 // the newly created node.
700 HUseListNode<T>* AddUse(T user, size_t index, ArenaAllocator* arena) {
David Brazdilea55b932015-01-27 17:12:29 +0000701 HUseListNode<T>* new_node = new (arena) HUseListNode<T>(user, index);
David Brazdiled596192015-01-23 10:39:45 +0000702 if (IsEmpty()) {
703 first_ = new_node;
704 } else {
705 first_->prev_ = new_node;
706 new_node->next_ = first_;
707 first_ = new_node;
708 }
709 return new_node;
710 }
711
712 HUseListNode<T>* GetFirst() const {
713 return first_;
714 }
715
716 void Remove(HUseListNode<T>* node) {
717 if (node->prev_ != nullptr) {
718 node->prev_->next_ = node->next_;
719 }
720 if (node->next_ != nullptr) {
721 node->next_->prev_ = node->prev_;
722 }
723 if (node == first_) {
724 first_ = node->next_;
725 }
726 }
727
728 bool IsEmpty() const {
729 return first_ == nullptr;
730 }
731
732 bool HasOnlyOneUse() const {
733 return first_ != nullptr && first_->next_ == nullptr;
734 }
735
736 private:
737 HUseListNode<T>* first_;
738};
739
740template<typename T>
741class HUseIterator : public ValueObject {
742 public:
743 explicit HUseIterator(const HUseList<T>& uses) : current_(uses.GetFirst()) {}
744
745 bool Done() const { return current_ == nullptr; }
746
747 void Advance() {
748 DCHECK(!Done());
749 current_ = current_->GetNext();
750 }
751
752 HUseListNode<T>* Current() const {
753 DCHECK(!Done());
754 return current_;
755 }
756
757 private:
758 HUseListNode<T>* current_;
759
760 friend class HValue;
761};
762
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100763// Represents the side effects an instruction may have.
764class SideEffects : public ValueObject {
765 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100766 SideEffects() : flags_(0) {}
767
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100768 static SideEffects None() {
769 return SideEffects(0);
770 }
771
772 static SideEffects All() {
773 return SideEffects(ChangesSomething().flags_ | DependsOnSomething().flags_);
774 }
775
776 static SideEffects ChangesSomething() {
777 return SideEffects((1 << kFlagChangesCount) - 1);
778 }
779
780 static SideEffects DependsOnSomething() {
781 int count = kFlagDependsOnCount - kFlagChangesCount;
782 return SideEffects(((1 << count) - 1) << kFlagChangesCount);
783 }
784
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100785 SideEffects Union(SideEffects other) const {
786 return SideEffects(flags_ | other.flags_);
787 }
788
Roland Levillain72bceff2014-09-15 18:29:00 +0100789 bool HasSideEffects() const {
790 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
791 return (flags_ & all_bits_set) != 0;
792 }
793
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100794 bool HasAllSideEffects() const {
795 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
796 return all_bits_set == (flags_ & all_bits_set);
797 }
798
799 bool DependsOn(SideEffects other) const {
800 size_t depends_flags = other.ComputeDependsFlags();
801 return (flags_ & depends_flags) != 0;
802 }
803
804 bool HasDependencies() const {
805 int count = kFlagDependsOnCount - kFlagChangesCount;
806 size_t all_bits_set = (1 << count) - 1;
807 return ((flags_ >> kFlagChangesCount) & all_bits_set) != 0;
808 }
809
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100810 private:
811 static constexpr int kFlagChangesSomething = 0;
812 static constexpr int kFlagChangesCount = kFlagChangesSomething + 1;
813
814 static constexpr int kFlagDependsOnSomething = kFlagChangesCount;
815 static constexpr int kFlagDependsOnCount = kFlagDependsOnSomething + 1;
816
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100817 explicit SideEffects(size_t flags) : flags_(flags) {}
818
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100819 size_t ComputeDependsFlags() const {
820 return flags_ << kFlagChangesCount;
821 }
822
823 size_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100824};
825
David Brazdiled596192015-01-23 10:39:45 +0000826// A HEnvironment object contains the values of virtual registers at a given location.
827class HEnvironment : public ArenaObject<kArenaAllocMisc> {
828 public:
829 HEnvironment(ArenaAllocator* arena, size_t number_of_vregs)
830 : vregs_(arena, number_of_vregs) {
831 vregs_.SetSize(number_of_vregs);
832 for (size_t i = 0; i < number_of_vregs; i++) {
833 vregs_.Put(i, VRegInfo(nullptr, nullptr));
834 }
835 }
836
837 void CopyFrom(HEnvironment* env);
838
839 void SetRawEnvAt(size_t index, HInstruction* instruction) {
840 vregs_.Put(index, VRegInfo(instruction, nullptr));
841 }
842
843 // Record instructions' use entries of this environment for constant-time removal.
844 void RecordEnvUse(HUseListNode<HEnvironment*>* env_use) {
845 DCHECK(env_use->GetUser() == this);
846 size_t index = env_use->GetIndex();
847 VRegInfo info = vregs_.Get(index);
848 DCHECK(info.vreg_ != nullptr);
849 DCHECK(info.node_ == nullptr);
850 vregs_.Put(index, VRegInfo(info.vreg_, env_use));
851 }
852
853 HInstruction* GetInstructionAt(size_t index) const {
854 return vregs_.Get(index).vreg_;
855 }
856
857 HUseListNode<HEnvironment*>* GetInstructionEnvUseAt(size_t index) const {
858 return vregs_.Get(index).node_;
859 }
860
861 size_t Size() const { return vregs_.Size(); }
862
863 private:
864 struct VRegInfo {
865 HInstruction* vreg_;
866 HUseListNode<HEnvironment*>* node_;
867
868 VRegInfo(HInstruction* instruction, HUseListNode<HEnvironment*>* env_use)
869 : vreg_(instruction), node_(env_use) {}
870 };
871
872 GrowableArray<VRegInfo> vregs_;
873
874 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
875};
876
Calin Juravleacf735c2015-02-12 15:25:22 +0000877class ReferenceTypeInfo : ValueObject {
878 public:
879 ReferenceTypeInfo() : is_exact_(false), is_top_(true) {}
880 ReferenceTypeInfo(Handle<mirror::Class> type_handle, bool is_exact) {
881 SetTypeHandle(type_handle, is_exact);
882 }
883
884 bool IsExact() const { return is_exact_; }
885 bool IsTop() const { return is_top_; }
886
887 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
888
889 void SetTop() {
890 is_top_ = true;
891 type_handle_ = Handle<mirror::Class>();
892 }
893
894 void SetInexact() { is_exact_ = false; }
895
896 void SetTypeHandle(Handle<mirror::Class> type_handle, bool is_exact)
897 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
898 is_exact_ = is_exact;
899 if (type_handle->IsObjectClass()) {
900 is_top_ = true;
901 // Override the type handle to be consistent with the case when we get to
902 // Top but don't have the Object class available. It avoids having to guess
903 // what value the type_handle has when it's Top.
904 type_handle_ = Handle<mirror::Class>();
905 } else {
906 is_top_ = false;
907 type_handle_ = type_handle;
908 }
909 }
910
911 bool IsSupertypeOf(ReferenceTypeInfo rti) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
912 if (IsTop()) {
913 // Top (equivalent for java.lang.Object) is supertype of anything.
914 return true;
915 }
916 if (rti.IsTop()) {
917 // If we get here `this` is not Top() so it can't be a supertype.
918 return false;
919 }
920 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
921 }
922
923 // Returns true if the type information provide the same amount of details.
924 // Note that it does not mean that the instructions have the same actual type
925 // (e.g. tops are equal but they can be the result of a merge).
926 bool IsEqual(ReferenceTypeInfo rti) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
927 if (IsExact() != rti.IsExact()) {
928 return false;
929 }
930 if (IsTop() && rti.IsTop()) {
931 // `Top` means java.lang.Object, so the types are equivalent.
932 return true;
933 }
934 if (IsTop() || rti.IsTop()) {
935 // If only one is top or object than they are not equivalent.
936 // NB: We need this extra check because the type_handle of `Top` is invalid
937 // and we cannot inspect its reference.
938 return false;
939 }
940
941 // Finally check the types.
942 return GetTypeHandle().Get() == rti.GetTypeHandle().Get();
943 }
944
945 private:
946 // The class of the object.
947 Handle<mirror::Class> type_handle_;
948 // Whether or not the type is exact or a superclass of the actual type.
949 bool is_exact_;
950 // A true value here means that the object type should be java.lang.Object.
951 // We don't have access to the corresponding mirror object every time so this
952 // flag acts as a substitute. When true, the TypeHandle refers to a null
953 // pointer and should not be used.
954 bool is_top_;
955};
956
957std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
958
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700959class HInstruction : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000960 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100961 explicit HInstruction(SideEffects side_effects)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000962 : previous_(nullptr),
963 next_(nullptr),
964 block_(nullptr),
965 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100966 ssa_index_(-1),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100967 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100968 locations_(nullptr),
969 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100970 lifetime_position_(kNoLifetime),
971 side_effects_(side_effects) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000972
Dave Allison20dfc792014-06-16 20:44:29 -0700973 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000974
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100975#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100976 enum InstructionKind {
977 FOR_EACH_INSTRUCTION(DECLARE_KIND)
978 };
979#undef DECLARE_KIND
980
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000981 HInstruction* GetNext() const { return next_; }
982 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000983
Calin Juravle77520bc2015-01-12 18:45:46 +0000984 HInstruction* GetNextDisregardingMoves() const;
985 HInstruction* GetPreviousDisregardingMoves() const;
986
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000987 HBasicBlock* GetBlock() const { return block_; }
988 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100989 bool IsInBlock() const { return block_ != nullptr; }
990 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +0100991 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000992
Roland Levillain6b879dd2014-09-22 17:13:44 +0100993 virtual size_t InputCount() const = 0;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100994 virtual HInstruction* InputAt(size_t i) const = 0;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000995
996 virtual void Accept(HGraphVisitor* visitor) = 0;
997 virtual const char* DebugName() const = 0;
998
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100999 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001000 virtual void SetRawInputAt(size_t index, HInstruction* input) = 0;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001001
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001002 virtual bool NeedsEnvironment() const { return false; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001003 virtual bool IsControlFlow() const { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01001004 virtual bool CanThrow() const { return false; }
Roland Levillain72bceff2014-09-15 18:29:00 +01001005 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001006
Calin Juravle10e244f2015-01-26 18:54:32 +00001007 // Does not apply for all instructions, but having this at top level greatly
1008 // simplifies the null check elimination.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001009 virtual bool CanBeNull() const {
1010 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1011 return true;
1012 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001013
Calin Juravle77520bc2015-01-12 18:45:46 +00001014 virtual bool CanDoImplicitNullCheck() const { return false; }
1015
Calin Juravleacf735c2015-02-12 15:25:22 +00001016 void SetReferenceTypeInfo(ReferenceTypeInfo reference_type_info) {
1017 reference_type_info_ = reference_type_info;
1018 }
1019
1020 ReferenceTypeInfo GetReferenceTypeInfo() const { return reference_type_info_; }
1021
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001022 void AddUseAt(HInstruction* user, size_t index) {
David Brazdiled596192015-01-23 10:39:45 +00001023 uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001024 }
1025
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001026 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001027 DCHECK(user != nullptr);
David Brazdiled596192015-01-23 10:39:45 +00001028 HUseListNode<HEnvironment*>* env_use =
1029 env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1030 user->RecordEnvUse(env_use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001031 }
1032
1033 void RemoveUser(HInstruction* user, size_t index);
David Brazdiled596192015-01-23 10:39:45 +00001034 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001035
David Brazdilea55b932015-01-27 17:12:29 +00001036 const HUseList<HInstruction*>& GetUses() { return uses_; }
1037 const HUseList<HEnvironment*>& GetEnvUses() { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001038
David Brazdiled596192015-01-23 10:39:45 +00001039 bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); }
1040 bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001041
Roland Levillain6c82d402014-10-13 16:10:27 +01001042 // Does this instruction strictly dominate `other_instruction`?
1043 // Returns false if this instruction and `other_instruction` are the same.
1044 // Aborts if this instruction and `other_instruction` are both phis.
1045 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001046
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001047 int GetId() const { return id_; }
1048 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001049
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001050 int GetSsaIndex() const { return ssa_index_; }
1051 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1052 bool HasSsaIndex() const { return ssa_index_ != -1; }
1053
1054 bool HasEnvironment() const { return environment_ != nullptr; }
1055 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001056 void SetEnvironment(HEnvironment* environment) { environment_ = environment; }
1057
Nicolas Geoffray39468442014-09-02 15:17:15 +01001058 // Returns the number of entries in the environment. Typically, that is the
1059 // number of dex registers in a method. It could be more in case of inlining.
1060 size_t EnvironmentSize() const;
1061
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001062 LocationSummary* GetLocations() const { return locations_; }
1063 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001064
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001065 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001066 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001067
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001068 // Move `this` instruction before `cursor`.
1069 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001070
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001071#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01001072 bool Is##type() const { return (As##type() != nullptr); } \
1073 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001074 virtual H##type* As##type() { return nullptr; }
1075
1076 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
1077#undef INSTRUCTION_TYPE_CHECK
1078
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001079 // Returns whether the instruction can be moved within the graph.
1080 virtual bool CanBeMoved() const { return false; }
1081
1082 // Returns whether the two instructions are of the same kind.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001083 virtual bool InstructionTypeEquals(HInstruction* other) const {
1084 UNUSED(other);
1085 return false;
1086 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001087
1088 // Returns whether any data encoded in the two instructions is equal.
1089 // This method does not look at the inputs. Both instructions must be
1090 // of the same type, otherwise the method has undefined behavior.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001091 virtual bool InstructionDataEquals(HInstruction* other) const {
1092 UNUSED(other);
1093 return false;
1094 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001095
1096 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00001097 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001098 // 2) Their inputs are identical.
1099 bool Equals(HInstruction* other) const;
1100
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001101 virtual InstructionKind GetKind() const = 0;
1102
1103 virtual size_t ComputeHashCode() const {
1104 size_t result = GetKind();
1105 for (size_t i = 0, e = InputCount(); i < e; ++i) {
1106 result = (result * 31) + InputAt(i)->GetId();
1107 }
1108 return result;
1109 }
1110
1111 SideEffects GetSideEffects() const { return side_effects_; }
1112
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001113 size_t GetLifetimePosition() const { return lifetime_position_; }
1114 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
1115 LiveInterval* GetLiveInterval() const { return live_interval_; }
1116 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
1117 bool HasLiveInterval() const { return live_interval_ != nullptr; }
1118
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001119 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
1120
1121 // Returns whether the code generation of the instruction will require to have access
1122 // to the current method. Such instructions are:
1123 // (1): Instructions that require an environment, as calling the runtime requires
1124 // to walk the stack and have the current method stored at a specific stack address.
1125 // (2): Object literals like classes and strings, that are loaded from the dex cache
1126 // fields of the current method.
1127 bool NeedsCurrentMethod() const {
1128 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
1129 }
1130
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001131 private:
1132 HInstruction* previous_;
1133 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001134 HBasicBlock* block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001135
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001136 // An instruction gets an id when it is added to the graph.
1137 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01001138 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001139 int id_;
1140
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001141 // When doing liveness analysis, instructions that have uses get an SSA index.
1142 int ssa_index_;
1143
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001144 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00001145 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001146
1147 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00001148 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001149
Nicolas Geoffray39468442014-09-02 15:17:15 +01001150 // The environment associated with this instruction. Not null if the instruction
1151 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001152 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001153
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001154 // Set by the code generator.
1155 LocationSummary* locations_;
1156
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001157 // Set by the liveness analysis.
1158 LiveInterval* live_interval_;
1159
1160 // Set by the liveness analysis, this is the position in a linear
1161 // order of blocks where this instruction's live interval start.
1162 size_t lifetime_position_;
1163
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001164 const SideEffects side_effects_;
1165
Calin Juravleacf735c2015-02-12 15:25:22 +00001166 // TODO: for primitive types this should be marked as invalid.
1167 ReferenceTypeInfo reference_type_info_;
1168
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001169 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001170 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001171 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001172
1173 DISALLOW_COPY_AND_ASSIGN(HInstruction);
1174};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001175std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001176
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001177class HInputIterator : public ValueObject {
1178 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001179 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001180
1181 bool Done() const { return index_ == instruction_->InputCount(); }
1182 HInstruction* Current() const { return instruction_->InputAt(index_); }
1183 void Advance() { index_++; }
1184
1185 private:
1186 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001187 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001188
1189 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
1190};
1191
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001192class HInstructionIterator : public ValueObject {
1193 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001194 explicit HInstructionIterator(const HInstructionList& instructions)
1195 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001196 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001197 }
1198
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001199 bool Done() const { return instruction_ == nullptr; }
1200 HInstruction* Current() const { return instruction_; }
1201 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001202 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001203 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001204 }
1205
1206 private:
1207 HInstruction* instruction_;
1208 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001209
1210 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001211};
1212
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001213class HBackwardInstructionIterator : public ValueObject {
1214 public:
1215 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
1216 : instruction_(instructions.last_instruction_) {
1217 next_ = Done() ? nullptr : instruction_->GetPrevious();
1218 }
1219
1220 bool Done() const { return instruction_ == nullptr; }
1221 HInstruction* Current() const { return instruction_; }
1222 void Advance() {
1223 instruction_ = next_;
1224 next_ = Done() ? nullptr : instruction_->GetPrevious();
1225 }
1226
1227 private:
1228 HInstruction* instruction_;
1229 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001230
1231 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001232};
1233
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001234// An embedded container with N elements of type T. Used (with partial
1235// specialization for N=0) because embedded arrays cannot have size 0.
1236template<typename T, intptr_t N>
1237class EmbeddedArray {
1238 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001239 EmbeddedArray() : elements_() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001240
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001241 intptr_t GetLength() const { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001242
1243 const T& operator[](intptr_t i) const {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001244 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001245 return elements_[i];
1246 }
1247
1248 T& operator[](intptr_t i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001249 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001250 return elements_[i];
1251 }
1252
1253 const T& At(intptr_t i) const {
1254 return (*this)[i];
1255 }
1256
1257 void SetAt(intptr_t i, const T& val) {
1258 (*this)[i] = val;
1259 }
1260
1261 private:
1262 T elements_[N];
1263};
1264
1265template<typename T>
1266class EmbeddedArray<T, 0> {
1267 public:
1268 intptr_t length() const { return 0; }
1269 const T& operator[](intptr_t i) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001270 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001271 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001272 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001273 }
1274 T& operator[](intptr_t i) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001275 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001276 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001277 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001278 }
1279};
1280
1281template<intptr_t N>
1282class HTemplateInstruction: public HInstruction {
1283 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001284 HTemplateInstruction<N>(SideEffects side_effects)
1285 : HInstruction(side_effects), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07001286 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001287
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001288 virtual size_t InputCount() const { return N; }
1289 virtual HInstruction* InputAt(size_t i) const { return inputs_[i]; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001290
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001291 protected:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001292 virtual void SetRawInputAt(size_t i, HInstruction* instruction) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001293 inputs_[i] = instruction;
1294 }
1295
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001296 private:
1297 EmbeddedArray<HInstruction*, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001298
1299 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001300};
1301
Dave Allison20dfc792014-06-16 20:44:29 -07001302template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001303class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07001304 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001305 HExpression<N>(Primitive::Type type, SideEffects side_effects)
1306 : HTemplateInstruction<N>(side_effects), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07001307 virtual ~HExpression() {}
1308
1309 virtual Primitive::Type GetType() const { return type_; }
1310
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001311 protected:
1312 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07001313};
1314
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001315// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
1316// instruction that branches to the exit block.
1317class HReturnVoid : public HTemplateInstruction<0> {
1318 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001319 HReturnVoid() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001320
1321 virtual bool IsControlFlow() const { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001322
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001323 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001324
1325 private:
1326 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
1327};
1328
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001329// Represents dex's RETURN opcodes. A HReturn is a control flow
1330// instruction that branches to the exit block.
1331class HReturn : public HTemplateInstruction<1> {
1332 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001333 explicit HReturn(HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001334 SetRawInputAt(0, value);
1335 }
1336
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001337 virtual bool IsControlFlow() const { return true; }
1338
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001339 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001340
1341 private:
1342 DISALLOW_COPY_AND_ASSIGN(HReturn);
1343};
1344
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001345// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001346// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001347// exit block.
1348class HExit : public HTemplateInstruction<0> {
1349 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001350 HExit() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001351
1352 virtual bool IsControlFlow() const { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001353
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001354 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001355
1356 private:
1357 DISALLOW_COPY_AND_ASSIGN(HExit);
1358};
1359
1360// Jumps from one block to another.
1361class HGoto : public HTemplateInstruction<0> {
1362 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001363 HGoto() : HTemplateInstruction(SideEffects::None()) {}
1364
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001365 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001366
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001367 HBasicBlock* GetSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001368 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001369 }
1370
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001371 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001372
1373 private:
1374 DISALLOW_COPY_AND_ASSIGN(HGoto);
1375};
1376
Dave Allison20dfc792014-06-16 20:44:29 -07001377
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001378// Conditional branch. A block ending with an HIf instruction must have
1379// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001380class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001381 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001382 explicit HIf(HInstruction* input) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001383 SetRawInputAt(0, input);
1384 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001385
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001386 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001387
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001388 HBasicBlock* IfTrueSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001389 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001390 }
1391
1392 HBasicBlock* IfFalseSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001393 return GetBlock()->GetSuccessors().Get(1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001394 }
1395
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001396 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001397
Dave Allison20dfc792014-06-16 20:44:29 -07001398 virtual bool IsIfInstruction() const { return true; }
1399
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001400 private:
1401 DISALLOW_COPY_AND_ASSIGN(HIf);
1402};
1403
Roland Levillain88cb1752014-10-20 16:36:47 +01001404class HUnaryOperation : public HExpression<1> {
1405 public:
1406 HUnaryOperation(Primitive::Type result_type, HInstruction* input)
1407 : HExpression(result_type, SideEffects::None()) {
1408 SetRawInputAt(0, input);
1409 }
1410
1411 HInstruction* GetInput() const { return InputAt(0); }
1412 Primitive::Type GetResultType() const { return GetType(); }
1413
1414 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001415 virtual bool InstructionDataEquals(HInstruction* other) const {
1416 UNUSED(other);
1417 return true;
1418 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001419
Roland Levillain9240d6a2014-10-20 16:47:04 +01001420 // Try to statically evaluate `operation` and return a HConstant
1421 // containing the result of this evaluation. If `operation` cannot
1422 // be evaluated as a constant, return nullptr.
1423 HConstant* TryStaticEvaluation() const;
1424
1425 // Apply this operation to `x`.
1426 virtual int32_t Evaluate(int32_t x) const = 0;
1427 virtual int64_t Evaluate(int64_t x) const = 0;
1428
Roland Levillain88cb1752014-10-20 16:36:47 +01001429 DECLARE_INSTRUCTION(UnaryOperation);
1430
1431 private:
1432 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
1433};
1434
Dave Allison20dfc792014-06-16 20:44:29 -07001435class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001436 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001437 HBinaryOperation(Primitive::Type result_type,
1438 HInstruction* left,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001439 HInstruction* right) : HExpression(result_type, SideEffects::None()) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001440 SetRawInputAt(0, left);
1441 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001442 }
1443
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001444 HInstruction* GetLeft() const { return InputAt(0); }
1445 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07001446 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001447
1448 virtual bool IsCommutative() { return false; }
1449
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001450 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001451 virtual bool InstructionDataEquals(HInstruction* other) const {
1452 UNUSED(other);
1453 return true;
1454 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001455
Roland Levillain9240d6a2014-10-20 16:47:04 +01001456 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01001457 // containing the result of this evaluation. If `operation` cannot
1458 // be evaluated as a constant, return nullptr.
Roland Levillain9240d6a2014-10-20 16:47:04 +01001459 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01001460
1461 // Apply this operation to `x` and `y`.
1462 virtual int32_t Evaluate(int32_t x, int32_t y) const = 0;
1463 virtual int64_t Evaluate(int64_t x, int64_t y) const = 0;
1464
Roland Levillainccc07a92014-09-16 14:48:16 +01001465 DECLARE_INSTRUCTION(BinaryOperation);
1466
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001467 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001468 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
1469};
1470
Dave Allison20dfc792014-06-16 20:44:29 -07001471class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001472 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001473 HCondition(HInstruction* first, HInstruction* second)
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001474 : HBinaryOperation(Primitive::kPrimBoolean, first, second),
1475 needs_materialization_(true) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001476
1477 virtual bool IsCommutative() { return true; }
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001478
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001479 bool NeedsMaterialization() const { return needs_materialization_; }
1480 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001481
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001482 // For code generation purposes, returns whether this instruction is just before
1483 // `if_`, and disregard moves in between.
1484 bool IsBeforeWhenDisregardMoves(HIf* if_) const;
1485
Dave Allison20dfc792014-06-16 20:44:29 -07001486 DECLARE_INSTRUCTION(Condition);
1487
1488 virtual IfCondition GetCondition() const = 0;
1489
1490 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001491 // For register allocation purposes, returns whether this instruction needs to be
1492 // materialized (that is, not just be in the processor flags).
1493 bool needs_materialization_;
1494
Dave Allison20dfc792014-06-16 20:44:29 -07001495 DISALLOW_COPY_AND_ASSIGN(HCondition);
1496};
1497
1498// Instruction to check if two inputs are equal to each other.
1499class HEqual : public HCondition {
1500 public:
1501 HEqual(HInstruction* first, HInstruction* second)
1502 : HCondition(first, second) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001503
Roland Levillain93445682014-10-06 19:24:02 +01001504 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1505 return x == y ? 1 : 0;
1506 }
1507 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1508 return x == y ? 1 : 0;
1509 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001510
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001511 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001512
Dave Allison20dfc792014-06-16 20:44:29 -07001513 virtual IfCondition GetCondition() const {
1514 return kCondEQ;
1515 }
1516
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001517 private:
1518 DISALLOW_COPY_AND_ASSIGN(HEqual);
1519};
1520
Dave Allison20dfc792014-06-16 20:44:29 -07001521class HNotEqual : public HCondition {
1522 public:
1523 HNotEqual(HInstruction* first, HInstruction* second)
1524 : HCondition(first, second) {}
1525
Roland Levillain93445682014-10-06 19:24:02 +01001526 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1527 return x != y ? 1 : 0;
1528 }
1529 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1530 return x != y ? 1 : 0;
1531 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001532
Dave Allison20dfc792014-06-16 20:44:29 -07001533 DECLARE_INSTRUCTION(NotEqual);
1534
1535 virtual IfCondition GetCondition() const {
1536 return kCondNE;
1537 }
1538
1539 private:
1540 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
1541};
1542
1543class HLessThan : public HCondition {
1544 public:
1545 HLessThan(HInstruction* first, HInstruction* second)
1546 : HCondition(first, second) {}
1547
Roland Levillain93445682014-10-06 19:24:02 +01001548 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1549 return x < y ? 1 : 0;
1550 }
1551 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1552 return x < y ? 1 : 0;
1553 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001554
Dave Allison20dfc792014-06-16 20:44:29 -07001555 DECLARE_INSTRUCTION(LessThan);
1556
1557 virtual IfCondition GetCondition() const {
1558 return kCondLT;
1559 }
1560
1561 private:
1562 DISALLOW_COPY_AND_ASSIGN(HLessThan);
1563};
1564
1565class HLessThanOrEqual : public HCondition {
1566 public:
1567 HLessThanOrEqual(HInstruction* first, HInstruction* second)
1568 : HCondition(first, second) {}
1569
Roland Levillain93445682014-10-06 19:24:02 +01001570 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1571 return x <= y ? 1 : 0;
1572 }
1573 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1574 return x <= y ? 1 : 0;
1575 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001576
Dave Allison20dfc792014-06-16 20:44:29 -07001577 DECLARE_INSTRUCTION(LessThanOrEqual);
1578
1579 virtual IfCondition GetCondition() const {
1580 return kCondLE;
1581 }
1582
1583 private:
1584 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
1585};
1586
1587class HGreaterThan : public HCondition {
1588 public:
1589 HGreaterThan(HInstruction* first, HInstruction* second)
1590 : HCondition(first, second) {}
1591
Roland Levillain93445682014-10-06 19:24:02 +01001592 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1593 return x > y ? 1 : 0;
1594 }
1595 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1596 return x > y ? 1 : 0;
1597 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001598
Dave Allison20dfc792014-06-16 20:44:29 -07001599 DECLARE_INSTRUCTION(GreaterThan);
1600
1601 virtual IfCondition GetCondition() const {
1602 return kCondGT;
1603 }
1604
1605 private:
1606 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
1607};
1608
1609class HGreaterThanOrEqual : public HCondition {
1610 public:
1611 HGreaterThanOrEqual(HInstruction* first, HInstruction* second)
1612 : HCondition(first, second) {}
1613
Roland Levillain93445682014-10-06 19:24:02 +01001614 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1615 return x >= y ? 1 : 0;
1616 }
1617 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1618 return x >= y ? 1 : 0;
1619 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001620
Dave Allison20dfc792014-06-16 20:44:29 -07001621 DECLARE_INSTRUCTION(GreaterThanOrEqual);
1622
1623 virtual IfCondition GetCondition() const {
1624 return kCondGE;
1625 }
1626
1627 private:
1628 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
1629};
1630
1631
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001632// Instruction to check how two inputs compare to each other.
1633// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
1634class HCompare : public HBinaryOperation {
1635 public:
Calin Juravleddb7df22014-11-25 20:56:51 +00001636 // The bias applies for floating point operations and indicates how NaN
1637 // comparisons are treated:
1638 enum Bias {
1639 kNoBias, // bias is not applicable (i.e. for long operation)
1640 kGtBias, // return 1 for NaN comparisons
1641 kLtBias, // return -1 for NaN comparisons
1642 };
1643
1644 HCompare(Primitive::Type type, HInstruction* first, HInstruction* second, Bias bias)
1645 : HBinaryOperation(Primitive::kPrimInt, first, second), bias_(bias) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001646 DCHECK_EQ(type, first->GetType());
1647 DCHECK_EQ(type, second->GetType());
1648 }
1649
Calin Juravleddb7df22014-11-25 20:56:51 +00001650 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01001651 return
1652 x == y ? 0 :
1653 x > y ? 1 :
1654 -1;
1655 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001656
1657 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01001658 return
1659 x == y ? 0 :
1660 x > y ? 1 :
1661 -1;
1662 }
1663
Calin Juravleddb7df22014-11-25 20:56:51 +00001664 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
1665 return bias_ == other->AsCompare()->bias_;
1666 }
1667
1668 bool IsGtBias() { return bias_ == kGtBias; }
1669
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001670 DECLARE_INSTRUCTION(Compare);
1671
1672 private:
Calin Juravleddb7df22014-11-25 20:56:51 +00001673 const Bias bias_;
1674
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001675 DISALLOW_COPY_AND_ASSIGN(HCompare);
1676};
1677
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001678// A local in the graph. Corresponds to a Dex register.
1679class HLocal : public HTemplateInstruction<0> {
1680 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001681 explicit HLocal(uint16_t reg_number)
1682 : HTemplateInstruction(SideEffects::None()), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001683
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001684 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001685
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001686 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001687
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001688 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001689 // The Dex register number.
1690 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001691
1692 DISALLOW_COPY_AND_ASSIGN(HLocal);
1693};
1694
1695// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07001696class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001697 public:
Roland Levillain5799fc02014-09-25 12:15:20 +01001698 HLoadLocal(HLocal* local, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001699 : HExpression(type, SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001700 SetRawInputAt(0, local);
1701 }
1702
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001703 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1704
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001705 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001706
1707 private:
1708 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
1709};
1710
1711// Store a value in a given local. This instruction has two inputs: the value
1712// and the local.
1713class HStoreLocal : public HTemplateInstruction<2> {
1714 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001715 HStoreLocal(HLocal* local, HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001716 SetRawInputAt(0, local);
1717 SetRawInputAt(1, value);
1718 }
1719
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001720 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1721
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001722 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001723
1724 private:
1725 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
1726};
1727
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001728class HConstant : public HExpression<0> {
1729 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001730 explicit HConstant(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
1731
1732 virtual bool CanBeMoved() const { return true; }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001733
1734 DECLARE_INSTRUCTION(Constant);
1735
1736 private:
1737 DISALLOW_COPY_AND_ASSIGN(HConstant);
1738};
1739
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001740class HFloatConstant : public HConstant {
1741 public:
1742 explicit HFloatConstant(float value) : HConstant(Primitive::kPrimFloat), value_(value) {}
1743
1744 float GetValue() const { return value_; }
1745
1746 virtual bool InstructionDataEquals(HInstruction* other) const {
1747 return bit_cast<float, int32_t>(other->AsFloatConstant()->value_) ==
1748 bit_cast<float, int32_t>(value_);
1749 }
1750
1751 virtual size_t ComputeHashCode() const { return static_cast<size_t>(GetValue()); }
1752
1753 DECLARE_INSTRUCTION(FloatConstant);
1754
1755 private:
1756 const float value_;
1757
1758 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
1759};
1760
1761class HDoubleConstant : public HConstant {
1762 public:
1763 explicit HDoubleConstant(double value) : HConstant(Primitive::kPrimDouble), value_(value) {}
1764
1765 double GetValue() const { return value_; }
1766
1767 virtual bool InstructionDataEquals(HInstruction* other) const {
1768 return bit_cast<double, int64_t>(other->AsDoubleConstant()->value_) ==
1769 bit_cast<double, int64_t>(value_);
1770 }
1771
1772 virtual size_t ComputeHashCode() const { return static_cast<size_t>(GetValue()); }
1773
1774 DECLARE_INSTRUCTION(DoubleConstant);
1775
1776 private:
1777 const double value_;
1778
1779 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
1780};
1781
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001782class HNullConstant : public HConstant {
1783 public:
1784 HNullConstant() : HConstant(Primitive::kPrimNot) {}
1785
1786 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
1787 return true;
1788 }
1789
1790 size_t ComputeHashCode() const OVERRIDE { return 0; }
1791
1792 DECLARE_INSTRUCTION(NullConstant);
1793
1794 private:
1795 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
1796};
1797
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001798// Constants of the type int. Those can be from Dex instructions, or
1799// synthesized (for example with the if-eqz instruction).
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001800class HIntConstant : public HConstant {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001801 public:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001802 explicit HIntConstant(int32_t value) : HConstant(Primitive::kPrimInt), value_(value) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001803
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001804 int32_t GetValue() const { return value_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001805
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001806 virtual bool InstructionDataEquals(HInstruction* other) const {
1807 return other->AsIntConstant()->value_ == value_;
1808 }
1809
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001810 virtual size_t ComputeHashCode() const { return GetValue(); }
1811
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001812 DECLARE_INSTRUCTION(IntConstant);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001813
1814 private:
1815 const int32_t value_;
1816
1817 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
1818};
1819
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001820class HLongConstant : public HConstant {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001821 public:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001822 explicit HLongConstant(int64_t value) : HConstant(Primitive::kPrimLong), value_(value) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001823
1824 int64_t GetValue() const { return value_; }
1825
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001826 virtual bool InstructionDataEquals(HInstruction* other) const {
1827 return other->AsLongConstant()->value_ == value_;
1828 }
1829
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001830 virtual size_t ComputeHashCode() const { return static_cast<size_t>(GetValue()); }
1831
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001832 DECLARE_INSTRUCTION(LongConstant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001833
1834 private:
1835 const int64_t value_;
1836
1837 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
1838};
1839
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001840enum class Intrinsics {
1841#define OPTIMIZING_INTRINSICS(Name, IsStatic) k ## Name,
1842#include "intrinsics_list.h"
1843 kNone,
1844 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
1845#undef INTRINSICS_LIST
1846#undef OPTIMIZING_INTRINSICS
1847};
1848std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
1849
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001850class HInvoke : public HInstruction {
1851 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001852 virtual size_t InputCount() const { return inputs_.Size(); }
1853 virtual HInstruction* InputAt(size_t i) const { return inputs_.Get(i); }
1854
1855 // Runtime needs to walk the stack, so Dex -> Dex calls need to
1856 // know their environment.
Calin Juravle77520bc2015-01-12 18:45:46 +00001857 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001858
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001859 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001860 SetRawInputAt(index, argument);
1861 }
1862
1863 virtual void SetRawInputAt(size_t index, HInstruction* input) {
1864 inputs_.Put(index, input);
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001865 }
1866
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001867 virtual Primitive::Type GetType() const { return return_type_; }
1868
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001869 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001870
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001871 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
1872
1873 Intrinsics GetIntrinsic() {
1874 return intrinsic_;
1875 }
1876
1877 void SetIntrinsic(Intrinsics intrinsic) {
1878 intrinsic_ = intrinsic;
1879 }
1880
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001881 DECLARE_INSTRUCTION(Invoke);
1882
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001883 protected:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001884 HInvoke(ArenaAllocator* arena,
1885 uint32_t number_of_arguments,
1886 Primitive::Type return_type,
1887 uint32_t dex_pc,
1888 uint32_t dex_method_index)
1889 : HInstruction(SideEffects::All()),
1890 inputs_(arena, number_of_arguments),
1891 return_type_(return_type),
1892 dex_pc_(dex_pc),
1893 dex_method_index_(dex_method_index),
1894 intrinsic_(Intrinsics::kNone) {
1895 inputs_.SetSize(number_of_arguments);
1896 }
1897
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001898 GrowableArray<HInstruction*> inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001899 const Primitive::Type return_type_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001900 const uint32_t dex_pc_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001901 const uint32_t dex_method_index_;
1902 Intrinsics intrinsic_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001903
1904 private:
1905 DISALLOW_COPY_AND_ASSIGN(HInvoke);
1906};
1907
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001908class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001909 public:
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001910 HInvokeStaticOrDirect(ArenaAllocator* arena,
1911 uint32_t number_of_arguments,
1912 Primitive::Type return_type,
1913 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001914 uint32_t dex_method_index,
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001915 bool is_recursive,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001916 InvokeType invoke_type)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001917 : HInvoke(arena, number_of_arguments, return_type, dex_pc, dex_method_index),
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001918 invoke_type_(invoke_type),
1919 is_recursive_(is_recursive) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001920
Calin Juravle77520bc2015-01-12 18:45:46 +00001921 bool CanDoImplicitNullCheck() const OVERRIDE {
1922 // We access the method via the dex cache so we can't do an implicit null check.
1923 // TODO: for intrinsics we can generate implicit null checks.
1924 return false;
1925 }
1926
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001927 InvokeType GetInvokeType() const { return invoke_type_; }
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001928 bool IsRecursive() const { return is_recursive_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001929
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001930 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001931
1932 private:
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001933 const InvokeType invoke_type_;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001934 const bool is_recursive_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001935
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001936 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001937};
1938
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001939class HInvokeVirtual : public HInvoke {
1940 public:
1941 HInvokeVirtual(ArenaAllocator* arena,
1942 uint32_t number_of_arguments,
1943 Primitive::Type return_type,
1944 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001945 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001946 uint32_t vtable_index)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001947 : HInvoke(arena, number_of_arguments, return_type, dex_pc, dex_method_index),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001948 vtable_index_(vtable_index) {}
1949
Calin Juravle77520bc2015-01-12 18:45:46 +00001950 bool CanDoImplicitNullCheck() const OVERRIDE {
1951 // TODO: Add implicit null checks in intrinsics.
1952 return !GetLocations()->Intrinsified();
1953 }
1954
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001955 uint32_t GetVTableIndex() const { return vtable_index_; }
1956
1957 DECLARE_INSTRUCTION(InvokeVirtual);
1958
1959 private:
1960 const uint32_t vtable_index_;
1961
1962 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
1963};
1964
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001965class HInvokeInterface : public HInvoke {
1966 public:
1967 HInvokeInterface(ArenaAllocator* arena,
1968 uint32_t number_of_arguments,
1969 Primitive::Type return_type,
1970 uint32_t dex_pc,
1971 uint32_t dex_method_index,
1972 uint32_t imt_index)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001973 : HInvoke(arena, number_of_arguments, return_type, dex_pc, dex_method_index),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001974 imt_index_(imt_index) {}
1975
Calin Juravle77520bc2015-01-12 18:45:46 +00001976 bool CanDoImplicitNullCheck() const OVERRIDE {
1977 // TODO: Add implicit null checks in intrinsics.
1978 return !GetLocations()->Intrinsified();
1979 }
1980
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001981 uint32_t GetImtIndex() const { return imt_index_; }
1982 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
1983
1984 DECLARE_INSTRUCTION(InvokeInterface);
1985
1986 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001987 const uint32_t imt_index_;
1988
1989 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
1990};
1991
Dave Allison20dfc792014-06-16 20:44:29 -07001992class HNewInstance : public HExpression<0> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001993 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00001994 HNewInstance(uint32_t dex_pc, uint16_t type_index, QuickEntrypointEnum entrypoint)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001995 : HExpression(Primitive::kPrimNot, SideEffects::None()),
1996 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00001997 type_index_(type_index),
1998 entrypoint_(entrypoint) {}
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001999
2000 uint32_t GetDexPc() const { return dex_pc_; }
2001 uint16_t GetTypeIndex() const { return type_index_; }
2002
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002003 // Calls runtime so needs an environment.
Calin Juravle92a6ed22014-12-02 18:58:03 +00002004 bool NeedsEnvironment() const OVERRIDE { return true; }
2005 // It may throw when called on:
2006 // - interfaces
2007 // - abstract/innaccessible/unknown classes
2008 // TODO: optimize when possible.
2009 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002010
Calin Juravle10e244f2015-01-26 18:54:32 +00002011 bool CanBeNull() const OVERRIDE { return false; }
2012
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002013 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
2014
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002015 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002016
2017 private:
2018 const uint32_t dex_pc_;
2019 const uint16_t type_index_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002020 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002021
2022 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
2023};
2024
Roland Levillain88cb1752014-10-20 16:36:47 +01002025class HNeg : public HUnaryOperation {
2026 public:
2027 explicit HNeg(Primitive::Type result_type, HInstruction* input)
2028 : HUnaryOperation(result_type, input) {}
2029
Roland Levillain9240d6a2014-10-20 16:47:04 +01002030 virtual int32_t Evaluate(int32_t x) const OVERRIDE { return -x; }
2031 virtual int64_t Evaluate(int64_t x) const OVERRIDE { return -x; }
2032
Roland Levillain88cb1752014-10-20 16:36:47 +01002033 DECLARE_INSTRUCTION(Neg);
2034
2035 private:
2036 DISALLOW_COPY_AND_ASSIGN(HNeg);
2037};
2038
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002039class HNewArray : public HExpression<1> {
2040 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002041 HNewArray(HInstruction* length,
2042 uint32_t dex_pc,
2043 uint16_t type_index,
2044 QuickEntrypointEnum entrypoint)
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002045 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2046 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002047 type_index_(type_index),
2048 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002049 SetRawInputAt(0, length);
2050 }
2051
2052 uint32_t GetDexPc() const { return dex_pc_; }
2053 uint16_t GetTypeIndex() const { return type_index_; }
2054
2055 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00002056 bool NeedsEnvironment() const OVERRIDE { return true; }
2057
2058 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002059
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002060 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
2061
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002062 DECLARE_INSTRUCTION(NewArray);
2063
2064 private:
2065 const uint32_t dex_pc_;
2066 const uint16_t type_index_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002067 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002068
2069 DISALLOW_COPY_AND_ASSIGN(HNewArray);
2070};
2071
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002072class HAdd : public HBinaryOperation {
2073 public:
2074 HAdd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2075 : HBinaryOperation(result_type, left, right) {}
2076
2077 virtual bool IsCommutative() { return true; }
2078
Roland Levillain93445682014-10-06 19:24:02 +01002079 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
2080 return x + y;
2081 }
2082 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
2083 return x + y;
2084 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002085
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002086 DECLARE_INSTRUCTION(Add);
2087
2088 private:
2089 DISALLOW_COPY_AND_ASSIGN(HAdd);
2090};
2091
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002092class HSub : public HBinaryOperation {
2093 public:
2094 HSub(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2095 : HBinaryOperation(result_type, left, right) {}
2096
Roland Levillain93445682014-10-06 19:24:02 +01002097 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
2098 return x - y;
2099 }
2100 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
2101 return x - y;
2102 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002103
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002104 DECLARE_INSTRUCTION(Sub);
2105
2106 private:
2107 DISALLOW_COPY_AND_ASSIGN(HSub);
2108};
2109
Calin Juravle34bacdf2014-10-07 20:23:36 +01002110class HMul : public HBinaryOperation {
2111 public:
2112 HMul(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2113 : HBinaryOperation(result_type, left, right) {}
2114
2115 virtual bool IsCommutative() { return true; }
2116
2117 virtual int32_t Evaluate(int32_t x, int32_t y) const { return x * y; }
2118 virtual int64_t Evaluate(int64_t x, int64_t y) const { return x * y; }
2119
2120 DECLARE_INSTRUCTION(Mul);
2121
2122 private:
2123 DISALLOW_COPY_AND_ASSIGN(HMul);
2124};
2125
Calin Juravle7c4954d2014-10-28 16:57:40 +00002126class HDiv : public HBinaryOperation {
2127 public:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002128 HDiv(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
2129 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00002130
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00002131 virtual int32_t Evaluate(int32_t x, int32_t y) const {
2132 // Our graph structure ensures we never have 0 for `y` during constant folding.
2133 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00002134 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00002135 return (y == -1) ? -x : x / y;
2136 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002137
2138 virtual int64_t Evaluate(int64_t x, int64_t y) const {
2139 DCHECK_NE(y, 0);
2140 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2141 return (y == -1) ? -x : x / y;
2142 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002143
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002144 uint32_t GetDexPc() const { return dex_pc_; }
2145
Calin Juravle7c4954d2014-10-28 16:57:40 +00002146 DECLARE_INSTRUCTION(Div);
2147
2148 private:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002149 const uint32_t dex_pc_;
2150
Calin Juravle7c4954d2014-10-28 16:57:40 +00002151 DISALLOW_COPY_AND_ASSIGN(HDiv);
2152};
2153
Calin Juravlebacfec32014-11-14 15:54:36 +00002154class HRem : public HBinaryOperation {
2155 public:
2156 HRem(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
2157 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
2158
2159 virtual int32_t Evaluate(int32_t x, int32_t y) const {
2160 DCHECK_NE(y, 0);
2161 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2162 return (y == -1) ? 0 : x % y;
2163 }
2164
2165 virtual int64_t Evaluate(int64_t x, int64_t y) const {
2166 DCHECK_NE(y, 0);
2167 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2168 return (y == -1) ? 0 : x % y;
2169 }
2170
2171 uint32_t GetDexPc() const { return dex_pc_; }
2172
2173 DECLARE_INSTRUCTION(Rem);
2174
2175 private:
2176 const uint32_t dex_pc_;
2177
2178 DISALLOW_COPY_AND_ASSIGN(HRem);
2179};
2180
Calin Juravled0d48522014-11-04 16:40:20 +00002181class HDivZeroCheck : public HExpression<1> {
2182 public:
2183 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
2184 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
2185 SetRawInputAt(0, value);
2186 }
2187
2188 bool CanBeMoved() const OVERRIDE { return true; }
2189
2190 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2191 UNUSED(other);
2192 return true;
2193 }
2194
2195 bool NeedsEnvironment() const OVERRIDE { return true; }
2196 bool CanThrow() const OVERRIDE { return true; }
2197
2198 uint32_t GetDexPc() const { return dex_pc_; }
2199
2200 DECLARE_INSTRUCTION(DivZeroCheck);
2201
2202 private:
2203 const uint32_t dex_pc_;
2204
2205 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
2206};
2207
Calin Juravle9aec02f2014-11-18 23:06:35 +00002208class HShl : public HBinaryOperation {
2209 public:
2210 HShl(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2211 : HBinaryOperation(result_type, left, right) {}
2212
2213 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x << (y & kMaxIntShiftValue); }
2214 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x << (y & kMaxLongShiftValue); }
2215
2216 DECLARE_INSTRUCTION(Shl);
2217
2218 private:
2219 DISALLOW_COPY_AND_ASSIGN(HShl);
2220};
2221
2222class HShr : public HBinaryOperation {
2223 public:
2224 HShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2225 : HBinaryOperation(result_type, left, right) {}
2226
2227 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x >> (y & kMaxIntShiftValue); }
2228 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x >> (y & kMaxLongShiftValue); }
2229
2230 DECLARE_INSTRUCTION(Shr);
2231
2232 private:
2233 DISALLOW_COPY_AND_ASSIGN(HShr);
2234};
2235
2236class HUShr : public HBinaryOperation {
2237 public:
2238 HUShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2239 : HBinaryOperation(result_type, left, right) {}
2240
2241 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
2242 uint32_t ux = static_cast<uint32_t>(x);
2243 uint32_t uy = static_cast<uint32_t>(y) & kMaxIntShiftValue;
2244 return static_cast<int32_t>(ux >> uy);
2245 }
2246
2247 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
2248 uint64_t ux = static_cast<uint64_t>(x);
2249 uint64_t uy = static_cast<uint64_t>(y) & kMaxLongShiftValue;
2250 return static_cast<int64_t>(ux >> uy);
2251 }
2252
2253 DECLARE_INSTRUCTION(UShr);
2254
2255 private:
2256 DISALLOW_COPY_AND_ASSIGN(HUShr);
2257};
2258
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002259class HAnd : public HBinaryOperation {
2260 public:
2261 HAnd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2262 : HBinaryOperation(result_type, left, right) {}
2263
2264 bool IsCommutative() OVERRIDE { return true; }
2265
2266 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x & y; }
2267 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x & y; }
2268
2269 DECLARE_INSTRUCTION(And);
2270
2271 private:
2272 DISALLOW_COPY_AND_ASSIGN(HAnd);
2273};
2274
2275class HOr : public HBinaryOperation {
2276 public:
2277 HOr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2278 : HBinaryOperation(result_type, left, right) {}
2279
2280 bool IsCommutative() OVERRIDE { return true; }
2281
2282 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x | y; }
2283 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x | y; }
2284
2285 DECLARE_INSTRUCTION(Or);
2286
2287 private:
2288 DISALLOW_COPY_AND_ASSIGN(HOr);
2289};
2290
2291class HXor : public HBinaryOperation {
2292 public:
2293 HXor(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2294 : HBinaryOperation(result_type, left, right) {}
2295
2296 bool IsCommutative() OVERRIDE { return true; }
2297
2298 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x ^ y; }
2299 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x ^ y; }
2300
2301 DECLARE_INSTRUCTION(Xor);
2302
2303 private:
2304 DISALLOW_COPY_AND_ASSIGN(HXor);
2305};
2306
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002307// The value of a parameter in this method. Its location depends on
2308// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07002309class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002310 public:
Calin Juravle10e244f2015-01-26 18:54:32 +00002311 HParameterValue(uint8_t index, Primitive::Type parameter_type, bool is_this = false)
2312 : HExpression(parameter_type, SideEffects::None()), index_(index), is_this_(is_this) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002313
2314 uint8_t GetIndex() const { return index_; }
2315
Calin Juravle10e244f2015-01-26 18:54:32 +00002316 bool CanBeNull() const OVERRIDE { return !is_this_; }
2317
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002318 DECLARE_INSTRUCTION(ParameterValue);
2319
2320 private:
2321 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00002322 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002323 const uint8_t index_;
2324
Calin Juravle10e244f2015-01-26 18:54:32 +00002325 // Whether or not the parameter value corresponds to 'this' argument.
2326 const bool is_this_;
2327
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002328 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
2329};
2330
Roland Levillain1cc5f252014-10-22 18:06:21 +01002331class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002332 public:
Roland Levillain1cc5f252014-10-22 18:06:21 +01002333 explicit HNot(Primitive::Type result_type, HInstruction* input)
2334 : HUnaryOperation(result_type, input) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002335
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002336 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002337 virtual bool InstructionDataEquals(HInstruction* other) const {
2338 UNUSED(other);
2339 return true;
2340 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002341
Roland Levillain1cc5f252014-10-22 18:06:21 +01002342 virtual int32_t Evaluate(int32_t x) const OVERRIDE { return ~x; }
2343 virtual int64_t Evaluate(int64_t x) const OVERRIDE { return ~x; }
2344
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002345 DECLARE_INSTRUCTION(Not);
2346
2347 private:
2348 DISALLOW_COPY_AND_ASSIGN(HNot);
2349};
2350
Roland Levillaindff1f282014-11-05 14:15:05 +00002351class HTypeConversion : public HExpression<1> {
2352 public:
2353 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00002354 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
2355 : HExpression(result_type, SideEffects::None()), dex_pc_(dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002356 SetRawInputAt(0, input);
2357 DCHECK_NE(input->GetType(), result_type);
2358 }
2359
2360 HInstruction* GetInput() const { return InputAt(0); }
2361 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
2362 Primitive::Type GetResultType() const { return GetType(); }
2363
Roland Levillain624279f2014-12-04 11:54:28 +00002364 // Required by the x86 and ARM code generators when producing calls
2365 // to the runtime.
2366 uint32_t GetDexPc() const { return dex_pc_; }
2367
Roland Levillaindff1f282014-11-05 14:15:05 +00002368 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00002369 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00002370
2371 DECLARE_INSTRUCTION(TypeConversion);
2372
2373 private:
Roland Levillain624279f2014-12-04 11:54:28 +00002374 const uint32_t dex_pc_;
2375
Roland Levillaindff1f282014-11-05 14:15:05 +00002376 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
2377};
2378
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00002379static constexpr uint32_t kNoRegNumber = -1;
2380
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002381class HPhi : public HInstruction {
2382 public:
2383 HPhi(ArenaAllocator* arena, uint32_t reg_number, size_t number_of_inputs, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002384 : HInstruction(SideEffects::None()),
2385 inputs_(arena, number_of_inputs),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002386 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002387 type_(type),
Calin Juravle10e244f2015-01-26 18:54:32 +00002388 is_live_(false),
2389 can_be_null_(true) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002390 inputs_.SetSize(number_of_inputs);
2391 }
2392
Calin Juravle10e244f2015-01-26 18:54:32 +00002393 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
2394 HInstruction* InputAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002395
Calin Juravle10e244f2015-01-26 18:54:32 +00002396 void SetRawInputAt(size_t index, HInstruction* input) OVERRIDE {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002397 inputs_.Put(index, input);
2398 }
2399
2400 void AddInput(HInstruction* input);
2401
Calin Juravle10e244f2015-01-26 18:54:32 +00002402 Primitive::Type GetType() const OVERRIDE { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002403 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002404
Calin Juravle10e244f2015-01-26 18:54:32 +00002405 bool CanBeNull() const OVERRIDE { return can_be_null_; }
2406 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
2407
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002408 uint32_t GetRegNumber() const { return reg_number_; }
2409
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002410 void SetDead() { is_live_ = false; }
2411 void SetLive() { is_live_ = true; }
2412 bool IsDead() const { return !is_live_; }
2413 bool IsLive() const { return is_live_; }
2414
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002415 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002416
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002417 private:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002418 GrowableArray<HInstruction*> inputs_;
2419 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002420 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002421 bool is_live_;
Calin Juravle10e244f2015-01-26 18:54:32 +00002422 bool can_be_null_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002423
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002424 DISALLOW_COPY_AND_ASSIGN(HPhi);
2425};
2426
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002427class HNullCheck : public HExpression<1> {
2428 public:
2429 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002430 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002431 SetRawInputAt(0, value);
2432 }
2433
Calin Juravle10e244f2015-01-26 18:54:32 +00002434 bool CanBeMoved() const OVERRIDE { return true; }
2435 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002436 UNUSED(other);
2437 return true;
2438 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002439
Calin Juravle10e244f2015-01-26 18:54:32 +00002440 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002441
Calin Juravle10e244f2015-01-26 18:54:32 +00002442 bool CanThrow() const OVERRIDE { return true; }
2443
2444 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01002445
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002446 uint32_t GetDexPc() const { return dex_pc_; }
2447
2448 DECLARE_INSTRUCTION(NullCheck);
2449
2450 private:
2451 const uint32_t dex_pc_;
2452
2453 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
2454};
2455
2456class FieldInfo : public ValueObject {
2457 public:
Calin Juravle52c48962014-12-16 17:02:57 +00002458 FieldInfo(MemberOffset field_offset, Primitive::Type field_type, bool is_volatile)
2459 : field_offset_(field_offset), field_type_(field_type), is_volatile_(is_volatile) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002460
2461 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002462 Primitive::Type GetFieldType() const { return field_type_; }
Calin Juravle52c48962014-12-16 17:02:57 +00002463 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002464
2465 private:
2466 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01002467 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00002468 const bool is_volatile_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002469};
2470
2471class HInstanceFieldGet : public HExpression<1> {
2472 public:
2473 HInstanceFieldGet(HInstruction* value,
2474 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00002475 MemberOffset field_offset,
2476 bool is_volatile)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002477 : HExpression(field_type, SideEffects::DependsOnSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00002478 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002479 SetRawInputAt(0, value);
2480 }
2481
Calin Juravle10c9cbe2014-12-19 10:50:19 +00002482 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002483
2484 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2485 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
2486 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002487 }
2488
Calin Juravle77520bc2015-01-12 18:45:46 +00002489 bool CanDoImplicitNullCheck() const OVERRIDE {
2490 return GetFieldOffset().Uint32Value() < kPageSize;
2491 }
2492
2493 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002494 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
2495 }
2496
Calin Juravle52c48962014-12-16 17:02:57 +00002497 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002498 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002499 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002500 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002501
2502 DECLARE_INSTRUCTION(InstanceFieldGet);
2503
2504 private:
2505 const FieldInfo field_info_;
2506
2507 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
2508};
2509
2510class HInstanceFieldSet : public HTemplateInstruction<2> {
2511 public:
2512 HInstanceFieldSet(HInstruction* object,
2513 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01002514 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00002515 MemberOffset field_offset,
2516 bool is_volatile)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002517 : HTemplateInstruction(SideEffects::ChangesSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00002518 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002519 SetRawInputAt(0, object);
2520 SetRawInputAt(1, value);
2521 }
2522
Calin Juravle77520bc2015-01-12 18:45:46 +00002523 bool CanDoImplicitNullCheck() const OVERRIDE {
2524 return GetFieldOffset().Uint32Value() < kPageSize;
2525 }
2526
Calin Juravle52c48962014-12-16 17:02:57 +00002527 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002528 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002529 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002530 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002531 HInstruction* GetValue() const { return InputAt(1); }
2532
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002533 DECLARE_INSTRUCTION(InstanceFieldSet);
2534
2535 private:
2536 const FieldInfo field_info_;
2537
2538 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
2539};
2540
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002541class HArrayGet : public HExpression<2> {
2542 public:
2543 HArrayGet(HInstruction* array, HInstruction* index, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002544 : HExpression(type, SideEffects::DependsOnSomething()) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002545 SetRawInputAt(0, array);
2546 SetRawInputAt(1, index);
2547 }
2548
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002549 bool CanBeMoved() const OVERRIDE { return true; }
2550 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002551 UNUSED(other);
2552 return true;
2553 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002554 bool CanDoImplicitNullCheck() const OVERRIDE {
2555 // TODO: We can be smarter here.
2556 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
2557 // which generates the implicit null check. There are cases when these can be removed
2558 // to produce better code. If we ever add optimizations to do so we should allow an
2559 // implicit check here (as long as the address falls in the first page).
2560 return false;
2561 }
2562
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002563 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002564
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002565 HInstruction* GetArray() const { return InputAt(0); }
2566 HInstruction* GetIndex() const { return InputAt(1); }
2567
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002568 DECLARE_INSTRUCTION(ArrayGet);
2569
2570 private:
2571 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
2572};
2573
2574class HArraySet : public HTemplateInstruction<3> {
2575 public:
2576 HArraySet(HInstruction* array,
2577 HInstruction* index,
2578 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002579 Primitive::Type expected_component_type,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002580 uint32_t dex_pc)
2581 : HTemplateInstruction(SideEffects::ChangesSomething()),
2582 dex_pc_(dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002583 expected_component_type_(expected_component_type),
2584 needs_type_check_(value->GetType() == Primitive::kPrimNot) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002585 SetRawInputAt(0, array);
2586 SetRawInputAt(1, index);
2587 SetRawInputAt(2, value);
2588 }
2589
Calin Juravle77520bc2015-01-12 18:45:46 +00002590 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002591 // We currently always call a runtime method to catch array store
2592 // exceptions.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002593 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002594 }
2595
Calin Juravle77520bc2015-01-12 18:45:46 +00002596 bool CanDoImplicitNullCheck() const OVERRIDE {
2597 // TODO: Same as for ArrayGet.
2598 return false;
2599 }
2600
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002601 void ClearNeedsTypeCheck() {
2602 needs_type_check_ = false;
2603 }
2604
2605 bool NeedsTypeCheck() const { return needs_type_check_; }
2606
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002607 uint32_t GetDexPc() const { return dex_pc_; }
2608
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002609 HInstruction* GetArray() const { return InputAt(0); }
2610 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002611 HInstruction* GetValue() const { return InputAt(2); }
2612
2613 Primitive::Type GetComponentType() const {
2614 // The Dex format does not type floating point index operations. Since the
2615 // `expected_component_type_` is set during building and can therefore not
2616 // be correct, we also check what is the value type. If it is a floating
2617 // point type, we must use that type.
2618 Primitive::Type value_type = GetValue()->GetType();
2619 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
2620 ? value_type
2621 : expected_component_type_;
2622 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002623
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002624 DECLARE_INSTRUCTION(ArraySet);
2625
2626 private:
2627 const uint32_t dex_pc_;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002628 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002629 bool needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002630
2631 DISALLOW_COPY_AND_ASSIGN(HArraySet);
2632};
2633
2634class HArrayLength : public HExpression<1> {
2635 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002636 explicit HArrayLength(HInstruction* array)
2637 : HExpression(Primitive::kPrimInt, SideEffects::None()) {
2638 // Note that arrays do not change length, so the instruction does not
2639 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002640 SetRawInputAt(0, array);
2641 }
2642
Calin Juravle77520bc2015-01-12 18:45:46 +00002643 bool CanBeMoved() const OVERRIDE { return true; }
2644 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002645 UNUSED(other);
2646 return true;
2647 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002648 bool CanDoImplicitNullCheck() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002649
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002650 DECLARE_INSTRUCTION(ArrayLength);
2651
2652 private:
2653 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
2654};
2655
2656class HBoundsCheck : public HExpression<2> {
2657 public:
2658 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002659 : HExpression(index->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002660 DCHECK(index->GetType() == Primitive::kPrimInt);
2661 SetRawInputAt(0, index);
2662 SetRawInputAt(1, length);
2663 }
2664
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002665 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002666 virtual bool InstructionDataEquals(HInstruction* other) const {
2667 UNUSED(other);
2668 return true;
2669 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002670
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002671 virtual bool NeedsEnvironment() const { return true; }
2672
Roland Levillaine161a2a2014-10-03 12:45:18 +01002673 virtual bool CanThrow() const { return true; }
2674
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002675 uint32_t GetDexPc() const { return dex_pc_; }
2676
2677 DECLARE_INSTRUCTION(BoundsCheck);
2678
2679 private:
2680 const uint32_t dex_pc_;
2681
2682 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
2683};
2684
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002685/**
2686 * Some DEX instructions are folded into multiple HInstructions that need
2687 * to stay live until the last HInstruction. This class
2688 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00002689 * HInstruction stays live. `index` represents the stack location index of the
2690 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002691 */
2692class HTemporary : public HTemplateInstruction<0> {
2693 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002694 explicit HTemporary(size_t index) : HTemplateInstruction(SideEffects::None()), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002695
2696 size_t GetIndex() const { return index_; }
2697
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00002698 Primitive::Type GetType() const OVERRIDE {
2699 // The previous instruction is the one that will be stored in the temporary location.
2700 DCHECK(GetPrevious() != nullptr);
2701 return GetPrevious()->GetType();
2702 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00002703
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002704 DECLARE_INSTRUCTION(Temporary);
2705
2706 private:
2707 const size_t index_;
2708
2709 DISALLOW_COPY_AND_ASSIGN(HTemporary);
2710};
2711
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002712class HSuspendCheck : public HTemplateInstruction<0> {
2713 public:
2714 explicit HSuspendCheck(uint32_t dex_pc)
Nicolas Geoffray9ebc72c2014-09-25 16:33:42 +01002715 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002716
2717 virtual bool NeedsEnvironment() const {
2718 return true;
2719 }
2720
2721 uint32_t GetDexPc() const { return dex_pc_; }
2722
2723 DECLARE_INSTRUCTION(SuspendCheck);
2724
2725 private:
2726 const uint32_t dex_pc_;
2727
2728 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
2729};
2730
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002731/**
2732 * Instruction to load a Class object.
2733 */
2734class HLoadClass : public HExpression<0> {
2735 public:
2736 HLoadClass(uint16_t type_index,
2737 bool is_referrers_class,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002738 uint32_t dex_pc)
2739 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2740 type_index_(type_index),
2741 is_referrers_class_(is_referrers_class),
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002742 dex_pc_(dex_pc),
2743 generate_clinit_check_(false) {}
2744
2745 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002746
2747 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2748 return other->AsLoadClass()->type_index_ == type_index_;
2749 }
2750
2751 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
2752
2753 uint32_t GetDexPc() const { return dex_pc_; }
2754 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002755 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002756
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002757 bool NeedsEnvironment() const OVERRIDE {
2758 // Will call runtime and load the class if the class is not loaded yet.
2759 // TODO: finer grain decision.
2760 return !is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002761 }
2762
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002763 bool MustGenerateClinitCheck() const {
2764 return generate_clinit_check_;
2765 }
2766
2767 void SetMustGenerateClinitCheck() {
2768 generate_clinit_check_ = true;
2769 }
2770
2771 bool CanCallRuntime() const {
2772 return MustGenerateClinitCheck() || !is_referrers_class_;
2773 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002774
Nicolas Geoffray82091da2015-01-26 10:02:45 +00002775 bool CanThrow() const OVERRIDE {
2776 // May call runtime and and therefore can throw.
2777 // TODO: finer grain decision.
2778 return !is_referrers_class_;
2779 }
2780
Calin Juravleacf735c2015-02-12 15:25:22 +00002781 ReferenceTypeInfo GetLoadedClassRTI() {
2782 return loaded_class_rti_;
2783 }
2784
2785 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
2786 // Make sure we only set exact types (the loaded class should never be merged).
2787 DCHECK(rti.IsExact());
2788 loaded_class_rti_ = rti;
2789 }
2790
2791 bool IsResolved() {
2792 return loaded_class_rti_.IsExact();
2793 }
2794
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002795 DECLARE_INSTRUCTION(LoadClass);
2796
2797 private:
2798 const uint16_t type_index_;
2799 const bool is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002800 const uint32_t dex_pc_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002801 // Whether this instruction must generate the initialization check.
2802 // Used for code generation.
2803 bool generate_clinit_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002804
Calin Juravleacf735c2015-02-12 15:25:22 +00002805 ReferenceTypeInfo loaded_class_rti_;
2806
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002807 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
2808};
2809
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002810class HLoadString : public HExpression<0> {
2811 public:
2812 HLoadString(uint32_t string_index, uint32_t dex_pc)
2813 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2814 string_index_(string_index),
2815 dex_pc_(dex_pc) {}
2816
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002817 bool CanBeMoved() const OVERRIDE { return true; }
2818
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002819 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2820 return other->AsLoadString()->string_index_ == string_index_;
2821 }
2822
2823 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
2824
2825 uint32_t GetDexPc() const { return dex_pc_; }
2826 uint32_t GetStringIndex() const { return string_index_; }
2827
2828 // TODO: Can we deopt or debug when we resolve a string?
2829 bool NeedsEnvironment() const OVERRIDE { return false; }
2830
2831 DECLARE_INSTRUCTION(LoadString);
2832
2833 private:
2834 const uint32_t string_index_;
2835 const uint32_t dex_pc_;
2836
2837 DISALLOW_COPY_AND_ASSIGN(HLoadString);
2838};
2839
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002840// TODO: Pass this check to HInvokeStaticOrDirect nodes.
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002841/**
2842 * Performs an initialization check on its Class object input.
2843 */
2844class HClinitCheck : public HExpression<1> {
2845 public:
2846 explicit HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
2847 : HExpression(Primitive::kPrimNot, SideEffects::All()),
2848 dex_pc_(dex_pc) {
2849 SetRawInputAt(0, constant);
2850 }
2851
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002852 bool CanBeMoved() const OVERRIDE { return true; }
2853 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2854 UNUSED(other);
2855 return true;
2856 }
2857
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002858 bool NeedsEnvironment() const OVERRIDE {
2859 // May call runtime to initialize the class.
2860 return true;
2861 }
2862
2863 uint32_t GetDexPc() const { return dex_pc_; }
2864
2865 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
2866
2867 DECLARE_INSTRUCTION(ClinitCheck);
2868
2869 private:
2870 const uint32_t dex_pc_;
2871
2872 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
2873};
2874
2875class HStaticFieldGet : public HExpression<1> {
2876 public:
2877 HStaticFieldGet(HInstruction* cls,
2878 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00002879 MemberOffset field_offset,
2880 bool is_volatile)
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002881 : HExpression(field_type, SideEffects::DependsOnSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00002882 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002883 SetRawInputAt(0, cls);
2884 }
2885
Calin Juravle52c48962014-12-16 17:02:57 +00002886
Calin Juravle10c9cbe2014-12-19 10:50:19 +00002887 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002888
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002889 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00002890 HStaticFieldGet* other_get = other->AsStaticFieldGet();
2891 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002892 }
2893
2894 size_t ComputeHashCode() const OVERRIDE {
2895 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
2896 }
2897
Calin Juravle52c48962014-12-16 17:02:57 +00002898 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002899 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
2900 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002901 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002902
2903 DECLARE_INSTRUCTION(StaticFieldGet);
2904
2905 private:
2906 const FieldInfo field_info_;
2907
2908 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
2909};
2910
2911class HStaticFieldSet : public HTemplateInstruction<2> {
2912 public:
2913 HStaticFieldSet(HInstruction* cls,
2914 HInstruction* value,
2915 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00002916 MemberOffset field_offset,
2917 bool is_volatile)
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002918 : HTemplateInstruction(SideEffects::ChangesSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00002919 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002920 SetRawInputAt(0, cls);
2921 SetRawInputAt(1, value);
2922 }
2923
Calin Juravle52c48962014-12-16 17:02:57 +00002924 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002925 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
2926 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002927 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002928
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002929 HInstruction* GetValue() const { return InputAt(1); }
2930
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002931 DECLARE_INSTRUCTION(StaticFieldSet);
2932
2933 private:
2934 const FieldInfo field_info_;
2935
2936 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
2937};
2938
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002939// Implement the move-exception DEX instruction.
2940class HLoadException : public HExpression<0> {
2941 public:
2942 HLoadException() : HExpression(Primitive::kPrimNot, SideEffects::None()) {}
2943
2944 DECLARE_INSTRUCTION(LoadException);
2945
2946 private:
2947 DISALLOW_COPY_AND_ASSIGN(HLoadException);
2948};
2949
2950class HThrow : public HTemplateInstruction<1> {
2951 public:
2952 HThrow(HInstruction* exception, uint32_t dex_pc)
2953 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {
2954 SetRawInputAt(0, exception);
2955 }
2956
2957 bool IsControlFlow() const OVERRIDE { return true; }
2958
2959 bool NeedsEnvironment() const OVERRIDE { return true; }
2960
Nicolas Geoffray82091da2015-01-26 10:02:45 +00002961 bool CanThrow() const OVERRIDE { return true; }
2962
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002963 uint32_t GetDexPc() const { return dex_pc_; }
2964
2965 DECLARE_INSTRUCTION(Throw);
2966
2967 private:
Nicolas Geoffray82091da2015-01-26 10:02:45 +00002968 const uint32_t dex_pc_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002969
2970 DISALLOW_COPY_AND_ASSIGN(HThrow);
2971};
2972
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002973class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002974 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002975 HInstanceOf(HInstruction* object,
2976 HLoadClass* constant,
2977 bool class_is_final,
2978 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002979 : HExpression(Primitive::kPrimBoolean, SideEffects::None()),
2980 class_is_final_(class_is_final),
2981 dex_pc_(dex_pc) {
2982 SetRawInputAt(0, object);
2983 SetRawInputAt(1, constant);
2984 }
2985
2986 bool CanBeMoved() const OVERRIDE { return true; }
2987
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002988 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002989 return true;
2990 }
2991
2992 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002993 return false;
2994 }
2995
2996 uint32_t GetDexPc() const { return dex_pc_; }
2997
2998 bool IsClassFinal() const { return class_is_final_; }
2999
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003000 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003001
3002 private:
3003 const bool class_is_final_;
3004 const uint32_t dex_pc_;
3005
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003006 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
3007};
3008
3009class HCheckCast : public HTemplateInstruction<2> {
3010 public:
3011 HCheckCast(HInstruction* object,
3012 HLoadClass* constant,
3013 bool class_is_final,
3014 uint32_t dex_pc)
3015 : HTemplateInstruction(SideEffects::None()),
3016 class_is_final_(class_is_final),
3017 dex_pc_(dex_pc) {
3018 SetRawInputAt(0, object);
3019 SetRawInputAt(1, constant);
3020 }
3021
3022 bool CanBeMoved() const OVERRIDE { return true; }
3023
3024 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
3025 return true;
3026 }
3027
3028 bool NeedsEnvironment() const OVERRIDE {
3029 // Instruction may throw a CheckCastError.
3030 return true;
3031 }
3032
3033 bool CanThrow() const OVERRIDE { return true; }
3034
3035 uint32_t GetDexPc() const { return dex_pc_; }
3036
3037 bool IsClassFinal() const { return class_is_final_; }
3038
3039 DECLARE_INSTRUCTION(CheckCast);
3040
3041 private:
3042 const bool class_is_final_;
3043 const uint32_t dex_pc_;
3044
3045 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003046};
3047
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003048class HMonitorOperation : public HTemplateInstruction<1> {
3049 public:
3050 enum OperationKind {
3051 kEnter,
3052 kExit,
3053 };
3054
3055 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
3056 : HTemplateInstruction(SideEffects::None()), kind_(kind), dex_pc_(dex_pc) {
3057 SetRawInputAt(0, object);
3058 }
3059
3060 // Instruction may throw a Java exception, so we need an environment.
3061 bool NeedsEnvironment() const OVERRIDE { return true; }
3062 bool CanThrow() const OVERRIDE { return true; }
3063
3064 uint32_t GetDexPc() const { return dex_pc_; }
3065
3066 bool IsEnter() const { return kind_ == kEnter; }
3067
3068 DECLARE_INSTRUCTION(MonitorOperation);
3069
Calin Juravle52c48962014-12-16 17:02:57 +00003070 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003071 const OperationKind kind_;
3072 const uint32_t dex_pc_;
3073
3074 private:
3075 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
3076};
3077
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003078class MoveOperands : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003079 public:
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003080 MoveOperands(Location source, Location destination, HInstruction* instruction)
3081 : source_(source), destination_(destination), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003082
3083 Location GetSource() const { return source_; }
3084 Location GetDestination() const { return destination_; }
3085
3086 void SetSource(Location value) { source_ = value; }
3087 void SetDestination(Location value) { destination_ = value; }
3088
3089 // The parallel move resolver marks moves as "in-progress" by clearing the
3090 // destination (but not the source).
3091 Location MarkPending() {
3092 DCHECK(!IsPending());
3093 Location dest = destination_;
3094 destination_ = Location::NoLocation();
3095 return dest;
3096 }
3097
3098 void ClearPending(Location dest) {
3099 DCHECK(IsPending());
3100 destination_ = dest;
3101 }
3102
3103 bool IsPending() const {
3104 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
3105 return destination_.IsInvalid() && !source_.IsInvalid();
3106 }
3107
3108 // True if this blocks a move from the given location.
3109 bool Blocks(Location loc) const {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003110 return !IsEliminated() && (source_.Contains(loc) || loc.Contains(source_));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003111 }
3112
3113 // A move is redundant if it's been eliminated, if its source and
3114 // destination are the same, or if its destination is unneeded.
3115 bool IsRedundant() const {
3116 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
3117 }
3118
3119 // We clear both operands to indicate move that's been eliminated.
3120 void Eliminate() {
3121 source_ = destination_ = Location::NoLocation();
3122 }
3123
3124 bool IsEliminated() const {
3125 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
3126 return source_.IsInvalid();
3127 }
3128
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003129 HInstruction* GetInstruction() const { return instruction_; }
3130
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003131 private:
3132 Location source_;
3133 Location destination_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003134 // The instruction this move is assocatied with. Null when this move is
3135 // for moving an input in the expected locations of user (including a phi user).
3136 // This is only used in debug mode, to ensure we do not connect interval siblings
3137 // in the same parallel move.
3138 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003139};
3140
3141static constexpr size_t kDefaultNumberOfMoves = 4;
3142
3143class HParallelMove : public HTemplateInstruction<0> {
3144 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003145 explicit HParallelMove(ArenaAllocator* arena)
3146 : HTemplateInstruction(SideEffects::None()), moves_(arena, kDefaultNumberOfMoves) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003147
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003148 void AddMove(Location source, Location destination, HInstruction* instruction) {
3149 DCHECK(source.IsValid());
3150 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003151 if (kIsDebugBuild) {
3152 if (instruction != nullptr) {
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00003153 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003154 DCHECK_NE(moves_.Get(i).GetInstruction(), instruction)
3155 << "Doing parallel moves for the same instruction.";
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00003156 }
3157 }
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003158 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
3159 DCHECK(!destination.Equals(moves_.Get(i).GetDestination()))
3160 << "Same destination for two moves in a parallel move.";
3161 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003162 }
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003163 moves_.Add(MoveOperands(source, destination, instruction));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003164 }
3165
3166 MoveOperands* MoveOperandsAt(size_t index) const {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003167 return moves_.GetRawStorage() + index;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003168 }
3169
3170 size_t NumMoves() const { return moves_.Size(); }
3171
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003172 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003173
3174 private:
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003175 GrowableArray<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003176
3177 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
3178};
3179
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003180class HGraphVisitor : public ValueObject {
3181 public:
Dave Allison20dfc792014-06-16 20:44:29 -07003182 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
3183 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003184
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003185 virtual void VisitInstruction(HInstruction* instruction) { UNUSED(instruction); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003186 virtual void VisitBasicBlock(HBasicBlock* block);
3187
Roland Levillain633021e2014-10-01 14:12:25 +01003188 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003189 void VisitInsertionOrder();
3190
Roland Levillain633021e2014-10-01 14:12:25 +01003191 // Visit the graph following dominator tree reverse post-order.
3192 void VisitReversePostOrder();
3193
Nicolas Geoffray787c3072014-03-17 10:20:19 +00003194 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00003195
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003196 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003197#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003198 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
3199
3200 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
3201
3202#undef DECLARE_VISIT_INSTRUCTION
3203
3204 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07003205 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003206
3207 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
3208};
3209
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003210class HGraphDelegateVisitor : public HGraphVisitor {
3211 public:
3212 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
3213 virtual ~HGraphDelegateVisitor() {}
3214
3215 // Visit functions that delegate to to super class.
3216#define DECLARE_VISIT_INSTRUCTION(name, super) \
3217 virtual void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
3218
3219 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
3220
3221#undef DECLARE_VISIT_INSTRUCTION
3222
3223 private:
3224 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
3225};
3226
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003227class HInsertionOrderIterator : public ValueObject {
3228 public:
3229 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
3230
3231 bool Done() const { return index_ == graph_.GetBlocks().Size(); }
3232 HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); }
3233 void Advance() { ++index_; }
3234
3235 private:
3236 const HGraph& graph_;
3237 size_t index_;
3238
3239 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
3240};
3241
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003242class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003243 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003244 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003245
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003246 bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); }
3247 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003248 void Advance() { ++index_; }
3249
3250 private:
3251 const HGraph& graph_;
3252 size_t index_;
3253
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003254 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003255};
3256
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003257class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003258 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003259 explicit HPostOrderIterator(const HGraph& graph)
3260 : graph_(graph), index_(graph_.GetReversePostOrder().Size()) {}
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003261
3262 bool Done() const { return index_ == 0; }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003263 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003264 void Advance() { --index_; }
3265
3266 private:
3267 const HGraph& graph_;
3268 size_t index_;
3269
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003270 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003271};
3272
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003273// Iterator over the blocks that art part of the loop. Includes blocks part
3274// of an inner loop. The order in which the blocks are iterated is on their
3275// block id.
3276class HBlocksInLoopIterator : public ValueObject {
3277 public:
3278 explicit HBlocksInLoopIterator(const HLoopInformation& info)
3279 : blocks_in_loop_(info.GetBlocks()),
3280 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
3281 index_(0) {
3282 if (!blocks_in_loop_.IsBitSet(index_)) {
3283 Advance();
3284 }
3285 }
3286
3287 bool Done() const { return index_ == blocks_.Size(); }
3288 HBasicBlock* Current() const { return blocks_.Get(index_); }
3289 void Advance() {
3290 ++index_;
3291 for (size_t e = blocks_.Size(); index_ < e; ++index_) {
3292 if (blocks_in_loop_.IsBitSet(index_)) {
3293 break;
3294 }
3295 }
3296 }
3297
3298 private:
3299 const BitVector& blocks_in_loop_;
3300 const GrowableArray<HBasicBlock*>& blocks_;
3301 size_t index_;
3302
3303 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
3304};
3305
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003306} // namespace art
3307
3308#endif // ART_COMPILER_OPTIMIZING_NODES_H_