blob: 4baea6635d943caf353a309aa438eb19052b03ab [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) \
Calin Juravleb1498f62015-02-16 13:13:29 +0000586 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000587 M(CheckCast, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100588 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000589 M(Compare, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100590 M(Condition, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000591 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +0000592 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000593 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100594 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000595 M(Exit, Instruction) \
596 M(FloatConstant, Constant) \
597 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100598 M(GreaterThan, Condition) \
599 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100600 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000601 M(InstanceFieldGet, Instruction) \
602 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000603 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100604 M(IntConstant, Constant) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +0000605 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000606 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100607 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000608 M(LessThan, Condition) \
609 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000610 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000611 M(LoadException, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100612 M(LoadLocal, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000613 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100614 M(Local, Instruction) \
615 M(LongConstant, Constant) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +0000616 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000617 M(Mul, BinaryOperation) \
618 M(Neg, UnaryOperation) \
619 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100620 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +0100621 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000622 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000623 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000624 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000625 M(Or, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100626 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000627 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100628 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000629 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100630 M(Return, Instruction) \
631 M(ReturnVoid, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000632 M(Shl, BinaryOperation) \
633 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100634 M(StaticFieldGet, Instruction) \
635 M(StaticFieldSet, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100636 M(StoreLocal, Instruction) \
637 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100638 M(SuspendCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000639 M(Temporary, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000640 M(Throw, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +0000641 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000642 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000643 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000644
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100645#define FOR_EACH_INSTRUCTION(M) \
646 FOR_EACH_CONCRETE_INSTRUCTION(M) \
647 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +0100648 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +0100649 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100650 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -0700651
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100652#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000653FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
654#undef FORWARD_DECLARATION
655
Roland Levillainccc07a92014-09-16 14:48:16 +0100656#define DECLARE_INSTRUCTION(type) \
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100657 virtual InstructionKind GetKind() const { return k##type; } \
Roland Levillainccc07a92014-09-16 14:48:16 +0100658 virtual const char* DebugName() const { return #type; } \
659 virtual const H##type* As##type() const OVERRIDE { return this; } \
660 virtual H##type* As##type() OVERRIDE { return this; } \
661 virtual bool InstructionTypeEquals(HInstruction* other) const { \
662 return other->Is##type(); \
663 } \
664 virtual void Accept(HGraphVisitor* visitor)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000665
David Brazdiled596192015-01-23 10:39:45 +0000666template <typename T> class HUseList;
667
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100668template <typename T>
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700669class HUseListNode : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000670 public:
David Brazdiled596192015-01-23 10:39:45 +0000671 HUseListNode* GetPrevious() const { return prev_; }
672 HUseListNode* GetNext() const { return next_; }
673 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100674 size_t GetIndex() const { return index_; }
675
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000676 private:
David Brazdiled596192015-01-23 10:39:45 +0000677 HUseListNode(T user, size_t index)
678 : user_(user), index_(index), prev_(nullptr), next_(nullptr) {}
679
680 T const user_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100681 const size_t index_;
David Brazdiled596192015-01-23 10:39:45 +0000682 HUseListNode<T>* prev_;
683 HUseListNode<T>* next_;
684
685 friend class HUseList<T>;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000686
687 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
688};
689
David Brazdiled596192015-01-23 10:39:45 +0000690template <typename T>
691class HUseList : public ValueObject {
692 public:
693 HUseList() : first_(nullptr) {}
694
695 void Clear() {
696 first_ = nullptr;
697 }
698
699 // Adds a new entry at the beginning of the use list and returns
700 // the newly created node.
701 HUseListNode<T>* AddUse(T user, size_t index, ArenaAllocator* arena) {
David Brazdilea55b932015-01-27 17:12:29 +0000702 HUseListNode<T>* new_node = new (arena) HUseListNode<T>(user, index);
David Brazdiled596192015-01-23 10:39:45 +0000703 if (IsEmpty()) {
704 first_ = new_node;
705 } else {
706 first_->prev_ = new_node;
707 new_node->next_ = first_;
708 first_ = new_node;
709 }
710 return new_node;
711 }
712
713 HUseListNode<T>* GetFirst() const {
714 return first_;
715 }
716
717 void Remove(HUseListNode<T>* node) {
718 if (node->prev_ != nullptr) {
719 node->prev_->next_ = node->next_;
720 }
721 if (node->next_ != nullptr) {
722 node->next_->prev_ = node->prev_;
723 }
724 if (node == first_) {
725 first_ = node->next_;
726 }
727 }
728
729 bool IsEmpty() const {
730 return first_ == nullptr;
731 }
732
733 bool HasOnlyOneUse() const {
734 return first_ != nullptr && first_->next_ == nullptr;
735 }
736
737 private:
738 HUseListNode<T>* first_;
739};
740
741template<typename T>
742class HUseIterator : public ValueObject {
743 public:
744 explicit HUseIterator(const HUseList<T>& uses) : current_(uses.GetFirst()) {}
745
746 bool Done() const { return current_ == nullptr; }
747
748 void Advance() {
749 DCHECK(!Done());
750 current_ = current_->GetNext();
751 }
752
753 HUseListNode<T>* Current() const {
754 DCHECK(!Done());
755 return current_;
756 }
757
758 private:
759 HUseListNode<T>* current_;
760
761 friend class HValue;
762};
763
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100764// Represents the side effects an instruction may have.
765class SideEffects : public ValueObject {
766 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100767 SideEffects() : flags_(0) {}
768
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100769 static SideEffects None() {
770 return SideEffects(0);
771 }
772
773 static SideEffects All() {
774 return SideEffects(ChangesSomething().flags_ | DependsOnSomething().flags_);
775 }
776
777 static SideEffects ChangesSomething() {
778 return SideEffects((1 << kFlagChangesCount) - 1);
779 }
780
781 static SideEffects DependsOnSomething() {
782 int count = kFlagDependsOnCount - kFlagChangesCount;
783 return SideEffects(((1 << count) - 1) << kFlagChangesCount);
784 }
785
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100786 SideEffects Union(SideEffects other) const {
787 return SideEffects(flags_ | other.flags_);
788 }
789
Roland Levillain72bceff2014-09-15 18:29:00 +0100790 bool HasSideEffects() const {
791 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
792 return (flags_ & all_bits_set) != 0;
793 }
794
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100795 bool HasAllSideEffects() const {
796 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
797 return all_bits_set == (flags_ & all_bits_set);
798 }
799
800 bool DependsOn(SideEffects other) const {
801 size_t depends_flags = other.ComputeDependsFlags();
802 return (flags_ & depends_flags) != 0;
803 }
804
805 bool HasDependencies() const {
806 int count = kFlagDependsOnCount - kFlagChangesCount;
807 size_t all_bits_set = (1 << count) - 1;
808 return ((flags_ >> kFlagChangesCount) & all_bits_set) != 0;
809 }
810
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100811 private:
812 static constexpr int kFlagChangesSomething = 0;
813 static constexpr int kFlagChangesCount = kFlagChangesSomething + 1;
814
815 static constexpr int kFlagDependsOnSomething = kFlagChangesCount;
816 static constexpr int kFlagDependsOnCount = kFlagDependsOnSomething + 1;
817
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100818 explicit SideEffects(size_t flags) : flags_(flags) {}
819
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100820 size_t ComputeDependsFlags() const {
821 return flags_ << kFlagChangesCount;
822 }
823
824 size_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100825};
826
David Brazdiled596192015-01-23 10:39:45 +0000827// A HEnvironment object contains the values of virtual registers at a given location.
828class HEnvironment : public ArenaObject<kArenaAllocMisc> {
829 public:
830 HEnvironment(ArenaAllocator* arena, size_t number_of_vregs)
831 : vregs_(arena, number_of_vregs) {
832 vregs_.SetSize(number_of_vregs);
833 for (size_t i = 0; i < number_of_vregs; i++) {
834 vregs_.Put(i, VRegInfo(nullptr, nullptr));
835 }
836 }
837
838 void CopyFrom(HEnvironment* env);
839
840 void SetRawEnvAt(size_t index, HInstruction* instruction) {
841 vregs_.Put(index, VRegInfo(instruction, nullptr));
842 }
843
844 // Record instructions' use entries of this environment for constant-time removal.
845 void RecordEnvUse(HUseListNode<HEnvironment*>* env_use) {
846 DCHECK(env_use->GetUser() == this);
847 size_t index = env_use->GetIndex();
848 VRegInfo info = vregs_.Get(index);
849 DCHECK(info.vreg_ != nullptr);
850 DCHECK(info.node_ == nullptr);
851 vregs_.Put(index, VRegInfo(info.vreg_, env_use));
852 }
853
854 HInstruction* GetInstructionAt(size_t index) const {
855 return vregs_.Get(index).vreg_;
856 }
857
858 HUseListNode<HEnvironment*>* GetInstructionEnvUseAt(size_t index) const {
859 return vregs_.Get(index).node_;
860 }
861
862 size_t Size() const { return vregs_.Size(); }
863
864 private:
865 struct VRegInfo {
866 HInstruction* vreg_;
867 HUseListNode<HEnvironment*>* node_;
868
869 VRegInfo(HInstruction* instruction, HUseListNode<HEnvironment*>* env_use)
870 : vreg_(instruction), node_(env_use) {}
871 };
872
873 GrowableArray<VRegInfo> vregs_;
874
875 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
876};
877
Calin Juravleacf735c2015-02-12 15:25:22 +0000878class ReferenceTypeInfo : ValueObject {
879 public:
Calin Juravleb1498f62015-02-16 13:13:29 +0000880 typedef Handle<mirror::Class> TypeHandle;
881
882 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact)
883 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
884 if (type_handle->IsObjectClass()) {
885 // Override the type handle to be consistent with the case when we get to
886 // Top but don't have the Object class available. It avoids having to guess
887 // what value the type_handle has when it's Top.
888 return ReferenceTypeInfo(TypeHandle(), is_exact, true);
889 } else {
890 return ReferenceTypeInfo(type_handle, is_exact, false);
891 }
892 }
893
894 static ReferenceTypeInfo CreateTop(bool is_exact) {
895 return ReferenceTypeInfo(TypeHandle(), is_exact, true);
Calin Juravleacf735c2015-02-12 15:25:22 +0000896 }
897
898 bool IsExact() const { return is_exact_; }
899 bool IsTop() const { return is_top_; }
900
901 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
902
Calin Juravleb1498f62015-02-16 13:13:29 +0000903 bool IsSupertypeOf(ReferenceTypeInfo rti) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Calin Juravleacf735c2015-02-12 15:25:22 +0000904 if (IsTop()) {
905 // Top (equivalent for java.lang.Object) is supertype of anything.
906 return true;
907 }
908 if (rti.IsTop()) {
909 // If we get here `this` is not Top() so it can't be a supertype.
910 return false;
911 }
912 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
913 }
914
915 // Returns true if the type information provide the same amount of details.
916 // Note that it does not mean that the instructions have the same actual type
917 // (e.g. tops are equal but they can be the result of a merge).
918 bool IsEqual(ReferenceTypeInfo rti) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
919 if (IsExact() != rti.IsExact()) {
920 return false;
921 }
922 if (IsTop() && rti.IsTop()) {
923 // `Top` means java.lang.Object, so the types are equivalent.
924 return true;
925 }
926 if (IsTop() || rti.IsTop()) {
927 // If only one is top or object than they are not equivalent.
928 // NB: We need this extra check because the type_handle of `Top` is invalid
929 // and we cannot inspect its reference.
930 return false;
931 }
932
933 // Finally check the types.
934 return GetTypeHandle().Get() == rti.GetTypeHandle().Get();
935 }
936
937 private:
Calin Juravleb1498f62015-02-16 13:13:29 +0000938 ReferenceTypeInfo() : ReferenceTypeInfo(TypeHandle(), false, true) {}
939 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact, bool is_top)
940 : type_handle_(type_handle), is_exact_(is_exact), is_top_(is_top) {}
941
Calin Juravleacf735c2015-02-12 15:25:22 +0000942 // The class of the object.
Calin Juravleb1498f62015-02-16 13:13:29 +0000943 TypeHandle type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +0000944 // Whether or not the type is exact or a superclass of the actual type.
Calin Juravleb1498f62015-02-16 13:13:29 +0000945 // Whether or not we have any information about this type.
Calin Juravleacf735c2015-02-12 15:25:22 +0000946 bool is_exact_;
947 // A true value here means that the object type should be java.lang.Object.
948 // We don't have access to the corresponding mirror object every time so this
949 // flag acts as a substitute. When true, the TypeHandle refers to a null
950 // pointer and should not be used.
951 bool is_top_;
952};
953
954std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
955
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700956class HInstruction : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000957 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100958 explicit HInstruction(SideEffects side_effects)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000959 : previous_(nullptr),
960 next_(nullptr),
961 block_(nullptr),
962 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100963 ssa_index_(-1),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100964 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100965 locations_(nullptr),
966 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100967 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +0000968 side_effects_(side_effects),
969 reference_type_info_(ReferenceTypeInfo::CreateTop(/* is_exact */ false)) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000970
Dave Allison20dfc792014-06-16 20:44:29 -0700971 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000972
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100973#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100974 enum InstructionKind {
975 FOR_EACH_INSTRUCTION(DECLARE_KIND)
976 };
977#undef DECLARE_KIND
978
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000979 HInstruction* GetNext() const { return next_; }
980 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000981
Calin Juravle77520bc2015-01-12 18:45:46 +0000982 HInstruction* GetNextDisregardingMoves() const;
983 HInstruction* GetPreviousDisregardingMoves() const;
984
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000985 HBasicBlock* GetBlock() const { return block_; }
986 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100987 bool IsInBlock() const { return block_ != nullptr; }
988 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +0100989 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000990
Roland Levillain6b879dd2014-09-22 17:13:44 +0100991 virtual size_t InputCount() const = 0;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100992 virtual HInstruction* InputAt(size_t i) const = 0;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000993
994 virtual void Accept(HGraphVisitor* visitor) = 0;
995 virtual const char* DebugName() const = 0;
996
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100997 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100998 virtual void SetRawInputAt(size_t index, HInstruction* input) = 0;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100999
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001000 virtual bool NeedsEnvironment() const { return false; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001001 virtual bool IsControlFlow() const { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01001002 virtual bool CanThrow() const { return false; }
Roland Levillain72bceff2014-09-15 18:29:00 +01001003 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001004
Calin Juravle10e244f2015-01-26 18:54:32 +00001005 // Does not apply for all instructions, but having this at top level greatly
1006 // simplifies the null check elimination.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001007 virtual bool CanBeNull() const {
1008 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1009 return true;
1010 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001011
Calin Juravle77520bc2015-01-12 18:45:46 +00001012 virtual bool CanDoImplicitNullCheck() const { return false; }
1013
Calin Juravleacf735c2015-02-12 15:25:22 +00001014 void SetReferenceTypeInfo(ReferenceTypeInfo reference_type_info) {
1015 reference_type_info_ = reference_type_info;
1016 }
1017
1018 ReferenceTypeInfo GetReferenceTypeInfo() const { return reference_type_info_; }
1019
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001020 void AddUseAt(HInstruction* user, size_t index) {
David Brazdiled596192015-01-23 10:39:45 +00001021 uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001022 }
1023
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001024 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001025 DCHECK(user != nullptr);
David Brazdiled596192015-01-23 10:39:45 +00001026 HUseListNode<HEnvironment*>* env_use =
1027 env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1028 user->RecordEnvUse(env_use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001029 }
1030
1031 void RemoveUser(HInstruction* user, size_t index);
David Brazdiled596192015-01-23 10:39:45 +00001032 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001033
David Brazdilea55b932015-01-27 17:12:29 +00001034 const HUseList<HInstruction*>& GetUses() { return uses_; }
1035 const HUseList<HEnvironment*>& GetEnvUses() { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001036
David Brazdiled596192015-01-23 10:39:45 +00001037 bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); }
1038 bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001039
Roland Levillain6c82d402014-10-13 16:10:27 +01001040 // Does this instruction strictly dominate `other_instruction`?
1041 // Returns false if this instruction and `other_instruction` are the same.
1042 // Aborts if this instruction and `other_instruction` are both phis.
1043 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001044
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001045 int GetId() const { return id_; }
1046 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001047
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001048 int GetSsaIndex() const { return ssa_index_; }
1049 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1050 bool HasSsaIndex() const { return ssa_index_ != -1; }
1051
1052 bool HasEnvironment() const { return environment_ != nullptr; }
1053 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001054 void SetEnvironment(HEnvironment* environment) { environment_ = environment; }
1055
Nicolas Geoffray39468442014-09-02 15:17:15 +01001056 // Returns the number of entries in the environment. Typically, that is the
1057 // number of dex registers in a method. It could be more in case of inlining.
1058 size_t EnvironmentSize() const;
1059
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001060 LocationSummary* GetLocations() const { return locations_; }
1061 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001062
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001063 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001064 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001065
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001066 // Move `this` instruction before `cursor`.
1067 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001068
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001069#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01001070 bool Is##type() const { return (As##type() != nullptr); } \
1071 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001072 virtual H##type* As##type() { return nullptr; }
1073
1074 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
1075#undef INSTRUCTION_TYPE_CHECK
1076
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001077 // Returns whether the instruction can be moved within the graph.
1078 virtual bool CanBeMoved() const { return false; }
1079
1080 // Returns whether the two instructions are of the same kind.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001081 virtual bool InstructionTypeEquals(HInstruction* other) const {
1082 UNUSED(other);
1083 return false;
1084 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001085
1086 // Returns whether any data encoded in the two instructions is equal.
1087 // This method does not look at the inputs. Both instructions must be
1088 // of the same type, otherwise the method has undefined behavior.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001089 virtual bool InstructionDataEquals(HInstruction* other) const {
1090 UNUSED(other);
1091 return false;
1092 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001093
1094 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00001095 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001096 // 2) Their inputs are identical.
1097 bool Equals(HInstruction* other) const;
1098
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001099 virtual InstructionKind GetKind() const = 0;
1100
1101 virtual size_t ComputeHashCode() const {
1102 size_t result = GetKind();
1103 for (size_t i = 0, e = InputCount(); i < e; ++i) {
1104 result = (result * 31) + InputAt(i)->GetId();
1105 }
1106 return result;
1107 }
1108
1109 SideEffects GetSideEffects() const { return side_effects_; }
1110
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001111 size_t GetLifetimePosition() const { return lifetime_position_; }
1112 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
1113 LiveInterval* GetLiveInterval() const { return live_interval_; }
1114 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
1115 bool HasLiveInterval() const { return live_interval_ != nullptr; }
1116
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001117 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
1118
1119 // Returns whether the code generation of the instruction will require to have access
1120 // to the current method. Such instructions are:
1121 // (1): Instructions that require an environment, as calling the runtime requires
1122 // to walk the stack and have the current method stored at a specific stack address.
1123 // (2): Object literals like classes and strings, that are loaded from the dex cache
1124 // fields of the current method.
1125 bool NeedsCurrentMethod() const {
1126 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
1127 }
1128
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001129 private:
1130 HInstruction* previous_;
1131 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001132 HBasicBlock* block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001133
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001134 // An instruction gets an id when it is added to the graph.
1135 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01001136 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001137 int id_;
1138
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001139 // When doing liveness analysis, instructions that have uses get an SSA index.
1140 int ssa_index_;
1141
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001142 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00001143 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001144
1145 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00001146 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001147
Nicolas Geoffray39468442014-09-02 15:17:15 +01001148 // The environment associated with this instruction. Not null if the instruction
1149 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001150 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001151
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001152 // Set by the code generator.
1153 LocationSummary* locations_;
1154
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001155 // Set by the liveness analysis.
1156 LiveInterval* live_interval_;
1157
1158 // Set by the liveness analysis, this is the position in a linear
1159 // order of blocks where this instruction's live interval start.
1160 size_t lifetime_position_;
1161
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001162 const SideEffects side_effects_;
1163
Calin Juravleacf735c2015-02-12 15:25:22 +00001164 // TODO: for primitive types this should be marked as invalid.
1165 ReferenceTypeInfo reference_type_info_;
1166
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001167 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001168 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001169 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001170
1171 DISALLOW_COPY_AND_ASSIGN(HInstruction);
1172};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001173std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001174
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001175class HInputIterator : public ValueObject {
1176 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001177 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001178
1179 bool Done() const { return index_ == instruction_->InputCount(); }
1180 HInstruction* Current() const { return instruction_->InputAt(index_); }
1181 void Advance() { index_++; }
1182
1183 private:
1184 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001185 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001186
1187 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
1188};
1189
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001190class HInstructionIterator : public ValueObject {
1191 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001192 explicit HInstructionIterator(const HInstructionList& instructions)
1193 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001194 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001195 }
1196
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001197 bool Done() const { return instruction_ == nullptr; }
1198 HInstruction* Current() const { return instruction_; }
1199 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001200 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001201 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001202 }
1203
1204 private:
1205 HInstruction* instruction_;
1206 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001207
1208 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001209};
1210
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001211class HBackwardInstructionIterator : public ValueObject {
1212 public:
1213 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
1214 : instruction_(instructions.last_instruction_) {
1215 next_ = Done() ? nullptr : instruction_->GetPrevious();
1216 }
1217
1218 bool Done() const { return instruction_ == nullptr; }
1219 HInstruction* Current() const { return instruction_; }
1220 void Advance() {
1221 instruction_ = next_;
1222 next_ = Done() ? nullptr : instruction_->GetPrevious();
1223 }
1224
1225 private:
1226 HInstruction* instruction_;
1227 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001228
1229 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001230};
1231
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001232// An embedded container with N elements of type T. Used (with partial
1233// specialization for N=0) because embedded arrays cannot have size 0.
1234template<typename T, intptr_t N>
1235class EmbeddedArray {
1236 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001237 EmbeddedArray() : elements_() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001238
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001239 intptr_t GetLength() const { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001240
1241 const T& operator[](intptr_t i) const {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001242 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001243 return elements_[i];
1244 }
1245
1246 T& operator[](intptr_t i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001247 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001248 return elements_[i];
1249 }
1250
1251 const T& At(intptr_t i) const {
1252 return (*this)[i];
1253 }
1254
1255 void SetAt(intptr_t i, const T& val) {
1256 (*this)[i] = val;
1257 }
1258
1259 private:
1260 T elements_[N];
1261};
1262
1263template<typename T>
1264class EmbeddedArray<T, 0> {
1265 public:
1266 intptr_t length() const { return 0; }
1267 const T& operator[](intptr_t i) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001268 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001269 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001270 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001271 }
1272 T& operator[](intptr_t i) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001273 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001274 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001275 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001276 }
1277};
1278
1279template<intptr_t N>
1280class HTemplateInstruction: public HInstruction {
1281 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001282 HTemplateInstruction<N>(SideEffects side_effects)
1283 : HInstruction(side_effects), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07001284 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001285
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001286 virtual size_t InputCount() const { return N; }
1287 virtual HInstruction* InputAt(size_t i) const { return inputs_[i]; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001288
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001289 protected:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001290 virtual void SetRawInputAt(size_t i, HInstruction* instruction) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001291 inputs_[i] = instruction;
1292 }
1293
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001294 private:
1295 EmbeddedArray<HInstruction*, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001296
1297 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001298};
1299
Dave Allison20dfc792014-06-16 20:44:29 -07001300template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001301class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07001302 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001303 HExpression<N>(Primitive::Type type, SideEffects side_effects)
1304 : HTemplateInstruction<N>(side_effects), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07001305 virtual ~HExpression() {}
1306
1307 virtual Primitive::Type GetType() const { return type_; }
1308
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001309 protected:
1310 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07001311};
1312
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001313// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
1314// instruction that branches to the exit block.
1315class HReturnVoid : public HTemplateInstruction<0> {
1316 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001317 HReturnVoid() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001318
1319 virtual bool IsControlFlow() const { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001320
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001321 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001322
1323 private:
1324 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
1325};
1326
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001327// Represents dex's RETURN opcodes. A HReturn is a control flow
1328// instruction that branches to the exit block.
1329class HReturn : public HTemplateInstruction<1> {
1330 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001331 explicit HReturn(HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001332 SetRawInputAt(0, value);
1333 }
1334
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001335 virtual bool IsControlFlow() const { return true; }
1336
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001337 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001338
1339 private:
1340 DISALLOW_COPY_AND_ASSIGN(HReturn);
1341};
1342
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001343// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001344// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001345// exit block.
1346class HExit : public HTemplateInstruction<0> {
1347 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001348 HExit() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001349
1350 virtual bool IsControlFlow() const { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001351
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001352 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001353
1354 private:
1355 DISALLOW_COPY_AND_ASSIGN(HExit);
1356};
1357
1358// Jumps from one block to another.
1359class HGoto : public HTemplateInstruction<0> {
1360 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001361 HGoto() : HTemplateInstruction(SideEffects::None()) {}
1362
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001363 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001364
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001365 HBasicBlock* GetSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001366 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001367 }
1368
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001369 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001370
1371 private:
1372 DISALLOW_COPY_AND_ASSIGN(HGoto);
1373};
1374
Dave Allison20dfc792014-06-16 20:44:29 -07001375
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001376// Conditional branch. A block ending with an HIf instruction must have
1377// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001378class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001379 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001380 explicit HIf(HInstruction* input) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001381 SetRawInputAt(0, input);
1382 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001383
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001384 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001385
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001386 HBasicBlock* IfTrueSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001387 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001388 }
1389
1390 HBasicBlock* IfFalseSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001391 return GetBlock()->GetSuccessors().Get(1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001392 }
1393
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001394 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001395
Dave Allison20dfc792014-06-16 20:44:29 -07001396 virtual bool IsIfInstruction() const { return true; }
1397
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001398 private:
1399 DISALLOW_COPY_AND_ASSIGN(HIf);
1400};
1401
Roland Levillain88cb1752014-10-20 16:36:47 +01001402class HUnaryOperation : public HExpression<1> {
1403 public:
1404 HUnaryOperation(Primitive::Type result_type, HInstruction* input)
1405 : HExpression(result_type, SideEffects::None()) {
1406 SetRawInputAt(0, input);
1407 }
1408
1409 HInstruction* GetInput() const { return InputAt(0); }
1410 Primitive::Type GetResultType() const { return GetType(); }
1411
1412 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001413 virtual bool InstructionDataEquals(HInstruction* other) const {
1414 UNUSED(other);
1415 return true;
1416 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001417
Roland Levillain9240d6a2014-10-20 16:47:04 +01001418 // Try to statically evaluate `operation` and return a HConstant
1419 // containing the result of this evaluation. If `operation` cannot
1420 // be evaluated as a constant, return nullptr.
1421 HConstant* TryStaticEvaluation() const;
1422
1423 // Apply this operation to `x`.
1424 virtual int32_t Evaluate(int32_t x) const = 0;
1425 virtual int64_t Evaluate(int64_t x) const = 0;
1426
Roland Levillain88cb1752014-10-20 16:36:47 +01001427 DECLARE_INSTRUCTION(UnaryOperation);
1428
1429 private:
1430 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
1431};
1432
Dave Allison20dfc792014-06-16 20:44:29 -07001433class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001434 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001435 HBinaryOperation(Primitive::Type result_type,
1436 HInstruction* left,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001437 HInstruction* right) : HExpression(result_type, SideEffects::None()) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001438 SetRawInputAt(0, left);
1439 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001440 }
1441
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001442 HInstruction* GetLeft() const { return InputAt(0); }
1443 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07001444 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001445
1446 virtual bool IsCommutative() { return false; }
1447
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001448 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001449 virtual bool InstructionDataEquals(HInstruction* other) const {
1450 UNUSED(other);
1451 return true;
1452 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001453
Roland Levillain9240d6a2014-10-20 16:47:04 +01001454 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01001455 // containing the result of this evaluation. If `operation` cannot
1456 // be evaluated as a constant, return nullptr.
Roland Levillain9240d6a2014-10-20 16:47:04 +01001457 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01001458
1459 // Apply this operation to `x` and `y`.
1460 virtual int32_t Evaluate(int32_t x, int32_t y) const = 0;
1461 virtual int64_t Evaluate(int64_t x, int64_t y) const = 0;
1462
Roland Levillainccc07a92014-09-16 14:48:16 +01001463 DECLARE_INSTRUCTION(BinaryOperation);
1464
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001465 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001466 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
1467};
1468
Dave Allison20dfc792014-06-16 20:44:29 -07001469class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001470 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001471 HCondition(HInstruction* first, HInstruction* second)
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001472 : HBinaryOperation(Primitive::kPrimBoolean, first, second),
1473 needs_materialization_(true) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001474
1475 virtual bool IsCommutative() { return true; }
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001476
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001477 bool NeedsMaterialization() const { return needs_materialization_; }
1478 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001479
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001480 // For code generation purposes, returns whether this instruction is just before
1481 // `if_`, and disregard moves in between.
1482 bool IsBeforeWhenDisregardMoves(HIf* if_) const;
1483
Dave Allison20dfc792014-06-16 20:44:29 -07001484 DECLARE_INSTRUCTION(Condition);
1485
1486 virtual IfCondition GetCondition() const = 0;
1487
1488 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001489 // For register allocation purposes, returns whether this instruction needs to be
1490 // materialized (that is, not just be in the processor flags).
1491 bool needs_materialization_;
1492
Dave Allison20dfc792014-06-16 20:44:29 -07001493 DISALLOW_COPY_AND_ASSIGN(HCondition);
1494};
1495
1496// Instruction to check if two inputs are equal to each other.
1497class HEqual : public HCondition {
1498 public:
1499 HEqual(HInstruction* first, HInstruction* second)
1500 : HCondition(first, second) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001501
Roland Levillain93445682014-10-06 19:24:02 +01001502 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1503 return x == y ? 1 : 0;
1504 }
1505 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1506 return x == y ? 1 : 0;
1507 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001508
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001509 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001510
Dave Allison20dfc792014-06-16 20:44:29 -07001511 virtual IfCondition GetCondition() const {
1512 return kCondEQ;
1513 }
1514
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001515 private:
1516 DISALLOW_COPY_AND_ASSIGN(HEqual);
1517};
1518
Dave Allison20dfc792014-06-16 20:44:29 -07001519class HNotEqual : public HCondition {
1520 public:
1521 HNotEqual(HInstruction* first, HInstruction* second)
1522 : HCondition(first, second) {}
1523
Roland Levillain93445682014-10-06 19:24:02 +01001524 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1525 return x != y ? 1 : 0;
1526 }
1527 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1528 return x != y ? 1 : 0;
1529 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001530
Dave Allison20dfc792014-06-16 20:44:29 -07001531 DECLARE_INSTRUCTION(NotEqual);
1532
1533 virtual IfCondition GetCondition() const {
1534 return kCondNE;
1535 }
1536
1537 private:
1538 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
1539};
1540
1541class HLessThan : public HCondition {
1542 public:
1543 HLessThan(HInstruction* first, HInstruction* second)
1544 : HCondition(first, second) {}
1545
Roland Levillain93445682014-10-06 19:24:02 +01001546 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1547 return x < y ? 1 : 0;
1548 }
1549 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1550 return x < y ? 1 : 0;
1551 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001552
Dave Allison20dfc792014-06-16 20:44:29 -07001553 DECLARE_INSTRUCTION(LessThan);
1554
1555 virtual IfCondition GetCondition() const {
1556 return kCondLT;
1557 }
1558
1559 private:
1560 DISALLOW_COPY_AND_ASSIGN(HLessThan);
1561};
1562
1563class HLessThanOrEqual : public HCondition {
1564 public:
1565 HLessThanOrEqual(HInstruction* first, HInstruction* second)
1566 : HCondition(first, second) {}
1567
Roland Levillain93445682014-10-06 19:24:02 +01001568 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1569 return x <= y ? 1 : 0;
1570 }
1571 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1572 return x <= y ? 1 : 0;
1573 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001574
Dave Allison20dfc792014-06-16 20:44:29 -07001575 DECLARE_INSTRUCTION(LessThanOrEqual);
1576
1577 virtual IfCondition GetCondition() const {
1578 return kCondLE;
1579 }
1580
1581 private:
1582 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
1583};
1584
1585class HGreaterThan : public HCondition {
1586 public:
1587 HGreaterThan(HInstruction* first, HInstruction* second)
1588 : HCondition(first, second) {}
1589
Roland Levillain93445682014-10-06 19:24:02 +01001590 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1591 return x > y ? 1 : 0;
1592 }
1593 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1594 return x > y ? 1 : 0;
1595 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001596
Dave Allison20dfc792014-06-16 20:44:29 -07001597 DECLARE_INSTRUCTION(GreaterThan);
1598
1599 virtual IfCondition GetCondition() const {
1600 return kCondGT;
1601 }
1602
1603 private:
1604 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
1605};
1606
1607class HGreaterThanOrEqual : public HCondition {
1608 public:
1609 HGreaterThanOrEqual(HInstruction* first, HInstruction* second)
1610 : HCondition(first, second) {}
1611
Roland Levillain93445682014-10-06 19:24:02 +01001612 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1613 return x >= y ? 1 : 0;
1614 }
1615 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1616 return x >= y ? 1 : 0;
1617 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001618
Dave Allison20dfc792014-06-16 20:44:29 -07001619 DECLARE_INSTRUCTION(GreaterThanOrEqual);
1620
1621 virtual IfCondition GetCondition() const {
1622 return kCondGE;
1623 }
1624
1625 private:
1626 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
1627};
1628
1629
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001630// Instruction to check how two inputs compare to each other.
1631// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
1632class HCompare : public HBinaryOperation {
1633 public:
Calin Juravleddb7df22014-11-25 20:56:51 +00001634 // The bias applies for floating point operations and indicates how NaN
1635 // comparisons are treated:
1636 enum Bias {
1637 kNoBias, // bias is not applicable (i.e. for long operation)
1638 kGtBias, // return 1 for NaN comparisons
1639 kLtBias, // return -1 for NaN comparisons
1640 };
1641
1642 HCompare(Primitive::Type type, HInstruction* first, HInstruction* second, Bias bias)
1643 : HBinaryOperation(Primitive::kPrimInt, first, second), bias_(bias) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001644 DCHECK_EQ(type, first->GetType());
1645 DCHECK_EQ(type, second->GetType());
1646 }
1647
Calin Juravleddb7df22014-11-25 20:56:51 +00001648 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01001649 return
1650 x == y ? 0 :
1651 x > y ? 1 :
1652 -1;
1653 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001654
1655 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01001656 return
1657 x == y ? 0 :
1658 x > y ? 1 :
1659 -1;
1660 }
1661
Calin Juravleddb7df22014-11-25 20:56:51 +00001662 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
1663 return bias_ == other->AsCompare()->bias_;
1664 }
1665
1666 bool IsGtBias() { return bias_ == kGtBias; }
1667
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001668 DECLARE_INSTRUCTION(Compare);
1669
1670 private:
Calin Juravleddb7df22014-11-25 20:56:51 +00001671 const Bias bias_;
1672
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001673 DISALLOW_COPY_AND_ASSIGN(HCompare);
1674};
1675
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001676// A local in the graph. Corresponds to a Dex register.
1677class HLocal : public HTemplateInstruction<0> {
1678 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001679 explicit HLocal(uint16_t reg_number)
1680 : HTemplateInstruction(SideEffects::None()), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001681
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001682 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001683
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001684 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001685
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001686 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001687 // The Dex register number.
1688 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001689
1690 DISALLOW_COPY_AND_ASSIGN(HLocal);
1691};
1692
1693// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07001694class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001695 public:
Roland Levillain5799fc02014-09-25 12:15:20 +01001696 HLoadLocal(HLocal* local, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001697 : HExpression(type, SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001698 SetRawInputAt(0, local);
1699 }
1700
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001701 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1702
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001703 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001704
1705 private:
1706 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
1707};
1708
1709// Store a value in a given local. This instruction has two inputs: the value
1710// and the local.
1711class HStoreLocal : public HTemplateInstruction<2> {
1712 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001713 HStoreLocal(HLocal* local, HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001714 SetRawInputAt(0, local);
1715 SetRawInputAt(1, value);
1716 }
1717
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001718 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1719
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001720 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001721
1722 private:
1723 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
1724};
1725
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001726class HConstant : public HExpression<0> {
1727 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001728 explicit HConstant(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
1729
1730 virtual bool CanBeMoved() const { return true; }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001731
1732 DECLARE_INSTRUCTION(Constant);
1733
1734 private:
1735 DISALLOW_COPY_AND_ASSIGN(HConstant);
1736};
1737
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001738class HFloatConstant : public HConstant {
1739 public:
1740 explicit HFloatConstant(float value) : HConstant(Primitive::kPrimFloat), value_(value) {}
1741
1742 float GetValue() const { return value_; }
1743
1744 virtual bool InstructionDataEquals(HInstruction* other) const {
1745 return bit_cast<float, int32_t>(other->AsFloatConstant()->value_) ==
1746 bit_cast<float, int32_t>(value_);
1747 }
1748
1749 virtual size_t ComputeHashCode() const { return static_cast<size_t>(GetValue()); }
1750
1751 DECLARE_INSTRUCTION(FloatConstant);
1752
1753 private:
1754 const float value_;
1755
1756 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
1757};
1758
1759class HDoubleConstant : public HConstant {
1760 public:
1761 explicit HDoubleConstant(double value) : HConstant(Primitive::kPrimDouble), value_(value) {}
1762
1763 double GetValue() const { return value_; }
1764
1765 virtual bool InstructionDataEquals(HInstruction* other) const {
1766 return bit_cast<double, int64_t>(other->AsDoubleConstant()->value_) ==
1767 bit_cast<double, int64_t>(value_);
1768 }
1769
1770 virtual size_t ComputeHashCode() const { return static_cast<size_t>(GetValue()); }
1771
1772 DECLARE_INSTRUCTION(DoubleConstant);
1773
1774 private:
1775 const double value_;
1776
1777 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
1778};
1779
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001780class HNullConstant : public HConstant {
1781 public:
1782 HNullConstant() : HConstant(Primitive::kPrimNot) {}
1783
1784 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
1785 return true;
1786 }
1787
1788 size_t ComputeHashCode() const OVERRIDE { return 0; }
1789
1790 DECLARE_INSTRUCTION(NullConstant);
1791
1792 private:
1793 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
1794};
1795
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001796// Constants of the type int. Those can be from Dex instructions, or
1797// synthesized (for example with the if-eqz instruction).
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001798class HIntConstant : public HConstant {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001799 public:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001800 explicit HIntConstant(int32_t value) : HConstant(Primitive::kPrimInt), value_(value) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001801
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001802 int32_t GetValue() const { return value_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001803
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001804 virtual bool InstructionDataEquals(HInstruction* other) const {
1805 return other->AsIntConstant()->value_ == value_;
1806 }
1807
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001808 virtual size_t ComputeHashCode() const { return GetValue(); }
1809
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001810 DECLARE_INSTRUCTION(IntConstant);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001811
1812 private:
1813 const int32_t value_;
1814
1815 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
1816};
1817
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001818class HLongConstant : public HConstant {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001819 public:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001820 explicit HLongConstant(int64_t value) : HConstant(Primitive::kPrimLong), value_(value) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001821
1822 int64_t GetValue() const { return value_; }
1823
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001824 virtual bool InstructionDataEquals(HInstruction* other) const {
1825 return other->AsLongConstant()->value_ == value_;
1826 }
1827
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001828 virtual size_t ComputeHashCode() const { return static_cast<size_t>(GetValue()); }
1829
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001830 DECLARE_INSTRUCTION(LongConstant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001831
1832 private:
1833 const int64_t value_;
1834
1835 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
1836};
1837
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001838enum class Intrinsics {
1839#define OPTIMIZING_INTRINSICS(Name, IsStatic) k ## Name,
1840#include "intrinsics_list.h"
1841 kNone,
1842 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
1843#undef INTRINSICS_LIST
1844#undef OPTIMIZING_INTRINSICS
1845};
1846std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
1847
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001848class HInvoke : public HInstruction {
1849 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001850 virtual size_t InputCount() const { return inputs_.Size(); }
1851 virtual HInstruction* InputAt(size_t i) const { return inputs_.Get(i); }
1852
1853 // Runtime needs to walk the stack, so Dex -> Dex calls need to
1854 // know their environment.
Calin Juravle77520bc2015-01-12 18:45:46 +00001855 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001856
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001857 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001858 SetRawInputAt(index, argument);
1859 }
1860
1861 virtual void SetRawInputAt(size_t index, HInstruction* input) {
1862 inputs_.Put(index, input);
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001863 }
1864
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001865 virtual Primitive::Type GetType() const { return return_type_; }
1866
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001867 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001868
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001869 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
1870
1871 Intrinsics GetIntrinsic() {
1872 return intrinsic_;
1873 }
1874
1875 void SetIntrinsic(Intrinsics intrinsic) {
1876 intrinsic_ = intrinsic;
1877 }
1878
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001879 DECLARE_INSTRUCTION(Invoke);
1880
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001881 protected:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001882 HInvoke(ArenaAllocator* arena,
1883 uint32_t number_of_arguments,
1884 Primitive::Type return_type,
1885 uint32_t dex_pc,
1886 uint32_t dex_method_index)
1887 : HInstruction(SideEffects::All()),
1888 inputs_(arena, number_of_arguments),
1889 return_type_(return_type),
1890 dex_pc_(dex_pc),
1891 dex_method_index_(dex_method_index),
1892 intrinsic_(Intrinsics::kNone) {
1893 inputs_.SetSize(number_of_arguments);
1894 }
1895
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001896 GrowableArray<HInstruction*> inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001897 const Primitive::Type return_type_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001898 const uint32_t dex_pc_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001899 const uint32_t dex_method_index_;
1900 Intrinsics intrinsic_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001901
1902 private:
1903 DISALLOW_COPY_AND_ASSIGN(HInvoke);
1904};
1905
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001906class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001907 public:
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001908 HInvokeStaticOrDirect(ArenaAllocator* arena,
1909 uint32_t number_of_arguments,
1910 Primitive::Type return_type,
1911 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001912 uint32_t dex_method_index,
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001913 bool is_recursive,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001914 InvokeType invoke_type)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001915 : HInvoke(arena, number_of_arguments, return_type, dex_pc, dex_method_index),
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001916 invoke_type_(invoke_type),
1917 is_recursive_(is_recursive) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001918
Calin Juravle77520bc2015-01-12 18:45:46 +00001919 bool CanDoImplicitNullCheck() const OVERRIDE {
1920 // We access the method via the dex cache so we can't do an implicit null check.
1921 // TODO: for intrinsics we can generate implicit null checks.
1922 return false;
1923 }
1924
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001925 InvokeType GetInvokeType() const { return invoke_type_; }
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001926 bool IsRecursive() const { return is_recursive_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001927
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001928 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001929
1930 private:
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001931 const InvokeType invoke_type_;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001932 const bool is_recursive_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001933
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001934 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001935};
1936
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001937class HInvokeVirtual : public HInvoke {
1938 public:
1939 HInvokeVirtual(ArenaAllocator* arena,
1940 uint32_t number_of_arguments,
1941 Primitive::Type return_type,
1942 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001943 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001944 uint32_t vtable_index)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001945 : HInvoke(arena, number_of_arguments, return_type, dex_pc, dex_method_index),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001946 vtable_index_(vtable_index) {}
1947
Calin Juravle77520bc2015-01-12 18:45:46 +00001948 bool CanDoImplicitNullCheck() const OVERRIDE {
1949 // TODO: Add implicit null checks in intrinsics.
1950 return !GetLocations()->Intrinsified();
1951 }
1952
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001953 uint32_t GetVTableIndex() const { return vtable_index_; }
1954
1955 DECLARE_INSTRUCTION(InvokeVirtual);
1956
1957 private:
1958 const uint32_t vtable_index_;
1959
1960 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
1961};
1962
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001963class HInvokeInterface : public HInvoke {
1964 public:
1965 HInvokeInterface(ArenaAllocator* arena,
1966 uint32_t number_of_arguments,
1967 Primitive::Type return_type,
1968 uint32_t dex_pc,
1969 uint32_t dex_method_index,
1970 uint32_t imt_index)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001971 : HInvoke(arena, number_of_arguments, return_type, dex_pc, dex_method_index),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001972 imt_index_(imt_index) {}
1973
Calin Juravle77520bc2015-01-12 18:45:46 +00001974 bool CanDoImplicitNullCheck() const OVERRIDE {
1975 // TODO: Add implicit null checks in intrinsics.
1976 return !GetLocations()->Intrinsified();
1977 }
1978
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001979 uint32_t GetImtIndex() const { return imt_index_; }
1980 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
1981
1982 DECLARE_INSTRUCTION(InvokeInterface);
1983
1984 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001985 const uint32_t imt_index_;
1986
1987 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
1988};
1989
Dave Allison20dfc792014-06-16 20:44:29 -07001990class HNewInstance : public HExpression<0> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001991 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00001992 HNewInstance(uint32_t dex_pc, uint16_t type_index, QuickEntrypointEnum entrypoint)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001993 : HExpression(Primitive::kPrimNot, SideEffects::None()),
1994 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00001995 type_index_(type_index),
1996 entrypoint_(entrypoint) {}
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001997
1998 uint32_t GetDexPc() const { return dex_pc_; }
1999 uint16_t GetTypeIndex() const { return type_index_; }
2000
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002001 // Calls runtime so needs an environment.
Calin Juravle92a6ed22014-12-02 18:58:03 +00002002 bool NeedsEnvironment() const OVERRIDE { return true; }
2003 // It may throw when called on:
2004 // - interfaces
2005 // - abstract/innaccessible/unknown classes
2006 // TODO: optimize when possible.
2007 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002008
Calin Juravle10e244f2015-01-26 18:54:32 +00002009 bool CanBeNull() const OVERRIDE { return false; }
2010
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002011 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
2012
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002013 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002014
2015 private:
2016 const uint32_t dex_pc_;
2017 const uint16_t type_index_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002018 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002019
2020 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
2021};
2022
Roland Levillain88cb1752014-10-20 16:36:47 +01002023class HNeg : public HUnaryOperation {
2024 public:
2025 explicit HNeg(Primitive::Type result_type, HInstruction* input)
2026 : HUnaryOperation(result_type, input) {}
2027
Roland Levillain9240d6a2014-10-20 16:47:04 +01002028 virtual int32_t Evaluate(int32_t x) const OVERRIDE { return -x; }
2029 virtual int64_t Evaluate(int64_t x) const OVERRIDE { return -x; }
2030
Roland Levillain88cb1752014-10-20 16:36:47 +01002031 DECLARE_INSTRUCTION(Neg);
2032
2033 private:
2034 DISALLOW_COPY_AND_ASSIGN(HNeg);
2035};
2036
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002037class HNewArray : public HExpression<1> {
2038 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002039 HNewArray(HInstruction* length,
2040 uint32_t dex_pc,
2041 uint16_t type_index,
2042 QuickEntrypointEnum entrypoint)
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002043 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2044 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002045 type_index_(type_index),
2046 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002047 SetRawInputAt(0, length);
2048 }
2049
2050 uint32_t GetDexPc() const { return dex_pc_; }
2051 uint16_t GetTypeIndex() const { return type_index_; }
2052
2053 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00002054 bool NeedsEnvironment() const OVERRIDE { return true; }
2055
2056 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002057
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002058 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
2059
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002060 DECLARE_INSTRUCTION(NewArray);
2061
2062 private:
2063 const uint32_t dex_pc_;
2064 const uint16_t type_index_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002065 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002066
2067 DISALLOW_COPY_AND_ASSIGN(HNewArray);
2068};
2069
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002070class HAdd : public HBinaryOperation {
2071 public:
2072 HAdd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2073 : HBinaryOperation(result_type, left, right) {}
2074
2075 virtual bool IsCommutative() { return true; }
2076
Roland Levillain93445682014-10-06 19:24:02 +01002077 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
2078 return x + y;
2079 }
2080 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
2081 return x + y;
2082 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002083
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002084 DECLARE_INSTRUCTION(Add);
2085
2086 private:
2087 DISALLOW_COPY_AND_ASSIGN(HAdd);
2088};
2089
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002090class HSub : public HBinaryOperation {
2091 public:
2092 HSub(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2093 : HBinaryOperation(result_type, left, right) {}
2094
Roland Levillain93445682014-10-06 19:24:02 +01002095 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
2096 return x - y;
2097 }
2098 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
2099 return x - y;
2100 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002101
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002102 DECLARE_INSTRUCTION(Sub);
2103
2104 private:
2105 DISALLOW_COPY_AND_ASSIGN(HSub);
2106};
2107
Calin Juravle34bacdf2014-10-07 20:23:36 +01002108class HMul : public HBinaryOperation {
2109 public:
2110 HMul(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2111 : HBinaryOperation(result_type, left, right) {}
2112
2113 virtual bool IsCommutative() { return true; }
2114
2115 virtual int32_t Evaluate(int32_t x, int32_t y) const { return x * y; }
2116 virtual int64_t Evaluate(int64_t x, int64_t y) const { return x * y; }
2117
2118 DECLARE_INSTRUCTION(Mul);
2119
2120 private:
2121 DISALLOW_COPY_AND_ASSIGN(HMul);
2122};
2123
Calin Juravle7c4954d2014-10-28 16:57:40 +00002124class HDiv : public HBinaryOperation {
2125 public:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002126 HDiv(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
2127 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00002128
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00002129 virtual int32_t Evaluate(int32_t x, int32_t y) const {
2130 // Our graph structure ensures we never have 0 for `y` during constant folding.
2131 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00002132 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00002133 return (y == -1) ? -x : x / y;
2134 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002135
2136 virtual int64_t Evaluate(int64_t x, int64_t y) const {
2137 DCHECK_NE(y, 0);
2138 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2139 return (y == -1) ? -x : x / y;
2140 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002141
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002142 uint32_t GetDexPc() const { return dex_pc_; }
2143
Calin Juravle7c4954d2014-10-28 16:57:40 +00002144 DECLARE_INSTRUCTION(Div);
2145
2146 private:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002147 const uint32_t dex_pc_;
2148
Calin Juravle7c4954d2014-10-28 16:57:40 +00002149 DISALLOW_COPY_AND_ASSIGN(HDiv);
2150};
2151
Calin Juravlebacfec32014-11-14 15:54:36 +00002152class HRem : public HBinaryOperation {
2153 public:
2154 HRem(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
2155 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
2156
2157 virtual int32_t Evaluate(int32_t x, int32_t y) const {
2158 DCHECK_NE(y, 0);
2159 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2160 return (y == -1) ? 0 : x % y;
2161 }
2162
2163 virtual int64_t Evaluate(int64_t x, int64_t y) const {
2164 DCHECK_NE(y, 0);
2165 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2166 return (y == -1) ? 0 : x % y;
2167 }
2168
2169 uint32_t GetDexPc() const { return dex_pc_; }
2170
2171 DECLARE_INSTRUCTION(Rem);
2172
2173 private:
2174 const uint32_t dex_pc_;
2175
2176 DISALLOW_COPY_AND_ASSIGN(HRem);
2177};
2178
Calin Juravled0d48522014-11-04 16:40:20 +00002179class HDivZeroCheck : public HExpression<1> {
2180 public:
2181 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
2182 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
2183 SetRawInputAt(0, value);
2184 }
2185
2186 bool CanBeMoved() const OVERRIDE { return true; }
2187
2188 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2189 UNUSED(other);
2190 return true;
2191 }
2192
2193 bool NeedsEnvironment() const OVERRIDE { return true; }
2194 bool CanThrow() const OVERRIDE { return true; }
2195
2196 uint32_t GetDexPc() const { return dex_pc_; }
2197
2198 DECLARE_INSTRUCTION(DivZeroCheck);
2199
2200 private:
2201 const uint32_t dex_pc_;
2202
2203 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
2204};
2205
Calin Juravle9aec02f2014-11-18 23:06:35 +00002206class HShl : public HBinaryOperation {
2207 public:
2208 HShl(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2209 : HBinaryOperation(result_type, left, right) {}
2210
2211 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x << (y & kMaxIntShiftValue); }
2212 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x << (y & kMaxLongShiftValue); }
2213
2214 DECLARE_INSTRUCTION(Shl);
2215
2216 private:
2217 DISALLOW_COPY_AND_ASSIGN(HShl);
2218};
2219
2220class HShr : public HBinaryOperation {
2221 public:
2222 HShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2223 : HBinaryOperation(result_type, left, right) {}
2224
2225 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x >> (y & kMaxIntShiftValue); }
2226 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x >> (y & kMaxLongShiftValue); }
2227
2228 DECLARE_INSTRUCTION(Shr);
2229
2230 private:
2231 DISALLOW_COPY_AND_ASSIGN(HShr);
2232};
2233
2234class HUShr : public HBinaryOperation {
2235 public:
2236 HUShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2237 : HBinaryOperation(result_type, left, right) {}
2238
2239 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
2240 uint32_t ux = static_cast<uint32_t>(x);
2241 uint32_t uy = static_cast<uint32_t>(y) & kMaxIntShiftValue;
2242 return static_cast<int32_t>(ux >> uy);
2243 }
2244
2245 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
2246 uint64_t ux = static_cast<uint64_t>(x);
2247 uint64_t uy = static_cast<uint64_t>(y) & kMaxLongShiftValue;
2248 return static_cast<int64_t>(ux >> uy);
2249 }
2250
2251 DECLARE_INSTRUCTION(UShr);
2252
2253 private:
2254 DISALLOW_COPY_AND_ASSIGN(HUShr);
2255};
2256
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002257class HAnd : public HBinaryOperation {
2258 public:
2259 HAnd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2260 : HBinaryOperation(result_type, left, right) {}
2261
2262 bool IsCommutative() OVERRIDE { return true; }
2263
2264 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x & y; }
2265 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x & y; }
2266
2267 DECLARE_INSTRUCTION(And);
2268
2269 private:
2270 DISALLOW_COPY_AND_ASSIGN(HAnd);
2271};
2272
2273class HOr : public HBinaryOperation {
2274 public:
2275 HOr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2276 : HBinaryOperation(result_type, left, right) {}
2277
2278 bool IsCommutative() OVERRIDE { return true; }
2279
2280 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x | y; }
2281 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x | y; }
2282
2283 DECLARE_INSTRUCTION(Or);
2284
2285 private:
2286 DISALLOW_COPY_AND_ASSIGN(HOr);
2287};
2288
2289class HXor : public HBinaryOperation {
2290 public:
2291 HXor(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2292 : HBinaryOperation(result_type, left, right) {}
2293
2294 bool IsCommutative() OVERRIDE { return true; }
2295
2296 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x ^ y; }
2297 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x ^ y; }
2298
2299 DECLARE_INSTRUCTION(Xor);
2300
2301 private:
2302 DISALLOW_COPY_AND_ASSIGN(HXor);
2303};
2304
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002305// The value of a parameter in this method. Its location depends on
2306// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07002307class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002308 public:
Calin Juravle10e244f2015-01-26 18:54:32 +00002309 HParameterValue(uint8_t index, Primitive::Type parameter_type, bool is_this = false)
2310 : HExpression(parameter_type, SideEffects::None()), index_(index), is_this_(is_this) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002311
2312 uint8_t GetIndex() const { return index_; }
2313
Calin Juravle10e244f2015-01-26 18:54:32 +00002314 bool CanBeNull() const OVERRIDE { return !is_this_; }
2315
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002316 DECLARE_INSTRUCTION(ParameterValue);
2317
2318 private:
2319 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00002320 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002321 const uint8_t index_;
2322
Calin Juravle10e244f2015-01-26 18:54:32 +00002323 // Whether or not the parameter value corresponds to 'this' argument.
2324 const bool is_this_;
2325
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002326 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
2327};
2328
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002329class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002330 public:
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002331 explicit HNot(Primitive::Type result_type, HInstruction* input)
2332 : HUnaryOperation(result_type, input) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002333
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002334 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002335 virtual bool InstructionDataEquals(HInstruction* other) const {
2336 UNUSED(other);
2337 return true;
2338 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002339
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002340 virtual int32_t Evaluate(int32_t x) const OVERRIDE { return ~x; }
2341 virtual int64_t Evaluate(int64_t x) const OVERRIDE { return ~x; }
2342
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002343 DECLARE_INSTRUCTION(Not);
2344
2345 private:
2346 DISALLOW_COPY_AND_ASSIGN(HNot);
2347};
2348
Roland Levillaindff1f282014-11-05 14:15:05 +00002349class HTypeConversion : public HExpression<1> {
2350 public:
2351 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00002352 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
2353 : HExpression(result_type, SideEffects::None()), dex_pc_(dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002354 SetRawInputAt(0, input);
2355 DCHECK_NE(input->GetType(), result_type);
2356 }
2357
2358 HInstruction* GetInput() const { return InputAt(0); }
2359 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
2360 Primitive::Type GetResultType() const { return GetType(); }
2361
Roland Levillain624279f2014-12-04 11:54:28 +00002362 // Required by the x86 and ARM code generators when producing calls
2363 // to the runtime.
2364 uint32_t GetDexPc() const { return dex_pc_; }
2365
Roland Levillaindff1f282014-11-05 14:15:05 +00002366 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00002367 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00002368
2369 DECLARE_INSTRUCTION(TypeConversion);
2370
2371 private:
Roland Levillain624279f2014-12-04 11:54:28 +00002372 const uint32_t dex_pc_;
2373
Roland Levillaindff1f282014-11-05 14:15:05 +00002374 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
2375};
2376
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00002377static constexpr uint32_t kNoRegNumber = -1;
2378
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002379class HPhi : public HInstruction {
2380 public:
2381 HPhi(ArenaAllocator* arena, uint32_t reg_number, size_t number_of_inputs, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002382 : HInstruction(SideEffects::None()),
2383 inputs_(arena, number_of_inputs),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002384 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002385 type_(type),
Calin Juravle10e244f2015-01-26 18:54:32 +00002386 is_live_(false),
2387 can_be_null_(true) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002388 inputs_.SetSize(number_of_inputs);
2389 }
2390
Calin Juravle10e244f2015-01-26 18:54:32 +00002391 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
2392 HInstruction* InputAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002393
Calin Juravle10e244f2015-01-26 18:54:32 +00002394 void SetRawInputAt(size_t index, HInstruction* input) OVERRIDE {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002395 inputs_.Put(index, input);
2396 }
2397
2398 void AddInput(HInstruction* input);
2399
Calin Juravle10e244f2015-01-26 18:54:32 +00002400 Primitive::Type GetType() const OVERRIDE { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002401 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002402
Calin Juravle10e244f2015-01-26 18:54:32 +00002403 bool CanBeNull() const OVERRIDE { return can_be_null_; }
2404 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
2405
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002406 uint32_t GetRegNumber() const { return reg_number_; }
2407
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002408 void SetDead() { is_live_ = false; }
2409 void SetLive() { is_live_ = true; }
2410 bool IsDead() const { return !is_live_; }
2411 bool IsLive() const { return is_live_; }
2412
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002413 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002414
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002415 private:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002416 GrowableArray<HInstruction*> inputs_;
2417 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002418 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002419 bool is_live_;
Calin Juravle10e244f2015-01-26 18:54:32 +00002420 bool can_be_null_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002421
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002422 DISALLOW_COPY_AND_ASSIGN(HPhi);
2423};
2424
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002425class HNullCheck : public HExpression<1> {
2426 public:
2427 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002428 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002429 SetRawInputAt(0, value);
2430 }
2431
Calin Juravle10e244f2015-01-26 18:54:32 +00002432 bool CanBeMoved() const OVERRIDE { return true; }
2433 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002434 UNUSED(other);
2435 return true;
2436 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002437
Calin Juravle10e244f2015-01-26 18:54:32 +00002438 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002439
Calin Juravle10e244f2015-01-26 18:54:32 +00002440 bool CanThrow() const OVERRIDE { return true; }
2441
2442 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01002443
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002444 uint32_t GetDexPc() const { return dex_pc_; }
2445
2446 DECLARE_INSTRUCTION(NullCheck);
2447
2448 private:
2449 const uint32_t dex_pc_;
2450
2451 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
2452};
2453
2454class FieldInfo : public ValueObject {
2455 public:
Calin Juravle52c48962014-12-16 17:02:57 +00002456 FieldInfo(MemberOffset field_offset, Primitive::Type field_type, bool is_volatile)
2457 : field_offset_(field_offset), field_type_(field_type), is_volatile_(is_volatile) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002458
2459 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002460 Primitive::Type GetFieldType() const { return field_type_; }
Calin Juravle52c48962014-12-16 17:02:57 +00002461 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002462
2463 private:
2464 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01002465 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00002466 const bool is_volatile_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002467};
2468
2469class HInstanceFieldGet : public HExpression<1> {
2470 public:
2471 HInstanceFieldGet(HInstruction* value,
2472 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00002473 MemberOffset field_offset,
2474 bool is_volatile)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002475 : HExpression(field_type, SideEffects::DependsOnSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00002476 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002477 SetRawInputAt(0, value);
2478 }
2479
Calin Juravle10c9cbe2014-12-19 10:50:19 +00002480 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002481
2482 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2483 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
2484 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002485 }
2486
Calin Juravle77520bc2015-01-12 18:45:46 +00002487 bool CanDoImplicitNullCheck() const OVERRIDE {
2488 return GetFieldOffset().Uint32Value() < kPageSize;
2489 }
2490
2491 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002492 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
2493 }
2494
Calin Juravle52c48962014-12-16 17:02:57 +00002495 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002496 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002497 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002498 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002499
2500 DECLARE_INSTRUCTION(InstanceFieldGet);
2501
2502 private:
2503 const FieldInfo field_info_;
2504
2505 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
2506};
2507
2508class HInstanceFieldSet : public HTemplateInstruction<2> {
2509 public:
2510 HInstanceFieldSet(HInstruction* object,
2511 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01002512 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00002513 MemberOffset field_offset,
2514 bool is_volatile)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002515 : HTemplateInstruction(SideEffects::ChangesSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00002516 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002517 SetRawInputAt(0, object);
2518 SetRawInputAt(1, value);
2519 }
2520
Calin Juravle77520bc2015-01-12 18:45:46 +00002521 bool CanDoImplicitNullCheck() const OVERRIDE {
2522 return GetFieldOffset().Uint32Value() < kPageSize;
2523 }
2524
Calin Juravle52c48962014-12-16 17:02:57 +00002525 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002526 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002527 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002528 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002529 HInstruction* GetValue() const { return InputAt(1); }
2530
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002531 DECLARE_INSTRUCTION(InstanceFieldSet);
2532
2533 private:
2534 const FieldInfo field_info_;
2535
2536 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
2537};
2538
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002539class HArrayGet : public HExpression<2> {
2540 public:
2541 HArrayGet(HInstruction* array, HInstruction* index, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002542 : HExpression(type, SideEffects::DependsOnSomething()) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002543 SetRawInputAt(0, array);
2544 SetRawInputAt(1, index);
2545 }
2546
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002547 bool CanBeMoved() const OVERRIDE { return true; }
2548 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002549 UNUSED(other);
2550 return true;
2551 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002552 bool CanDoImplicitNullCheck() const OVERRIDE {
2553 // TODO: We can be smarter here.
2554 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
2555 // which generates the implicit null check. There are cases when these can be removed
2556 // to produce better code. If we ever add optimizations to do so we should allow an
2557 // implicit check here (as long as the address falls in the first page).
2558 return false;
2559 }
2560
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002561 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002562
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002563 HInstruction* GetArray() const { return InputAt(0); }
2564 HInstruction* GetIndex() const { return InputAt(1); }
2565
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002566 DECLARE_INSTRUCTION(ArrayGet);
2567
2568 private:
2569 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
2570};
2571
2572class HArraySet : public HTemplateInstruction<3> {
2573 public:
2574 HArraySet(HInstruction* array,
2575 HInstruction* index,
2576 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002577 Primitive::Type expected_component_type,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002578 uint32_t dex_pc)
2579 : HTemplateInstruction(SideEffects::ChangesSomething()),
2580 dex_pc_(dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002581 expected_component_type_(expected_component_type),
2582 needs_type_check_(value->GetType() == Primitive::kPrimNot) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002583 SetRawInputAt(0, array);
2584 SetRawInputAt(1, index);
2585 SetRawInputAt(2, value);
2586 }
2587
Calin Juravle77520bc2015-01-12 18:45:46 +00002588 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002589 // We currently always call a runtime method to catch array store
2590 // exceptions.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002591 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002592 }
2593
Calin Juravle77520bc2015-01-12 18:45:46 +00002594 bool CanDoImplicitNullCheck() const OVERRIDE {
2595 // TODO: Same as for ArrayGet.
2596 return false;
2597 }
2598
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002599 void ClearNeedsTypeCheck() {
2600 needs_type_check_ = false;
2601 }
2602
2603 bool NeedsTypeCheck() const { return needs_type_check_; }
2604
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002605 uint32_t GetDexPc() const { return dex_pc_; }
2606
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002607 HInstruction* GetArray() const { return InputAt(0); }
2608 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002609 HInstruction* GetValue() const { return InputAt(2); }
2610
2611 Primitive::Type GetComponentType() const {
2612 // The Dex format does not type floating point index operations. Since the
2613 // `expected_component_type_` is set during building and can therefore not
2614 // be correct, we also check what is the value type. If it is a floating
2615 // point type, we must use that type.
2616 Primitive::Type value_type = GetValue()->GetType();
2617 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
2618 ? value_type
2619 : expected_component_type_;
2620 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002621
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002622 DECLARE_INSTRUCTION(ArraySet);
2623
2624 private:
2625 const uint32_t dex_pc_;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002626 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002627 bool needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002628
2629 DISALLOW_COPY_AND_ASSIGN(HArraySet);
2630};
2631
2632class HArrayLength : public HExpression<1> {
2633 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002634 explicit HArrayLength(HInstruction* array)
2635 : HExpression(Primitive::kPrimInt, SideEffects::None()) {
2636 // Note that arrays do not change length, so the instruction does not
2637 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002638 SetRawInputAt(0, array);
2639 }
2640
Calin Juravle77520bc2015-01-12 18:45:46 +00002641 bool CanBeMoved() const OVERRIDE { return true; }
2642 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002643 UNUSED(other);
2644 return true;
2645 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002646 bool CanDoImplicitNullCheck() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002647
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002648 DECLARE_INSTRUCTION(ArrayLength);
2649
2650 private:
2651 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
2652};
2653
2654class HBoundsCheck : public HExpression<2> {
2655 public:
2656 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002657 : HExpression(index->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002658 DCHECK(index->GetType() == Primitive::kPrimInt);
2659 SetRawInputAt(0, index);
2660 SetRawInputAt(1, length);
2661 }
2662
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002663 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002664 virtual bool InstructionDataEquals(HInstruction* other) const {
2665 UNUSED(other);
2666 return true;
2667 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002668
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002669 virtual bool NeedsEnvironment() const { return true; }
2670
Roland Levillaine161a2a2014-10-03 12:45:18 +01002671 virtual bool CanThrow() const { return true; }
2672
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002673 uint32_t GetDexPc() const { return dex_pc_; }
2674
2675 DECLARE_INSTRUCTION(BoundsCheck);
2676
2677 private:
2678 const uint32_t dex_pc_;
2679
2680 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
2681};
2682
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002683/**
2684 * Some DEX instructions are folded into multiple HInstructions that need
2685 * to stay live until the last HInstruction. This class
2686 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00002687 * HInstruction stays live. `index` represents the stack location index of the
2688 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002689 */
2690class HTemporary : public HTemplateInstruction<0> {
2691 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002692 explicit HTemporary(size_t index) : HTemplateInstruction(SideEffects::None()), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002693
2694 size_t GetIndex() const { return index_; }
2695
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00002696 Primitive::Type GetType() const OVERRIDE {
2697 // The previous instruction is the one that will be stored in the temporary location.
2698 DCHECK(GetPrevious() != nullptr);
2699 return GetPrevious()->GetType();
2700 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00002701
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002702 DECLARE_INSTRUCTION(Temporary);
2703
2704 private:
2705 const size_t index_;
2706
2707 DISALLOW_COPY_AND_ASSIGN(HTemporary);
2708};
2709
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002710class HSuspendCheck : public HTemplateInstruction<0> {
2711 public:
2712 explicit HSuspendCheck(uint32_t dex_pc)
Nicolas Geoffray9ebc72c2014-09-25 16:33:42 +01002713 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002714
2715 virtual bool NeedsEnvironment() const {
2716 return true;
2717 }
2718
2719 uint32_t GetDexPc() const { return dex_pc_; }
2720
2721 DECLARE_INSTRUCTION(SuspendCheck);
2722
2723 private:
2724 const uint32_t dex_pc_;
2725
2726 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
2727};
2728
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002729/**
2730 * Instruction to load a Class object.
2731 */
2732class HLoadClass : public HExpression<0> {
2733 public:
2734 HLoadClass(uint16_t type_index,
2735 bool is_referrers_class,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002736 uint32_t dex_pc)
2737 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2738 type_index_(type_index),
2739 is_referrers_class_(is_referrers_class),
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002740 dex_pc_(dex_pc),
Calin Juravleb1498f62015-02-16 13:13:29 +00002741 generate_clinit_check_(false),
2742 loaded_class_rti_(ReferenceTypeInfo::CreateTop(/* is_exact */ false)) {}
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002743
2744 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002745
2746 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2747 return other->AsLoadClass()->type_index_ == type_index_;
2748 }
2749
2750 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
2751
2752 uint32_t GetDexPc() const { return dex_pc_; }
2753 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002754 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002755
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002756 bool NeedsEnvironment() const OVERRIDE {
2757 // Will call runtime and load the class if the class is not loaded yet.
2758 // TODO: finer grain decision.
2759 return !is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002760 }
2761
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002762 bool MustGenerateClinitCheck() const {
2763 return generate_clinit_check_;
2764 }
2765
2766 void SetMustGenerateClinitCheck() {
2767 generate_clinit_check_ = true;
2768 }
2769
2770 bool CanCallRuntime() const {
2771 return MustGenerateClinitCheck() || !is_referrers_class_;
2772 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002773
Nicolas Geoffray82091da2015-01-26 10:02:45 +00002774 bool CanThrow() const OVERRIDE {
2775 // May call runtime and and therefore can throw.
2776 // TODO: finer grain decision.
2777 return !is_referrers_class_;
2778 }
2779
Calin Juravleacf735c2015-02-12 15:25:22 +00002780 ReferenceTypeInfo GetLoadedClassRTI() {
2781 return loaded_class_rti_;
2782 }
2783
2784 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
2785 // Make sure we only set exact types (the loaded class should never be merged).
2786 DCHECK(rti.IsExact());
2787 loaded_class_rti_ = rti;
2788 }
2789
2790 bool IsResolved() {
2791 return loaded_class_rti_.IsExact();
2792 }
2793
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002794 DECLARE_INSTRUCTION(LoadClass);
2795
2796 private:
2797 const uint16_t type_index_;
2798 const bool is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002799 const uint32_t dex_pc_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002800 // Whether this instruction must generate the initialization check.
2801 // Used for code generation.
2802 bool generate_clinit_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002803
Calin Juravleacf735c2015-02-12 15:25:22 +00002804 ReferenceTypeInfo loaded_class_rti_;
2805
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002806 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
2807};
2808
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002809class HLoadString : public HExpression<0> {
2810 public:
2811 HLoadString(uint32_t string_index, uint32_t dex_pc)
2812 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2813 string_index_(string_index),
2814 dex_pc_(dex_pc) {}
2815
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002816 bool CanBeMoved() const OVERRIDE { return true; }
2817
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002818 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2819 return other->AsLoadString()->string_index_ == string_index_;
2820 }
2821
2822 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
2823
2824 uint32_t GetDexPc() const { return dex_pc_; }
2825 uint32_t GetStringIndex() const { return string_index_; }
2826
2827 // TODO: Can we deopt or debug when we resolve a string?
2828 bool NeedsEnvironment() const OVERRIDE { return false; }
2829
2830 DECLARE_INSTRUCTION(LoadString);
2831
2832 private:
2833 const uint32_t string_index_;
2834 const uint32_t dex_pc_;
2835
2836 DISALLOW_COPY_AND_ASSIGN(HLoadString);
2837};
2838
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002839// TODO: Pass this check to HInvokeStaticOrDirect nodes.
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002840/**
2841 * Performs an initialization check on its Class object input.
2842 */
2843class HClinitCheck : public HExpression<1> {
2844 public:
2845 explicit HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
2846 : HExpression(Primitive::kPrimNot, SideEffects::All()),
2847 dex_pc_(dex_pc) {
2848 SetRawInputAt(0, constant);
2849 }
2850
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002851 bool CanBeMoved() const OVERRIDE { return true; }
2852 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2853 UNUSED(other);
2854 return true;
2855 }
2856
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002857 bool NeedsEnvironment() const OVERRIDE {
2858 // May call runtime to initialize the class.
2859 return true;
2860 }
2861
2862 uint32_t GetDexPc() const { return dex_pc_; }
2863
2864 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
2865
2866 DECLARE_INSTRUCTION(ClinitCheck);
2867
2868 private:
2869 const uint32_t dex_pc_;
2870
2871 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
2872};
2873
2874class HStaticFieldGet : public HExpression<1> {
2875 public:
2876 HStaticFieldGet(HInstruction* cls,
2877 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00002878 MemberOffset field_offset,
2879 bool is_volatile)
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002880 : HExpression(field_type, SideEffects::DependsOnSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00002881 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002882 SetRawInputAt(0, cls);
2883 }
2884
Calin Juravle52c48962014-12-16 17:02:57 +00002885
Calin Juravle10c9cbe2014-12-19 10:50:19 +00002886 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002887
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002888 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00002889 HStaticFieldGet* other_get = other->AsStaticFieldGet();
2890 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002891 }
2892
2893 size_t ComputeHashCode() const OVERRIDE {
2894 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
2895 }
2896
Calin Juravle52c48962014-12-16 17:02:57 +00002897 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002898 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
2899 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002900 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002901
2902 DECLARE_INSTRUCTION(StaticFieldGet);
2903
2904 private:
2905 const FieldInfo field_info_;
2906
2907 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
2908};
2909
2910class HStaticFieldSet : public HTemplateInstruction<2> {
2911 public:
2912 HStaticFieldSet(HInstruction* cls,
2913 HInstruction* value,
2914 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00002915 MemberOffset field_offset,
2916 bool is_volatile)
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002917 : HTemplateInstruction(SideEffects::ChangesSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00002918 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002919 SetRawInputAt(0, cls);
2920 SetRawInputAt(1, value);
2921 }
2922
Calin Juravle52c48962014-12-16 17:02:57 +00002923 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002924 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
2925 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002926 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002927
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002928 HInstruction* GetValue() const { return InputAt(1); }
2929
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002930 DECLARE_INSTRUCTION(StaticFieldSet);
2931
2932 private:
2933 const FieldInfo field_info_;
2934
2935 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
2936};
2937
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002938// Implement the move-exception DEX instruction.
2939class HLoadException : public HExpression<0> {
2940 public:
2941 HLoadException() : HExpression(Primitive::kPrimNot, SideEffects::None()) {}
2942
2943 DECLARE_INSTRUCTION(LoadException);
2944
2945 private:
2946 DISALLOW_COPY_AND_ASSIGN(HLoadException);
2947};
2948
2949class HThrow : public HTemplateInstruction<1> {
2950 public:
2951 HThrow(HInstruction* exception, uint32_t dex_pc)
2952 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {
2953 SetRawInputAt(0, exception);
2954 }
2955
2956 bool IsControlFlow() const OVERRIDE { return true; }
2957
2958 bool NeedsEnvironment() const OVERRIDE { return true; }
2959
Nicolas Geoffray82091da2015-01-26 10:02:45 +00002960 bool CanThrow() const OVERRIDE { return true; }
2961
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002962 uint32_t GetDexPc() const { return dex_pc_; }
2963
2964 DECLARE_INSTRUCTION(Throw);
2965
2966 private:
Nicolas Geoffray82091da2015-01-26 10:02:45 +00002967 const uint32_t dex_pc_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002968
2969 DISALLOW_COPY_AND_ASSIGN(HThrow);
2970};
2971
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002972class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002973 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002974 HInstanceOf(HInstruction* object,
2975 HLoadClass* constant,
2976 bool class_is_final,
2977 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002978 : HExpression(Primitive::kPrimBoolean, SideEffects::None()),
2979 class_is_final_(class_is_final),
2980 dex_pc_(dex_pc) {
2981 SetRawInputAt(0, object);
2982 SetRawInputAt(1, constant);
2983 }
2984
2985 bool CanBeMoved() const OVERRIDE { return true; }
2986
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002987 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002988 return true;
2989 }
2990
2991 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002992 return false;
2993 }
2994
2995 uint32_t GetDexPc() const { return dex_pc_; }
2996
2997 bool IsClassFinal() const { return class_is_final_; }
2998
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002999 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003000
3001 private:
3002 const bool class_is_final_;
3003 const uint32_t dex_pc_;
3004
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003005 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
3006};
3007
Calin Juravleb1498f62015-02-16 13:13:29 +00003008class HBoundType : public HExpression<1> {
3009 public:
3010 HBoundType(HInstruction* input, ReferenceTypeInfo bound_type)
3011 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3012 bound_type_(bound_type) {
3013 SetRawInputAt(0, input);
3014 }
3015
3016 const ReferenceTypeInfo& GetBoundType() const { return bound_type_; }
3017
3018 bool CanBeNull() const OVERRIDE {
3019 // `null instanceof ClassX` always return false so we can't be null.
3020 return false;
3021 }
3022
3023 DECLARE_INSTRUCTION(BoundType);
3024
3025 private:
3026 // Encodes the most upper class that this instruction can have. In other words
3027 // it is always the case that GetBoundType().IsSupertypeOf(GetReferenceType()).
3028 // It is used to bound the type in cases like `if (x instanceof ClassX) {}`
3029 const ReferenceTypeInfo bound_type_;
3030
3031 DISALLOW_COPY_AND_ASSIGN(HBoundType);
3032};
3033
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003034class HCheckCast : public HTemplateInstruction<2> {
3035 public:
3036 HCheckCast(HInstruction* object,
3037 HLoadClass* constant,
3038 bool class_is_final,
3039 uint32_t dex_pc)
3040 : HTemplateInstruction(SideEffects::None()),
3041 class_is_final_(class_is_final),
3042 dex_pc_(dex_pc) {
3043 SetRawInputAt(0, object);
3044 SetRawInputAt(1, constant);
3045 }
3046
3047 bool CanBeMoved() const OVERRIDE { return true; }
3048
3049 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
3050 return true;
3051 }
3052
3053 bool NeedsEnvironment() const OVERRIDE {
3054 // Instruction may throw a CheckCastError.
3055 return true;
3056 }
3057
3058 bool CanThrow() const OVERRIDE { return true; }
3059
3060 uint32_t GetDexPc() const { return dex_pc_; }
3061
3062 bool IsClassFinal() const { return class_is_final_; }
3063
3064 DECLARE_INSTRUCTION(CheckCast);
3065
3066 private:
3067 const bool class_is_final_;
3068 const uint32_t dex_pc_;
3069
3070 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003071};
3072
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003073class HMonitorOperation : public HTemplateInstruction<1> {
3074 public:
3075 enum OperationKind {
3076 kEnter,
3077 kExit,
3078 };
3079
3080 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
3081 : HTemplateInstruction(SideEffects::None()), kind_(kind), dex_pc_(dex_pc) {
3082 SetRawInputAt(0, object);
3083 }
3084
3085 // Instruction may throw a Java exception, so we need an environment.
3086 bool NeedsEnvironment() const OVERRIDE { return true; }
3087 bool CanThrow() const OVERRIDE { return true; }
3088
3089 uint32_t GetDexPc() const { return dex_pc_; }
3090
3091 bool IsEnter() const { return kind_ == kEnter; }
3092
3093 DECLARE_INSTRUCTION(MonitorOperation);
3094
Calin Juravle52c48962014-12-16 17:02:57 +00003095 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003096 const OperationKind kind_;
3097 const uint32_t dex_pc_;
3098
3099 private:
3100 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
3101};
3102
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003103class MoveOperands : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003104 public:
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003105 MoveOperands(Location source, Location destination, HInstruction* instruction)
3106 : source_(source), destination_(destination), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003107
3108 Location GetSource() const { return source_; }
3109 Location GetDestination() const { return destination_; }
3110
3111 void SetSource(Location value) { source_ = value; }
3112 void SetDestination(Location value) { destination_ = value; }
3113
3114 // The parallel move resolver marks moves as "in-progress" by clearing the
3115 // destination (but not the source).
3116 Location MarkPending() {
3117 DCHECK(!IsPending());
3118 Location dest = destination_;
3119 destination_ = Location::NoLocation();
3120 return dest;
3121 }
3122
3123 void ClearPending(Location dest) {
3124 DCHECK(IsPending());
3125 destination_ = dest;
3126 }
3127
3128 bool IsPending() const {
3129 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
3130 return destination_.IsInvalid() && !source_.IsInvalid();
3131 }
3132
3133 // True if this blocks a move from the given location.
3134 bool Blocks(Location loc) const {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003135 return !IsEliminated() && (source_.Contains(loc) || loc.Contains(source_));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003136 }
3137
3138 // A move is redundant if it's been eliminated, if its source and
3139 // destination are the same, or if its destination is unneeded.
3140 bool IsRedundant() const {
3141 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
3142 }
3143
3144 // We clear both operands to indicate move that's been eliminated.
3145 void Eliminate() {
3146 source_ = destination_ = Location::NoLocation();
3147 }
3148
3149 bool IsEliminated() const {
3150 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
3151 return source_.IsInvalid();
3152 }
3153
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003154 HInstruction* GetInstruction() const { return instruction_; }
3155
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003156 private:
3157 Location source_;
3158 Location destination_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003159 // The instruction this move is assocatied with. Null when this move is
3160 // for moving an input in the expected locations of user (including a phi user).
3161 // This is only used in debug mode, to ensure we do not connect interval siblings
3162 // in the same parallel move.
3163 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003164};
3165
3166static constexpr size_t kDefaultNumberOfMoves = 4;
3167
3168class HParallelMove : public HTemplateInstruction<0> {
3169 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003170 explicit HParallelMove(ArenaAllocator* arena)
3171 : HTemplateInstruction(SideEffects::None()), moves_(arena, kDefaultNumberOfMoves) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003172
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003173 void AddMove(Location source, Location destination, HInstruction* instruction) {
3174 DCHECK(source.IsValid());
3175 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003176 if (kIsDebugBuild) {
3177 if (instruction != nullptr) {
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00003178 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003179 DCHECK_NE(moves_.Get(i).GetInstruction(), instruction)
3180 << "Doing parallel moves for the same instruction.";
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00003181 }
3182 }
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003183 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
3184 DCHECK(!destination.Equals(moves_.Get(i).GetDestination()))
3185 << "Same destination for two moves in a parallel move.";
3186 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003187 }
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003188 moves_.Add(MoveOperands(source, destination, instruction));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003189 }
3190
3191 MoveOperands* MoveOperandsAt(size_t index) const {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003192 return moves_.GetRawStorage() + index;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003193 }
3194
3195 size_t NumMoves() const { return moves_.Size(); }
3196
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003197 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003198
3199 private:
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003200 GrowableArray<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003201
3202 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
3203};
3204
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003205class HGraphVisitor : public ValueObject {
3206 public:
Dave Allison20dfc792014-06-16 20:44:29 -07003207 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
3208 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003209
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003210 virtual void VisitInstruction(HInstruction* instruction) { UNUSED(instruction); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003211 virtual void VisitBasicBlock(HBasicBlock* block);
3212
Roland Levillain633021e2014-10-01 14:12:25 +01003213 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003214 void VisitInsertionOrder();
3215
Roland Levillain633021e2014-10-01 14:12:25 +01003216 // Visit the graph following dominator tree reverse post-order.
3217 void VisitReversePostOrder();
3218
Nicolas Geoffray787c3072014-03-17 10:20:19 +00003219 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00003220
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003221 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003222#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003223 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
3224
3225 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
3226
3227#undef DECLARE_VISIT_INSTRUCTION
3228
3229 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07003230 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003231
3232 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
3233};
3234
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003235class HGraphDelegateVisitor : public HGraphVisitor {
3236 public:
3237 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
3238 virtual ~HGraphDelegateVisitor() {}
3239
3240 // Visit functions that delegate to to super class.
3241#define DECLARE_VISIT_INSTRUCTION(name, super) \
3242 virtual void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
3243
3244 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
3245
3246#undef DECLARE_VISIT_INSTRUCTION
3247
3248 private:
3249 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
3250};
3251
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003252class HInsertionOrderIterator : public ValueObject {
3253 public:
3254 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
3255
3256 bool Done() const { return index_ == graph_.GetBlocks().Size(); }
3257 HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); }
3258 void Advance() { ++index_; }
3259
3260 private:
3261 const HGraph& graph_;
3262 size_t index_;
3263
3264 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
3265};
3266
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003267class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003268 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003269 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003270
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003271 bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); }
3272 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003273 void Advance() { ++index_; }
3274
3275 private:
3276 const HGraph& graph_;
3277 size_t index_;
3278
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003279 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003280};
3281
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003282class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003283 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003284 explicit HPostOrderIterator(const HGraph& graph)
3285 : graph_(graph), index_(graph_.GetReversePostOrder().Size()) {}
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003286
3287 bool Done() const { return index_ == 0; }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003288 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003289 void Advance() { --index_; }
3290
3291 private:
3292 const HGraph& graph_;
3293 size_t index_;
3294
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003295 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003296};
3297
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003298// Iterator over the blocks that art part of the loop. Includes blocks part
3299// of an inner loop. The order in which the blocks are iterated is on their
3300// block id.
3301class HBlocksInLoopIterator : public ValueObject {
3302 public:
3303 explicit HBlocksInLoopIterator(const HLoopInformation& info)
3304 : blocks_in_loop_(info.GetBlocks()),
3305 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
3306 index_(0) {
3307 if (!blocks_in_loop_.IsBitSet(index_)) {
3308 Advance();
3309 }
3310 }
3311
3312 bool Done() const { return index_ == blocks_.Size(); }
3313 HBasicBlock* Current() const { return blocks_.Get(index_); }
3314 void Advance() {
3315 ++index_;
3316 for (size_t e = blocks_.Size(); index_ < e; ++index_) {
3317 if (blocks_in_loop_.IsBitSet(index_)) {
3318 break;
3319 }
3320 }
3321 }
3322
3323 private:
3324 const BitVector& blocks_in_loop_;
3325 const GrowableArray<HBasicBlock*>& blocks_;
3326 size_t index_;
3327
3328 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
3329};
3330
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003331} // namespace art
3332
3333#endif // ART_COMPILER_OPTIMIZING_NODES_H_