blob: da1468ecdb794f13ebf2a34adee35744985b5a00 [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) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000685 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +0000686 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000687 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100688 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000689 M(Exit, Instruction) \
690 M(FloatConstant, Constant) \
691 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100692 M(GreaterThan, Condition) \
693 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100694 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000695 M(InstanceFieldGet, Instruction) \
696 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000697 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100698 M(IntConstant, Constant) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +0000699 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000700 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100701 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000702 M(LessThan, Condition) \
703 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000704 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000705 M(LoadException, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100706 M(LoadLocal, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000707 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100708 M(Local, Instruction) \
709 M(LongConstant, Constant) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +0000710 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000711 M(Mul, BinaryOperation) \
712 M(Neg, UnaryOperation) \
713 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100714 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +0100715 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000716 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000717 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000718 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000719 M(Or, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100720 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000721 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100722 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000723 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100724 M(Return, Instruction) \
725 M(ReturnVoid, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000726 M(Shl, BinaryOperation) \
727 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100728 M(StaticFieldGet, Instruction) \
729 M(StaticFieldSet, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100730 M(StoreLocal, Instruction) \
731 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100732 M(SuspendCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000733 M(Temporary, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000734 M(Throw, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +0000735 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000736 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000737 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000738
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100739#define FOR_EACH_INSTRUCTION(M) \
740 FOR_EACH_CONCRETE_INSTRUCTION(M) \
741 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +0100742 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +0100743 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100744 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -0700745
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100746#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000747FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
748#undef FORWARD_DECLARATION
749
Roland Levillainccc07a92014-09-16 14:48:16 +0100750#define DECLARE_INSTRUCTION(type) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000751 InstructionKind GetKind() const OVERRIDE { return k##type; } \
752 const char* DebugName() const OVERRIDE { return #type; } \
753 const H##type* As##type() const OVERRIDE { return this; } \
754 H##type* As##type() OVERRIDE { return this; } \
755 bool InstructionTypeEquals(HInstruction* other) const OVERRIDE { \
Roland Levillainccc07a92014-09-16 14:48:16 +0100756 return other->Is##type(); \
757 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000758 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000759
David Brazdiled596192015-01-23 10:39:45 +0000760template <typename T> class HUseList;
761
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100762template <typename T>
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700763class HUseListNode : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000764 public:
David Brazdiled596192015-01-23 10:39:45 +0000765 HUseListNode* GetPrevious() const { return prev_; }
766 HUseListNode* GetNext() const { return next_; }
767 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100768 size_t GetIndex() const { return index_; }
769
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000770 private:
David Brazdiled596192015-01-23 10:39:45 +0000771 HUseListNode(T user, size_t index)
772 : user_(user), index_(index), prev_(nullptr), next_(nullptr) {}
773
774 T const user_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100775 const size_t index_;
David Brazdiled596192015-01-23 10:39:45 +0000776 HUseListNode<T>* prev_;
777 HUseListNode<T>* next_;
778
779 friend class HUseList<T>;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000780
781 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
782};
783
David Brazdiled596192015-01-23 10:39:45 +0000784template <typename T>
785class HUseList : public ValueObject {
786 public:
787 HUseList() : first_(nullptr) {}
788
789 void Clear() {
790 first_ = nullptr;
791 }
792
793 // Adds a new entry at the beginning of the use list and returns
794 // the newly created node.
795 HUseListNode<T>* AddUse(T user, size_t index, ArenaAllocator* arena) {
David Brazdilea55b932015-01-27 17:12:29 +0000796 HUseListNode<T>* new_node = new (arena) HUseListNode<T>(user, index);
David Brazdiled596192015-01-23 10:39:45 +0000797 if (IsEmpty()) {
798 first_ = new_node;
799 } else {
800 first_->prev_ = new_node;
801 new_node->next_ = first_;
802 first_ = new_node;
803 }
804 return new_node;
805 }
806
807 HUseListNode<T>* GetFirst() const {
808 return first_;
809 }
810
811 void Remove(HUseListNode<T>* node) {
David Brazdil1abb4192015-02-17 18:33:36 +0000812 DCHECK(node != nullptr);
813 DCHECK(Contains(node));
814
David Brazdiled596192015-01-23 10:39:45 +0000815 if (node->prev_ != nullptr) {
816 node->prev_->next_ = node->next_;
817 }
818 if (node->next_ != nullptr) {
819 node->next_->prev_ = node->prev_;
820 }
821 if (node == first_) {
822 first_ = node->next_;
823 }
824 }
825
David Brazdil1abb4192015-02-17 18:33:36 +0000826 bool Contains(const HUseListNode<T>* node) const {
827 if (node == nullptr) {
828 return false;
829 }
830 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
831 if (current == node) {
832 return true;
833 }
834 }
835 return false;
836 }
837
David Brazdiled596192015-01-23 10:39:45 +0000838 bool IsEmpty() const {
839 return first_ == nullptr;
840 }
841
842 bool HasOnlyOneUse() const {
843 return first_ != nullptr && first_->next_ == nullptr;
844 }
845
846 private:
847 HUseListNode<T>* first_;
848};
849
850template<typename T>
851class HUseIterator : public ValueObject {
852 public:
853 explicit HUseIterator(const HUseList<T>& uses) : current_(uses.GetFirst()) {}
854
855 bool Done() const { return current_ == nullptr; }
856
857 void Advance() {
858 DCHECK(!Done());
859 current_ = current_->GetNext();
860 }
861
862 HUseListNode<T>* Current() const {
863 DCHECK(!Done());
864 return current_;
865 }
866
867 private:
868 HUseListNode<T>* current_;
869
870 friend class HValue;
871};
872
David Brazdil1abb4192015-02-17 18:33:36 +0000873// This class is used by HEnvironment and HInstruction classes to record the
874// instructions they use and pointers to the corresponding HUseListNodes kept
875// by the used instructions.
876template <typename T>
877class HUserRecord : public ValueObject {
878 public:
879 HUserRecord() : instruction_(nullptr), use_node_(nullptr) {}
880 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), use_node_(nullptr) {}
881
882 HUserRecord(const HUserRecord<T>& old_record, HUseListNode<T>* use_node)
883 : instruction_(old_record.instruction_), use_node_(use_node) {
884 DCHECK(instruction_ != nullptr);
885 DCHECK(use_node_ != nullptr);
886 DCHECK(old_record.use_node_ == nullptr);
887 }
888
889 HInstruction* GetInstruction() const { return instruction_; }
890 HUseListNode<T>* GetUseNode() const { return use_node_; }
891
892 private:
893 // Instruction used by the user.
894 HInstruction* instruction_;
895
896 // Corresponding entry in the use list kept by 'instruction_'.
897 HUseListNode<T>* use_node_;
898};
899
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100900// Represents the side effects an instruction may have.
901class SideEffects : public ValueObject {
902 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100903 SideEffects() : flags_(0) {}
904
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100905 static SideEffects None() {
906 return SideEffects(0);
907 }
908
909 static SideEffects All() {
910 return SideEffects(ChangesSomething().flags_ | DependsOnSomething().flags_);
911 }
912
913 static SideEffects ChangesSomething() {
914 return SideEffects((1 << kFlagChangesCount) - 1);
915 }
916
917 static SideEffects DependsOnSomething() {
918 int count = kFlagDependsOnCount - kFlagChangesCount;
919 return SideEffects(((1 << count) - 1) << kFlagChangesCount);
920 }
921
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100922 SideEffects Union(SideEffects other) const {
923 return SideEffects(flags_ | other.flags_);
924 }
925
Roland Levillain72bceff2014-09-15 18:29:00 +0100926 bool HasSideEffects() const {
927 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
928 return (flags_ & all_bits_set) != 0;
929 }
930
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100931 bool HasAllSideEffects() const {
932 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
933 return all_bits_set == (flags_ & all_bits_set);
934 }
935
936 bool DependsOn(SideEffects other) const {
937 size_t depends_flags = other.ComputeDependsFlags();
938 return (flags_ & depends_flags) != 0;
939 }
940
941 bool HasDependencies() const {
942 int count = kFlagDependsOnCount - kFlagChangesCount;
943 size_t all_bits_set = (1 << count) - 1;
944 return ((flags_ >> kFlagChangesCount) & all_bits_set) != 0;
945 }
946
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100947 private:
948 static constexpr int kFlagChangesSomething = 0;
949 static constexpr int kFlagChangesCount = kFlagChangesSomething + 1;
950
951 static constexpr int kFlagDependsOnSomething = kFlagChangesCount;
952 static constexpr int kFlagDependsOnCount = kFlagDependsOnSomething + 1;
953
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100954 explicit SideEffects(size_t flags) : flags_(flags) {}
955
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100956 size_t ComputeDependsFlags() const {
957 return flags_ << kFlagChangesCount;
958 }
959
960 size_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100961};
962
David Brazdiled596192015-01-23 10:39:45 +0000963// A HEnvironment object contains the values of virtual registers at a given location.
964class HEnvironment : public ArenaObject<kArenaAllocMisc> {
965 public:
966 HEnvironment(ArenaAllocator* arena, size_t number_of_vregs)
967 : vregs_(arena, number_of_vregs) {
968 vregs_.SetSize(number_of_vregs);
969 for (size_t i = 0; i < number_of_vregs; i++) {
David Brazdil1abb4192015-02-17 18:33:36 +0000970 vregs_.Put(i, HUserRecord<HEnvironment*>());
David Brazdiled596192015-01-23 10:39:45 +0000971 }
972 }
973
974 void CopyFrom(HEnvironment* env);
975
976 void SetRawEnvAt(size_t index, HInstruction* instruction) {
David Brazdil1abb4192015-02-17 18:33:36 +0000977 vregs_.Put(index, HUserRecord<HEnvironment*>(instruction));
David Brazdiled596192015-01-23 10:39:45 +0000978 }
979
980 HInstruction* GetInstructionAt(size_t index) const {
David Brazdil1abb4192015-02-17 18:33:36 +0000981 return vregs_.Get(index).GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +0000982 }
983
David Brazdil1abb4192015-02-17 18:33:36 +0000984 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +0000985
986 size_t Size() const { return vregs_.Size(); }
987
988 private:
David Brazdil1abb4192015-02-17 18:33:36 +0000989 // Record instructions' use entries of this environment for constant-time removal.
990 // It should only be called by HInstruction when a new environment use is added.
991 void RecordEnvUse(HUseListNode<HEnvironment*>* env_use) {
992 DCHECK(env_use->GetUser() == this);
993 size_t index = env_use->GetIndex();
994 vregs_.Put(index, HUserRecord<HEnvironment*>(vregs_.Get(index), env_use));
995 }
David Brazdiled596192015-01-23 10:39:45 +0000996
David Brazdil1abb4192015-02-17 18:33:36 +0000997 GrowableArray<HUserRecord<HEnvironment*> > vregs_;
David Brazdiled596192015-01-23 10:39:45 +0000998
David Brazdil1abb4192015-02-17 18:33:36 +0000999 friend HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001000
1001 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1002};
1003
Calin Juravleacf735c2015-02-12 15:25:22 +00001004class ReferenceTypeInfo : ValueObject {
1005 public:
Calin Juravleb1498f62015-02-16 13:13:29 +00001006 typedef Handle<mirror::Class> TypeHandle;
1007
1008 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact)
1009 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1010 if (type_handle->IsObjectClass()) {
1011 // Override the type handle to be consistent with the case when we get to
1012 // Top but don't have the Object class available. It avoids having to guess
1013 // what value the type_handle has when it's Top.
1014 return ReferenceTypeInfo(TypeHandle(), is_exact, true);
1015 } else {
1016 return ReferenceTypeInfo(type_handle, is_exact, false);
1017 }
1018 }
1019
1020 static ReferenceTypeInfo CreateTop(bool is_exact) {
1021 return ReferenceTypeInfo(TypeHandle(), is_exact, true);
Calin Juravleacf735c2015-02-12 15:25:22 +00001022 }
1023
1024 bool IsExact() const { return is_exact_; }
1025 bool IsTop() const { return is_top_; }
1026
1027 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
1028
Calin Juravleb1498f62015-02-16 13:13:29 +00001029 bool IsSupertypeOf(ReferenceTypeInfo rti) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Calin Juravleacf735c2015-02-12 15:25:22 +00001030 if (IsTop()) {
1031 // Top (equivalent for java.lang.Object) is supertype of anything.
1032 return true;
1033 }
1034 if (rti.IsTop()) {
1035 // If we get here `this` is not Top() so it can't be a supertype.
1036 return false;
1037 }
1038 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
1039 }
1040
1041 // Returns true if the type information provide the same amount of details.
1042 // Note that it does not mean that the instructions have the same actual type
1043 // (e.g. tops are equal but they can be the result of a merge).
1044 bool IsEqual(ReferenceTypeInfo rti) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1045 if (IsExact() != rti.IsExact()) {
1046 return false;
1047 }
1048 if (IsTop() && rti.IsTop()) {
1049 // `Top` means java.lang.Object, so the types are equivalent.
1050 return true;
1051 }
1052 if (IsTop() || rti.IsTop()) {
1053 // If only one is top or object than they are not equivalent.
1054 // NB: We need this extra check because the type_handle of `Top` is invalid
1055 // and we cannot inspect its reference.
1056 return false;
1057 }
1058
1059 // Finally check the types.
1060 return GetTypeHandle().Get() == rti.GetTypeHandle().Get();
1061 }
1062
1063 private:
Calin Juravleb1498f62015-02-16 13:13:29 +00001064 ReferenceTypeInfo() : ReferenceTypeInfo(TypeHandle(), false, true) {}
1065 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact, bool is_top)
1066 : type_handle_(type_handle), is_exact_(is_exact), is_top_(is_top) {}
1067
Calin Juravleacf735c2015-02-12 15:25:22 +00001068 // The class of the object.
Calin Juravleb1498f62015-02-16 13:13:29 +00001069 TypeHandle type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001070 // Whether or not the type is exact or a superclass of the actual type.
Calin Juravleb1498f62015-02-16 13:13:29 +00001071 // Whether or not we have any information about this type.
Calin Juravleacf735c2015-02-12 15:25:22 +00001072 bool is_exact_;
1073 // A true value here means that the object type should be java.lang.Object.
1074 // We don't have access to the corresponding mirror object every time so this
1075 // flag acts as a substitute. When true, the TypeHandle refers to a null
1076 // pointer and should not be used.
1077 bool is_top_;
1078};
1079
1080std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
1081
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001082class HInstruction : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001083 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001084 explicit HInstruction(SideEffects side_effects)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001085 : previous_(nullptr),
1086 next_(nullptr),
1087 block_(nullptr),
1088 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001089 ssa_index_(-1),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001090 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001091 locations_(nullptr),
1092 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001093 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001094 side_effects_(side_effects),
1095 reference_type_info_(ReferenceTypeInfo::CreateTop(/* is_exact */ false)) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001096
Dave Allison20dfc792014-06-16 20:44:29 -07001097 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001098
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001099#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001100 enum InstructionKind {
1101 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1102 };
1103#undef DECLARE_KIND
1104
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001105 HInstruction* GetNext() const { return next_; }
1106 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001107
Calin Juravle77520bc2015-01-12 18:45:46 +00001108 HInstruction* GetNextDisregardingMoves() const;
1109 HInstruction* GetPreviousDisregardingMoves() const;
1110
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001111 HBasicBlock* GetBlock() const { return block_; }
1112 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001113 bool IsInBlock() const { return block_ != nullptr; }
1114 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +01001115 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001116
Roland Levillain6b879dd2014-09-22 17:13:44 +01001117 virtual size_t InputCount() const = 0;
David Brazdil1abb4192015-02-17 18:33:36 +00001118 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001119
1120 virtual void Accept(HGraphVisitor* visitor) = 0;
1121 virtual const char* DebugName() const = 0;
1122
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001123 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
David Brazdil1abb4192015-02-17 18:33:36 +00001124 void SetRawInputAt(size_t index, HInstruction* input) {
1125 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1126 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001127
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001128 virtual bool NeedsEnvironment() const { return false; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001129 virtual bool IsControlFlow() const { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01001130 virtual bool CanThrow() const { return false; }
Roland Levillain72bceff2014-09-15 18:29:00 +01001131 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001132
Calin Juravle61d544b2015-02-23 16:46:57 +00001133 virtual bool ActAsNullConstant() const { return false; }
1134
Calin Juravle10e244f2015-01-26 18:54:32 +00001135 // Does not apply for all instructions, but having this at top level greatly
1136 // simplifies the null check elimination.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001137 virtual bool CanBeNull() const {
1138 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1139 return true;
1140 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001141
Calin Juravle77520bc2015-01-12 18:45:46 +00001142 virtual bool CanDoImplicitNullCheck() const { return false; }
1143
Calin Juravleacf735c2015-02-12 15:25:22 +00001144 void SetReferenceTypeInfo(ReferenceTypeInfo reference_type_info) {
Calin Juravle61d544b2015-02-23 16:46:57 +00001145 DCHECK_EQ(GetType(), Primitive::kPrimNot);
Calin Juravleacf735c2015-02-12 15:25:22 +00001146 reference_type_info_ = reference_type_info;
1147 }
1148
Calin Juravle61d544b2015-02-23 16:46:57 +00001149 ReferenceTypeInfo GetReferenceTypeInfo() const {
1150 DCHECK_EQ(GetType(), Primitive::kPrimNot);
1151 return reference_type_info_;
1152 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001153
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001154 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001155 DCHECK(user != nullptr);
1156 HUseListNode<HInstruction*>* use =
1157 uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1158 user->SetRawInputRecordAt(index, HUserRecord<HInstruction*>(user->InputRecordAt(index), use));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001159 }
1160
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001161 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001162 DCHECK(user != nullptr);
David Brazdiled596192015-01-23 10:39:45 +00001163 HUseListNode<HEnvironment*>* env_use =
1164 env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1165 user->RecordEnvUse(env_use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001166 }
1167
David Brazdil1abb4192015-02-17 18:33:36 +00001168 void RemoveAsUserOfInput(size_t input) {
1169 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
1170 input_use.GetInstruction()->uses_.Remove(input_use.GetUseNode());
1171 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001172
David Brazdil1abb4192015-02-17 18:33:36 +00001173 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1174 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001175
David Brazdiled596192015-01-23 10:39:45 +00001176 bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); }
1177 bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); }
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001178 bool HasNonEnvironmentUses() const { return !uses_.IsEmpty(); }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001179
Roland Levillain6c82d402014-10-13 16:10:27 +01001180 // Does this instruction strictly dominate `other_instruction`?
1181 // Returns false if this instruction and `other_instruction` are the same.
1182 // Aborts if this instruction and `other_instruction` are both phis.
1183 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001184
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001185 int GetId() const { return id_; }
1186 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001187
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001188 int GetSsaIndex() const { return ssa_index_; }
1189 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1190 bool HasSsaIndex() const { return ssa_index_ != -1; }
1191
1192 bool HasEnvironment() const { return environment_ != nullptr; }
1193 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001194 void SetEnvironment(HEnvironment* environment) { environment_ = environment; }
1195
Nicolas Geoffray39468442014-09-02 15:17:15 +01001196 // Returns the number of entries in the environment. Typically, that is the
1197 // number of dex registers in a method. It could be more in case of inlining.
1198 size_t EnvironmentSize() const;
1199
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001200 LocationSummary* GetLocations() const { return locations_; }
1201 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001202
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001203 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001204 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001205
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001206 // Move `this` instruction before `cursor`.
1207 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001208
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001209#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01001210 bool Is##type() const { return (As##type() != nullptr); } \
1211 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001212 virtual H##type* As##type() { return nullptr; }
1213
1214 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
1215#undef INSTRUCTION_TYPE_CHECK
1216
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001217 // Returns whether the instruction can be moved within the graph.
1218 virtual bool CanBeMoved() const { return false; }
1219
1220 // Returns whether the two instructions are of the same kind.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001221 virtual bool InstructionTypeEquals(HInstruction* other) const {
1222 UNUSED(other);
1223 return false;
1224 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001225
1226 // Returns whether any data encoded in the two instructions is equal.
1227 // This method does not look at the inputs. Both instructions must be
1228 // of the same type, otherwise the method has undefined behavior.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001229 virtual bool InstructionDataEquals(HInstruction* other) const {
1230 UNUSED(other);
1231 return false;
1232 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001233
1234 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00001235 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001236 // 2) Their inputs are identical.
1237 bool Equals(HInstruction* other) const;
1238
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001239 virtual InstructionKind GetKind() const = 0;
1240
1241 virtual size_t ComputeHashCode() const {
1242 size_t result = GetKind();
1243 for (size_t i = 0, e = InputCount(); i < e; ++i) {
1244 result = (result * 31) + InputAt(i)->GetId();
1245 }
1246 return result;
1247 }
1248
1249 SideEffects GetSideEffects() const { return side_effects_; }
1250
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001251 size_t GetLifetimePosition() const { return lifetime_position_; }
1252 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
1253 LiveInterval* GetLiveInterval() const { return live_interval_; }
1254 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
1255 bool HasLiveInterval() const { return live_interval_ != nullptr; }
1256
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001257 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
1258
1259 // Returns whether the code generation of the instruction will require to have access
1260 // to the current method. Such instructions are:
1261 // (1): Instructions that require an environment, as calling the runtime requires
1262 // to walk the stack and have the current method stored at a specific stack address.
1263 // (2): Object literals like classes and strings, that are loaded from the dex cache
1264 // fields of the current method.
1265 bool NeedsCurrentMethod() const {
1266 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
1267 }
1268
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001269 virtual bool NeedsDexCache() const { return false; }
1270
David Brazdil1abb4192015-02-17 18:33:36 +00001271 protected:
1272 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
1273 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
1274
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001275 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001276 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); }
1277
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001278 HInstruction* previous_;
1279 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001280 HBasicBlock* block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001281
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001282 // An instruction gets an id when it is added to the graph.
1283 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01001284 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001285 int id_;
1286
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001287 // When doing liveness analysis, instructions that have uses get an SSA index.
1288 int ssa_index_;
1289
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001290 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00001291 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001292
1293 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00001294 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001295
Nicolas Geoffray39468442014-09-02 15:17:15 +01001296 // The environment associated with this instruction. Not null if the instruction
1297 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001298 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001299
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001300 // Set by the code generator.
1301 LocationSummary* locations_;
1302
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001303 // Set by the liveness analysis.
1304 LiveInterval* live_interval_;
1305
1306 // Set by the liveness analysis, this is the position in a linear
1307 // order of blocks where this instruction's live interval start.
1308 size_t lifetime_position_;
1309
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001310 const SideEffects side_effects_;
1311
Calin Juravleacf735c2015-02-12 15:25:22 +00001312 // TODO: for primitive types this should be marked as invalid.
1313 ReferenceTypeInfo reference_type_info_;
1314
David Brazdil1abb4192015-02-17 18:33:36 +00001315 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001316 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00001317 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001318 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001319 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001320
1321 DISALLOW_COPY_AND_ASSIGN(HInstruction);
1322};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001323std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001324
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001325class HInputIterator : public ValueObject {
1326 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001327 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001328
1329 bool Done() const { return index_ == instruction_->InputCount(); }
1330 HInstruction* Current() const { return instruction_->InputAt(index_); }
1331 void Advance() { index_++; }
1332
1333 private:
1334 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001335 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001336
1337 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
1338};
1339
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001340class HInstructionIterator : public ValueObject {
1341 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001342 explicit HInstructionIterator(const HInstructionList& instructions)
1343 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001344 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001345 }
1346
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001347 bool Done() const { return instruction_ == nullptr; }
1348 HInstruction* Current() const { return instruction_; }
1349 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001350 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001351 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001352 }
1353
1354 private:
1355 HInstruction* instruction_;
1356 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001357
1358 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001359};
1360
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001361class HBackwardInstructionIterator : public ValueObject {
1362 public:
1363 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
1364 : instruction_(instructions.last_instruction_) {
1365 next_ = Done() ? nullptr : instruction_->GetPrevious();
1366 }
1367
1368 bool Done() const { return instruction_ == nullptr; }
1369 HInstruction* Current() const { return instruction_; }
1370 void Advance() {
1371 instruction_ = next_;
1372 next_ = Done() ? nullptr : instruction_->GetPrevious();
1373 }
1374
1375 private:
1376 HInstruction* instruction_;
1377 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001378
1379 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001380};
1381
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001382// An embedded container with N elements of type T. Used (with partial
1383// specialization for N=0) because embedded arrays cannot have size 0.
1384template<typename T, intptr_t N>
1385class EmbeddedArray {
1386 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001387 EmbeddedArray() : elements_() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001388
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001389 intptr_t GetLength() const { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001390
1391 const T& operator[](intptr_t i) const {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001392 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001393 return elements_[i];
1394 }
1395
1396 T& operator[](intptr_t i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001397 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001398 return elements_[i];
1399 }
1400
1401 const T& At(intptr_t i) const {
1402 return (*this)[i];
1403 }
1404
1405 void SetAt(intptr_t i, const T& val) {
1406 (*this)[i] = val;
1407 }
1408
1409 private:
1410 T elements_[N];
1411};
1412
1413template<typename T>
1414class EmbeddedArray<T, 0> {
1415 public:
1416 intptr_t length() const { return 0; }
1417 const T& operator[](intptr_t i) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001418 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001419 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001420 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001421 }
1422 T& operator[](intptr_t i) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001423 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001424 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001425 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001426 }
1427};
1428
1429template<intptr_t N>
1430class HTemplateInstruction: public HInstruction {
1431 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001432 HTemplateInstruction<N>(SideEffects side_effects)
1433 : HInstruction(side_effects), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07001434 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001435
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001436 size_t InputCount() const OVERRIDE { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001437
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001438 protected:
David Brazdil1abb4192015-02-17 18:33:36 +00001439 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_[i]; }
1440
1441 void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE {
1442 inputs_[i] = input;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001443 }
1444
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001445 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001446 EmbeddedArray<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001447
1448 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001449};
1450
Dave Allison20dfc792014-06-16 20:44:29 -07001451template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001452class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07001453 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001454 HExpression<N>(Primitive::Type type, SideEffects side_effects)
1455 : HTemplateInstruction<N>(side_effects), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07001456 virtual ~HExpression() {}
1457
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001458 Primitive::Type GetType() const OVERRIDE { return type_; }
Dave Allison20dfc792014-06-16 20:44:29 -07001459
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001460 protected:
1461 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07001462};
1463
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001464// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
1465// instruction that branches to the exit block.
1466class HReturnVoid : public HTemplateInstruction<0> {
1467 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001468 HReturnVoid() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001469
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001470 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001471
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001472 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001473
1474 private:
1475 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
1476};
1477
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001478// Represents dex's RETURN opcodes. A HReturn is a control flow
1479// instruction that branches to the exit block.
1480class HReturn : public HTemplateInstruction<1> {
1481 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001482 explicit HReturn(HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001483 SetRawInputAt(0, value);
1484 }
1485
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001486 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001487
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001488 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001489
1490 private:
1491 DISALLOW_COPY_AND_ASSIGN(HReturn);
1492};
1493
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001494// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001495// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001496// exit block.
1497class HExit : public HTemplateInstruction<0> {
1498 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001499 HExit() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001500
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001501 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001502
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001503 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001504
1505 private:
1506 DISALLOW_COPY_AND_ASSIGN(HExit);
1507};
1508
1509// Jumps from one block to another.
1510class HGoto : public HTemplateInstruction<0> {
1511 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001512 HGoto() : HTemplateInstruction(SideEffects::None()) {}
1513
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001514 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001515
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001516 HBasicBlock* GetSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001517 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001518 }
1519
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001520 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001521
1522 private:
1523 DISALLOW_COPY_AND_ASSIGN(HGoto);
1524};
1525
Dave Allison20dfc792014-06-16 20:44:29 -07001526
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001527// Conditional branch. A block ending with an HIf instruction must have
1528// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001529class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001530 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001531 explicit HIf(HInstruction* input) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001532 SetRawInputAt(0, input);
1533 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001534
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001535 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001536
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001537 HBasicBlock* IfTrueSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001538 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001539 }
1540
1541 HBasicBlock* IfFalseSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001542 return GetBlock()->GetSuccessors().Get(1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001543 }
1544
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001545 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001546
Dave Allison20dfc792014-06-16 20:44:29 -07001547 virtual bool IsIfInstruction() const { return true; }
1548
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001549 private:
1550 DISALLOW_COPY_AND_ASSIGN(HIf);
1551};
1552
Roland Levillain88cb1752014-10-20 16:36:47 +01001553class HUnaryOperation : public HExpression<1> {
1554 public:
1555 HUnaryOperation(Primitive::Type result_type, HInstruction* input)
1556 : HExpression(result_type, SideEffects::None()) {
1557 SetRawInputAt(0, input);
1558 }
1559
1560 HInstruction* GetInput() const { return InputAt(0); }
1561 Primitive::Type GetResultType() const { return GetType(); }
1562
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001563 bool CanBeMoved() const OVERRIDE { return true; }
1564 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001565 UNUSED(other);
1566 return true;
1567 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001568
Roland Levillain9240d6a2014-10-20 16:47:04 +01001569 // Try to statically evaluate `operation` and return a HConstant
1570 // containing the result of this evaluation. If `operation` cannot
1571 // be evaluated as a constant, return nullptr.
1572 HConstant* TryStaticEvaluation() const;
1573
1574 // Apply this operation to `x`.
1575 virtual int32_t Evaluate(int32_t x) const = 0;
1576 virtual int64_t Evaluate(int64_t x) const = 0;
1577
Roland Levillain88cb1752014-10-20 16:36:47 +01001578 DECLARE_INSTRUCTION(UnaryOperation);
1579
1580 private:
1581 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
1582};
1583
Dave Allison20dfc792014-06-16 20:44:29 -07001584class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001585 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001586 HBinaryOperation(Primitive::Type result_type,
1587 HInstruction* left,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001588 HInstruction* right) : HExpression(result_type, SideEffects::None()) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001589 SetRawInputAt(0, left);
1590 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001591 }
1592
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001593 HInstruction* GetLeft() const { return InputAt(0); }
1594 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07001595 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001596
Mingyao Yangdc5ac732015-02-25 11:28:05 -08001597 virtual bool IsCommutative() const { return false; }
1598
1599 // Put constant on the right.
1600 // Returns whether order is changed.
1601 bool OrderInputsWithConstantOnTheRight() {
1602 HInstruction* left = InputAt(0);
1603 HInstruction* right = InputAt(1);
1604 if (left->IsConstant() && !right->IsConstant()) {
1605 ReplaceInput(right, 0);
1606 ReplaceInput(left, 1);
1607 return true;
1608 }
1609 return false;
1610 }
1611
1612 // Order inputs by instruction id, but favor constant on the right side.
1613 // This helps GVN for commutative ops.
1614 void OrderInputs() {
1615 DCHECK(IsCommutative());
1616 HInstruction* left = InputAt(0);
1617 HInstruction* right = InputAt(1);
1618 if (left == right || (!left->IsConstant() && right->IsConstant())) {
1619 return;
1620 }
1621 if (OrderInputsWithConstantOnTheRight()) {
1622 return;
1623 }
1624 // Order according to instruction id.
1625 if (left->GetId() > right->GetId()) {
1626 ReplaceInput(right, 0);
1627 ReplaceInput(left, 1);
1628 }
1629 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001630
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001631 bool CanBeMoved() const OVERRIDE { return true; }
1632 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001633 UNUSED(other);
1634 return true;
1635 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001636
Roland Levillain9240d6a2014-10-20 16:47:04 +01001637 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01001638 // containing the result of this evaluation. If `operation` cannot
1639 // be evaluated as a constant, return nullptr.
Roland Levillain9240d6a2014-10-20 16:47:04 +01001640 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01001641
1642 // Apply this operation to `x` and `y`.
1643 virtual int32_t Evaluate(int32_t x, int32_t y) const = 0;
1644 virtual int64_t Evaluate(int64_t x, int64_t y) const = 0;
1645
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001646 // Returns an input that can legally be used as the right input and is
1647 // constant, or nullptr.
1648 HConstant* GetConstantRight() const;
1649
1650 // If `GetConstantRight()` returns one of the input, this returns the other
1651 // one. Otherwise it returns nullptr.
1652 HInstruction* GetLeastConstantLeft() const;
1653
Roland Levillainccc07a92014-09-16 14:48:16 +01001654 DECLARE_INSTRUCTION(BinaryOperation);
1655
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001656 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001657 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
1658};
1659
Dave Allison20dfc792014-06-16 20:44:29 -07001660class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001661 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001662 HCondition(HInstruction* first, HInstruction* second)
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001663 : HBinaryOperation(Primitive::kPrimBoolean, first, second),
1664 needs_materialization_(true) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001665
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001666 bool NeedsMaterialization() const { return needs_materialization_; }
1667 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001668
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001669 // For code generation purposes, returns whether this instruction is just before
1670 // `if_`, and disregard moves in between.
1671 bool IsBeforeWhenDisregardMoves(HIf* if_) const;
1672
Dave Allison20dfc792014-06-16 20:44:29 -07001673 DECLARE_INSTRUCTION(Condition);
1674
1675 virtual IfCondition GetCondition() const = 0;
1676
1677 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001678 // For register allocation purposes, returns whether this instruction needs to be
1679 // materialized (that is, not just be in the processor flags).
1680 bool needs_materialization_;
1681
Dave Allison20dfc792014-06-16 20:44:29 -07001682 DISALLOW_COPY_AND_ASSIGN(HCondition);
1683};
1684
1685// Instruction to check if two inputs are equal to each other.
1686class HEqual : public HCondition {
1687 public:
1688 HEqual(HInstruction* first, HInstruction* second)
1689 : HCondition(first, second) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001690
Mingyao Yangdc5ac732015-02-25 11:28:05 -08001691 bool IsCommutative() const OVERRIDE { return true; }
1692
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001693 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001694 return x == y ? 1 : 0;
1695 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001696 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001697 return x == y ? 1 : 0;
1698 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001699
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001700 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001701
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001702 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001703 return kCondEQ;
1704 }
1705
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001706 private:
1707 DISALLOW_COPY_AND_ASSIGN(HEqual);
1708};
1709
Dave Allison20dfc792014-06-16 20:44:29 -07001710class HNotEqual : public HCondition {
1711 public:
1712 HNotEqual(HInstruction* first, HInstruction* second)
1713 : HCondition(first, second) {}
1714
Mingyao Yangdc5ac732015-02-25 11:28:05 -08001715 bool IsCommutative() const OVERRIDE { return true; }
1716
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001717 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001718 return x != y ? 1 : 0;
1719 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001720 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001721 return x != y ? 1 : 0;
1722 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001723
Dave Allison20dfc792014-06-16 20:44:29 -07001724 DECLARE_INSTRUCTION(NotEqual);
1725
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001726 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001727 return kCondNE;
1728 }
1729
1730 private:
1731 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
1732};
1733
1734class HLessThan : public HCondition {
1735 public:
1736 HLessThan(HInstruction* first, HInstruction* second)
1737 : HCondition(first, second) {}
1738
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001739 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001740 return x < y ? 1 : 0;
1741 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001742 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001743 return x < y ? 1 : 0;
1744 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001745
Dave Allison20dfc792014-06-16 20:44:29 -07001746 DECLARE_INSTRUCTION(LessThan);
1747
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001748 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001749 return kCondLT;
1750 }
1751
1752 private:
1753 DISALLOW_COPY_AND_ASSIGN(HLessThan);
1754};
1755
1756class HLessThanOrEqual : public HCondition {
1757 public:
1758 HLessThanOrEqual(HInstruction* first, HInstruction* second)
1759 : HCondition(first, second) {}
1760
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001761 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001762 return x <= y ? 1 : 0;
1763 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001764 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001765 return x <= y ? 1 : 0;
1766 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001767
Dave Allison20dfc792014-06-16 20:44:29 -07001768 DECLARE_INSTRUCTION(LessThanOrEqual);
1769
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001770 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001771 return kCondLE;
1772 }
1773
1774 private:
1775 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
1776};
1777
1778class HGreaterThan : public HCondition {
1779 public:
1780 HGreaterThan(HInstruction* first, HInstruction* second)
1781 : HCondition(first, second) {}
1782
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001783 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001784 return x > y ? 1 : 0;
1785 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001786 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001787 return x > y ? 1 : 0;
1788 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001789
Dave Allison20dfc792014-06-16 20:44:29 -07001790 DECLARE_INSTRUCTION(GreaterThan);
1791
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001792 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001793 return kCondGT;
1794 }
1795
1796 private:
1797 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
1798};
1799
1800class HGreaterThanOrEqual : public HCondition {
1801 public:
1802 HGreaterThanOrEqual(HInstruction* first, HInstruction* second)
1803 : HCondition(first, second) {}
1804
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001805 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001806 return x >= y ? 1 : 0;
1807 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001808 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001809 return x >= y ? 1 : 0;
1810 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001811
Dave Allison20dfc792014-06-16 20:44:29 -07001812 DECLARE_INSTRUCTION(GreaterThanOrEqual);
1813
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001814 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001815 return kCondGE;
1816 }
1817
1818 private:
1819 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
1820};
1821
1822
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001823// Instruction to check how two inputs compare to each other.
1824// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
1825class HCompare : public HBinaryOperation {
1826 public:
Calin Juravleddb7df22014-11-25 20:56:51 +00001827 // The bias applies for floating point operations and indicates how NaN
1828 // comparisons are treated:
1829 enum Bias {
1830 kNoBias, // bias is not applicable (i.e. for long operation)
1831 kGtBias, // return 1 for NaN comparisons
1832 kLtBias, // return -1 for NaN comparisons
1833 };
1834
1835 HCompare(Primitive::Type type, HInstruction* first, HInstruction* second, Bias bias)
1836 : HBinaryOperation(Primitive::kPrimInt, first, second), bias_(bias) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001837 DCHECK_EQ(type, first->GetType());
1838 DCHECK_EQ(type, second->GetType());
1839 }
1840
Calin Juravleddb7df22014-11-25 20:56:51 +00001841 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01001842 return
1843 x == y ? 0 :
1844 x > y ? 1 :
1845 -1;
1846 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001847
1848 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01001849 return
1850 x == y ? 0 :
1851 x > y ? 1 :
1852 -1;
1853 }
1854
Calin Juravleddb7df22014-11-25 20:56:51 +00001855 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
1856 return bias_ == other->AsCompare()->bias_;
1857 }
1858
1859 bool IsGtBias() { return bias_ == kGtBias; }
1860
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001861 DECLARE_INSTRUCTION(Compare);
1862
1863 private:
Calin Juravleddb7df22014-11-25 20:56:51 +00001864 const Bias bias_;
1865
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001866 DISALLOW_COPY_AND_ASSIGN(HCompare);
1867};
1868
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001869// A local in the graph. Corresponds to a Dex register.
1870class HLocal : public HTemplateInstruction<0> {
1871 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001872 explicit HLocal(uint16_t reg_number)
1873 : HTemplateInstruction(SideEffects::None()), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001874
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001875 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001876
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001877 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001878
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001879 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001880 // The Dex register number.
1881 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001882
1883 DISALLOW_COPY_AND_ASSIGN(HLocal);
1884};
1885
1886// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07001887class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001888 public:
Roland Levillain5799fc02014-09-25 12:15:20 +01001889 HLoadLocal(HLocal* local, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001890 : HExpression(type, SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001891 SetRawInputAt(0, local);
1892 }
1893
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001894 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1895
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001896 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001897
1898 private:
1899 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
1900};
1901
1902// Store a value in a given local. This instruction has two inputs: the value
1903// and the local.
1904class HStoreLocal : public HTemplateInstruction<2> {
1905 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001906 HStoreLocal(HLocal* local, HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001907 SetRawInputAt(0, local);
1908 SetRawInputAt(1, value);
1909 }
1910
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001911 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1912
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001913 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001914
1915 private:
1916 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
1917};
1918
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001919class HConstant : public HExpression<0> {
1920 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001921 explicit HConstant(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
1922
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001923 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001924
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001925 virtual bool IsMinusOne() const { return false; }
1926 virtual bool IsZero() const { return false; }
1927 virtual bool IsOne() const { return false; }
1928
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001929 DECLARE_INSTRUCTION(Constant);
1930
1931 private:
1932 DISALLOW_COPY_AND_ASSIGN(HConstant);
1933};
1934
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001935class HFloatConstant : public HConstant {
1936 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001937 float GetValue() const { return value_; }
1938
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001939 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001940 return bit_cast<uint32_t, float>(other->AsFloatConstant()->value_) ==
1941 bit_cast<uint32_t, float>(value_);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001942 }
1943
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001944 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001945
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001946 bool IsMinusOne() const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001947 return bit_cast<uint32_t, float>(AsFloatConstant()->GetValue()) ==
1948 bit_cast<uint32_t, float>((-1.0f));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001949 }
1950 bool IsZero() const OVERRIDE {
1951 return AsFloatConstant()->GetValue() == 0.0f;
1952 }
1953 bool IsOne() const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001954 return bit_cast<uint32_t, float>(AsFloatConstant()->GetValue()) ==
1955 bit_cast<uint32_t, float>(1.0f);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001956 }
1957
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001958 DECLARE_INSTRUCTION(FloatConstant);
1959
1960 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00001961 explicit HFloatConstant(float value) : HConstant(Primitive::kPrimFloat), value_(value) {}
1962
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001963 const float value_;
1964
David Brazdil8d5b8b22015-03-24 10:51:52 +00001965 // Only the SsaBuilder can currently create floating-point constants. If we
1966 // ever need to create them later in the pipeline, we will have to handle them
1967 // the same way as integral constants.
1968 friend class SsaBuilder;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001969 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
1970};
1971
1972class HDoubleConstant : public HConstant {
1973 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001974 double GetValue() const { return value_; }
1975
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001976 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001977 return bit_cast<uint64_t, double>(other->AsDoubleConstant()->value_) ==
1978 bit_cast<uint64_t, double>(value_);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001979 }
1980
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001981 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001982
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001983 bool IsMinusOne() const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001984 return bit_cast<uint64_t, double>(AsDoubleConstant()->GetValue()) ==
1985 bit_cast<uint64_t, double>((-1.0));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001986 }
1987 bool IsZero() const OVERRIDE {
1988 return AsDoubleConstant()->GetValue() == 0.0;
1989 }
1990 bool IsOne() const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001991 return bit_cast<uint64_t, double>(AsDoubleConstant()->GetValue()) ==
1992 bit_cast<uint64_t, double>(1.0);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001993 }
1994
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001995 DECLARE_INSTRUCTION(DoubleConstant);
1996
1997 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00001998 explicit HDoubleConstant(double value) : HConstant(Primitive::kPrimDouble), value_(value) {}
1999
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002000 const double value_;
2001
David Brazdil8d5b8b22015-03-24 10:51:52 +00002002 // Only the SsaBuilder can currently create floating-point constants. If we
2003 // ever need to create them later in the pipeline, we will have to handle them
2004 // the same way as integral constants.
2005 friend class SsaBuilder;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002006 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
2007};
2008
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002009class HNullConstant : public HConstant {
2010 public:
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002011 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2012 return true;
2013 }
2014
2015 size_t ComputeHashCode() const OVERRIDE { return 0; }
2016
Calin Juravle61d544b2015-02-23 16:46:57 +00002017 bool ActAsNullConstant() const OVERRIDE { return true; }
2018
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002019 DECLARE_INSTRUCTION(NullConstant);
2020
2021 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002022 HNullConstant() : HConstant(Primitive::kPrimNot) {}
2023
2024 friend class HGraph;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002025 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2026};
2027
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002028// Constants of the type int. Those can be from Dex instructions, or
2029// synthesized (for example with the if-eqz instruction).
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002030class HIntConstant : public HConstant {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002031 public:
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002032 int32_t GetValue() const { return value_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002033
Calin Juravle61d544b2015-02-23 16:46:57 +00002034 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002035 return other->AsIntConstant()->value_ == value_;
2036 }
2037
Calin Juravle61d544b2015-02-23 16:46:57 +00002038 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2039
2040 // TODO: Null is represented by the `0` constant. In most cases we replace it
2041 // with a HNullConstant but we don't do it when comparing (a != null). This
2042 // method is an workaround until we fix the above.
2043 bool ActAsNullConstant() const OVERRIDE { return value_ == 0; }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002044
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002045 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2046 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2047 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2048
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002049 DECLARE_INSTRUCTION(IntConstant);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002050
2051 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002052 explicit HIntConstant(int32_t value) : HConstant(Primitive::kPrimInt), value_(value) {}
2053
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002054 const int32_t value_;
2055
David Brazdil8d5b8b22015-03-24 10:51:52 +00002056 friend class HGraph;
2057 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2058 ART_FRIEND_TEST(ParallelMoveTest, ConstantLast);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002059 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2060};
2061
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002062class HLongConstant : public HConstant {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002063 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002064 int64_t GetValue() const { return value_; }
2065
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002066 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002067 return other->AsLongConstant()->value_ == value_;
2068 }
2069
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002070 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002071
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002072 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2073 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2074 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2075
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002076 DECLARE_INSTRUCTION(LongConstant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002077
2078 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002079 explicit HLongConstant(int64_t value) : HConstant(Primitive::kPrimLong), value_(value) {}
2080
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002081 const int64_t value_;
2082
David Brazdil8d5b8b22015-03-24 10:51:52 +00002083 friend class HGraph;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002084 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2085};
2086
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002087enum class Intrinsics {
2088#define OPTIMIZING_INTRINSICS(Name, IsStatic) k ## Name,
2089#include "intrinsics_list.h"
2090 kNone,
2091 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
2092#undef INTRINSICS_LIST
2093#undef OPTIMIZING_INTRINSICS
2094};
2095std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
2096
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002097class HInvoke : public HInstruction {
2098 public:
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002099 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002100
2101 // Runtime needs to walk the stack, so Dex -> Dex calls need to
2102 // know their environment.
Calin Juravle77520bc2015-01-12 18:45:46 +00002103 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002104
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01002105 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002106 SetRawInputAt(index, argument);
2107 }
2108
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002109 Primitive::Type GetType() const OVERRIDE { return return_type_; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002110
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002111 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002112
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002113 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
2114
2115 Intrinsics GetIntrinsic() {
2116 return intrinsic_;
2117 }
2118
2119 void SetIntrinsic(Intrinsics intrinsic) {
2120 intrinsic_ = intrinsic;
2121 }
2122
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002123 DECLARE_INSTRUCTION(Invoke);
2124
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002125 protected:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002126 HInvoke(ArenaAllocator* arena,
2127 uint32_t number_of_arguments,
2128 Primitive::Type return_type,
2129 uint32_t dex_pc,
2130 uint32_t dex_method_index)
2131 : HInstruction(SideEffects::All()),
2132 inputs_(arena, number_of_arguments),
2133 return_type_(return_type),
2134 dex_pc_(dex_pc),
2135 dex_method_index_(dex_method_index),
2136 intrinsic_(Intrinsics::kNone) {
2137 inputs_.SetSize(number_of_arguments);
2138 }
2139
David Brazdil1abb4192015-02-17 18:33:36 +00002140 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
2141 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
2142 inputs_.Put(index, input);
2143 }
2144
2145 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002146 const Primitive::Type return_type_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002147 const uint32_t dex_pc_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002148 const uint32_t dex_method_index_;
2149 Intrinsics intrinsic_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002150
2151 private:
2152 DISALLOW_COPY_AND_ASSIGN(HInvoke);
2153};
2154
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002155class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002156 public:
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002157 HInvokeStaticOrDirect(ArenaAllocator* arena,
2158 uint32_t number_of_arguments,
2159 Primitive::Type return_type,
2160 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002161 uint32_t dex_method_index,
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002162 bool is_recursive,
Nicolas Geoffray79041292015-03-26 10:05:54 +00002163 InvokeType original_invoke_type,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002164 InvokeType invoke_type)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002165 : HInvoke(arena, number_of_arguments, return_type, dex_pc, dex_method_index),
Nicolas Geoffray79041292015-03-26 10:05:54 +00002166 original_invoke_type_(original_invoke_type),
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002167 invoke_type_(invoke_type),
2168 is_recursive_(is_recursive) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002169
Calin Juravle77520bc2015-01-12 18:45:46 +00002170 bool CanDoImplicitNullCheck() const OVERRIDE {
2171 // We access the method via the dex cache so we can't do an implicit null check.
2172 // TODO: for intrinsics we can generate implicit null checks.
2173 return false;
2174 }
2175
Nicolas Geoffray79041292015-03-26 10:05:54 +00002176 InvokeType GetOriginalInvokeType() const { return original_invoke_type_; }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002177 InvokeType GetInvokeType() const { return invoke_type_; }
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002178 bool IsRecursive() const { return is_recursive_; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002179 bool NeedsDexCache() const OVERRIDE { return !IsRecursive(); }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002180
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002181 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002182
2183 private:
Nicolas Geoffray79041292015-03-26 10:05:54 +00002184 const InvokeType original_invoke_type_;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002185 const InvokeType invoke_type_;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002186 const bool is_recursive_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002187
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002188 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002189};
2190
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002191class HInvokeVirtual : public HInvoke {
2192 public:
2193 HInvokeVirtual(ArenaAllocator* arena,
2194 uint32_t number_of_arguments,
2195 Primitive::Type return_type,
2196 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002197 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002198 uint32_t vtable_index)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002199 : HInvoke(arena, number_of_arguments, return_type, dex_pc, dex_method_index),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002200 vtable_index_(vtable_index) {}
2201
Calin Juravle77520bc2015-01-12 18:45:46 +00002202 bool CanDoImplicitNullCheck() const OVERRIDE {
2203 // TODO: Add implicit null checks in intrinsics.
2204 return !GetLocations()->Intrinsified();
2205 }
2206
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002207 uint32_t GetVTableIndex() const { return vtable_index_; }
2208
2209 DECLARE_INSTRUCTION(InvokeVirtual);
2210
2211 private:
2212 const uint32_t vtable_index_;
2213
2214 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
2215};
2216
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002217class HInvokeInterface : public HInvoke {
2218 public:
2219 HInvokeInterface(ArenaAllocator* arena,
2220 uint32_t number_of_arguments,
2221 Primitive::Type return_type,
2222 uint32_t dex_pc,
2223 uint32_t dex_method_index,
2224 uint32_t imt_index)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002225 : HInvoke(arena, number_of_arguments, return_type, dex_pc, dex_method_index),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002226 imt_index_(imt_index) {}
2227
Calin Juravle77520bc2015-01-12 18:45:46 +00002228 bool CanDoImplicitNullCheck() const OVERRIDE {
2229 // TODO: Add implicit null checks in intrinsics.
2230 return !GetLocations()->Intrinsified();
2231 }
2232
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002233 uint32_t GetImtIndex() const { return imt_index_; }
2234 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
2235
2236 DECLARE_INSTRUCTION(InvokeInterface);
2237
2238 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002239 const uint32_t imt_index_;
2240
2241 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
2242};
2243
Dave Allison20dfc792014-06-16 20:44:29 -07002244class HNewInstance : public HExpression<0> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002245 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002246 HNewInstance(uint32_t dex_pc, uint16_t type_index, QuickEntrypointEnum entrypoint)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002247 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2248 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002249 type_index_(type_index),
2250 entrypoint_(entrypoint) {}
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002251
2252 uint32_t GetDexPc() const { return dex_pc_; }
2253 uint16_t GetTypeIndex() const { return type_index_; }
2254
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002255 // Calls runtime so needs an environment.
Calin Juravle92a6ed22014-12-02 18:58:03 +00002256 bool NeedsEnvironment() const OVERRIDE { return true; }
2257 // It may throw when called on:
2258 // - interfaces
2259 // - abstract/innaccessible/unknown classes
2260 // TODO: optimize when possible.
2261 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002262
Calin Juravle10e244f2015-01-26 18:54:32 +00002263 bool CanBeNull() const OVERRIDE { return false; }
2264
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002265 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
2266
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002267 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002268
2269 private:
2270 const uint32_t dex_pc_;
2271 const uint16_t type_index_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002272 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002273
2274 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
2275};
2276
Roland Levillain88cb1752014-10-20 16:36:47 +01002277class HNeg : public HUnaryOperation {
2278 public:
2279 explicit HNeg(Primitive::Type result_type, HInstruction* input)
2280 : HUnaryOperation(result_type, input) {}
2281
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002282 int32_t Evaluate(int32_t x) const OVERRIDE { return -x; }
2283 int64_t Evaluate(int64_t x) const OVERRIDE { return -x; }
Roland Levillain9240d6a2014-10-20 16:47:04 +01002284
Roland Levillain88cb1752014-10-20 16:36:47 +01002285 DECLARE_INSTRUCTION(Neg);
2286
2287 private:
2288 DISALLOW_COPY_AND_ASSIGN(HNeg);
2289};
2290
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002291class HNewArray : public HExpression<1> {
2292 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002293 HNewArray(HInstruction* length,
2294 uint32_t dex_pc,
2295 uint16_t type_index,
2296 QuickEntrypointEnum entrypoint)
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002297 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2298 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002299 type_index_(type_index),
2300 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002301 SetRawInputAt(0, length);
2302 }
2303
2304 uint32_t GetDexPc() const { return dex_pc_; }
2305 uint16_t GetTypeIndex() const { return type_index_; }
2306
2307 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00002308 bool NeedsEnvironment() const OVERRIDE { return true; }
2309
Mingyao Yang0c365e62015-03-31 15:09:29 -07002310 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
2311 bool CanThrow() const OVERRIDE { return true; }
2312
Calin Juravle10e244f2015-01-26 18:54:32 +00002313 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002314
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002315 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
2316
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002317 DECLARE_INSTRUCTION(NewArray);
2318
2319 private:
2320 const uint32_t dex_pc_;
2321 const uint16_t type_index_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002322 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002323
2324 DISALLOW_COPY_AND_ASSIGN(HNewArray);
2325};
2326
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002327class HAdd : public HBinaryOperation {
2328 public:
2329 HAdd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2330 : HBinaryOperation(result_type, left, right) {}
2331
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002332 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002333
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002334 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002335 return x + y;
2336 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002337 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002338 return x + y;
2339 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002340
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002341 DECLARE_INSTRUCTION(Add);
2342
2343 private:
2344 DISALLOW_COPY_AND_ASSIGN(HAdd);
2345};
2346
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002347class HSub : public HBinaryOperation {
2348 public:
2349 HSub(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2350 : HBinaryOperation(result_type, left, right) {}
2351
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002352 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002353 return x - y;
2354 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002355 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002356 return x - y;
2357 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002358
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002359 DECLARE_INSTRUCTION(Sub);
2360
2361 private:
2362 DISALLOW_COPY_AND_ASSIGN(HSub);
2363};
2364
Calin Juravle34bacdf2014-10-07 20:23:36 +01002365class HMul : public HBinaryOperation {
2366 public:
2367 HMul(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2368 : HBinaryOperation(result_type, left, right) {}
2369
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002370 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002371
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002372 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x * y; }
2373 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x * y; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002374
2375 DECLARE_INSTRUCTION(Mul);
2376
2377 private:
2378 DISALLOW_COPY_AND_ASSIGN(HMul);
2379};
2380
Calin Juravle7c4954d2014-10-28 16:57:40 +00002381class HDiv : public HBinaryOperation {
2382 public:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002383 HDiv(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
2384 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00002385
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002386 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00002387 // Our graph structure ensures we never have 0 for `y` during constant folding.
2388 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00002389 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00002390 return (y == -1) ? -x : x / y;
2391 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002392
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002393 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002394 DCHECK_NE(y, 0);
2395 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2396 return (y == -1) ? -x : x / y;
2397 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002398
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002399 uint32_t GetDexPc() const { return dex_pc_; }
2400
Calin Juravle7c4954d2014-10-28 16:57:40 +00002401 DECLARE_INSTRUCTION(Div);
2402
2403 private:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002404 const uint32_t dex_pc_;
2405
Calin Juravle7c4954d2014-10-28 16:57:40 +00002406 DISALLOW_COPY_AND_ASSIGN(HDiv);
2407};
2408
Calin Juravlebacfec32014-11-14 15:54:36 +00002409class HRem : public HBinaryOperation {
2410 public:
2411 HRem(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
2412 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
2413
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002414 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002415 DCHECK_NE(y, 0);
2416 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2417 return (y == -1) ? 0 : x % y;
2418 }
2419
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002420 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002421 DCHECK_NE(y, 0);
2422 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2423 return (y == -1) ? 0 : x % y;
2424 }
2425
2426 uint32_t GetDexPc() const { return dex_pc_; }
2427
2428 DECLARE_INSTRUCTION(Rem);
2429
2430 private:
2431 const uint32_t dex_pc_;
2432
2433 DISALLOW_COPY_AND_ASSIGN(HRem);
2434};
2435
Calin Juravled0d48522014-11-04 16:40:20 +00002436class HDivZeroCheck : public HExpression<1> {
2437 public:
2438 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
2439 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
2440 SetRawInputAt(0, value);
2441 }
2442
2443 bool CanBeMoved() const OVERRIDE { return true; }
2444
2445 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2446 UNUSED(other);
2447 return true;
2448 }
2449
2450 bool NeedsEnvironment() const OVERRIDE { return true; }
2451 bool CanThrow() const OVERRIDE { return true; }
2452
2453 uint32_t GetDexPc() const { return dex_pc_; }
2454
2455 DECLARE_INSTRUCTION(DivZeroCheck);
2456
2457 private:
2458 const uint32_t dex_pc_;
2459
2460 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
2461};
2462
Calin Juravle9aec02f2014-11-18 23:06:35 +00002463class HShl : public HBinaryOperation {
2464 public:
2465 HShl(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2466 : HBinaryOperation(result_type, left, right) {}
2467
2468 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x << (y & kMaxIntShiftValue); }
2469 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x << (y & kMaxLongShiftValue); }
2470
2471 DECLARE_INSTRUCTION(Shl);
2472
2473 private:
2474 DISALLOW_COPY_AND_ASSIGN(HShl);
2475};
2476
2477class HShr : public HBinaryOperation {
2478 public:
2479 HShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2480 : HBinaryOperation(result_type, left, right) {}
2481
2482 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x >> (y & kMaxIntShiftValue); }
2483 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x >> (y & kMaxLongShiftValue); }
2484
2485 DECLARE_INSTRUCTION(Shr);
2486
2487 private:
2488 DISALLOW_COPY_AND_ASSIGN(HShr);
2489};
2490
2491class HUShr : public HBinaryOperation {
2492 public:
2493 HUShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2494 : HBinaryOperation(result_type, left, right) {}
2495
2496 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
2497 uint32_t ux = static_cast<uint32_t>(x);
2498 uint32_t uy = static_cast<uint32_t>(y) & kMaxIntShiftValue;
2499 return static_cast<int32_t>(ux >> uy);
2500 }
2501
2502 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
2503 uint64_t ux = static_cast<uint64_t>(x);
2504 uint64_t uy = static_cast<uint64_t>(y) & kMaxLongShiftValue;
2505 return static_cast<int64_t>(ux >> uy);
2506 }
2507
2508 DECLARE_INSTRUCTION(UShr);
2509
2510 private:
2511 DISALLOW_COPY_AND_ASSIGN(HUShr);
2512};
2513
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002514class HAnd : public HBinaryOperation {
2515 public:
2516 HAnd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2517 : HBinaryOperation(result_type, left, right) {}
2518
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002519 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002520
2521 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x & y; }
2522 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x & y; }
2523
2524 DECLARE_INSTRUCTION(And);
2525
2526 private:
2527 DISALLOW_COPY_AND_ASSIGN(HAnd);
2528};
2529
2530class HOr : public HBinaryOperation {
2531 public:
2532 HOr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2533 : HBinaryOperation(result_type, left, right) {}
2534
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002535 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002536
2537 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x | y; }
2538 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x | y; }
2539
2540 DECLARE_INSTRUCTION(Or);
2541
2542 private:
2543 DISALLOW_COPY_AND_ASSIGN(HOr);
2544};
2545
2546class HXor : public HBinaryOperation {
2547 public:
2548 HXor(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2549 : HBinaryOperation(result_type, left, right) {}
2550
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002551 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002552
2553 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x ^ y; }
2554 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x ^ y; }
2555
2556 DECLARE_INSTRUCTION(Xor);
2557
2558 private:
2559 DISALLOW_COPY_AND_ASSIGN(HXor);
2560};
2561
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002562// The value of a parameter in this method. Its location depends on
2563// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07002564class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002565 public:
Calin Juravle10e244f2015-01-26 18:54:32 +00002566 HParameterValue(uint8_t index, Primitive::Type parameter_type, bool is_this = false)
2567 : HExpression(parameter_type, SideEffects::None()), index_(index), is_this_(is_this) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002568
2569 uint8_t GetIndex() const { return index_; }
2570
Calin Juravle10e244f2015-01-26 18:54:32 +00002571 bool CanBeNull() const OVERRIDE { return !is_this_; }
2572
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002573 DECLARE_INSTRUCTION(ParameterValue);
2574
2575 private:
2576 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00002577 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002578 const uint8_t index_;
2579
Calin Juravle10e244f2015-01-26 18:54:32 +00002580 // Whether or not the parameter value corresponds to 'this' argument.
2581 const bool is_this_;
2582
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002583 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
2584};
2585
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002586class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002587 public:
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002588 explicit HNot(Primitive::Type result_type, HInstruction* input)
2589 : HUnaryOperation(result_type, input) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002590
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002591 bool CanBeMoved() const OVERRIDE { return true; }
2592 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002593 UNUSED(other);
2594 return true;
2595 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002596
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002597 int32_t Evaluate(int32_t x) const OVERRIDE { return ~x; }
2598 int64_t Evaluate(int64_t x) const OVERRIDE { return ~x; }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002599
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002600 DECLARE_INSTRUCTION(Not);
2601
2602 private:
2603 DISALLOW_COPY_AND_ASSIGN(HNot);
2604};
2605
Roland Levillaindff1f282014-11-05 14:15:05 +00002606class HTypeConversion : public HExpression<1> {
2607 public:
2608 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00002609 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
2610 : HExpression(result_type, SideEffects::None()), dex_pc_(dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002611 SetRawInputAt(0, input);
2612 DCHECK_NE(input->GetType(), result_type);
2613 }
2614
2615 HInstruction* GetInput() const { return InputAt(0); }
2616 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
2617 Primitive::Type GetResultType() const { return GetType(); }
2618
Roland Levillain624279f2014-12-04 11:54:28 +00002619 // Required by the x86 and ARM code generators when producing calls
2620 // to the runtime.
2621 uint32_t GetDexPc() const { return dex_pc_; }
2622
Roland Levillaindff1f282014-11-05 14:15:05 +00002623 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00002624 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00002625
2626 DECLARE_INSTRUCTION(TypeConversion);
2627
2628 private:
Roland Levillain624279f2014-12-04 11:54:28 +00002629 const uint32_t dex_pc_;
2630
Roland Levillaindff1f282014-11-05 14:15:05 +00002631 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
2632};
2633
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00002634static constexpr uint32_t kNoRegNumber = -1;
2635
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002636class HPhi : public HInstruction {
2637 public:
2638 HPhi(ArenaAllocator* arena, uint32_t reg_number, size_t number_of_inputs, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002639 : HInstruction(SideEffects::None()),
2640 inputs_(arena, number_of_inputs),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002641 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002642 type_(type),
Calin Juravle10e244f2015-01-26 18:54:32 +00002643 is_live_(false),
2644 can_be_null_(true) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002645 inputs_.SetSize(number_of_inputs);
2646 }
2647
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00002648 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
2649 static Primitive::Type ToPhiType(Primitive::Type type) {
2650 switch (type) {
2651 case Primitive::kPrimBoolean:
2652 case Primitive::kPrimByte:
2653 case Primitive::kPrimShort:
2654 case Primitive::kPrimChar:
2655 return Primitive::kPrimInt;
2656 default:
2657 return type;
2658 }
2659 }
2660
Calin Juravle10e244f2015-01-26 18:54:32 +00002661 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002662
2663 void AddInput(HInstruction* input);
2664
Calin Juravle10e244f2015-01-26 18:54:32 +00002665 Primitive::Type GetType() const OVERRIDE { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002666 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002667
Calin Juravle10e244f2015-01-26 18:54:32 +00002668 bool CanBeNull() const OVERRIDE { return can_be_null_; }
2669 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
2670
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002671 uint32_t GetRegNumber() const { return reg_number_; }
2672
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002673 void SetDead() { is_live_ = false; }
2674 void SetLive() { is_live_ = true; }
2675 bool IsDead() const { return !is_live_; }
2676 bool IsLive() const { return is_live_; }
2677
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002678 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002679
David Brazdil1abb4192015-02-17 18:33:36 +00002680 protected:
2681 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
2682
2683 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
2684 inputs_.Put(index, input);
2685 }
2686
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002687 private:
David Brazdil1abb4192015-02-17 18:33:36 +00002688 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002689 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002690 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002691 bool is_live_;
Calin Juravle10e244f2015-01-26 18:54:32 +00002692 bool can_be_null_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002693
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002694 DISALLOW_COPY_AND_ASSIGN(HPhi);
2695};
2696
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002697class HNullCheck : public HExpression<1> {
2698 public:
2699 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002700 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002701 SetRawInputAt(0, value);
2702 }
2703
Calin Juravle10e244f2015-01-26 18:54:32 +00002704 bool CanBeMoved() const OVERRIDE { return true; }
2705 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002706 UNUSED(other);
2707 return true;
2708 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002709
Calin Juravle10e244f2015-01-26 18:54:32 +00002710 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002711
Calin Juravle10e244f2015-01-26 18:54:32 +00002712 bool CanThrow() const OVERRIDE { return true; }
2713
2714 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01002715
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002716 uint32_t GetDexPc() const { return dex_pc_; }
2717
2718 DECLARE_INSTRUCTION(NullCheck);
2719
2720 private:
2721 const uint32_t dex_pc_;
2722
2723 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
2724};
2725
2726class FieldInfo : public ValueObject {
2727 public:
Calin Juravle52c48962014-12-16 17:02:57 +00002728 FieldInfo(MemberOffset field_offset, Primitive::Type field_type, bool is_volatile)
2729 : field_offset_(field_offset), field_type_(field_type), is_volatile_(is_volatile) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002730
2731 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002732 Primitive::Type GetFieldType() const { return field_type_; }
Calin Juravle52c48962014-12-16 17:02:57 +00002733 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002734
2735 private:
2736 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01002737 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00002738 const bool is_volatile_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002739};
2740
2741class HInstanceFieldGet : public HExpression<1> {
2742 public:
2743 HInstanceFieldGet(HInstruction* value,
2744 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00002745 MemberOffset field_offset,
2746 bool is_volatile)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002747 : HExpression(field_type, SideEffects::DependsOnSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00002748 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002749 SetRawInputAt(0, value);
2750 }
2751
Calin Juravle10c9cbe2014-12-19 10:50:19 +00002752 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002753
2754 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2755 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
2756 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002757 }
2758
Calin Juravle77520bc2015-01-12 18:45:46 +00002759 bool CanDoImplicitNullCheck() const OVERRIDE {
2760 return GetFieldOffset().Uint32Value() < kPageSize;
2761 }
2762
2763 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002764 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
2765 }
2766
Calin Juravle52c48962014-12-16 17:02:57 +00002767 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002768 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002769 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002770 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002771
2772 DECLARE_INSTRUCTION(InstanceFieldGet);
2773
2774 private:
2775 const FieldInfo field_info_;
2776
2777 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
2778};
2779
2780class HInstanceFieldSet : public HTemplateInstruction<2> {
2781 public:
2782 HInstanceFieldSet(HInstruction* object,
2783 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01002784 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 : HTemplateInstruction(SideEffects::ChangesSomething()),
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, object);
2790 SetRawInputAt(1, value);
2791 }
2792
Calin Juravle77520bc2015-01-12 18:45:46 +00002793 bool CanDoImplicitNullCheck() const OVERRIDE {
2794 return GetFieldOffset().Uint32Value() < kPageSize;
2795 }
2796
Calin Juravle52c48962014-12-16 17:02:57 +00002797 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002798 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002799 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002800 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002801 HInstruction* GetValue() const { return InputAt(1); }
2802
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002803 DECLARE_INSTRUCTION(InstanceFieldSet);
2804
2805 private:
2806 const FieldInfo field_info_;
2807
2808 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
2809};
2810
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002811class HArrayGet : public HExpression<2> {
2812 public:
2813 HArrayGet(HInstruction* array, HInstruction* index, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002814 : HExpression(type, SideEffects::DependsOnSomething()) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002815 SetRawInputAt(0, array);
2816 SetRawInputAt(1, index);
2817 }
2818
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002819 bool CanBeMoved() const OVERRIDE { return true; }
2820 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002821 UNUSED(other);
2822 return true;
2823 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002824 bool CanDoImplicitNullCheck() const OVERRIDE {
2825 // TODO: We can be smarter here.
2826 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
2827 // which generates the implicit null check. There are cases when these can be removed
2828 // to produce better code. If we ever add optimizations to do so we should allow an
2829 // implicit check here (as long as the address falls in the first page).
2830 return false;
2831 }
2832
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002833 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002834
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002835 HInstruction* GetArray() const { return InputAt(0); }
2836 HInstruction* GetIndex() const { return InputAt(1); }
2837
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002838 DECLARE_INSTRUCTION(ArrayGet);
2839
2840 private:
2841 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
2842};
2843
2844class HArraySet : public HTemplateInstruction<3> {
2845 public:
2846 HArraySet(HInstruction* array,
2847 HInstruction* index,
2848 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002849 Primitive::Type expected_component_type,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002850 uint32_t dex_pc)
2851 : HTemplateInstruction(SideEffects::ChangesSomething()),
2852 dex_pc_(dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002853 expected_component_type_(expected_component_type),
2854 needs_type_check_(value->GetType() == Primitive::kPrimNot) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002855 SetRawInputAt(0, array);
2856 SetRawInputAt(1, index);
2857 SetRawInputAt(2, value);
2858 }
2859
Calin Juravle77520bc2015-01-12 18:45:46 +00002860 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002861 // We currently always call a runtime method to catch array store
2862 // exceptions.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002863 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002864 }
2865
Calin Juravle77520bc2015-01-12 18:45:46 +00002866 bool CanDoImplicitNullCheck() const OVERRIDE {
2867 // TODO: Same as for ArrayGet.
2868 return false;
2869 }
2870
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002871 void ClearNeedsTypeCheck() {
2872 needs_type_check_ = false;
2873 }
2874
2875 bool NeedsTypeCheck() const { return needs_type_check_; }
2876
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002877 uint32_t GetDexPc() const { return dex_pc_; }
2878
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002879 HInstruction* GetArray() const { return InputAt(0); }
2880 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002881 HInstruction* GetValue() const { return InputAt(2); }
2882
2883 Primitive::Type GetComponentType() const {
2884 // The Dex format does not type floating point index operations. Since the
2885 // `expected_component_type_` is set during building and can therefore not
2886 // be correct, we also check what is the value type. If it is a floating
2887 // point type, we must use that type.
2888 Primitive::Type value_type = GetValue()->GetType();
2889 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
2890 ? value_type
2891 : expected_component_type_;
2892 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002893
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002894 DECLARE_INSTRUCTION(ArraySet);
2895
2896 private:
2897 const uint32_t dex_pc_;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002898 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002899 bool needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002900
2901 DISALLOW_COPY_AND_ASSIGN(HArraySet);
2902};
2903
2904class HArrayLength : public HExpression<1> {
2905 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002906 explicit HArrayLength(HInstruction* array)
2907 : HExpression(Primitive::kPrimInt, SideEffects::None()) {
2908 // Note that arrays do not change length, so the instruction does not
2909 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002910 SetRawInputAt(0, array);
2911 }
2912
Calin Juravle77520bc2015-01-12 18:45:46 +00002913 bool CanBeMoved() const OVERRIDE { return true; }
2914 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002915 UNUSED(other);
2916 return true;
2917 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002918 bool CanDoImplicitNullCheck() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002919
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002920 DECLARE_INSTRUCTION(ArrayLength);
2921
2922 private:
2923 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
2924};
2925
2926class HBoundsCheck : public HExpression<2> {
2927 public:
2928 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002929 : HExpression(index->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002930 DCHECK(index->GetType() == Primitive::kPrimInt);
2931 SetRawInputAt(0, index);
2932 SetRawInputAt(1, length);
2933 }
2934
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002935 bool CanBeMoved() const OVERRIDE { return true; }
2936 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002937 UNUSED(other);
2938 return true;
2939 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002940
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002941 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002942
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002943 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01002944
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002945 uint32_t GetDexPc() const { return dex_pc_; }
2946
2947 DECLARE_INSTRUCTION(BoundsCheck);
2948
2949 private:
2950 const uint32_t dex_pc_;
2951
2952 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
2953};
2954
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002955/**
2956 * Some DEX instructions are folded into multiple HInstructions that need
2957 * to stay live until the last HInstruction. This class
2958 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00002959 * HInstruction stays live. `index` represents the stack location index of the
2960 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002961 */
2962class HTemporary : public HTemplateInstruction<0> {
2963 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002964 explicit HTemporary(size_t index) : HTemplateInstruction(SideEffects::None()), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002965
2966 size_t GetIndex() const { return index_; }
2967
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00002968 Primitive::Type GetType() const OVERRIDE {
2969 // The previous instruction is the one that will be stored in the temporary location.
2970 DCHECK(GetPrevious() != nullptr);
2971 return GetPrevious()->GetType();
2972 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00002973
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002974 DECLARE_INSTRUCTION(Temporary);
2975
2976 private:
2977 const size_t index_;
2978
2979 DISALLOW_COPY_AND_ASSIGN(HTemporary);
2980};
2981
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002982class HSuspendCheck : public HTemplateInstruction<0> {
2983 public:
2984 explicit HSuspendCheck(uint32_t dex_pc)
Nicolas Geoffray9ebc72c2014-09-25 16:33:42 +01002985 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002986
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002987 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002988 return true;
2989 }
2990
2991 uint32_t GetDexPc() const { return dex_pc_; }
2992
2993 DECLARE_INSTRUCTION(SuspendCheck);
2994
2995 private:
2996 const uint32_t dex_pc_;
2997
2998 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
2999};
3000
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003001/**
3002 * Instruction to load a Class object.
3003 */
3004class HLoadClass : public HExpression<0> {
3005 public:
3006 HLoadClass(uint16_t type_index,
3007 bool is_referrers_class,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003008 uint32_t dex_pc)
3009 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3010 type_index_(type_index),
3011 is_referrers_class_(is_referrers_class),
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003012 dex_pc_(dex_pc),
Calin Juravleb1498f62015-02-16 13:13:29 +00003013 generate_clinit_check_(false),
3014 loaded_class_rti_(ReferenceTypeInfo::CreateTop(/* is_exact */ false)) {}
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003015
3016 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003017
3018 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3019 return other->AsLoadClass()->type_index_ == type_index_;
3020 }
3021
3022 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
3023
3024 uint32_t GetDexPc() const { return dex_pc_; }
3025 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003026 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003027
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003028 bool NeedsEnvironment() const OVERRIDE {
3029 // Will call runtime and load the class if the class is not loaded yet.
3030 // TODO: finer grain decision.
3031 return !is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003032 }
3033
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003034 bool MustGenerateClinitCheck() const {
3035 return generate_clinit_check_;
3036 }
3037
3038 void SetMustGenerateClinitCheck() {
3039 generate_clinit_check_ = true;
3040 }
3041
3042 bool CanCallRuntime() const {
3043 return MustGenerateClinitCheck() || !is_referrers_class_;
3044 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003045
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003046 bool CanThrow() const OVERRIDE {
3047 // May call runtime and and therefore can throw.
3048 // TODO: finer grain decision.
3049 return !is_referrers_class_;
3050 }
3051
Calin Juravleacf735c2015-02-12 15:25:22 +00003052 ReferenceTypeInfo GetLoadedClassRTI() {
3053 return loaded_class_rti_;
3054 }
3055
3056 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
3057 // Make sure we only set exact types (the loaded class should never be merged).
3058 DCHECK(rti.IsExact());
3059 loaded_class_rti_ = rti;
3060 }
3061
3062 bool IsResolved() {
3063 return loaded_class_rti_.IsExact();
3064 }
3065
Nicolas Geoffray9437b782015-03-25 10:08:51 +00003066 bool NeedsDexCache() const OVERRIDE { return !is_referrers_class_; }
3067
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003068 DECLARE_INSTRUCTION(LoadClass);
3069
3070 private:
3071 const uint16_t type_index_;
3072 const bool is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003073 const uint32_t dex_pc_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003074 // Whether this instruction must generate the initialization check.
3075 // Used for code generation.
3076 bool generate_clinit_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003077
Calin Juravleacf735c2015-02-12 15:25:22 +00003078 ReferenceTypeInfo loaded_class_rti_;
3079
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003080 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
3081};
3082
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003083class HLoadString : public HExpression<0> {
3084 public:
3085 HLoadString(uint32_t string_index, uint32_t dex_pc)
3086 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3087 string_index_(string_index),
3088 dex_pc_(dex_pc) {}
3089
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003090 bool CanBeMoved() const OVERRIDE { return true; }
3091
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003092 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3093 return other->AsLoadString()->string_index_ == string_index_;
3094 }
3095
3096 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
3097
3098 uint32_t GetDexPc() const { return dex_pc_; }
3099 uint32_t GetStringIndex() const { return string_index_; }
3100
3101 // TODO: Can we deopt or debug when we resolve a string?
3102 bool NeedsEnvironment() const OVERRIDE { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00003103 bool NeedsDexCache() const OVERRIDE { return true; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003104
3105 DECLARE_INSTRUCTION(LoadString);
3106
3107 private:
3108 const uint32_t string_index_;
3109 const uint32_t dex_pc_;
3110
3111 DISALLOW_COPY_AND_ASSIGN(HLoadString);
3112};
3113
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003114// TODO: Pass this check to HInvokeStaticOrDirect nodes.
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003115/**
3116 * Performs an initialization check on its Class object input.
3117 */
3118class HClinitCheck : public HExpression<1> {
3119 public:
3120 explicit HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Nicolas Geoffraya0466e12015-03-27 15:00:40 +00003121 : HExpression(Primitive::kPrimNot, SideEffects::ChangesSomething()),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003122 dex_pc_(dex_pc) {
3123 SetRawInputAt(0, constant);
3124 }
3125
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003126 bool CanBeMoved() const OVERRIDE { return true; }
3127 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3128 UNUSED(other);
3129 return true;
3130 }
3131
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003132 bool NeedsEnvironment() const OVERRIDE {
3133 // May call runtime to initialize the class.
3134 return true;
3135 }
3136
3137 uint32_t GetDexPc() const { return dex_pc_; }
3138
3139 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
3140
3141 DECLARE_INSTRUCTION(ClinitCheck);
3142
3143 private:
3144 const uint32_t dex_pc_;
3145
3146 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
3147};
3148
3149class HStaticFieldGet : public HExpression<1> {
3150 public:
3151 HStaticFieldGet(HInstruction* cls,
3152 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003153 MemberOffset field_offset,
3154 bool is_volatile)
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003155 : HExpression(field_type, SideEffects::DependsOnSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00003156 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003157 SetRawInputAt(0, cls);
3158 }
3159
Calin Juravle52c48962014-12-16 17:02:57 +00003160
Calin Juravle10c9cbe2014-12-19 10:50:19 +00003161 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003162
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003163 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00003164 HStaticFieldGet* other_get = other->AsStaticFieldGet();
3165 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003166 }
3167
3168 size_t ComputeHashCode() const OVERRIDE {
3169 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
3170 }
3171
Calin Juravle52c48962014-12-16 17:02:57 +00003172 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003173 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
3174 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003175 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003176
3177 DECLARE_INSTRUCTION(StaticFieldGet);
3178
3179 private:
3180 const FieldInfo field_info_;
3181
3182 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
3183};
3184
3185class HStaticFieldSet : public HTemplateInstruction<2> {
3186 public:
3187 HStaticFieldSet(HInstruction* cls,
3188 HInstruction* value,
3189 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003190 MemberOffset field_offset,
3191 bool is_volatile)
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003192 : HTemplateInstruction(SideEffects::ChangesSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00003193 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003194 SetRawInputAt(0, cls);
3195 SetRawInputAt(1, value);
3196 }
3197
Calin Juravle52c48962014-12-16 17:02:57 +00003198 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003199 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
3200 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003201 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003202
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003203 HInstruction* GetValue() const { return InputAt(1); }
3204
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003205 DECLARE_INSTRUCTION(StaticFieldSet);
3206
3207 private:
3208 const FieldInfo field_info_;
3209
3210 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
3211};
3212
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003213// Implement the move-exception DEX instruction.
3214class HLoadException : public HExpression<0> {
3215 public:
3216 HLoadException() : HExpression(Primitive::kPrimNot, SideEffects::None()) {}
3217
3218 DECLARE_INSTRUCTION(LoadException);
3219
3220 private:
3221 DISALLOW_COPY_AND_ASSIGN(HLoadException);
3222};
3223
3224class HThrow : public HTemplateInstruction<1> {
3225 public:
3226 HThrow(HInstruction* exception, uint32_t dex_pc)
3227 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {
3228 SetRawInputAt(0, exception);
3229 }
3230
3231 bool IsControlFlow() const OVERRIDE { return true; }
3232
3233 bool NeedsEnvironment() const OVERRIDE { return true; }
3234
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003235 bool CanThrow() const OVERRIDE { return true; }
3236
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003237 uint32_t GetDexPc() const { return dex_pc_; }
3238
3239 DECLARE_INSTRUCTION(Throw);
3240
3241 private:
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003242 const uint32_t dex_pc_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003243
3244 DISALLOW_COPY_AND_ASSIGN(HThrow);
3245};
3246
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003247class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003248 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003249 HInstanceOf(HInstruction* object,
3250 HLoadClass* constant,
3251 bool class_is_final,
3252 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003253 : HExpression(Primitive::kPrimBoolean, SideEffects::None()),
3254 class_is_final_(class_is_final),
3255 dex_pc_(dex_pc) {
3256 SetRawInputAt(0, object);
3257 SetRawInputAt(1, constant);
3258 }
3259
3260 bool CanBeMoved() const OVERRIDE { return true; }
3261
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003262 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003263 return true;
3264 }
3265
3266 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003267 return false;
3268 }
3269
3270 uint32_t GetDexPc() const { return dex_pc_; }
3271
3272 bool IsClassFinal() const { return class_is_final_; }
3273
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003274 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003275
3276 private:
3277 const bool class_is_final_;
3278 const uint32_t dex_pc_;
3279
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003280 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
3281};
3282
Calin Juravleb1498f62015-02-16 13:13:29 +00003283class HBoundType : public HExpression<1> {
3284 public:
3285 HBoundType(HInstruction* input, ReferenceTypeInfo bound_type)
3286 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3287 bound_type_(bound_type) {
Calin Juravle61d544b2015-02-23 16:46:57 +00003288 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00003289 SetRawInputAt(0, input);
3290 }
3291
3292 const ReferenceTypeInfo& GetBoundType() const { return bound_type_; }
3293
3294 bool CanBeNull() const OVERRIDE {
3295 // `null instanceof ClassX` always return false so we can't be null.
3296 return false;
3297 }
3298
3299 DECLARE_INSTRUCTION(BoundType);
3300
3301 private:
3302 // Encodes the most upper class that this instruction can have. In other words
3303 // it is always the case that GetBoundType().IsSupertypeOf(GetReferenceType()).
3304 // It is used to bound the type in cases like `if (x instanceof ClassX) {}`
3305 const ReferenceTypeInfo bound_type_;
3306
3307 DISALLOW_COPY_AND_ASSIGN(HBoundType);
3308};
3309
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003310class HCheckCast : public HTemplateInstruction<2> {
3311 public:
3312 HCheckCast(HInstruction* object,
3313 HLoadClass* constant,
3314 bool class_is_final,
3315 uint32_t dex_pc)
3316 : HTemplateInstruction(SideEffects::None()),
3317 class_is_final_(class_is_final),
3318 dex_pc_(dex_pc) {
3319 SetRawInputAt(0, object);
3320 SetRawInputAt(1, constant);
3321 }
3322
3323 bool CanBeMoved() const OVERRIDE { return true; }
3324
3325 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
3326 return true;
3327 }
3328
3329 bool NeedsEnvironment() const OVERRIDE {
3330 // Instruction may throw a CheckCastError.
3331 return true;
3332 }
3333
3334 bool CanThrow() const OVERRIDE { return true; }
3335
3336 uint32_t GetDexPc() const { return dex_pc_; }
3337
3338 bool IsClassFinal() const { return class_is_final_; }
3339
3340 DECLARE_INSTRUCTION(CheckCast);
3341
3342 private:
3343 const bool class_is_final_;
3344 const uint32_t dex_pc_;
3345
3346 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003347};
3348
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003349class HMonitorOperation : public HTemplateInstruction<1> {
3350 public:
3351 enum OperationKind {
3352 kEnter,
3353 kExit,
3354 };
3355
3356 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
3357 : HTemplateInstruction(SideEffects::None()), kind_(kind), dex_pc_(dex_pc) {
3358 SetRawInputAt(0, object);
3359 }
3360
3361 // Instruction may throw a Java exception, so we need an environment.
3362 bool NeedsEnvironment() const OVERRIDE { return true; }
3363 bool CanThrow() const OVERRIDE { return true; }
3364
3365 uint32_t GetDexPc() const { return dex_pc_; }
3366
3367 bool IsEnter() const { return kind_ == kEnter; }
3368
3369 DECLARE_INSTRUCTION(MonitorOperation);
3370
Calin Juravle52c48962014-12-16 17:02:57 +00003371 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003372 const OperationKind kind_;
3373 const uint32_t dex_pc_;
3374
3375 private:
3376 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
3377};
3378
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003379class MoveOperands : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003380 public:
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003381 MoveOperands(Location source, Location destination, HInstruction* instruction)
3382 : source_(source), destination_(destination), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003383
3384 Location GetSource() const { return source_; }
3385 Location GetDestination() const { return destination_; }
3386
3387 void SetSource(Location value) { source_ = value; }
3388 void SetDestination(Location value) { destination_ = value; }
3389
3390 // The parallel move resolver marks moves as "in-progress" by clearing the
3391 // destination (but not the source).
3392 Location MarkPending() {
3393 DCHECK(!IsPending());
3394 Location dest = destination_;
3395 destination_ = Location::NoLocation();
3396 return dest;
3397 }
3398
3399 void ClearPending(Location dest) {
3400 DCHECK(IsPending());
3401 destination_ = dest;
3402 }
3403
3404 bool IsPending() const {
3405 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
3406 return destination_.IsInvalid() && !source_.IsInvalid();
3407 }
3408
3409 // True if this blocks a move from the given location.
3410 bool Blocks(Location loc) const {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003411 return !IsEliminated() && (source_.Contains(loc) || loc.Contains(source_));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003412 }
3413
3414 // A move is redundant if it's been eliminated, if its source and
3415 // destination are the same, or if its destination is unneeded.
3416 bool IsRedundant() const {
3417 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
3418 }
3419
3420 // We clear both operands to indicate move that's been eliminated.
3421 void Eliminate() {
3422 source_ = destination_ = Location::NoLocation();
3423 }
3424
3425 bool IsEliminated() const {
3426 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
3427 return source_.IsInvalid();
3428 }
3429
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003430 HInstruction* GetInstruction() const { return instruction_; }
3431
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003432 private:
3433 Location source_;
3434 Location destination_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003435 // The instruction this move is assocatied with. Null when this move is
3436 // for moving an input in the expected locations of user (including a phi user).
3437 // This is only used in debug mode, to ensure we do not connect interval siblings
3438 // in the same parallel move.
3439 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003440};
3441
3442static constexpr size_t kDefaultNumberOfMoves = 4;
3443
3444class HParallelMove : public HTemplateInstruction<0> {
3445 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003446 explicit HParallelMove(ArenaAllocator* arena)
3447 : HTemplateInstruction(SideEffects::None()), moves_(arena, kDefaultNumberOfMoves) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003448
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003449 void AddMove(Location source, Location destination, HInstruction* instruction) {
3450 DCHECK(source.IsValid());
3451 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003452 if (kIsDebugBuild) {
3453 if (instruction != nullptr) {
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00003454 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003455 if (moves_.Get(i).GetInstruction() == instruction) {
3456 // Special case the situation where the move is for the spill slot
3457 // of the instruction.
3458 if ((GetPrevious() == instruction)
3459 || ((GetPrevious() == nullptr)
3460 && instruction->IsPhi()
3461 && instruction->GetBlock() == GetBlock())) {
3462 DCHECK_NE(destination.GetKind(), moves_.Get(i).GetDestination().GetKind())
3463 << "Doing parallel moves for the same instruction.";
3464 } else {
3465 DCHECK(false) << "Doing parallel moves for the same instruction.";
3466 }
3467 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00003468 }
3469 }
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003470 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
3471 DCHECK(!destination.Equals(moves_.Get(i).GetDestination()))
3472 << "Same destination for two moves in a parallel move.";
3473 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003474 }
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003475 moves_.Add(MoveOperands(source, destination, instruction));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003476 }
3477
3478 MoveOperands* MoveOperandsAt(size_t index) const {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003479 return moves_.GetRawStorage() + index;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003480 }
3481
3482 size_t NumMoves() const { return moves_.Size(); }
3483
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003484 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003485
3486 private:
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003487 GrowableArray<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003488
3489 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
3490};
3491
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003492class HGraphVisitor : public ValueObject {
3493 public:
Dave Allison20dfc792014-06-16 20:44:29 -07003494 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
3495 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003496
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003497 virtual void VisitInstruction(HInstruction* instruction) { UNUSED(instruction); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003498 virtual void VisitBasicBlock(HBasicBlock* block);
3499
Roland Levillain633021e2014-10-01 14:12:25 +01003500 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003501 void VisitInsertionOrder();
3502
Roland Levillain633021e2014-10-01 14:12:25 +01003503 // Visit the graph following dominator tree reverse post-order.
3504 void VisitReversePostOrder();
3505
Nicolas Geoffray787c3072014-03-17 10:20:19 +00003506 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00003507
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003508 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003509#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003510 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
3511
3512 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
3513
3514#undef DECLARE_VISIT_INSTRUCTION
3515
3516 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07003517 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003518
3519 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
3520};
3521
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003522class HGraphDelegateVisitor : public HGraphVisitor {
3523 public:
3524 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
3525 virtual ~HGraphDelegateVisitor() {}
3526
3527 // Visit functions that delegate to to super class.
3528#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003529 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003530
3531 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
3532
3533#undef DECLARE_VISIT_INSTRUCTION
3534
3535 private:
3536 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
3537};
3538
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003539class HInsertionOrderIterator : public ValueObject {
3540 public:
3541 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
3542
3543 bool Done() const { return index_ == graph_.GetBlocks().Size(); }
3544 HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); }
3545 void Advance() { ++index_; }
3546
3547 private:
3548 const HGraph& graph_;
3549 size_t index_;
3550
3551 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
3552};
3553
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003554class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003555 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00003556 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
3557 // Check that reverse post order of the graph has been built.
3558 DCHECK(!graph.GetReversePostOrder().IsEmpty());
3559 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003560
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003561 bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); }
3562 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003563 void Advance() { ++index_; }
3564
3565 private:
3566 const HGraph& graph_;
3567 size_t index_;
3568
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003569 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003570};
3571
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003572class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003573 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003574 explicit HPostOrderIterator(const HGraph& graph)
David Brazdil10f56cb2015-03-24 18:49:14 +00003575 : graph_(graph), index_(graph_.GetReversePostOrder().Size()) {
3576 // Check that reverse post order of the graph has been built.
3577 DCHECK(!graph.GetReversePostOrder().IsEmpty());
3578 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003579
3580 bool Done() const { return index_ == 0; }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003581 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003582 void Advance() { --index_; }
3583
3584 private:
3585 const HGraph& graph_;
3586 size_t index_;
3587
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003588 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003589};
3590
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003591// Iterator over the blocks that art part of the loop. Includes blocks part
3592// of an inner loop. The order in which the blocks are iterated is on their
3593// block id.
3594class HBlocksInLoopIterator : public ValueObject {
3595 public:
3596 explicit HBlocksInLoopIterator(const HLoopInformation& info)
3597 : blocks_in_loop_(info.GetBlocks()),
3598 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
3599 index_(0) {
3600 if (!blocks_in_loop_.IsBitSet(index_)) {
3601 Advance();
3602 }
3603 }
3604
3605 bool Done() const { return index_ == blocks_.Size(); }
3606 HBasicBlock* Current() const { return blocks_.Get(index_); }
3607 void Advance() {
3608 ++index_;
3609 for (size_t e = blocks_.Size(); index_ < e; ++index_) {
3610 if (blocks_in_loop_.IsBitSet(index_)) {
3611 break;
3612 }
3613 }
3614 }
3615
3616 private:
3617 const BitVector& blocks_in_loop_;
3618 const GrowableArray<HBasicBlock*>& blocks_;
3619 size_t index_;
3620
3621 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
3622};
3623
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003624inline int64_t Int64FromConstant(HConstant* constant) {
3625 DCHECK(constant->IsIntConstant() || constant->IsLongConstant());
3626 return constant->IsIntConstant() ? constant->AsIntConstant()->GetValue()
3627 : constant->AsLongConstant()->GetValue();
3628}
3629
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003630} // namespace art
3631
3632#endif // ART_COMPILER_OPTIMIZING_NODES_H_