blob: 5f50494482542b7c4c7db29a23513e88c8564035 [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
David Brazdil8d5b8b22015-03-24 10:51:52 +000020#include "base/arena_containers.h"
Mathieu Chartierb666f482015-02-18 14:33:14 -080021#include "base/arena_object.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000022#include "entrypoints/quick/quick_entrypoints_enum.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000023#include "handle.h"
24#include "handle_scope.h"
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000025#include "invoke_type.h"
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +010026#include "locations.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000027#include "mirror/class.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010028#include "offsets.h"
Ian Rogerse63db272014-07-15 15:36:11 -070029#include "primitive.h"
Nicolas Geoffray0e336432014-02-26 18:24:38 +000030#include "utils/arena_bit_vector.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000031#include "utils/growable_array.h"
32
33namespace art {
34
David Brazdil1abb4192015-02-17 18:33:36 +000035class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000036class HBasicBlock;
David Brazdil8d5b8b22015-03-24 10:51:52 +000037class HDoubleConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010038class HEnvironment;
David Brazdil8d5b8b22015-03-24 10:51:52 +000039class HFloatConstant;
40class HGraphVisitor;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000041class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000042class HIntConstant;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000043class HInvoke;
David Brazdil8d5b8b22015-03-24 10:51:52 +000044class HLongConstant;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +000045class HNullConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010046class HPhi;
Nicolas Geoffray3c049742014-09-24 18:10:46 +010047class HSuspendCheck;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010048class LiveInterval;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000049class LocationSummary;
David Brazdil8d5b8b22015-03-24 10:51:52 +000050class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000051
52static const int kDefaultNumberOfBlocks = 8;
53static const int kDefaultNumberOfSuccessors = 2;
54static const int kDefaultNumberOfPredecessors = 2;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010055static const int kDefaultNumberOfDominatedBlocks = 1;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000056static const int kDefaultNumberOfBackEdges = 1;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000057
Calin Juravle9aec02f2014-11-18 23:06:35 +000058static constexpr uint32_t kMaxIntShiftValue = 0x1f;
59static constexpr uint64_t kMaxLongShiftValue = 0x3f;
60
Dave Allison20dfc792014-06-16 20:44:29 -070061enum IfCondition {
62 kCondEQ,
63 kCondNE,
64 kCondLT,
65 kCondLE,
66 kCondGT,
67 kCondGE,
68};
69
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010070class HInstructionList {
71 public:
72 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
73
74 void AddInstruction(HInstruction* instruction);
75 void RemoveInstruction(HInstruction* instruction);
76
Roland Levillain6b469232014-09-25 10:10:38 +010077 // Return true if this list contains `instruction`.
78 bool Contains(HInstruction* instruction) const;
79
Roland Levillainccc07a92014-09-16 14:48:16 +010080 // Return true if `instruction1` is found before `instruction2` in
81 // this instruction list and false otherwise. Abort if none
82 // of these instructions is found.
83 bool FoundBefore(const HInstruction* instruction1,
84 const HInstruction* instruction2) const;
85
Nicolas Geoffray276d9da2015-02-02 18:24:11 +000086 bool IsEmpty() const { return first_instruction_ == nullptr; }
87 void Clear() { first_instruction_ = last_instruction_ = nullptr; }
88
89 // Update the block of all instructions to be `block`.
90 void SetBlockOfInstructions(HBasicBlock* block) const;
91
92 void AddAfter(HInstruction* cursor, const HInstructionList& instruction_list);
93 void Add(const HInstructionList& instruction_list);
94
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010095 private:
96 HInstruction* first_instruction_;
97 HInstruction* last_instruction_;
98
99 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000100 friend class HGraph;
101 friend class HInstruction;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100102 friend class HInstructionIterator;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100103 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100104
105 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
106};
107
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000108// Control-flow graph of a method. Contains a list of basic blocks.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700109class HGraph : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000110 public:
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000111 HGraph(ArenaAllocator* arena, bool debuggable = false, int start_instruction_id = 0)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000112 : arena_(arena),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000113 blocks_(arena, kDefaultNumberOfBlocks),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100114 reverse_post_order_(arena, kDefaultNumberOfBlocks),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700115 entry_block_(nullptr),
116 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100117 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100118 number_of_vregs_(0),
119 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000120 temporaries_vreg_slots_(0),
Mingyao Yange4335eb2015-03-02 15:14:13 -0800121 has_array_accesses_(false),
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000122 debuggable_(debuggable),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000123 current_instruction_id_(start_instruction_id),
124 cached_null_constant_(nullptr),
125 cached_int_constants_(std::less<int32_t>(), arena->Adapter()),
126 cached_long_constants_(std::less<int64_t>(), arena->Adapter()) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000127
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000128 ArenaAllocator* GetArena() const { return arena_; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100129 const GrowableArray<HBasicBlock*>& GetBlocks() const { return blocks_; }
Roland Levillain93445682014-10-06 19:24:02 +0100130 HBasicBlock* GetBlock(size_t id) const { return blocks_.Get(id); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000131
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000132 HBasicBlock* GetEntryBlock() const { return entry_block_; }
133 HBasicBlock* GetExitBlock() const { return exit_block_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000134
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000135 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
136 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000137
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000138 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100139
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000140 // Try building the SSA form of this graph, with dominance computation and loop
141 // recognition. Returns whether it was successful in doing all these steps.
142 bool TryBuildingSsa() {
143 BuildDominatorTree();
Nicolas Geoffrayd3350832015-03-12 11:16:23 +0000144 // The SSA builder requires loops to all be natural. Specifically, the dead phi
145 // elimination phase checks the consistency of the graph when doing a post-order
146 // visit for eliminating dead phis: a dead phi can only have loop header phi
147 // users remaining when being visited.
148 if (!AnalyzeNaturalLoops()) return false;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000149 TransformToSsa();
Nicolas Geoffrayd3350832015-03-12 11:16:23 +0000150 return true;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000151 }
152
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000153 void BuildDominatorTree();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000154 void TransformToSsa();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100155 void SimplifyCFG();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000156
Nicolas Geoffrayf5370122014-12-02 11:51:19 +0000157 // Analyze all natural loops in this graph. Returns false if one
158 // loop is not natural, that is the header does not dominate the
159 // back edge.
160 bool AnalyzeNaturalLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100161
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000162 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
163 void InlineInto(HGraph* outer_graph, HInvoke* invoke);
164
David Brazdil46e2a392015-03-16 17:31:52 +0000165 void MergeEmptyBranches(HBasicBlock* start_block, HBasicBlock* end_block);
166
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100167 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
168 void SimplifyLoop(HBasicBlock* header);
169
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000170 int32_t GetNextInstructionId() {
171 DCHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000172 return current_instruction_id_++;
173 }
174
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000175 int32_t GetCurrentInstructionId() const {
176 return current_instruction_id_;
177 }
178
179 void SetCurrentInstructionId(int32_t id) {
180 current_instruction_id_ = id;
181 }
182
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100183 uint16_t GetMaximumNumberOfOutVRegs() const {
184 return maximum_number_of_out_vregs_;
185 }
186
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000187 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
188 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100189 }
190
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000191 void UpdateTemporariesVRegSlots(size_t slots) {
192 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100193 }
194
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000195 size_t GetTemporariesVRegSlots() const {
196 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100197 }
198
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100199 void SetNumberOfVRegs(uint16_t number_of_vregs) {
200 number_of_vregs_ = number_of_vregs;
201 }
202
203 uint16_t GetNumberOfVRegs() const {
204 return number_of_vregs_;
205 }
206
207 void SetNumberOfInVRegs(uint16_t value) {
208 number_of_in_vregs_ = value;
209 }
210
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100211 uint16_t GetNumberOfLocalVRegs() const {
212 return number_of_vregs_ - number_of_in_vregs_;
213 }
214
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100215 const GrowableArray<HBasicBlock*>& GetReversePostOrder() const {
216 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100217 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100218
Mingyao Yange4335eb2015-03-02 15:14:13 -0800219 bool HasArrayAccesses() const {
220 return has_array_accesses_;
221 }
222
223 void SetHasArrayAccesses(bool value) {
224 has_array_accesses_ = value;
225 }
226
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000227 bool IsDebuggable() const { return debuggable_; }
228
David Brazdil8d5b8b22015-03-24 10:51:52 +0000229 // Returns a constant of the given type and value. If it does not exist
230 // already, it is created and inserted into the graph. Only integral types
231 // are currently supported.
232 HConstant* GetConstant(Primitive::Type type, int64_t value);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000233 HNullConstant* GetNullConstant();
David Brazdil8d5b8b22015-03-24 10:51:52 +0000234 HIntConstant* GetIntConstant(int32_t value) {
235 return CreateConstant(value, &cached_int_constants_);
236 }
237 HLongConstant* GetLongConstant(int64_t value) {
238 return CreateConstant(value, &cached_long_constants_);
239 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000240
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000241 private:
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000242 HBasicBlock* FindCommonDominator(HBasicBlock* first, HBasicBlock* second) const;
243 void VisitBlockForDominatorTree(HBasicBlock* block,
244 HBasicBlock* predecessor,
245 GrowableArray<size_t>* visits);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100246 void FindBackEdges(ArenaBitVector* visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000247 void VisitBlockForBackEdges(HBasicBlock* block,
248 ArenaBitVector* visited,
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100249 ArenaBitVector* visiting);
Roland Levillainfc600dc2014-12-02 17:16:31 +0000250 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000251 void RemoveDeadBlocks(const ArenaBitVector& visited) const;
252
David Brazdil8d5b8b22015-03-24 10:51:52 +0000253 template <class InstType, typename ValueType>
254 InstType* CreateConstant(ValueType value, ArenaSafeMap<ValueType, InstType*>* cache);
255 void InsertConstant(HConstant* instruction);
256
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000257 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000258
259 // List of blocks in insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000260 GrowableArray<HBasicBlock*> blocks_;
261
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100262 // List of blocks to perform a reverse post order tree traversal.
263 GrowableArray<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000264
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000265 HBasicBlock* entry_block_;
266 HBasicBlock* exit_block_;
267
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100268 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100269 uint16_t maximum_number_of_out_vregs_;
270
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100271 // The number of virtual registers in this method. Contains the parameters.
272 uint16_t number_of_vregs_;
273
274 // The number of virtual registers used by parameters of this method.
275 uint16_t number_of_in_vregs_;
276
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000277 // Number of vreg size slots that the temporaries use (used in baseline compiler).
278 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100279
Mingyao Yange4335eb2015-03-02 15:14:13 -0800280 // Has array accesses. We can totally skip BCE if it's false.
281 bool has_array_accesses_;
282
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000283 // Indicates whether the graph should be compiled in a way that
284 // ensures full debuggability. If false, we can apply more
285 // aggressive optimizations that may limit the level of debugging.
286 const bool debuggable_;
287
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000288 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000289 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000290
David Brazdil46e2a392015-03-16 17:31:52 +0000291 // Cached common constants often needed by optimization passes.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000292 HNullConstant* cached_null_constant_;
293 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
294 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000295
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000296 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000297 DISALLOW_COPY_AND_ASSIGN(HGraph);
298};
299
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700300class HLoopInformation : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000301 public:
302 HLoopInformation(HBasicBlock* header, HGraph* graph)
303 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100304 suspend_check_(nullptr),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100305 back_edges_(graph->GetArena(), kDefaultNumberOfBackEdges),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100306 // Make bit vector growable, as the number of blocks may change.
307 blocks_(graph->GetArena(), graph->GetBlocks().Size(), true) {}
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100308
309 HBasicBlock* GetHeader() const {
310 return header_;
311 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000312
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000313 void SetHeader(HBasicBlock* block) {
314 header_ = block;
315 }
316
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100317 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
318 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
319 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
320
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000321 void AddBackEdge(HBasicBlock* back_edge) {
322 back_edges_.Add(back_edge);
323 }
324
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100325 void RemoveBackEdge(HBasicBlock* back_edge) {
326 back_edges_.Delete(back_edge);
327 }
328
David Brazdil46e2a392015-03-16 17:31:52 +0000329 bool IsBackEdge(const HBasicBlock& block) const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100330 for (size_t i = 0, e = back_edges_.Size(); i < e; ++i) {
David Brazdil46e2a392015-03-16 17:31:52 +0000331 if (back_edges_.Get(i) == &block) return true;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100332 }
333 return false;
334 }
335
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000336 size_t NumberOfBackEdges() const {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000337 return back_edges_.Size();
338 }
339
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100340 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100341
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100342 const GrowableArray<HBasicBlock*>& GetBackEdges() const {
343 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100344 }
345
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100346 void ClearBackEdges() {
347 back_edges_.Reset();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100348 }
349
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100350 // Find blocks that are part of this loop. Returns whether the loop is a natural loop,
351 // that is the header dominates the back edge.
352 bool Populate();
353
354 // Returns whether this loop information contains `block`.
355 // Note that this loop information *must* be populated before entering this function.
356 bool Contains(const HBasicBlock& block) const;
357
358 // Returns whether this loop information is an inner loop of `other`.
359 // Note that `other` *must* be populated before entering this function.
360 bool IsIn(const HLoopInformation& other) const;
361
362 const ArenaBitVector& GetBlocks() const { return blocks_; }
363
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000364 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000365 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000366
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000367 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100368 // Internal recursive implementation of `Populate`.
369 void PopulateRecursive(HBasicBlock* block);
370
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000371 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100372 HSuspendCheck* suspend_check_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000373 GrowableArray<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100374 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000375
376 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
377};
378
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100379static constexpr size_t kNoLifetime = -1;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100380static constexpr uint32_t kNoDexPc = -1;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100381
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000382// A block in a method. Contains the list of instructions represented
383// as a double linked list. Each block knows its predecessors and
384// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100385
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700386class HBasicBlock : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000387 public:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100388 explicit HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000389 : graph_(graph),
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000390 predecessors_(graph->GetArena(), kDefaultNumberOfPredecessors),
391 successors_(graph->GetArena(), kDefaultNumberOfSuccessors),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000392 loop_information_(nullptr),
393 dominator_(nullptr),
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100394 dominated_blocks_(graph->GetArena(), kDefaultNumberOfDominatedBlocks),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100395 block_id_(-1),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100396 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100397 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000398 lifetime_end_(kNoLifetime),
399 is_catch_block_(false) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000400
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100401 const GrowableArray<HBasicBlock*>& GetPredecessors() const {
402 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000403 }
404
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100405 const GrowableArray<HBasicBlock*>& GetSuccessors() const {
406 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000407 }
408
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100409 const GrowableArray<HBasicBlock*>& GetDominatedBlocks() const {
410 return dominated_blocks_;
411 }
412
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100413 bool IsEntryBlock() const {
414 return graph_->GetEntryBlock() == this;
415 }
416
417 bool IsExitBlock() const {
418 return graph_->GetExitBlock() == this;
419 }
420
David Brazdil46e2a392015-03-16 17:31:52 +0000421 bool IsSingleGoto() const;
422
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000423 void AddBackEdge(HBasicBlock* back_edge) {
424 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000425 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000426 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100427 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000428 loop_information_->AddBackEdge(back_edge);
429 }
430
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000431 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000432 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000433
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000434 int GetBlockId() const { return block_id_; }
435 void SetBlockId(int id) { block_id_ = id; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000436
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000437 HBasicBlock* GetDominator() const { return dominator_; }
438 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100439 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.Add(block); }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000440 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
441 for (size_t i = 0, e = dominated_blocks_.Size(); i < e; ++i) {
442 if (dominated_blocks_.Get(i) == existing) {
443 dominated_blocks_.Put(i, new_block);
444 return;
445 }
446 }
447 LOG(FATAL) << "Unreachable";
448 UNREACHABLE();
449 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000450
451 int NumberOfBackEdges() const {
452 return loop_information_ == nullptr
453 ? 0
454 : loop_information_->NumberOfBackEdges();
455 }
456
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100457 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
458 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100459 const HInstructionList& GetInstructions() const { return instructions_; }
460 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100461 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000462
463 void AddSuccessor(HBasicBlock* block) {
464 successors_.Add(block);
465 block->predecessors_.Add(this);
466 }
467
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100468 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
469 size_t successor_index = GetSuccessorIndexOf(existing);
470 DCHECK_NE(successor_index, static_cast<size_t>(-1));
471 existing->RemovePredecessor(this);
472 new_block->predecessors_.Add(this);
473 successors_.Put(successor_index, new_block);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000474 }
475
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000476 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
477 size_t predecessor_index = GetPredecessorIndexOf(existing);
478 DCHECK_NE(predecessor_index, static_cast<size_t>(-1));
479 existing->RemoveSuccessor(this);
480 new_block->successors_.Add(this);
481 predecessors_.Put(predecessor_index, new_block);
482 }
483
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100484 void RemovePredecessor(HBasicBlock* block) {
485 predecessors_.Delete(block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100486 }
487
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000488 void RemoveSuccessor(HBasicBlock* block) {
489 successors_.Delete(block);
490 }
491
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100492 void ClearAllPredecessors() {
493 predecessors_.Reset();
494 }
495
496 void AddPredecessor(HBasicBlock* block) {
497 predecessors_.Add(block);
498 block->successors_.Add(this);
499 }
500
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100501 void SwapPredecessors() {
Nicolas Geoffrayc83d4412014-09-18 16:46:20 +0100502 DCHECK_EQ(predecessors_.Size(), 2u);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100503 HBasicBlock* temp = predecessors_.Get(0);
504 predecessors_.Put(0, predecessors_.Get(1));
505 predecessors_.Put(1, temp);
506 }
507
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100508 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) {
509 for (size_t i = 0, e = predecessors_.Size(); i < e; ++i) {
510 if (predecessors_.Get(i) == predecessor) {
511 return i;
512 }
513 }
514 return -1;
515 }
516
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100517 size_t GetSuccessorIndexOf(HBasicBlock* successor) {
518 for (size_t i = 0, e = successors_.Size(); i < e; ++i) {
519 if (successors_.Get(i) == successor) {
520 return i;
521 }
522 }
523 return -1;
524 }
525
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000526 // Split the block into two blocks just after `cursor`. Returns the newly
527 // created block. Note that this method just updates raw block information,
528 // like predecessors, successors, dominators, and instruction list. It does not
529 // update the graph, reverse post order, loop information, nor make sure the
530 // blocks are consistent (for example ending with a control flow instruction).
531 HBasicBlock* SplitAfter(HInstruction* cursor);
532
533 // Merge `other` at the end of `this`. Successors and dominated blocks of
534 // `other` are changed to be successors and dominated blocks of `this`. Note
535 // that this method does not update the graph, reverse post order, loop
536 // information, nor make sure the blocks are consistent (for example ending
537 // with a control flow instruction).
538 void MergeWith(HBasicBlock* other);
539
540 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
541 // of `this` are moved to `other`.
542 // Note that this method does not update the graph, reverse post order, loop
543 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +0000544 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000545 void ReplaceWith(HBasicBlock* other);
546
David Brazdil46e2a392015-03-16 17:31:52 +0000547 // Disconnects `this` from all its predecessors, successors and the dominator.
548 // It assumes that `this` does not dominate any blocks.
549 // Note that this method does not update the graph, reverse post order, loop
550 // information, nor make sure the blocks are consistent (for example ending
551 // with a control flow instruction).
552 void DisconnectFromAll();
553
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000554 void AddInstruction(HInstruction* instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100555 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +0100556 // Replace instruction `initial` with `replacement` within this block.
557 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
558 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100559 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100560 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +0000561 // RemoveInstruction and RemovePhi delete a given instruction from the respective
562 // instruction list. With 'ensure_safety' set to true, it verifies that the
563 // instruction is not in use and removes it from the use lists of its inputs.
564 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
565 void RemovePhi(HPhi* phi, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100566
567 bool IsLoopHeader() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100568 return (loop_information_ != nullptr) && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100569 }
570
Roland Levillain6b879dd2014-09-22 17:13:44 +0100571 bool IsLoopPreHeaderFirstPredecessor() const {
572 DCHECK(IsLoopHeader());
573 DCHECK(!GetPredecessors().IsEmpty());
574 return GetPredecessors().Get(0) == GetLoopInformation()->GetPreHeader();
575 }
576
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100577 HLoopInformation* GetLoopInformation() const {
578 return loop_information_;
579 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000580
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000581 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100582 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000583 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100584 void SetInLoop(HLoopInformation* info) {
585 if (IsLoopHeader()) {
586 // Nothing to do. This just means `info` is an outer loop.
587 } else if (loop_information_ == nullptr) {
588 loop_information_ = info;
589 } else if (loop_information_->Contains(*info->GetHeader())) {
590 // Block is currently part of an outer loop. Make it part of this inner loop.
591 // Note that a non loop header having a loop information means this loop information
592 // has already been populated
593 loop_information_ = info;
594 } else {
595 // Block is part of an inner loop. Do not update the loop information.
596 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
597 // at this point, because this method is being called while populating `info`.
598 }
599 }
600
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000601 // Raw update of the loop information.
602 void SetLoopInformation(HLoopInformation* info) {
603 loop_information_ = info;
604 }
605
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100606 bool IsInLoop() const { return loop_information_ != nullptr; }
607
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100608 // Returns wheter this block dominates the blocked passed as parameter.
609 bool Dominates(HBasicBlock* block) const;
610
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100611 size_t GetLifetimeStart() const { return lifetime_start_; }
612 size_t GetLifetimeEnd() const { return lifetime_end_; }
613
614 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
615 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
616
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100617 uint32_t GetDexPc() const { return dex_pc_; }
618
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000619 bool IsCatchBlock() const { return is_catch_block_; }
620 void SetIsCatchBlock() { is_catch_block_ = true; }
621
David Brazdil8d5b8b22015-03-24 10:51:52 +0000622 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +0000623 bool EndsWithIf() const;
624 bool HasSinglePhi() const;
625
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000626 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000627 HGraph* graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000628 GrowableArray<HBasicBlock*> predecessors_;
629 GrowableArray<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100630 HInstructionList instructions_;
631 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000632 HLoopInformation* loop_information_;
633 HBasicBlock* dominator_;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100634 GrowableArray<HBasicBlock*> dominated_blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000635 int block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100636 // The dex program counter of the first instruction of this block.
637 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100638 size_t lifetime_start_;
639 size_t lifetime_end_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000640 bool is_catch_block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000641
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000642 friend class HGraph;
643 friend class HInstruction;
644
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000645 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
646};
647
David Brazdilb2bd1c52015-03-25 11:17:37 +0000648// Iterates over the LoopInformation of all loops which contain 'block'
649// from the innermost to the outermost.
650class HLoopInformationOutwardIterator : public ValueObject {
651 public:
652 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
653 : current_(block.GetLoopInformation()) {}
654
655 bool Done() const { return current_ == nullptr; }
656
657 void Advance() {
658 DCHECK(!Done());
659 current_ = current_->GetHeader()->GetDominator()->GetLoopInformation();
660 }
661
662 HLoopInformation* Current() const {
663 DCHECK(!Done());
664 return current_;
665 }
666
667 private:
668 HLoopInformation* current_;
669
670 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
671};
672
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100673#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
674 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000675 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000676 M(ArrayGet, Instruction) \
677 M(ArrayLength, Instruction) \
678 M(ArraySet, Instruction) \
679 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +0000680 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000681 M(CheckCast, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100682 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000683 M(Compare, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100684 M(Condition, BinaryOperation) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700685 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000686 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +0000687 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000688 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100689 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000690 M(Exit, Instruction) \
691 M(FloatConstant, Constant) \
692 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100693 M(GreaterThan, Condition) \
694 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100695 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000696 M(InstanceFieldGet, Instruction) \
697 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000698 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100699 M(IntConstant, Constant) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +0000700 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000701 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100702 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000703 M(LessThan, Condition) \
704 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000705 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000706 M(LoadException, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100707 M(LoadLocal, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000708 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100709 M(Local, Instruction) \
710 M(LongConstant, Constant) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +0000711 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000712 M(Mul, BinaryOperation) \
713 M(Neg, UnaryOperation) \
714 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100715 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +0100716 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000717 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000718 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000719 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000720 M(Or, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100721 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000722 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100723 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000724 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100725 M(Return, Instruction) \
726 M(ReturnVoid, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000727 M(Shl, BinaryOperation) \
728 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100729 M(StaticFieldGet, Instruction) \
730 M(StaticFieldSet, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100731 M(StoreLocal, Instruction) \
732 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100733 M(SuspendCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000734 M(Temporary, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000735 M(Throw, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +0000736 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000737 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000738 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000739
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100740#define FOR_EACH_INSTRUCTION(M) \
741 FOR_EACH_CONCRETE_INSTRUCTION(M) \
742 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +0100743 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +0100744 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100745 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -0700746
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100747#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000748FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
749#undef FORWARD_DECLARATION
750
Roland Levillainccc07a92014-09-16 14:48:16 +0100751#define DECLARE_INSTRUCTION(type) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000752 InstructionKind GetKind() const OVERRIDE { return k##type; } \
753 const char* DebugName() const OVERRIDE { return #type; } \
754 const H##type* As##type() const OVERRIDE { return this; } \
755 H##type* As##type() OVERRIDE { return this; } \
756 bool InstructionTypeEquals(HInstruction* other) const OVERRIDE { \
Roland Levillainccc07a92014-09-16 14:48:16 +0100757 return other->Is##type(); \
758 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000759 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000760
David Brazdiled596192015-01-23 10:39:45 +0000761template <typename T> class HUseList;
762
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100763template <typename T>
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700764class HUseListNode : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000765 public:
David Brazdiled596192015-01-23 10:39:45 +0000766 HUseListNode* GetPrevious() const { return prev_; }
767 HUseListNode* GetNext() const { return next_; }
768 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100769 size_t GetIndex() const { return index_; }
770
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000771 private:
David Brazdiled596192015-01-23 10:39:45 +0000772 HUseListNode(T user, size_t index)
773 : user_(user), index_(index), prev_(nullptr), next_(nullptr) {}
774
775 T const user_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100776 const size_t index_;
David Brazdiled596192015-01-23 10:39:45 +0000777 HUseListNode<T>* prev_;
778 HUseListNode<T>* next_;
779
780 friend class HUseList<T>;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000781
782 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
783};
784
David Brazdiled596192015-01-23 10:39:45 +0000785template <typename T>
786class HUseList : public ValueObject {
787 public:
788 HUseList() : first_(nullptr) {}
789
790 void Clear() {
791 first_ = nullptr;
792 }
793
794 // Adds a new entry at the beginning of the use list and returns
795 // the newly created node.
796 HUseListNode<T>* AddUse(T user, size_t index, ArenaAllocator* arena) {
David Brazdilea55b932015-01-27 17:12:29 +0000797 HUseListNode<T>* new_node = new (arena) HUseListNode<T>(user, index);
David Brazdiled596192015-01-23 10:39:45 +0000798 if (IsEmpty()) {
799 first_ = new_node;
800 } else {
801 first_->prev_ = new_node;
802 new_node->next_ = first_;
803 first_ = new_node;
804 }
805 return new_node;
806 }
807
808 HUseListNode<T>* GetFirst() const {
809 return first_;
810 }
811
812 void Remove(HUseListNode<T>* node) {
David Brazdil1abb4192015-02-17 18:33:36 +0000813 DCHECK(node != nullptr);
814 DCHECK(Contains(node));
815
David Brazdiled596192015-01-23 10:39:45 +0000816 if (node->prev_ != nullptr) {
817 node->prev_->next_ = node->next_;
818 }
819 if (node->next_ != nullptr) {
820 node->next_->prev_ = node->prev_;
821 }
822 if (node == first_) {
823 first_ = node->next_;
824 }
825 }
826
David Brazdil1abb4192015-02-17 18:33:36 +0000827 bool Contains(const HUseListNode<T>* node) const {
828 if (node == nullptr) {
829 return false;
830 }
831 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
832 if (current == node) {
833 return true;
834 }
835 }
836 return false;
837 }
838
David Brazdiled596192015-01-23 10:39:45 +0000839 bool IsEmpty() const {
840 return first_ == nullptr;
841 }
842
843 bool HasOnlyOneUse() const {
844 return first_ != nullptr && first_->next_ == nullptr;
845 }
846
847 private:
848 HUseListNode<T>* first_;
849};
850
851template<typename T>
852class HUseIterator : public ValueObject {
853 public:
854 explicit HUseIterator(const HUseList<T>& uses) : current_(uses.GetFirst()) {}
855
856 bool Done() const { return current_ == nullptr; }
857
858 void Advance() {
859 DCHECK(!Done());
860 current_ = current_->GetNext();
861 }
862
863 HUseListNode<T>* Current() const {
864 DCHECK(!Done());
865 return current_;
866 }
867
868 private:
869 HUseListNode<T>* current_;
870
871 friend class HValue;
872};
873
David Brazdil1abb4192015-02-17 18:33:36 +0000874// This class is used by HEnvironment and HInstruction classes to record the
875// instructions they use and pointers to the corresponding HUseListNodes kept
876// by the used instructions.
877template <typename T>
878class HUserRecord : public ValueObject {
879 public:
880 HUserRecord() : instruction_(nullptr), use_node_(nullptr) {}
881 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), use_node_(nullptr) {}
882
883 HUserRecord(const HUserRecord<T>& old_record, HUseListNode<T>* use_node)
884 : instruction_(old_record.instruction_), use_node_(use_node) {
885 DCHECK(instruction_ != nullptr);
886 DCHECK(use_node_ != nullptr);
887 DCHECK(old_record.use_node_ == nullptr);
888 }
889
890 HInstruction* GetInstruction() const { return instruction_; }
891 HUseListNode<T>* GetUseNode() const { return use_node_; }
892
893 private:
894 // Instruction used by the user.
895 HInstruction* instruction_;
896
897 // Corresponding entry in the use list kept by 'instruction_'.
898 HUseListNode<T>* use_node_;
899};
900
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100901// Represents the side effects an instruction may have.
902class SideEffects : public ValueObject {
903 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100904 SideEffects() : flags_(0) {}
905
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100906 static SideEffects None() {
907 return SideEffects(0);
908 }
909
910 static SideEffects All() {
911 return SideEffects(ChangesSomething().flags_ | DependsOnSomething().flags_);
912 }
913
914 static SideEffects ChangesSomething() {
915 return SideEffects((1 << kFlagChangesCount) - 1);
916 }
917
918 static SideEffects DependsOnSomething() {
919 int count = kFlagDependsOnCount - kFlagChangesCount;
920 return SideEffects(((1 << count) - 1) << kFlagChangesCount);
921 }
922
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100923 SideEffects Union(SideEffects other) const {
924 return SideEffects(flags_ | other.flags_);
925 }
926
Roland Levillain72bceff2014-09-15 18:29:00 +0100927 bool HasSideEffects() const {
928 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
929 return (flags_ & all_bits_set) != 0;
930 }
931
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100932 bool HasAllSideEffects() const {
933 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
934 return all_bits_set == (flags_ & all_bits_set);
935 }
936
937 bool DependsOn(SideEffects other) const {
938 size_t depends_flags = other.ComputeDependsFlags();
939 return (flags_ & depends_flags) != 0;
940 }
941
942 bool HasDependencies() const {
943 int count = kFlagDependsOnCount - kFlagChangesCount;
944 size_t all_bits_set = (1 << count) - 1;
945 return ((flags_ >> kFlagChangesCount) & all_bits_set) != 0;
946 }
947
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100948 private:
949 static constexpr int kFlagChangesSomething = 0;
950 static constexpr int kFlagChangesCount = kFlagChangesSomething + 1;
951
952 static constexpr int kFlagDependsOnSomething = kFlagChangesCount;
953 static constexpr int kFlagDependsOnCount = kFlagDependsOnSomething + 1;
954
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100955 explicit SideEffects(size_t flags) : flags_(flags) {}
956
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100957 size_t ComputeDependsFlags() const {
958 return flags_ << kFlagChangesCount;
959 }
960
961 size_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100962};
963
David Brazdiled596192015-01-23 10:39:45 +0000964// A HEnvironment object contains the values of virtual registers at a given location.
965class HEnvironment : public ArenaObject<kArenaAllocMisc> {
966 public:
967 HEnvironment(ArenaAllocator* arena, size_t number_of_vregs)
968 : vregs_(arena, number_of_vregs) {
969 vregs_.SetSize(number_of_vregs);
970 for (size_t i = 0; i < number_of_vregs; i++) {
David Brazdil1abb4192015-02-17 18:33:36 +0000971 vregs_.Put(i, HUserRecord<HEnvironment*>());
David Brazdiled596192015-01-23 10:39:45 +0000972 }
973 }
974
975 void CopyFrom(HEnvironment* env);
976
977 void SetRawEnvAt(size_t index, HInstruction* instruction) {
David Brazdil1abb4192015-02-17 18:33:36 +0000978 vregs_.Put(index, HUserRecord<HEnvironment*>(instruction));
David Brazdiled596192015-01-23 10:39:45 +0000979 }
980
981 HInstruction* GetInstructionAt(size_t index) const {
David Brazdil1abb4192015-02-17 18:33:36 +0000982 return vregs_.Get(index).GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +0000983 }
984
David Brazdil1abb4192015-02-17 18:33:36 +0000985 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +0000986
987 size_t Size() const { return vregs_.Size(); }
988
989 private:
David Brazdil1abb4192015-02-17 18:33:36 +0000990 // Record instructions' use entries of this environment for constant-time removal.
991 // It should only be called by HInstruction when a new environment use is added.
992 void RecordEnvUse(HUseListNode<HEnvironment*>* env_use) {
993 DCHECK(env_use->GetUser() == this);
994 size_t index = env_use->GetIndex();
995 vregs_.Put(index, HUserRecord<HEnvironment*>(vregs_.Get(index), env_use));
996 }
David Brazdiled596192015-01-23 10:39:45 +0000997
David Brazdil1abb4192015-02-17 18:33:36 +0000998 GrowableArray<HUserRecord<HEnvironment*> > vregs_;
David Brazdiled596192015-01-23 10:39:45 +0000999
David Brazdil1abb4192015-02-17 18:33:36 +00001000 friend HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001001
1002 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1003};
1004
Calin Juravleacf735c2015-02-12 15:25:22 +00001005class ReferenceTypeInfo : ValueObject {
1006 public:
Calin Juravleb1498f62015-02-16 13:13:29 +00001007 typedef Handle<mirror::Class> TypeHandle;
1008
1009 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact)
1010 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1011 if (type_handle->IsObjectClass()) {
1012 // Override the type handle to be consistent with the case when we get to
1013 // Top but don't have the Object class available. It avoids having to guess
1014 // what value the type_handle has when it's Top.
1015 return ReferenceTypeInfo(TypeHandle(), is_exact, true);
1016 } else {
1017 return ReferenceTypeInfo(type_handle, is_exact, false);
1018 }
1019 }
1020
1021 static ReferenceTypeInfo CreateTop(bool is_exact) {
1022 return ReferenceTypeInfo(TypeHandle(), is_exact, true);
Calin Juravleacf735c2015-02-12 15:25:22 +00001023 }
1024
1025 bool IsExact() const { return is_exact_; }
1026 bool IsTop() const { return is_top_; }
1027
1028 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
1029
Calin Juravleb1498f62015-02-16 13:13:29 +00001030 bool IsSupertypeOf(ReferenceTypeInfo rti) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Calin Juravleacf735c2015-02-12 15:25:22 +00001031 if (IsTop()) {
1032 // Top (equivalent for java.lang.Object) is supertype of anything.
1033 return true;
1034 }
1035 if (rti.IsTop()) {
1036 // If we get here `this` is not Top() so it can't be a supertype.
1037 return false;
1038 }
1039 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
1040 }
1041
1042 // Returns true if the type information provide the same amount of details.
1043 // Note that it does not mean that the instructions have the same actual type
1044 // (e.g. tops are equal but they can be the result of a merge).
1045 bool IsEqual(ReferenceTypeInfo rti) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1046 if (IsExact() != rti.IsExact()) {
1047 return false;
1048 }
1049 if (IsTop() && rti.IsTop()) {
1050 // `Top` means java.lang.Object, so the types are equivalent.
1051 return true;
1052 }
1053 if (IsTop() || rti.IsTop()) {
1054 // If only one is top or object than they are not equivalent.
1055 // NB: We need this extra check because the type_handle of `Top` is invalid
1056 // and we cannot inspect its reference.
1057 return false;
1058 }
1059
1060 // Finally check the types.
1061 return GetTypeHandle().Get() == rti.GetTypeHandle().Get();
1062 }
1063
1064 private:
Calin Juravleb1498f62015-02-16 13:13:29 +00001065 ReferenceTypeInfo() : ReferenceTypeInfo(TypeHandle(), false, true) {}
1066 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact, bool is_top)
1067 : type_handle_(type_handle), is_exact_(is_exact), is_top_(is_top) {}
1068
Calin Juravleacf735c2015-02-12 15:25:22 +00001069 // The class of the object.
Calin Juravleb1498f62015-02-16 13:13:29 +00001070 TypeHandle type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001071 // Whether or not the type is exact or a superclass of the actual type.
Calin Juravleb1498f62015-02-16 13:13:29 +00001072 // Whether or not we have any information about this type.
Calin Juravleacf735c2015-02-12 15:25:22 +00001073 bool is_exact_;
1074 // A true value here means that the object type should be java.lang.Object.
1075 // We don't have access to the corresponding mirror object every time so this
1076 // flag acts as a substitute. When true, the TypeHandle refers to a null
1077 // pointer and should not be used.
1078 bool is_top_;
1079};
1080
1081std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
1082
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001083class HInstruction : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001084 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001085 explicit HInstruction(SideEffects side_effects)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001086 : previous_(nullptr),
1087 next_(nullptr),
1088 block_(nullptr),
1089 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001090 ssa_index_(-1),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001091 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001092 locations_(nullptr),
1093 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001094 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001095 side_effects_(side_effects),
1096 reference_type_info_(ReferenceTypeInfo::CreateTop(/* is_exact */ false)) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001097
Dave Allison20dfc792014-06-16 20:44:29 -07001098 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001099
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001100#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001101 enum InstructionKind {
1102 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1103 };
1104#undef DECLARE_KIND
1105
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001106 HInstruction* GetNext() const { return next_; }
1107 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001108
Calin Juravle77520bc2015-01-12 18:45:46 +00001109 HInstruction* GetNextDisregardingMoves() const;
1110 HInstruction* GetPreviousDisregardingMoves() const;
1111
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001112 HBasicBlock* GetBlock() const { return block_; }
1113 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001114 bool IsInBlock() const { return block_ != nullptr; }
1115 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +01001116 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001117
Roland Levillain6b879dd2014-09-22 17:13:44 +01001118 virtual size_t InputCount() const = 0;
David Brazdil1abb4192015-02-17 18:33:36 +00001119 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001120
1121 virtual void Accept(HGraphVisitor* visitor) = 0;
1122 virtual const char* DebugName() const = 0;
1123
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001124 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
David Brazdil1abb4192015-02-17 18:33:36 +00001125 void SetRawInputAt(size_t index, HInstruction* input) {
1126 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1127 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001128
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001129 virtual bool NeedsEnvironment() const { return false; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001130 virtual bool IsControlFlow() const { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01001131 virtual bool CanThrow() const { return false; }
Roland Levillain72bceff2014-09-15 18:29:00 +01001132 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001133
Calin Juravle61d544b2015-02-23 16:46:57 +00001134 virtual bool ActAsNullConstant() const { return false; }
1135
Calin Juravle10e244f2015-01-26 18:54:32 +00001136 // Does not apply for all instructions, but having this at top level greatly
1137 // simplifies the null check elimination.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001138 virtual bool CanBeNull() const {
1139 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1140 return true;
1141 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001142
Calin Juravle77520bc2015-01-12 18:45:46 +00001143 virtual bool CanDoImplicitNullCheck() const { return false; }
1144
Calin Juravleacf735c2015-02-12 15:25:22 +00001145 void SetReferenceTypeInfo(ReferenceTypeInfo reference_type_info) {
Calin Juravle61d544b2015-02-23 16:46:57 +00001146 DCHECK_EQ(GetType(), Primitive::kPrimNot);
Calin Juravleacf735c2015-02-12 15:25:22 +00001147 reference_type_info_ = reference_type_info;
1148 }
1149
Calin Juravle61d544b2015-02-23 16:46:57 +00001150 ReferenceTypeInfo GetReferenceTypeInfo() const {
1151 DCHECK_EQ(GetType(), Primitive::kPrimNot);
1152 return reference_type_info_;
1153 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001154
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001155 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001156 DCHECK(user != nullptr);
1157 HUseListNode<HInstruction*>* use =
1158 uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1159 user->SetRawInputRecordAt(index, HUserRecord<HInstruction*>(user->InputRecordAt(index), use));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001160 }
1161
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001162 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001163 DCHECK(user != nullptr);
David Brazdiled596192015-01-23 10:39:45 +00001164 HUseListNode<HEnvironment*>* env_use =
1165 env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1166 user->RecordEnvUse(env_use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001167 }
1168
David Brazdil1abb4192015-02-17 18:33:36 +00001169 void RemoveAsUserOfInput(size_t input) {
1170 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
1171 input_use.GetInstruction()->uses_.Remove(input_use.GetUseNode());
1172 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001173
David Brazdil1abb4192015-02-17 18:33:36 +00001174 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1175 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001176
David Brazdiled596192015-01-23 10:39:45 +00001177 bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); }
1178 bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); }
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001179 bool HasNonEnvironmentUses() const { return !uses_.IsEmpty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001180 bool HasOnlyOneNonEnvironmentUse() const {
1181 return !HasEnvironmentUses() && GetUses().HasOnlyOneUse();
1182 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001183
Roland Levillain6c82d402014-10-13 16:10:27 +01001184 // Does this instruction strictly dominate `other_instruction`?
1185 // Returns false if this instruction and `other_instruction` are the same.
1186 // Aborts if this instruction and `other_instruction` are both phis.
1187 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001188
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001189 int GetId() const { return id_; }
1190 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001191
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001192 int GetSsaIndex() const { return ssa_index_; }
1193 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1194 bool HasSsaIndex() const { return ssa_index_ != -1; }
1195
1196 bool HasEnvironment() const { return environment_ != nullptr; }
1197 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001198 // Set the `environment_` field. Raw because this method does not
1199 // update the uses lists.
1200 void SetRawEnvironment(HEnvironment* environment) { environment_ = environment; }
1201
1202 // Set the environment of this instruction, copying it from `environment`. While
1203 // copying, the uses lists are being updated.
1204 void CopyEnvironmentFrom(HEnvironment* environment) {
1205 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
1206 environment_ = new (allocator) HEnvironment(allocator, environment->Size());
1207 environment_->CopyFrom(environment);
1208 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001209
Nicolas Geoffray39468442014-09-02 15:17:15 +01001210 // Returns the number of entries in the environment. Typically, that is the
1211 // number of dex registers in a method. It could be more in case of inlining.
1212 size_t EnvironmentSize() const;
1213
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001214 LocationSummary* GetLocations() const { return locations_; }
1215 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001216
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001217 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001218 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001219
Alexandre Rames188d4312015-04-09 18:30:21 +01001220 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
1221 // uses of this instruction by `other` are *not* updated.
1222 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
1223 ReplaceWith(other);
1224 other->ReplaceInput(this, use_index);
1225 }
1226
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001227 // Move `this` instruction before `cursor`.
1228 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001229
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001230#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01001231 bool Is##type() const { return (As##type() != nullptr); } \
1232 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001233 virtual H##type* As##type() { return nullptr; }
1234
1235 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
1236#undef INSTRUCTION_TYPE_CHECK
1237
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001238 // Returns whether the instruction can be moved within the graph.
1239 virtual bool CanBeMoved() const { return false; }
1240
1241 // Returns whether the two instructions are of the same kind.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001242 virtual bool InstructionTypeEquals(HInstruction* other) const {
1243 UNUSED(other);
1244 return false;
1245 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001246
1247 // Returns whether any data encoded in the two instructions is equal.
1248 // This method does not look at the inputs. Both instructions must be
1249 // of the same type, otherwise the method has undefined behavior.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001250 virtual bool InstructionDataEquals(HInstruction* other) const {
1251 UNUSED(other);
1252 return false;
1253 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001254
1255 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00001256 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001257 // 2) Their inputs are identical.
1258 bool Equals(HInstruction* other) const;
1259
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001260 virtual InstructionKind GetKind() const = 0;
1261
1262 virtual size_t ComputeHashCode() const {
1263 size_t result = GetKind();
1264 for (size_t i = 0, e = InputCount(); i < e; ++i) {
1265 result = (result * 31) + InputAt(i)->GetId();
1266 }
1267 return result;
1268 }
1269
1270 SideEffects GetSideEffects() const { return side_effects_; }
1271
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001272 size_t GetLifetimePosition() const { return lifetime_position_; }
1273 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
1274 LiveInterval* GetLiveInterval() const { return live_interval_; }
1275 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
1276 bool HasLiveInterval() const { return live_interval_ != nullptr; }
1277
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001278 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
1279
1280 // Returns whether the code generation of the instruction will require to have access
1281 // to the current method. Such instructions are:
1282 // (1): Instructions that require an environment, as calling the runtime requires
1283 // to walk the stack and have the current method stored at a specific stack address.
1284 // (2): Object literals like classes and strings, that are loaded from the dex cache
1285 // fields of the current method.
1286 bool NeedsCurrentMethod() const {
1287 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
1288 }
1289
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001290 virtual bool NeedsDexCache() const { return false; }
1291
David Brazdil1abb4192015-02-17 18:33:36 +00001292 protected:
1293 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
1294 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
1295
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001296 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001297 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); }
1298
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001299 HInstruction* previous_;
1300 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001301 HBasicBlock* block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001302
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001303 // An instruction gets an id when it is added to the graph.
1304 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01001305 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001306 int id_;
1307
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001308 // When doing liveness analysis, instructions that have uses get an SSA index.
1309 int ssa_index_;
1310
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001311 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00001312 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001313
1314 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00001315 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001316
Nicolas Geoffray39468442014-09-02 15:17:15 +01001317 // The environment associated with this instruction. Not null if the instruction
1318 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001319 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001320
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001321 // Set by the code generator.
1322 LocationSummary* locations_;
1323
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001324 // Set by the liveness analysis.
1325 LiveInterval* live_interval_;
1326
1327 // Set by the liveness analysis, this is the position in a linear
1328 // order of blocks where this instruction's live interval start.
1329 size_t lifetime_position_;
1330
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001331 const SideEffects side_effects_;
1332
Calin Juravleacf735c2015-02-12 15:25:22 +00001333 // TODO: for primitive types this should be marked as invalid.
1334 ReferenceTypeInfo reference_type_info_;
1335
David Brazdil1abb4192015-02-17 18:33:36 +00001336 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001337 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00001338 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001339 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001340 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001341
1342 DISALLOW_COPY_AND_ASSIGN(HInstruction);
1343};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001344std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001345
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001346class HInputIterator : public ValueObject {
1347 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001348 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001349
1350 bool Done() const { return index_ == instruction_->InputCount(); }
1351 HInstruction* Current() const { return instruction_->InputAt(index_); }
1352 void Advance() { index_++; }
1353
1354 private:
1355 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001356 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001357
1358 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
1359};
1360
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001361class HInstructionIterator : public ValueObject {
1362 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001363 explicit HInstructionIterator(const HInstructionList& instructions)
1364 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001365 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001366 }
1367
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001368 bool Done() const { return instruction_ == nullptr; }
1369 HInstruction* Current() const { return instruction_; }
1370 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001371 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001372 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001373 }
1374
1375 private:
1376 HInstruction* instruction_;
1377 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001378
1379 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001380};
1381
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001382class HBackwardInstructionIterator : public ValueObject {
1383 public:
1384 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
1385 : instruction_(instructions.last_instruction_) {
1386 next_ = Done() ? nullptr : instruction_->GetPrevious();
1387 }
1388
1389 bool Done() const { return instruction_ == nullptr; }
1390 HInstruction* Current() const { return instruction_; }
1391 void Advance() {
1392 instruction_ = next_;
1393 next_ = Done() ? nullptr : instruction_->GetPrevious();
1394 }
1395
1396 private:
1397 HInstruction* instruction_;
1398 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001399
1400 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001401};
1402
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001403// An embedded container with N elements of type T. Used (with partial
1404// specialization for N=0) because embedded arrays cannot have size 0.
1405template<typename T, intptr_t N>
1406class EmbeddedArray {
1407 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001408 EmbeddedArray() : elements_() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001409
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001410 intptr_t GetLength() const { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001411
1412 const T& operator[](intptr_t i) const {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001413 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001414 return elements_[i];
1415 }
1416
1417 T& operator[](intptr_t i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001418 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001419 return elements_[i];
1420 }
1421
1422 const T& At(intptr_t i) const {
1423 return (*this)[i];
1424 }
1425
1426 void SetAt(intptr_t i, const T& val) {
1427 (*this)[i] = val;
1428 }
1429
1430 private:
1431 T elements_[N];
1432};
1433
1434template<typename T>
1435class EmbeddedArray<T, 0> {
1436 public:
1437 intptr_t length() const { return 0; }
1438 const T& operator[](intptr_t i) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001439 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001440 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001441 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001442 }
1443 T& operator[](intptr_t i) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001444 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001445 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001446 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001447 }
1448};
1449
1450template<intptr_t N>
1451class HTemplateInstruction: public HInstruction {
1452 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001453 HTemplateInstruction<N>(SideEffects side_effects)
1454 : HInstruction(side_effects), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07001455 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001456
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001457 size_t InputCount() const OVERRIDE { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001458
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001459 protected:
David Brazdil1abb4192015-02-17 18:33:36 +00001460 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_[i]; }
1461
1462 void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE {
1463 inputs_[i] = input;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001464 }
1465
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001466 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001467 EmbeddedArray<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001468
1469 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001470};
1471
Dave Allison20dfc792014-06-16 20:44:29 -07001472template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001473class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07001474 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001475 HExpression<N>(Primitive::Type type, SideEffects side_effects)
1476 : HTemplateInstruction<N>(side_effects), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07001477 virtual ~HExpression() {}
1478
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001479 Primitive::Type GetType() const OVERRIDE { return type_; }
Dave Allison20dfc792014-06-16 20:44:29 -07001480
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001481 protected:
1482 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07001483};
1484
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001485// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
1486// instruction that branches to the exit block.
1487class HReturnVoid : public HTemplateInstruction<0> {
1488 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001489 HReturnVoid() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001490
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001491 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001492
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001493 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001494
1495 private:
1496 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
1497};
1498
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001499// Represents dex's RETURN opcodes. A HReturn is a control flow
1500// instruction that branches to the exit block.
1501class HReturn : public HTemplateInstruction<1> {
1502 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001503 explicit HReturn(HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001504 SetRawInputAt(0, value);
1505 }
1506
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001507 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001508
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001509 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001510
1511 private:
1512 DISALLOW_COPY_AND_ASSIGN(HReturn);
1513};
1514
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001515// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001516// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001517// exit block.
1518class HExit : public HTemplateInstruction<0> {
1519 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001520 HExit() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001521
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001522 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001523
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001524 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001525
1526 private:
1527 DISALLOW_COPY_AND_ASSIGN(HExit);
1528};
1529
1530// Jumps from one block to another.
1531class HGoto : public HTemplateInstruction<0> {
1532 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001533 HGoto() : HTemplateInstruction(SideEffects::None()) {}
1534
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001535 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001536
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001537 HBasicBlock* GetSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001538 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001539 }
1540
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001541 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001542
1543 private:
1544 DISALLOW_COPY_AND_ASSIGN(HGoto);
1545};
1546
Dave Allison20dfc792014-06-16 20:44:29 -07001547
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001548// Conditional branch. A block ending with an HIf instruction must have
1549// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001550class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001551 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001552 explicit HIf(HInstruction* input) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001553 SetRawInputAt(0, input);
1554 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001555
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001556 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001557
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001558 HBasicBlock* IfTrueSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001559 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001560 }
1561
1562 HBasicBlock* IfFalseSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001563 return GetBlock()->GetSuccessors().Get(1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001564 }
1565
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001566 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001567
1568 private:
1569 DISALLOW_COPY_AND_ASSIGN(HIf);
1570};
1571
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001572// Deoptimize to interpreter, upon checking a condition.
1573class HDeoptimize : public HTemplateInstruction<1> {
1574 public:
1575 HDeoptimize(HInstruction* cond, uint32_t dex_pc)
1576 : HTemplateInstruction(SideEffects::None()),
1577 dex_pc_(dex_pc) {
1578 SetRawInputAt(0, cond);
1579 }
1580
1581 bool NeedsEnvironment() const OVERRIDE { return true; }
1582 bool CanThrow() const OVERRIDE { return true; }
1583 uint32_t GetDexPc() const { return dex_pc_; }
1584
1585 DECLARE_INSTRUCTION(Deoptimize);
1586
1587 private:
1588 uint32_t dex_pc_;
1589
1590 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
1591};
1592
Roland Levillain88cb1752014-10-20 16:36:47 +01001593class HUnaryOperation : public HExpression<1> {
1594 public:
1595 HUnaryOperation(Primitive::Type result_type, HInstruction* input)
1596 : HExpression(result_type, SideEffects::None()) {
1597 SetRawInputAt(0, input);
1598 }
1599
1600 HInstruction* GetInput() const { return InputAt(0); }
1601 Primitive::Type GetResultType() const { return GetType(); }
1602
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001603 bool CanBeMoved() const OVERRIDE { return true; }
1604 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001605 UNUSED(other);
1606 return true;
1607 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001608
Roland Levillain9240d6a2014-10-20 16:47:04 +01001609 // Try to statically evaluate `operation` and return a HConstant
1610 // containing the result of this evaluation. If `operation` cannot
1611 // be evaluated as a constant, return nullptr.
1612 HConstant* TryStaticEvaluation() const;
1613
1614 // Apply this operation to `x`.
1615 virtual int32_t Evaluate(int32_t x) const = 0;
1616 virtual int64_t Evaluate(int64_t x) const = 0;
1617
Roland Levillain88cb1752014-10-20 16:36:47 +01001618 DECLARE_INSTRUCTION(UnaryOperation);
1619
1620 private:
1621 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
1622};
1623
Dave Allison20dfc792014-06-16 20:44:29 -07001624class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001625 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001626 HBinaryOperation(Primitive::Type result_type,
1627 HInstruction* left,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001628 HInstruction* right) : HExpression(result_type, SideEffects::None()) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001629 SetRawInputAt(0, left);
1630 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001631 }
1632
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001633 HInstruction* GetLeft() const { return InputAt(0); }
1634 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07001635 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001636
Mingyao Yangdc5ac732015-02-25 11:28:05 -08001637 virtual bool IsCommutative() const { return false; }
1638
1639 // Put constant on the right.
1640 // Returns whether order is changed.
1641 bool OrderInputsWithConstantOnTheRight() {
1642 HInstruction* left = InputAt(0);
1643 HInstruction* right = InputAt(1);
1644 if (left->IsConstant() && !right->IsConstant()) {
1645 ReplaceInput(right, 0);
1646 ReplaceInput(left, 1);
1647 return true;
1648 }
1649 return false;
1650 }
1651
1652 // Order inputs by instruction id, but favor constant on the right side.
1653 // This helps GVN for commutative ops.
1654 void OrderInputs() {
1655 DCHECK(IsCommutative());
1656 HInstruction* left = InputAt(0);
1657 HInstruction* right = InputAt(1);
1658 if (left == right || (!left->IsConstant() && right->IsConstant())) {
1659 return;
1660 }
1661 if (OrderInputsWithConstantOnTheRight()) {
1662 return;
1663 }
1664 // Order according to instruction id.
1665 if (left->GetId() > right->GetId()) {
1666 ReplaceInput(right, 0);
1667 ReplaceInput(left, 1);
1668 }
1669 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001670
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001671 bool CanBeMoved() const OVERRIDE { return true; }
1672 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001673 UNUSED(other);
1674 return true;
1675 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001676
Roland Levillain9240d6a2014-10-20 16:47:04 +01001677 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01001678 // containing the result of this evaluation. If `operation` cannot
1679 // be evaluated as a constant, return nullptr.
Roland Levillain9240d6a2014-10-20 16:47:04 +01001680 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01001681
1682 // Apply this operation to `x` and `y`.
1683 virtual int32_t Evaluate(int32_t x, int32_t y) const = 0;
1684 virtual int64_t Evaluate(int64_t x, int64_t y) const = 0;
1685
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001686 // Returns an input that can legally be used as the right input and is
1687 // constant, or nullptr.
1688 HConstant* GetConstantRight() const;
1689
1690 // If `GetConstantRight()` returns one of the input, this returns the other
1691 // one. Otherwise it returns nullptr.
1692 HInstruction* GetLeastConstantLeft() const;
1693
Roland Levillainccc07a92014-09-16 14:48:16 +01001694 DECLARE_INSTRUCTION(BinaryOperation);
1695
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001696 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001697 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
1698};
1699
Dave Allison20dfc792014-06-16 20:44:29 -07001700class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001701 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001702 HCondition(HInstruction* first, HInstruction* second)
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001703 : HBinaryOperation(Primitive::kPrimBoolean, first, second),
1704 needs_materialization_(true) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001705
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001706 bool NeedsMaterialization() const { return needs_materialization_; }
1707 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001708
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001709 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001710 // `instruction`, and disregard moves in between.
1711 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001712
Dave Allison20dfc792014-06-16 20:44:29 -07001713 DECLARE_INSTRUCTION(Condition);
1714
1715 virtual IfCondition GetCondition() const = 0;
1716
1717 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001718 // For register allocation purposes, returns whether this instruction needs to be
1719 // materialized (that is, not just be in the processor flags).
1720 bool needs_materialization_;
1721
Dave Allison20dfc792014-06-16 20:44:29 -07001722 DISALLOW_COPY_AND_ASSIGN(HCondition);
1723};
1724
1725// Instruction to check if two inputs are equal to each other.
1726class HEqual : public HCondition {
1727 public:
1728 HEqual(HInstruction* first, HInstruction* second)
1729 : HCondition(first, second) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001730
Mingyao Yangdc5ac732015-02-25 11:28:05 -08001731 bool IsCommutative() const OVERRIDE { return true; }
1732
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001733 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001734 return x == y ? 1 : 0;
1735 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001736 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001737 return x == y ? 1 : 0;
1738 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001739
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001740 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001741
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001742 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001743 return kCondEQ;
1744 }
1745
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001746 private:
1747 DISALLOW_COPY_AND_ASSIGN(HEqual);
1748};
1749
Dave Allison20dfc792014-06-16 20:44:29 -07001750class HNotEqual : public HCondition {
1751 public:
1752 HNotEqual(HInstruction* first, HInstruction* second)
1753 : HCondition(first, second) {}
1754
Mingyao Yangdc5ac732015-02-25 11:28:05 -08001755 bool IsCommutative() const OVERRIDE { return true; }
1756
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001757 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001758 return x != y ? 1 : 0;
1759 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001760 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001761 return x != y ? 1 : 0;
1762 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001763
Dave Allison20dfc792014-06-16 20:44:29 -07001764 DECLARE_INSTRUCTION(NotEqual);
1765
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001766 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001767 return kCondNE;
1768 }
1769
1770 private:
1771 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
1772};
1773
1774class HLessThan : public HCondition {
1775 public:
1776 HLessThan(HInstruction* first, HInstruction* second)
1777 : HCondition(first, second) {}
1778
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001779 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001780 return x < y ? 1 : 0;
1781 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001782 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001783 return x < y ? 1 : 0;
1784 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001785
Dave Allison20dfc792014-06-16 20:44:29 -07001786 DECLARE_INSTRUCTION(LessThan);
1787
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001788 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001789 return kCondLT;
1790 }
1791
1792 private:
1793 DISALLOW_COPY_AND_ASSIGN(HLessThan);
1794};
1795
1796class HLessThanOrEqual : public HCondition {
1797 public:
1798 HLessThanOrEqual(HInstruction* first, HInstruction* second)
1799 : HCondition(first, second) {}
1800
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001801 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001802 return x <= y ? 1 : 0;
1803 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001804 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001805 return x <= y ? 1 : 0;
1806 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001807
Dave Allison20dfc792014-06-16 20:44:29 -07001808 DECLARE_INSTRUCTION(LessThanOrEqual);
1809
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001810 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001811 return kCondLE;
1812 }
1813
1814 private:
1815 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
1816};
1817
1818class HGreaterThan : public HCondition {
1819 public:
1820 HGreaterThan(HInstruction* first, HInstruction* second)
1821 : HCondition(first, second) {}
1822
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001823 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001824 return x > y ? 1 : 0;
1825 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001826 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001827 return x > y ? 1 : 0;
1828 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001829
Dave Allison20dfc792014-06-16 20:44:29 -07001830 DECLARE_INSTRUCTION(GreaterThan);
1831
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001832 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001833 return kCondGT;
1834 }
1835
1836 private:
1837 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
1838};
1839
1840class HGreaterThanOrEqual : public HCondition {
1841 public:
1842 HGreaterThanOrEqual(HInstruction* first, HInstruction* second)
1843 : HCondition(first, second) {}
1844
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001845 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001846 return x >= y ? 1 : 0;
1847 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001848 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001849 return x >= y ? 1 : 0;
1850 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001851
Dave Allison20dfc792014-06-16 20:44:29 -07001852 DECLARE_INSTRUCTION(GreaterThanOrEqual);
1853
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001854 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001855 return kCondGE;
1856 }
1857
1858 private:
1859 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
1860};
1861
1862
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001863// Instruction to check how two inputs compare to each other.
1864// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
1865class HCompare : public HBinaryOperation {
1866 public:
Calin Juravleddb7df22014-11-25 20:56:51 +00001867 // The bias applies for floating point operations and indicates how NaN
1868 // comparisons are treated:
1869 enum Bias {
1870 kNoBias, // bias is not applicable (i.e. for long operation)
1871 kGtBias, // return 1 for NaN comparisons
1872 kLtBias, // return -1 for NaN comparisons
1873 };
1874
1875 HCompare(Primitive::Type type, HInstruction* first, HInstruction* second, Bias bias)
1876 : HBinaryOperation(Primitive::kPrimInt, first, second), bias_(bias) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001877 DCHECK_EQ(type, first->GetType());
1878 DCHECK_EQ(type, second->GetType());
1879 }
1880
Calin Juravleddb7df22014-11-25 20:56:51 +00001881 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01001882 return
1883 x == y ? 0 :
1884 x > y ? 1 :
1885 -1;
1886 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001887
1888 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01001889 return
1890 x == y ? 0 :
1891 x > y ? 1 :
1892 -1;
1893 }
1894
Calin Juravleddb7df22014-11-25 20:56:51 +00001895 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
1896 return bias_ == other->AsCompare()->bias_;
1897 }
1898
1899 bool IsGtBias() { return bias_ == kGtBias; }
1900
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001901 DECLARE_INSTRUCTION(Compare);
1902
1903 private:
Calin Juravleddb7df22014-11-25 20:56:51 +00001904 const Bias bias_;
1905
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001906 DISALLOW_COPY_AND_ASSIGN(HCompare);
1907};
1908
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001909// A local in the graph. Corresponds to a Dex register.
1910class HLocal : public HTemplateInstruction<0> {
1911 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001912 explicit HLocal(uint16_t reg_number)
1913 : HTemplateInstruction(SideEffects::None()), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001914
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001915 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001916
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001917 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001918
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001919 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001920 // The Dex register number.
1921 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001922
1923 DISALLOW_COPY_AND_ASSIGN(HLocal);
1924};
1925
1926// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07001927class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001928 public:
Roland Levillain5799fc02014-09-25 12:15:20 +01001929 HLoadLocal(HLocal* local, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001930 : HExpression(type, SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001931 SetRawInputAt(0, local);
1932 }
1933
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001934 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1935
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001936 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001937
1938 private:
1939 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
1940};
1941
1942// Store a value in a given local. This instruction has two inputs: the value
1943// and the local.
1944class HStoreLocal : public HTemplateInstruction<2> {
1945 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001946 HStoreLocal(HLocal* local, HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001947 SetRawInputAt(0, local);
1948 SetRawInputAt(1, value);
1949 }
1950
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001951 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1952
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001953 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001954
1955 private:
1956 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
1957};
1958
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001959class HConstant : public HExpression<0> {
1960 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001961 explicit HConstant(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
1962
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001963 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001964
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001965 virtual bool IsMinusOne() const { return false; }
1966 virtual bool IsZero() const { return false; }
1967 virtual bool IsOne() const { return false; }
1968
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001969 DECLARE_INSTRUCTION(Constant);
1970
1971 private:
1972 DISALLOW_COPY_AND_ASSIGN(HConstant);
1973};
1974
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001975class HFloatConstant : public HConstant {
1976 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001977 float GetValue() const { return value_; }
1978
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001979 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001980 return bit_cast<uint32_t, float>(other->AsFloatConstant()->value_) ==
1981 bit_cast<uint32_t, float>(value_);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001982 }
1983
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001984 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001985
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001986 bool IsMinusOne() const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001987 return bit_cast<uint32_t, float>(AsFloatConstant()->GetValue()) ==
1988 bit_cast<uint32_t, float>((-1.0f));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001989 }
1990 bool IsZero() const OVERRIDE {
1991 return AsFloatConstant()->GetValue() == 0.0f;
1992 }
1993 bool IsOne() const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001994 return bit_cast<uint32_t, float>(AsFloatConstant()->GetValue()) ==
1995 bit_cast<uint32_t, float>(1.0f);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001996 }
1997
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001998 DECLARE_INSTRUCTION(FloatConstant);
1999
2000 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002001 explicit HFloatConstant(float value) : HConstant(Primitive::kPrimFloat), value_(value) {}
2002
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002003 const float value_;
2004
David Brazdil8d5b8b22015-03-24 10:51:52 +00002005 // Only the SsaBuilder can currently create floating-point constants. If we
2006 // ever need to create them later in the pipeline, we will have to handle them
2007 // the same way as integral constants.
2008 friend class SsaBuilder;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002009 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2010};
2011
2012class HDoubleConstant : public HConstant {
2013 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002014 double GetValue() const { return value_; }
2015
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002016 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002017 return bit_cast<uint64_t, double>(other->AsDoubleConstant()->value_) ==
2018 bit_cast<uint64_t, double>(value_);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002019 }
2020
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002021 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002022
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002023 bool IsMinusOne() const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002024 return bit_cast<uint64_t, double>(AsDoubleConstant()->GetValue()) ==
2025 bit_cast<uint64_t, double>((-1.0));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002026 }
2027 bool IsZero() const OVERRIDE {
2028 return AsDoubleConstant()->GetValue() == 0.0;
2029 }
2030 bool IsOne() const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002031 return bit_cast<uint64_t, double>(AsDoubleConstant()->GetValue()) ==
2032 bit_cast<uint64_t, double>(1.0);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002033 }
2034
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002035 DECLARE_INSTRUCTION(DoubleConstant);
2036
2037 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002038 explicit HDoubleConstant(double value) : HConstant(Primitive::kPrimDouble), value_(value) {}
2039
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002040 const double value_;
2041
David Brazdil8d5b8b22015-03-24 10:51:52 +00002042 // Only the SsaBuilder can currently create floating-point constants. If we
2043 // ever need to create them later in the pipeline, we will have to handle them
2044 // the same way as integral constants.
2045 friend class SsaBuilder;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002046 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
2047};
2048
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002049class HNullConstant : public HConstant {
2050 public:
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002051 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2052 return true;
2053 }
2054
2055 size_t ComputeHashCode() const OVERRIDE { return 0; }
2056
Calin Juravle61d544b2015-02-23 16:46:57 +00002057 bool ActAsNullConstant() const OVERRIDE { return true; }
2058
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002059 DECLARE_INSTRUCTION(NullConstant);
2060
2061 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002062 HNullConstant() : HConstant(Primitive::kPrimNot) {}
2063
2064 friend class HGraph;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002065 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2066};
2067
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002068// Constants of the type int. Those can be from Dex instructions, or
2069// synthesized (for example with the if-eqz instruction).
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002070class HIntConstant : public HConstant {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002071 public:
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002072 int32_t GetValue() const { return value_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002073
Calin Juravle61d544b2015-02-23 16:46:57 +00002074 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002075 return other->AsIntConstant()->value_ == value_;
2076 }
2077
Calin Juravle61d544b2015-02-23 16:46:57 +00002078 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2079
2080 // TODO: Null is represented by the `0` constant. In most cases we replace it
2081 // with a HNullConstant but we don't do it when comparing (a != null). This
2082 // method is an workaround until we fix the above.
2083 bool ActAsNullConstant() const OVERRIDE { return value_ == 0; }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002084
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002085 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2086 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2087 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2088
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002089 DECLARE_INSTRUCTION(IntConstant);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002090
2091 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002092 explicit HIntConstant(int32_t value) : HConstant(Primitive::kPrimInt), value_(value) {}
2093
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002094 const int32_t value_;
2095
David Brazdil8d5b8b22015-03-24 10:51:52 +00002096 friend class HGraph;
2097 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2098 ART_FRIEND_TEST(ParallelMoveTest, ConstantLast);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002099 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2100};
2101
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002102class HLongConstant : public HConstant {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002103 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002104 int64_t GetValue() const { return value_; }
2105
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002106 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002107 return other->AsLongConstant()->value_ == value_;
2108 }
2109
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002110 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002111
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002112 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2113 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2114 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2115
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002116 DECLARE_INSTRUCTION(LongConstant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002117
2118 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002119 explicit HLongConstant(int64_t value) : HConstant(Primitive::kPrimLong), value_(value) {}
2120
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002121 const int64_t value_;
2122
David Brazdil8d5b8b22015-03-24 10:51:52 +00002123 friend class HGraph;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002124 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2125};
2126
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002127enum class Intrinsics {
2128#define OPTIMIZING_INTRINSICS(Name, IsStatic) k ## Name,
2129#include "intrinsics_list.h"
2130 kNone,
2131 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
2132#undef INTRINSICS_LIST
2133#undef OPTIMIZING_INTRINSICS
2134};
2135std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
2136
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002137class HInvoke : public HInstruction {
2138 public:
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002139 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002140
2141 // Runtime needs to walk the stack, so Dex -> Dex calls need to
2142 // know their environment.
Calin Juravle77520bc2015-01-12 18:45:46 +00002143 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002144
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01002145 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002146 SetRawInputAt(index, argument);
2147 }
2148
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002149 Primitive::Type GetType() const OVERRIDE { return return_type_; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002150
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002151 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002152
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002153 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
2154
2155 Intrinsics GetIntrinsic() {
2156 return intrinsic_;
2157 }
2158
2159 void SetIntrinsic(Intrinsics intrinsic) {
2160 intrinsic_ = intrinsic;
2161 }
2162
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002163 DECLARE_INSTRUCTION(Invoke);
2164
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002165 protected:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002166 HInvoke(ArenaAllocator* arena,
2167 uint32_t number_of_arguments,
2168 Primitive::Type return_type,
2169 uint32_t dex_pc,
2170 uint32_t dex_method_index)
2171 : HInstruction(SideEffects::All()),
2172 inputs_(arena, number_of_arguments),
2173 return_type_(return_type),
2174 dex_pc_(dex_pc),
2175 dex_method_index_(dex_method_index),
2176 intrinsic_(Intrinsics::kNone) {
2177 inputs_.SetSize(number_of_arguments);
2178 }
2179
David Brazdil1abb4192015-02-17 18:33:36 +00002180 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
2181 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
2182 inputs_.Put(index, input);
2183 }
2184
2185 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002186 const Primitive::Type return_type_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002187 const uint32_t dex_pc_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002188 const uint32_t dex_method_index_;
2189 Intrinsics intrinsic_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002190
2191 private:
2192 DISALLOW_COPY_AND_ASSIGN(HInvoke);
2193};
2194
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002195class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002196 public:
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002197 HInvokeStaticOrDirect(ArenaAllocator* arena,
2198 uint32_t number_of_arguments,
2199 Primitive::Type return_type,
2200 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002201 uint32_t dex_method_index,
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002202 bool is_recursive,
Nicolas Geoffray79041292015-03-26 10:05:54 +00002203 InvokeType original_invoke_type,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002204 InvokeType invoke_type)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002205 : HInvoke(arena, number_of_arguments, return_type, dex_pc, dex_method_index),
Nicolas Geoffray79041292015-03-26 10:05:54 +00002206 original_invoke_type_(original_invoke_type),
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002207 invoke_type_(invoke_type),
2208 is_recursive_(is_recursive) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002209
Calin Juravle77520bc2015-01-12 18:45:46 +00002210 bool CanDoImplicitNullCheck() const OVERRIDE {
2211 // We access the method via the dex cache so we can't do an implicit null check.
2212 // TODO: for intrinsics we can generate implicit null checks.
2213 return false;
2214 }
2215
Nicolas Geoffray79041292015-03-26 10:05:54 +00002216 InvokeType GetOriginalInvokeType() const { return original_invoke_type_; }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002217 InvokeType GetInvokeType() const { return invoke_type_; }
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002218 bool IsRecursive() const { return is_recursive_; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002219 bool NeedsDexCache() const OVERRIDE { return !IsRecursive(); }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002220
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002221 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002222
2223 private:
Nicolas Geoffray79041292015-03-26 10:05:54 +00002224 const InvokeType original_invoke_type_;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002225 const InvokeType invoke_type_;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002226 const bool is_recursive_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002227
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002228 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002229};
2230
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002231class HInvokeVirtual : public HInvoke {
2232 public:
2233 HInvokeVirtual(ArenaAllocator* arena,
2234 uint32_t number_of_arguments,
2235 Primitive::Type return_type,
2236 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002237 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002238 uint32_t vtable_index)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002239 : HInvoke(arena, number_of_arguments, return_type, dex_pc, dex_method_index),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002240 vtable_index_(vtable_index) {}
2241
Calin Juravle77520bc2015-01-12 18:45:46 +00002242 bool CanDoImplicitNullCheck() const OVERRIDE {
2243 // TODO: Add implicit null checks in intrinsics.
2244 return !GetLocations()->Intrinsified();
2245 }
2246
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002247 uint32_t GetVTableIndex() const { return vtable_index_; }
2248
2249 DECLARE_INSTRUCTION(InvokeVirtual);
2250
2251 private:
2252 const uint32_t vtable_index_;
2253
2254 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
2255};
2256
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002257class HInvokeInterface : public HInvoke {
2258 public:
2259 HInvokeInterface(ArenaAllocator* arena,
2260 uint32_t number_of_arguments,
2261 Primitive::Type return_type,
2262 uint32_t dex_pc,
2263 uint32_t dex_method_index,
2264 uint32_t imt_index)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002265 : HInvoke(arena, number_of_arguments, return_type, dex_pc, dex_method_index),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002266 imt_index_(imt_index) {}
2267
Calin Juravle77520bc2015-01-12 18:45:46 +00002268 bool CanDoImplicitNullCheck() const OVERRIDE {
2269 // TODO: Add implicit null checks in intrinsics.
2270 return !GetLocations()->Intrinsified();
2271 }
2272
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002273 uint32_t GetImtIndex() const { return imt_index_; }
2274 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
2275
2276 DECLARE_INSTRUCTION(InvokeInterface);
2277
2278 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002279 const uint32_t imt_index_;
2280
2281 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
2282};
2283
Dave Allison20dfc792014-06-16 20:44:29 -07002284class HNewInstance : public HExpression<0> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002285 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002286 HNewInstance(uint32_t dex_pc, uint16_t type_index, QuickEntrypointEnum entrypoint)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002287 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2288 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002289 type_index_(type_index),
2290 entrypoint_(entrypoint) {}
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002291
2292 uint32_t GetDexPc() const { return dex_pc_; }
2293 uint16_t GetTypeIndex() const { return type_index_; }
2294
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002295 // Calls runtime so needs an environment.
Calin Juravle92a6ed22014-12-02 18:58:03 +00002296 bool NeedsEnvironment() const OVERRIDE { return true; }
2297 // It may throw when called on:
2298 // - interfaces
2299 // - abstract/innaccessible/unknown classes
2300 // TODO: optimize when possible.
2301 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002302
Calin Juravle10e244f2015-01-26 18:54:32 +00002303 bool CanBeNull() const OVERRIDE { return false; }
2304
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002305 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
2306
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002307 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002308
2309 private:
2310 const uint32_t dex_pc_;
2311 const uint16_t type_index_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002312 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002313
2314 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
2315};
2316
Roland Levillain88cb1752014-10-20 16:36:47 +01002317class HNeg : public HUnaryOperation {
2318 public:
2319 explicit HNeg(Primitive::Type result_type, HInstruction* input)
2320 : HUnaryOperation(result_type, input) {}
2321
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002322 int32_t Evaluate(int32_t x) const OVERRIDE { return -x; }
2323 int64_t Evaluate(int64_t x) const OVERRIDE { return -x; }
Roland Levillain9240d6a2014-10-20 16:47:04 +01002324
Roland Levillain88cb1752014-10-20 16:36:47 +01002325 DECLARE_INSTRUCTION(Neg);
2326
2327 private:
2328 DISALLOW_COPY_AND_ASSIGN(HNeg);
2329};
2330
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002331class HNewArray : public HExpression<1> {
2332 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002333 HNewArray(HInstruction* length,
2334 uint32_t dex_pc,
2335 uint16_t type_index,
2336 QuickEntrypointEnum entrypoint)
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002337 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2338 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002339 type_index_(type_index),
2340 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002341 SetRawInputAt(0, length);
2342 }
2343
2344 uint32_t GetDexPc() const { return dex_pc_; }
2345 uint16_t GetTypeIndex() const { return type_index_; }
2346
2347 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00002348 bool NeedsEnvironment() const OVERRIDE { return true; }
2349
Mingyao Yang0c365e62015-03-31 15:09:29 -07002350 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
2351 bool CanThrow() const OVERRIDE { return true; }
2352
Calin Juravle10e244f2015-01-26 18:54:32 +00002353 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002354
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002355 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
2356
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002357 DECLARE_INSTRUCTION(NewArray);
2358
2359 private:
2360 const uint32_t dex_pc_;
2361 const uint16_t type_index_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002362 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002363
2364 DISALLOW_COPY_AND_ASSIGN(HNewArray);
2365};
2366
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002367class HAdd : public HBinaryOperation {
2368 public:
2369 HAdd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2370 : HBinaryOperation(result_type, left, right) {}
2371
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002372 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002373
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002374 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002375 return x + y;
2376 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002377 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002378 return x + y;
2379 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002380
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002381 DECLARE_INSTRUCTION(Add);
2382
2383 private:
2384 DISALLOW_COPY_AND_ASSIGN(HAdd);
2385};
2386
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002387class HSub : public HBinaryOperation {
2388 public:
2389 HSub(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2390 : HBinaryOperation(result_type, left, right) {}
2391
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002392 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002393 return x - y;
2394 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002395 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002396 return x - y;
2397 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002398
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002399 DECLARE_INSTRUCTION(Sub);
2400
2401 private:
2402 DISALLOW_COPY_AND_ASSIGN(HSub);
2403};
2404
Calin Juravle34bacdf2014-10-07 20:23:36 +01002405class HMul : public HBinaryOperation {
2406 public:
2407 HMul(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2408 : HBinaryOperation(result_type, left, right) {}
2409
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002410 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002411
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002412 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x * y; }
2413 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x * y; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002414
2415 DECLARE_INSTRUCTION(Mul);
2416
2417 private:
2418 DISALLOW_COPY_AND_ASSIGN(HMul);
2419};
2420
Calin Juravle7c4954d2014-10-28 16:57:40 +00002421class HDiv : public HBinaryOperation {
2422 public:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002423 HDiv(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
2424 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00002425
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002426 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00002427 // Our graph structure ensures we never have 0 for `y` during constant folding.
2428 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00002429 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00002430 return (y == -1) ? -x : x / y;
2431 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002432
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002433 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002434 DCHECK_NE(y, 0);
2435 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2436 return (y == -1) ? -x : x / y;
2437 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002438
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002439 uint32_t GetDexPc() const { return dex_pc_; }
2440
Calin Juravle7c4954d2014-10-28 16:57:40 +00002441 DECLARE_INSTRUCTION(Div);
2442
2443 private:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002444 const uint32_t dex_pc_;
2445
Calin Juravle7c4954d2014-10-28 16:57:40 +00002446 DISALLOW_COPY_AND_ASSIGN(HDiv);
2447};
2448
Calin Juravlebacfec32014-11-14 15:54:36 +00002449class HRem : public HBinaryOperation {
2450 public:
2451 HRem(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
2452 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
2453
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002454 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002455 DCHECK_NE(y, 0);
2456 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2457 return (y == -1) ? 0 : x % y;
2458 }
2459
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002460 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002461 DCHECK_NE(y, 0);
2462 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2463 return (y == -1) ? 0 : x % y;
2464 }
2465
2466 uint32_t GetDexPc() const { return dex_pc_; }
2467
2468 DECLARE_INSTRUCTION(Rem);
2469
2470 private:
2471 const uint32_t dex_pc_;
2472
2473 DISALLOW_COPY_AND_ASSIGN(HRem);
2474};
2475
Calin Juravled0d48522014-11-04 16:40:20 +00002476class HDivZeroCheck : public HExpression<1> {
2477 public:
2478 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
2479 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
2480 SetRawInputAt(0, value);
2481 }
2482
2483 bool CanBeMoved() const OVERRIDE { return true; }
2484
2485 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2486 UNUSED(other);
2487 return true;
2488 }
2489
2490 bool NeedsEnvironment() const OVERRIDE { return true; }
2491 bool CanThrow() const OVERRIDE { return true; }
2492
2493 uint32_t GetDexPc() const { return dex_pc_; }
2494
2495 DECLARE_INSTRUCTION(DivZeroCheck);
2496
2497 private:
2498 const uint32_t dex_pc_;
2499
2500 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
2501};
2502
Calin Juravle9aec02f2014-11-18 23:06:35 +00002503class HShl : public HBinaryOperation {
2504 public:
2505 HShl(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2506 : HBinaryOperation(result_type, left, right) {}
2507
2508 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x << (y & kMaxIntShiftValue); }
2509 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x << (y & kMaxLongShiftValue); }
2510
2511 DECLARE_INSTRUCTION(Shl);
2512
2513 private:
2514 DISALLOW_COPY_AND_ASSIGN(HShl);
2515};
2516
2517class HShr : public HBinaryOperation {
2518 public:
2519 HShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2520 : HBinaryOperation(result_type, left, right) {}
2521
2522 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x >> (y & kMaxIntShiftValue); }
2523 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x >> (y & kMaxLongShiftValue); }
2524
2525 DECLARE_INSTRUCTION(Shr);
2526
2527 private:
2528 DISALLOW_COPY_AND_ASSIGN(HShr);
2529};
2530
2531class HUShr : public HBinaryOperation {
2532 public:
2533 HUShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2534 : HBinaryOperation(result_type, left, right) {}
2535
2536 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
2537 uint32_t ux = static_cast<uint32_t>(x);
2538 uint32_t uy = static_cast<uint32_t>(y) & kMaxIntShiftValue;
2539 return static_cast<int32_t>(ux >> uy);
2540 }
2541
2542 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
2543 uint64_t ux = static_cast<uint64_t>(x);
2544 uint64_t uy = static_cast<uint64_t>(y) & kMaxLongShiftValue;
2545 return static_cast<int64_t>(ux >> uy);
2546 }
2547
2548 DECLARE_INSTRUCTION(UShr);
2549
2550 private:
2551 DISALLOW_COPY_AND_ASSIGN(HUShr);
2552};
2553
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002554class HAnd : public HBinaryOperation {
2555 public:
2556 HAnd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2557 : HBinaryOperation(result_type, left, right) {}
2558
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002559 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002560
2561 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x & y; }
2562 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x & y; }
2563
2564 DECLARE_INSTRUCTION(And);
2565
2566 private:
2567 DISALLOW_COPY_AND_ASSIGN(HAnd);
2568};
2569
2570class HOr : public HBinaryOperation {
2571 public:
2572 HOr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2573 : HBinaryOperation(result_type, left, right) {}
2574
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002575 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002576
2577 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x | y; }
2578 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x | y; }
2579
2580 DECLARE_INSTRUCTION(Or);
2581
2582 private:
2583 DISALLOW_COPY_AND_ASSIGN(HOr);
2584};
2585
2586class HXor : public HBinaryOperation {
2587 public:
2588 HXor(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2589 : HBinaryOperation(result_type, left, right) {}
2590
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002591 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002592
2593 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x ^ y; }
2594 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x ^ y; }
2595
2596 DECLARE_INSTRUCTION(Xor);
2597
2598 private:
2599 DISALLOW_COPY_AND_ASSIGN(HXor);
2600};
2601
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002602// The value of a parameter in this method. Its location depends on
2603// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07002604class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002605 public:
Calin Juravle10e244f2015-01-26 18:54:32 +00002606 HParameterValue(uint8_t index, Primitive::Type parameter_type, bool is_this = false)
2607 : HExpression(parameter_type, SideEffects::None()), index_(index), is_this_(is_this) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002608
2609 uint8_t GetIndex() const { return index_; }
2610
Calin Juravle10e244f2015-01-26 18:54:32 +00002611 bool CanBeNull() const OVERRIDE { return !is_this_; }
2612
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002613 DECLARE_INSTRUCTION(ParameterValue);
2614
2615 private:
2616 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00002617 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002618 const uint8_t index_;
2619
Calin Juravle10e244f2015-01-26 18:54:32 +00002620 // Whether or not the parameter value corresponds to 'this' argument.
2621 const bool is_this_;
2622
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002623 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
2624};
2625
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002626class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002627 public:
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002628 explicit HNot(Primitive::Type result_type, HInstruction* input)
2629 : HUnaryOperation(result_type, input) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002630
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002631 bool CanBeMoved() const OVERRIDE { return true; }
2632 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002633 UNUSED(other);
2634 return true;
2635 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002636
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002637 int32_t Evaluate(int32_t x) const OVERRIDE { return ~x; }
2638 int64_t Evaluate(int64_t x) const OVERRIDE { return ~x; }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002639
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002640 DECLARE_INSTRUCTION(Not);
2641
2642 private:
2643 DISALLOW_COPY_AND_ASSIGN(HNot);
2644};
2645
Roland Levillaindff1f282014-11-05 14:15:05 +00002646class HTypeConversion : public HExpression<1> {
2647 public:
2648 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00002649 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
2650 : HExpression(result_type, SideEffects::None()), dex_pc_(dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002651 SetRawInputAt(0, input);
2652 DCHECK_NE(input->GetType(), result_type);
2653 }
2654
2655 HInstruction* GetInput() const { return InputAt(0); }
2656 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
2657 Primitive::Type GetResultType() const { return GetType(); }
2658
Roland Levillain624279f2014-12-04 11:54:28 +00002659 // Required by the x86 and ARM code generators when producing calls
2660 // to the runtime.
2661 uint32_t GetDexPc() const { return dex_pc_; }
2662
Roland Levillaindff1f282014-11-05 14:15:05 +00002663 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00002664 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00002665
2666 DECLARE_INSTRUCTION(TypeConversion);
2667
2668 private:
Roland Levillain624279f2014-12-04 11:54:28 +00002669 const uint32_t dex_pc_;
2670
Roland Levillaindff1f282014-11-05 14:15:05 +00002671 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
2672};
2673
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00002674static constexpr uint32_t kNoRegNumber = -1;
2675
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002676class HPhi : public HInstruction {
2677 public:
2678 HPhi(ArenaAllocator* arena, uint32_t reg_number, size_t number_of_inputs, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002679 : HInstruction(SideEffects::None()),
2680 inputs_(arena, number_of_inputs),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002681 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002682 type_(type),
Calin Juravle10e244f2015-01-26 18:54:32 +00002683 is_live_(false),
2684 can_be_null_(true) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002685 inputs_.SetSize(number_of_inputs);
2686 }
2687
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00002688 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
2689 static Primitive::Type ToPhiType(Primitive::Type type) {
2690 switch (type) {
2691 case Primitive::kPrimBoolean:
2692 case Primitive::kPrimByte:
2693 case Primitive::kPrimShort:
2694 case Primitive::kPrimChar:
2695 return Primitive::kPrimInt;
2696 default:
2697 return type;
2698 }
2699 }
2700
Calin Juravle10e244f2015-01-26 18:54:32 +00002701 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002702
2703 void AddInput(HInstruction* input);
2704
Calin Juravle10e244f2015-01-26 18:54:32 +00002705 Primitive::Type GetType() const OVERRIDE { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002706 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002707
Calin Juravle10e244f2015-01-26 18:54:32 +00002708 bool CanBeNull() const OVERRIDE { return can_be_null_; }
2709 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
2710
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002711 uint32_t GetRegNumber() const { return reg_number_; }
2712
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002713 void SetDead() { is_live_ = false; }
2714 void SetLive() { is_live_ = true; }
2715 bool IsDead() const { return !is_live_; }
2716 bool IsLive() const { return is_live_; }
2717
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002718 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002719
David Brazdil1abb4192015-02-17 18:33:36 +00002720 protected:
2721 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
2722
2723 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
2724 inputs_.Put(index, input);
2725 }
2726
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002727 private:
David Brazdil1abb4192015-02-17 18:33:36 +00002728 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002729 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002730 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002731 bool is_live_;
Calin Juravle10e244f2015-01-26 18:54:32 +00002732 bool can_be_null_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002733
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002734 DISALLOW_COPY_AND_ASSIGN(HPhi);
2735};
2736
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002737class HNullCheck : public HExpression<1> {
2738 public:
2739 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002740 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002741 SetRawInputAt(0, value);
2742 }
2743
Calin Juravle10e244f2015-01-26 18:54:32 +00002744 bool CanBeMoved() const OVERRIDE { return true; }
2745 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002746 UNUSED(other);
2747 return true;
2748 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002749
Calin Juravle10e244f2015-01-26 18:54:32 +00002750 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002751
Calin Juravle10e244f2015-01-26 18:54:32 +00002752 bool CanThrow() const OVERRIDE { return true; }
2753
2754 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01002755
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002756 uint32_t GetDexPc() const { return dex_pc_; }
2757
2758 DECLARE_INSTRUCTION(NullCheck);
2759
2760 private:
2761 const uint32_t dex_pc_;
2762
2763 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
2764};
2765
2766class FieldInfo : public ValueObject {
2767 public:
Calin Juravle52c48962014-12-16 17:02:57 +00002768 FieldInfo(MemberOffset field_offset, Primitive::Type field_type, bool is_volatile)
2769 : field_offset_(field_offset), field_type_(field_type), is_volatile_(is_volatile) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002770
2771 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002772 Primitive::Type GetFieldType() const { return field_type_; }
Calin Juravle52c48962014-12-16 17:02:57 +00002773 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002774
2775 private:
2776 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01002777 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00002778 const bool is_volatile_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002779};
2780
2781class HInstanceFieldGet : public HExpression<1> {
2782 public:
2783 HInstanceFieldGet(HInstruction* value,
2784 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00002785 MemberOffset field_offset,
2786 bool is_volatile)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002787 : HExpression(field_type, SideEffects::DependsOnSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00002788 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002789 SetRawInputAt(0, value);
2790 }
2791
Calin Juravle10c9cbe2014-12-19 10:50:19 +00002792 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002793
2794 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2795 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
2796 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002797 }
2798
Calin Juravle77520bc2015-01-12 18:45:46 +00002799 bool CanDoImplicitNullCheck() const OVERRIDE {
2800 return GetFieldOffset().Uint32Value() < kPageSize;
2801 }
2802
2803 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002804 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
2805 }
2806
Calin Juravle52c48962014-12-16 17:02:57 +00002807 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002808 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002809 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002810 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002811
2812 DECLARE_INSTRUCTION(InstanceFieldGet);
2813
2814 private:
2815 const FieldInfo field_info_;
2816
2817 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
2818};
2819
2820class HInstanceFieldSet : public HTemplateInstruction<2> {
2821 public:
2822 HInstanceFieldSet(HInstruction* object,
2823 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01002824 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00002825 MemberOffset field_offset,
2826 bool is_volatile)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002827 : HTemplateInstruction(SideEffects::ChangesSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00002828 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002829 SetRawInputAt(0, object);
2830 SetRawInputAt(1, value);
2831 }
2832
Calin Juravle77520bc2015-01-12 18:45:46 +00002833 bool CanDoImplicitNullCheck() const OVERRIDE {
2834 return GetFieldOffset().Uint32Value() < kPageSize;
2835 }
2836
Calin Juravle52c48962014-12-16 17:02:57 +00002837 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002838 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002839 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002840 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002841 HInstruction* GetValue() const { return InputAt(1); }
2842
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002843 DECLARE_INSTRUCTION(InstanceFieldSet);
2844
2845 private:
2846 const FieldInfo field_info_;
2847
2848 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
2849};
2850
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002851class HArrayGet : public HExpression<2> {
2852 public:
2853 HArrayGet(HInstruction* array, HInstruction* index, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002854 : HExpression(type, SideEffects::DependsOnSomething()) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002855 SetRawInputAt(0, array);
2856 SetRawInputAt(1, index);
2857 }
2858
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002859 bool CanBeMoved() const OVERRIDE { return true; }
2860 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002861 UNUSED(other);
2862 return true;
2863 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002864 bool CanDoImplicitNullCheck() const OVERRIDE {
2865 // TODO: We can be smarter here.
2866 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
2867 // which generates the implicit null check. There are cases when these can be removed
2868 // to produce better code. If we ever add optimizations to do so we should allow an
2869 // implicit check here (as long as the address falls in the first page).
2870 return false;
2871 }
2872
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002873 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002874
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002875 HInstruction* GetArray() const { return InputAt(0); }
2876 HInstruction* GetIndex() const { return InputAt(1); }
2877
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002878 DECLARE_INSTRUCTION(ArrayGet);
2879
2880 private:
2881 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
2882};
2883
2884class HArraySet : public HTemplateInstruction<3> {
2885 public:
2886 HArraySet(HInstruction* array,
2887 HInstruction* index,
2888 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002889 Primitive::Type expected_component_type,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002890 uint32_t dex_pc)
2891 : HTemplateInstruction(SideEffects::ChangesSomething()),
2892 dex_pc_(dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002893 expected_component_type_(expected_component_type),
2894 needs_type_check_(value->GetType() == Primitive::kPrimNot) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002895 SetRawInputAt(0, array);
2896 SetRawInputAt(1, index);
2897 SetRawInputAt(2, value);
2898 }
2899
Calin Juravle77520bc2015-01-12 18:45:46 +00002900 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002901 // We currently always call a runtime method to catch array store
2902 // exceptions.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002903 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002904 }
2905
Calin Juravle77520bc2015-01-12 18:45:46 +00002906 bool CanDoImplicitNullCheck() const OVERRIDE {
2907 // TODO: Same as for ArrayGet.
2908 return false;
2909 }
2910
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002911 void ClearNeedsTypeCheck() {
2912 needs_type_check_ = false;
2913 }
2914
2915 bool NeedsTypeCheck() const { return needs_type_check_; }
2916
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002917 uint32_t GetDexPc() const { return dex_pc_; }
2918
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002919 HInstruction* GetArray() const { return InputAt(0); }
2920 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002921 HInstruction* GetValue() const { return InputAt(2); }
2922
2923 Primitive::Type GetComponentType() const {
2924 // The Dex format does not type floating point index operations. Since the
2925 // `expected_component_type_` is set during building and can therefore not
2926 // be correct, we also check what is the value type. If it is a floating
2927 // point type, we must use that type.
2928 Primitive::Type value_type = GetValue()->GetType();
2929 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
2930 ? value_type
2931 : expected_component_type_;
2932 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002933
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002934 DECLARE_INSTRUCTION(ArraySet);
2935
2936 private:
2937 const uint32_t dex_pc_;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002938 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002939 bool needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002940
2941 DISALLOW_COPY_AND_ASSIGN(HArraySet);
2942};
2943
2944class HArrayLength : public HExpression<1> {
2945 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002946 explicit HArrayLength(HInstruction* array)
2947 : HExpression(Primitive::kPrimInt, SideEffects::None()) {
2948 // Note that arrays do not change length, so the instruction does not
2949 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002950 SetRawInputAt(0, array);
2951 }
2952
Calin Juravle77520bc2015-01-12 18:45:46 +00002953 bool CanBeMoved() const OVERRIDE { return true; }
2954 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002955 UNUSED(other);
2956 return true;
2957 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002958 bool CanDoImplicitNullCheck() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002959
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002960 DECLARE_INSTRUCTION(ArrayLength);
2961
2962 private:
2963 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
2964};
2965
2966class HBoundsCheck : public HExpression<2> {
2967 public:
2968 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002969 : HExpression(index->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002970 DCHECK(index->GetType() == Primitive::kPrimInt);
2971 SetRawInputAt(0, index);
2972 SetRawInputAt(1, length);
2973 }
2974
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002975 bool CanBeMoved() const OVERRIDE { return true; }
2976 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002977 UNUSED(other);
2978 return true;
2979 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002980
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002981 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002982
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002983 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01002984
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002985 uint32_t GetDexPc() const { return dex_pc_; }
2986
2987 DECLARE_INSTRUCTION(BoundsCheck);
2988
2989 private:
2990 const uint32_t dex_pc_;
2991
2992 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
2993};
2994
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002995/**
2996 * Some DEX instructions are folded into multiple HInstructions that need
2997 * to stay live until the last HInstruction. This class
2998 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00002999 * HInstruction stays live. `index` represents the stack location index of the
3000 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003001 */
3002class HTemporary : public HTemplateInstruction<0> {
3003 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003004 explicit HTemporary(size_t index) : HTemplateInstruction(SideEffects::None()), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003005
3006 size_t GetIndex() const { return index_; }
3007
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00003008 Primitive::Type GetType() const OVERRIDE {
3009 // The previous instruction is the one that will be stored in the temporary location.
3010 DCHECK(GetPrevious() != nullptr);
3011 return GetPrevious()->GetType();
3012 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00003013
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003014 DECLARE_INSTRUCTION(Temporary);
3015
3016 private:
3017 const size_t index_;
3018
3019 DISALLOW_COPY_AND_ASSIGN(HTemporary);
3020};
3021
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003022class HSuspendCheck : public HTemplateInstruction<0> {
3023 public:
3024 explicit HSuspendCheck(uint32_t dex_pc)
Nicolas Geoffray9ebc72c2014-09-25 16:33:42 +01003025 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003026
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003027 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003028 return true;
3029 }
3030
3031 uint32_t GetDexPc() const { return dex_pc_; }
3032
3033 DECLARE_INSTRUCTION(SuspendCheck);
3034
3035 private:
3036 const uint32_t dex_pc_;
3037
3038 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
3039};
3040
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003041/**
3042 * Instruction to load a Class object.
3043 */
3044class HLoadClass : public HExpression<0> {
3045 public:
3046 HLoadClass(uint16_t type_index,
3047 bool is_referrers_class,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003048 uint32_t dex_pc)
3049 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3050 type_index_(type_index),
3051 is_referrers_class_(is_referrers_class),
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003052 dex_pc_(dex_pc),
Calin Juravleb1498f62015-02-16 13:13:29 +00003053 generate_clinit_check_(false),
3054 loaded_class_rti_(ReferenceTypeInfo::CreateTop(/* is_exact */ false)) {}
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003055
3056 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003057
3058 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3059 return other->AsLoadClass()->type_index_ == type_index_;
3060 }
3061
3062 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
3063
3064 uint32_t GetDexPc() const { return dex_pc_; }
3065 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003066 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003067
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003068 bool NeedsEnvironment() const OVERRIDE {
3069 // Will call runtime and load the class if the class is not loaded yet.
3070 // TODO: finer grain decision.
3071 return !is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003072 }
3073
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003074 bool MustGenerateClinitCheck() const {
3075 return generate_clinit_check_;
3076 }
3077
3078 void SetMustGenerateClinitCheck() {
3079 generate_clinit_check_ = true;
3080 }
3081
3082 bool CanCallRuntime() const {
3083 return MustGenerateClinitCheck() || !is_referrers_class_;
3084 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003085
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003086 bool CanThrow() const OVERRIDE {
3087 // May call runtime and and therefore can throw.
3088 // TODO: finer grain decision.
3089 return !is_referrers_class_;
3090 }
3091
Calin Juravleacf735c2015-02-12 15:25:22 +00003092 ReferenceTypeInfo GetLoadedClassRTI() {
3093 return loaded_class_rti_;
3094 }
3095
3096 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
3097 // Make sure we only set exact types (the loaded class should never be merged).
3098 DCHECK(rti.IsExact());
3099 loaded_class_rti_ = rti;
3100 }
3101
3102 bool IsResolved() {
3103 return loaded_class_rti_.IsExact();
3104 }
3105
Nicolas Geoffray9437b782015-03-25 10:08:51 +00003106 bool NeedsDexCache() const OVERRIDE { return !is_referrers_class_; }
3107
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003108 DECLARE_INSTRUCTION(LoadClass);
3109
3110 private:
3111 const uint16_t type_index_;
3112 const bool is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003113 const uint32_t dex_pc_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003114 // Whether this instruction must generate the initialization check.
3115 // Used for code generation.
3116 bool generate_clinit_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003117
Calin Juravleacf735c2015-02-12 15:25:22 +00003118 ReferenceTypeInfo loaded_class_rti_;
3119
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003120 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
3121};
3122
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003123class HLoadString : public HExpression<0> {
3124 public:
3125 HLoadString(uint32_t string_index, uint32_t dex_pc)
3126 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3127 string_index_(string_index),
3128 dex_pc_(dex_pc) {}
3129
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003130 bool CanBeMoved() const OVERRIDE { return true; }
3131
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003132 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3133 return other->AsLoadString()->string_index_ == string_index_;
3134 }
3135
3136 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
3137
3138 uint32_t GetDexPc() const { return dex_pc_; }
3139 uint32_t GetStringIndex() const { return string_index_; }
3140
3141 // TODO: Can we deopt or debug when we resolve a string?
3142 bool NeedsEnvironment() const OVERRIDE { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00003143 bool NeedsDexCache() const OVERRIDE { return true; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003144
3145 DECLARE_INSTRUCTION(LoadString);
3146
3147 private:
3148 const uint32_t string_index_;
3149 const uint32_t dex_pc_;
3150
3151 DISALLOW_COPY_AND_ASSIGN(HLoadString);
3152};
3153
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003154// TODO: Pass this check to HInvokeStaticOrDirect nodes.
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003155/**
3156 * Performs an initialization check on its Class object input.
3157 */
3158class HClinitCheck : public HExpression<1> {
3159 public:
3160 explicit HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Nicolas Geoffraya0466e12015-03-27 15:00:40 +00003161 : HExpression(Primitive::kPrimNot, SideEffects::ChangesSomething()),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003162 dex_pc_(dex_pc) {
3163 SetRawInputAt(0, constant);
3164 }
3165
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003166 bool CanBeMoved() const OVERRIDE { return true; }
3167 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3168 UNUSED(other);
3169 return true;
3170 }
3171
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003172 bool NeedsEnvironment() const OVERRIDE {
3173 // May call runtime to initialize the class.
3174 return true;
3175 }
3176
3177 uint32_t GetDexPc() const { return dex_pc_; }
3178
3179 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
3180
3181 DECLARE_INSTRUCTION(ClinitCheck);
3182
3183 private:
3184 const uint32_t dex_pc_;
3185
3186 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
3187};
3188
3189class HStaticFieldGet : public HExpression<1> {
3190 public:
3191 HStaticFieldGet(HInstruction* cls,
3192 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003193 MemberOffset field_offset,
3194 bool is_volatile)
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003195 : HExpression(field_type, SideEffects::DependsOnSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00003196 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003197 SetRawInputAt(0, cls);
3198 }
3199
Calin Juravle52c48962014-12-16 17:02:57 +00003200
Calin Juravle10c9cbe2014-12-19 10:50:19 +00003201 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003202
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003203 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00003204 HStaticFieldGet* other_get = other->AsStaticFieldGet();
3205 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003206 }
3207
3208 size_t ComputeHashCode() const OVERRIDE {
3209 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
3210 }
3211
Calin Juravle52c48962014-12-16 17:02:57 +00003212 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003213 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
3214 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003215 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003216
3217 DECLARE_INSTRUCTION(StaticFieldGet);
3218
3219 private:
3220 const FieldInfo field_info_;
3221
3222 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
3223};
3224
3225class HStaticFieldSet : public HTemplateInstruction<2> {
3226 public:
3227 HStaticFieldSet(HInstruction* cls,
3228 HInstruction* value,
3229 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003230 MemberOffset field_offset,
3231 bool is_volatile)
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003232 : HTemplateInstruction(SideEffects::ChangesSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00003233 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003234 SetRawInputAt(0, cls);
3235 SetRawInputAt(1, value);
3236 }
3237
Calin Juravle52c48962014-12-16 17:02:57 +00003238 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003239 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
3240 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003241 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003242
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003243 HInstruction* GetValue() const { return InputAt(1); }
3244
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003245 DECLARE_INSTRUCTION(StaticFieldSet);
3246
3247 private:
3248 const FieldInfo field_info_;
3249
3250 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
3251};
3252
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003253// Implement the move-exception DEX instruction.
3254class HLoadException : public HExpression<0> {
3255 public:
3256 HLoadException() : HExpression(Primitive::kPrimNot, SideEffects::None()) {}
3257
3258 DECLARE_INSTRUCTION(LoadException);
3259
3260 private:
3261 DISALLOW_COPY_AND_ASSIGN(HLoadException);
3262};
3263
3264class HThrow : public HTemplateInstruction<1> {
3265 public:
3266 HThrow(HInstruction* exception, uint32_t dex_pc)
3267 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {
3268 SetRawInputAt(0, exception);
3269 }
3270
3271 bool IsControlFlow() const OVERRIDE { return true; }
3272
3273 bool NeedsEnvironment() const OVERRIDE { return true; }
3274
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003275 bool CanThrow() const OVERRIDE { return true; }
3276
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003277 uint32_t GetDexPc() const { return dex_pc_; }
3278
3279 DECLARE_INSTRUCTION(Throw);
3280
3281 private:
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003282 const uint32_t dex_pc_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003283
3284 DISALLOW_COPY_AND_ASSIGN(HThrow);
3285};
3286
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003287class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003288 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003289 HInstanceOf(HInstruction* object,
3290 HLoadClass* constant,
3291 bool class_is_final,
3292 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003293 : HExpression(Primitive::kPrimBoolean, SideEffects::None()),
3294 class_is_final_(class_is_final),
3295 dex_pc_(dex_pc) {
3296 SetRawInputAt(0, object);
3297 SetRawInputAt(1, constant);
3298 }
3299
3300 bool CanBeMoved() const OVERRIDE { return true; }
3301
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003302 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003303 return true;
3304 }
3305
3306 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003307 return false;
3308 }
3309
3310 uint32_t GetDexPc() const { return dex_pc_; }
3311
3312 bool IsClassFinal() const { return class_is_final_; }
3313
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003314 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003315
3316 private:
3317 const bool class_is_final_;
3318 const uint32_t dex_pc_;
3319
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003320 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
3321};
3322
Calin Juravleb1498f62015-02-16 13:13:29 +00003323class HBoundType : public HExpression<1> {
3324 public:
3325 HBoundType(HInstruction* input, ReferenceTypeInfo bound_type)
3326 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3327 bound_type_(bound_type) {
Calin Juravle61d544b2015-02-23 16:46:57 +00003328 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00003329 SetRawInputAt(0, input);
3330 }
3331
3332 const ReferenceTypeInfo& GetBoundType() const { return bound_type_; }
3333
3334 bool CanBeNull() const OVERRIDE {
3335 // `null instanceof ClassX` always return false so we can't be null.
3336 return false;
3337 }
3338
3339 DECLARE_INSTRUCTION(BoundType);
3340
3341 private:
3342 // Encodes the most upper class that this instruction can have. In other words
3343 // it is always the case that GetBoundType().IsSupertypeOf(GetReferenceType()).
3344 // It is used to bound the type in cases like `if (x instanceof ClassX) {}`
3345 const ReferenceTypeInfo bound_type_;
3346
3347 DISALLOW_COPY_AND_ASSIGN(HBoundType);
3348};
3349
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003350class HCheckCast : public HTemplateInstruction<2> {
3351 public:
3352 HCheckCast(HInstruction* object,
3353 HLoadClass* constant,
3354 bool class_is_final,
3355 uint32_t dex_pc)
3356 : HTemplateInstruction(SideEffects::None()),
3357 class_is_final_(class_is_final),
3358 dex_pc_(dex_pc) {
3359 SetRawInputAt(0, object);
3360 SetRawInputAt(1, constant);
3361 }
3362
3363 bool CanBeMoved() const OVERRIDE { return true; }
3364
3365 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
3366 return true;
3367 }
3368
3369 bool NeedsEnvironment() const OVERRIDE {
3370 // Instruction may throw a CheckCastError.
3371 return true;
3372 }
3373
3374 bool CanThrow() const OVERRIDE { return true; }
3375
3376 uint32_t GetDexPc() const { return dex_pc_; }
3377
3378 bool IsClassFinal() const { return class_is_final_; }
3379
3380 DECLARE_INSTRUCTION(CheckCast);
3381
3382 private:
3383 const bool class_is_final_;
3384 const uint32_t dex_pc_;
3385
3386 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003387};
3388
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003389class HMonitorOperation : public HTemplateInstruction<1> {
3390 public:
3391 enum OperationKind {
3392 kEnter,
3393 kExit,
3394 };
3395
3396 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
3397 : HTemplateInstruction(SideEffects::None()), kind_(kind), dex_pc_(dex_pc) {
3398 SetRawInputAt(0, object);
3399 }
3400
3401 // Instruction may throw a Java exception, so we need an environment.
3402 bool NeedsEnvironment() const OVERRIDE { return true; }
3403 bool CanThrow() const OVERRIDE { return true; }
3404
3405 uint32_t GetDexPc() const { return dex_pc_; }
3406
3407 bool IsEnter() const { return kind_ == kEnter; }
3408
3409 DECLARE_INSTRUCTION(MonitorOperation);
3410
Calin Juravle52c48962014-12-16 17:02:57 +00003411 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003412 const OperationKind kind_;
3413 const uint32_t dex_pc_;
3414
3415 private:
3416 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
3417};
3418
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003419class MoveOperands : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003420 public:
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003421 MoveOperands(Location source, Location destination, HInstruction* instruction)
3422 : source_(source), destination_(destination), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003423
3424 Location GetSource() const { return source_; }
3425 Location GetDestination() const { return destination_; }
3426
3427 void SetSource(Location value) { source_ = value; }
3428 void SetDestination(Location value) { destination_ = value; }
3429
3430 // The parallel move resolver marks moves as "in-progress" by clearing the
3431 // destination (but not the source).
3432 Location MarkPending() {
3433 DCHECK(!IsPending());
3434 Location dest = destination_;
3435 destination_ = Location::NoLocation();
3436 return dest;
3437 }
3438
3439 void ClearPending(Location dest) {
3440 DCHECK(IsPending());
3441 destination_ = dest;
3442 }
3443
3444 bool IsPending() const {
3445 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
3446 return destination_.IsInvalid() && !source_.IsInvalid();
3447 }
3448
3449 // True if this blocks a move from the given location.
3450 bool Blocks(Location loc) const {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003451 return !IsEliminated() && (source_.Contains(loc) || loc.Contains(source_));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003452 }
3453
3454 // A move is redundant if it's been eliminated, if its source and
3455 // destination are the same, or if its destination is unneeded.
3456 bool IsRedundant() const {
3457 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
3458 }
3459
3460 // We clear both operands to indicate move that's been eliminated.
3461 void Eliminate() {
3462 source_ = destination_ = Location::NoLocation();
3463 }
3464
3465 bool IsEliminated() const {
3466 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
3467 return source_.IsInvalid();
3468 }
3469
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003470 HInstruction* GetInstruction() const { return instruction_; }
3471
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003472 private:
3473 Location source_;
3474 Location destination_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003475 // The instruction this move is assocatied with. Null when this move is
3476 // for moving an input in the expected locations of user (including a phi user).
3477 // This is only used in debug mode, to ensure we do not connect interval siblings
3478 // in the same parallel move.
3479 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003480};
3481
3482static constexpr size_t kDefaultNumberOfMoves = 4;
3483
3484class HParallelMove : public HTemplateInstruction<0> {
3485 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003486 explicit HParallelMove(ArenaAllocator* arena)
3487 : HTemplateInstruction(SideEffects::None()), moves_(arena, kDefaultNumberOfMoves) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003488
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003489 void AddMove(Location source, Location destination, HInstruction* instruction) {
3490 DCHECK(source.IsValid());
3491 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003492 if (kIsDebugBuild) {
3493 if (instruction != nullptr) {
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00003494 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003495 if (moves_.Get(i).GetInstruction() == instruction) {
3496 // Special case the situation where the move is for the spill slot
3497 // of the instruction.
3498 if ((GetPrevious() == instruction)
3499 || ((GetPrevious() == nullptr)
3500 && instruction->IsPhi()
3501 && instruction->GetBlock() == GetBlock())) {
3502 DCHECK_NE(destination.GetKind(), moves_.Get(i).GetDestination().GetKind())
3503 << "Doing parallel moves for the same instruction.";
3504 } else {
3505 DCHECK(false) << "Doing parallel moves for the same instruction.";
3506 }
3507 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00003508 }
3509 }
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003510 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
3511 DCHECK(!destination.Equals(moves_.Get(i).GetDestination()))
3512 << "Same destination for two moves in a parallel move.";
3513 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003514 }
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003515 moves_.Add(MoveOperands(source, destination, instruction));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003516 }
3517
3518 MoveOperands* MoveOperandsAt(size_t index) const {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003519 return moves_.GetRawStorage() + index;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003520 }
3521
3522 size_t NumMoves() const { return moves_.Size(); }
3523
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003524 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003525
3526 private:
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003527 GrowableArray<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003528
3529 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
3530};
3531
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003532class HGraphVisitor : public ValueObject {
3533 public:
Dave Allison20dfc792014-06-16 20:44:29 -07003534 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
3535 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003536
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003537 virtual void VisitInstruction(HInstruction* instruction) { UNUSED(instruction); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003538 virtual void VisitBasicBlock(HBasicBlock* block);
3539
Roland Levillain633021e2014-10-01 14:12:25 +01003540 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003541 void VisitInsertionOrder();
3542
Roland Levillain633021e2014-10-01 14:12:25 +01003543 // Visit the graph following dominator tree reverse post-order.
3544 void VisitReversePostOrder();
3545
Nicolas Geoffray787c3072014-03-17 10:20:19 +00003546 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00003547
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003548 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003549#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003550 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
3551
3552 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
3553
3554#undef DECLARE_VISIT_INSTRUCTION
3555
3556 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07003557 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003558
3559 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
3560};
3561
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003562class HGraphDelegateVisitor : public HGraphVisitor {
3563 public:
3564 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
3565 virtual ~HGraphDelegateVisitor() {}
3566
3567 // Visit functions that delegate to to super class.
3568#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003569 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003570
3571 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
3572
3573#undef DECLARE_VISIT_INSTRUCTION
3574
3575 private:
3576 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
3577};
3578
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003579class HInsertionOrderIterator : public ValueObject {
3580 public:
3581 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
3582
3583 bool Done() const { return index_ == graph_.GetBlocks().Size(); }
3584 HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); }
3585 void Advance() { ++index_; }
3586
3587 private:
3588 const HGraph& graph_;
3589 size_t index_;
3590
3591 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
3592};
3593
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003594class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003595 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00003596 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
3597 // Check that reverse post order of the graph has been built.
3598 DCHECK(!graph.GetReversePostOrder().IsEmpty());
3599 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003600
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003601 bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); }
3602 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003603 void Advance() { ++index_; }
3604
3605 private:
3606 const HGraph& graph_;
3607 size_t index_;
3608
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003609 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003610};
3611
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003612class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003613 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003614 explicit HPostOrderIterator(const HGraph& graph)
David Brazdil10f56cb2015-03-24 18:49:14 +00003615 : graph_(graph), index_(graph_.GetReversePostOrder().Size()) {
3616 // Check that reverse post order of the graph has been built.
3617 DCHECK(!graph.GetReversePostOrder().IsEmpty());
3618 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003619
3620 bool Done() const { return index_ == 0; }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003621 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003622 void Advance() { --index_; }
3623
3624 private:
3625 const HGraph& graph_;
3626 size_t index_;
3627
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003628 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003629};
3630
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003631// Iterator over the blocks that art part of the loop. Includes blocks part
3632// of an inner loop. The order in which the blocks are iterated is on their
3633// block id.
3634class HBlocksInLoopIterator : public ValueObject {
3635 public:
3636 explicit HBlocksInLoopIterator(const HLoopInformation& info)
3637 : blocks_in_loop_(info.GetBlocks()),
3638 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
3639 index_(0) {
3640 if (!blocks_in_loop_.IsBitSet(index_)) {
3641 Advance();
3642 }
3643 }
3644
3645 bool Done() const { return index_ == blocks_.Size(); }
3646 HBasicBlock* Current() const { return blocks_.Get(index_); }
3647 void Advance() {
3648 ++index_;
3649 for (size_t e = blocks_.Size(); index_ < e; ++index_) {
3650 if (blocks_in_loop_.IsBitSet(index_)) {
3651 break;
3652 }
3653 }
3654 }
3655
3656 private:
3657 const BitVector& blocks_in_loop_;
3658 const GrowableArray<HBasicBlock*>& blocks_;
3659 size_t index_;
3660
3661 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
3662};
3663
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003664inline int64_t Int64FromConstant(HConstant* constant) {
3665 DCHECK(constant->IsIntConstant() || constant->IsLongConstant());
3666 return constant->IsIntConstant() ? constant->AsIntConstant()->GetValue()
3667 : constant->AsLongConstant()->GetValue();
3668}
3669
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003670} // namespace art
3671
3672#endif // ART_COMPILER_OPTIMIZING_NODES_H_