blob: 4bca9aaf82e95d9ca944aeb932dd9d46f5bf3a8e [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),
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100115 linear_order_(arena, kDefaultNumberOfBlocks),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700116 entry_block_(nullptr),
117 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100118 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100119 number_of_vregs_(0),
120 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000121 temporaries_vreg_slots_(0),
Mingyao Yange4335eb2015-03-02 15:14:13 -0800122 has_array_accesses_(false),
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000123 debuggable_(debuggable),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000124 current_instruction_id_(start_instruction_id),
125 cached_null_constant_(nullptr),
126 cached_int_constants_(std::less<int32_t>(), arena->Adapter()),
127 cached_long_constants_(std::less<int64_t>(), arena->Adapter()) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000128
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000129 ArenaAllocator* GetArena() const { return arena_; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100130 const GrowableArray<HBasicBlock*>& GetBlocks() const { return blocks_; }
Roland Levillain93445682014-10-06 19:24:02 +0100131 HBasicBlock* GetBlock(size_t id) const { return blocks_.Get(id); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000132
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000133 HBasicBlock* GetEntryBlock() const { return entry_block_; }
134 HBasicBlock* GetExitBlock() const { return exit_block_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000135
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000136 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
137 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000138
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000139 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100140
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000141 // Try building the SSA form of this graph, with dominance computation and loop
142 // recognition. Returns whether it was successful in doing all these steps.
143 bool TryBuildingSsa() {
144 BuildDominatorTree();
Nicolas Geoffrayd3350832015-03-12 11:16:23 +0000145 // The SSA builder requires loops to all be natural. Specifically, the dead phi
146 // elimination phase checks the consistency of the graph when doing a post-order
147 // visit for eliminating dead phis: a dead phi can only have loop header phi
148 // users remaining when being visited.
149 if (!AnalyzeNaturalLoops()) return false;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000150 TransformToSsa();
Nicolas Geoffrayd3350832015-03-12 11:16:23 +0000151 return true;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000152 }
153
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000154 void BuildDominatorTree();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000155 void TransformToSsa();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100156 void SimplifyCFG();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000157
Nicolas Geoffrayf5370122014-12-02 11:51:19 +0000158 // Analyze all natural loops in this graph. Returns false if one
159 // loop is not natural, that is the header does not dominate the
160 // back edge.
161 bool AnalyzeNaturalLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100162
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000163 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
164 void InlineInto(HGraph* outer_graph, HInvoke* invoke);
165
David Brazdil46e2a392015-03-16 17:31:52 +0000166 void MergeEmptyBranches(HBasicBlock* start_block, HBasicBlock* end_block);
167
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100168 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
169 void SimplifyLoop(HBasicBlock* header);
170
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000171 int32_t GetNextInstructionId() {
172 DCHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000173 return current_instruction_id_++;
174 }
175
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000176 int32_t GetCurrentInstructionId() const {
177 return current_instruction_id_;
178 }
179
180 void SetCurrentInstructionId(int32_t id) {
181 current_instruction_id_ = id;
182 }
183
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100184 uint16_t GetMaximumNumberOfOutVRegs() const {
185 return maximum_number_of_out_vregs_;
186 }
187
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000188 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
189 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100190 }
191
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000192 void UpdateTemporariesVRegSlots(size_t slots) {
193 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100194 }
195
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000196 size_t GetTemporariesVRegSlots() const {
197 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100198 }
199
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100200 void SetNumberOfVRegs(uint16_t number_of_vregs) {
201 number_of_vregs_ = number_of_vregs;
202 }
203
204 uint16_t GetNumberOfVRegs() const {
205 return number_of_vregs_;
206 }
207
208 void SetNumberOfInVRegs(uint16_t value) {
209 number_of_in_vregs_ = value;
210 }
211
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100212 uint16_t GetNumberOfLocalVRegs() const {
213 return number_of_vregs_ - number_of_in_vregs_;
214 }
215
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100216 const GrowableArray<HBasicBlock*>& GetReversePostOrder() const {
217 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100218 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100219
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100220 const GrowableArray<HBasicBlock*>& GetLinearOrder() const {
221 return linear_order_;
222 }
223
Mingyao Yange4335eb2015-03-02 15:14:13 -0800224 bool HasArrayAccesses() const {
225 return has_array_accesses_;
226 }
227
228 void SetHasArrayAccesses(bool value) {
229 has_array_accesses_ = value;
230 }
231
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000232 bool IsDebuggable() const { return debuggable_; }
233
David Brazdil8d5b8b22015-03-24 10:51:52 +0000234 // Returns a constant of the given type and value. If it does not exist
235 // already, it is created and inserted into the graph. Only integral types
236 // are currently supported.
237 HConstant* GetConstant(Primitive::Type type, int64_t value);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000238 HNullConstant* GetNullConstant();
David Brazdil8d5b8b22015-03-24 10:51:52 +0000239 HIntConstant* GetIntConstant(int32_t value) {
240 return CreateConstant(value, &cached_int_constants_);
241 }
242 HLongConstant* GetLongConstant(int64_t value) {
243 return CreateConstant(value, &cached_long_constants_);
244 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000245
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000246 private:
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000247 HBasicBlock* FindCommonDominator(HBasicBlock* first, HBasicBlock* second) const;
248 void VisitBlockForDominatorTree(HBasicBlock* block,
249 HBasicBlock* predecessor,
250 GrowableArray<size_t>* visits);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100251 void FindBackEdges(ArenaBitVector* visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000252 void VisitBlockForBackEdges(HBasicBlock* block,
253 ArenaBitVector* visited,
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100254 ArenaBitVector* visiting);
Roland Levillainfc600dc2014-12-02 17:16:31 +0000255 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100256 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000257
David Brazdil8d5b8b22015-03-24 10:51:52 +0000258 template <class InstType, typename ValueType>
259 InstType* CreateConstant(ValueType value, ArenaSafeMap<ValueType, InstType*>* cache);
260 void InsertConstant(HConstant* instruction);
261
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000262 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000263
264 // List of blocks in insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000265 GrowableArray<HBasicBlock*> blocks_;
266
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100267 // List of blocks to perform a reverse post order tree traversal.
268 GrowableArray<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000269
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100270 // List of blocks to perform a linear order tree traversal.
271 GrowableArray<HBasicBlock*> linear_order_;
272
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000273 HBasicBlock* entry_block_;
274 HBasicBlock* exit_block_;
275
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100276 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100277 uint16_t maximum_number_of_out_vregs_;
278
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100279 // The number of virtual registers in this method. Contains the parameters.
280 uint16_t number_of_vregs_;
281
282 // The number of virtual registers used by parameters of this method.
283 uint16_t number_of_in_vregs_;
284
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000285 // Number of vreg size slots that the temporaries use (used in baseline compiler).
286 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100287
Mingyao Yange4335eb2015-03-02 15:14:13 -0800288 // Has array accesses. We can totally skip BCE if it's false.
289 bool has_array_accesses_;
290
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000291 // Indicates whether the graph should be compiled in a way that
292 // ensures full debuggability. If false, we can apply more
293 // aggressive optimizations that may limit the level of debugging.
294 const bool debuggable_;
295
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000296 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000297 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000298
David Brazdil46e2a392015-03-16 17:31:52 +0000299 // Cached common constants often needed by optimization passes.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000300 HNullConstant* cached_null_constant_;
301 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
302 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000303
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100304 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000305 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000306 DISALLOW_COPY_AND_ASSIGN(HGraph);
307};
308
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700309class HLoopInformation : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000310 public:
311 HLoopInformation(HBasicBlock* header, HGraph* graph)
312 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100313 suspend_check_(nullptr),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100314 back_edges_(graph->GetArena(), kDefaultNumberOfBackEdges),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100315 // Make bit vector growable, as the number of blocks may change.
316 blocks_(graph->GetArena(), graph->GetBlocks().Size(), true) {}
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100317
318 HBasicBlock* GetHeader() const {
319 return header_;
320 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000321
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000322 void SetHeader(HBasicBlock* block) {
323 header_ = block;
324 }
325
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100326 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
327 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
328 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
329
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000330 void AddBackEdge(HBasicBlock* back_edge) {
331 back_edges_.Add(back_edge);
332 }
333
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100334 void RemoveBackEdge(HBasicBlock* back_edge) {
335 back_edges_.Delete(back_edge);
336 }
337
David Brazdil46e2a392015-03-16 17:31:52 +0000338 bool IsBackEdge(const HBasicBlock& block) const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100339 for (size_t i = 0, e = back_edges_.Size(); i < e; ++i) {
David Brazdil46e2a392015-03-16 17:31:52 +0000340 if (back_edges_.Get(i) == &block) return true;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100341 }
342 return false;
343 }
344
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000345 size_t NumberOfBackEdges() const {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000346 return back_edges_.Size();
347 }
348
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100349 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100350
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100351 const GrowableArray<HBasicBlock*>& GetBackEdges() const {
352 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100353 }
354
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100355 void ClearBackEdges() {
356 back_edges_.Reset();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100357 }
358
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100359 // Find blocks that are part of this loop. Returns whether the loop is a natural loop,
360 // that is the header dominates the back edge.
361 bool Populate();
362
363 // Returns whether this loop information contains `block`.
364 // Note that this loop information *must* be populated before entering this function.
365 bool Contains(const HBasicBlock& block) const;
366
367 // Returns whether this loop information is an inner loop of `other`.
368 // Note that `other` *must* be populated before entering this function.
369 bool IsIn(const HLoopInformation& other) const;
370
371 const ArenaBitVector& GetBlocks() const { return blocks_; }
372
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000373 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000374 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000375
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000376 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100377 // Internal recursive implementation of `Populate`.
378 void PopulateRecursive(HBasicBlock* block);
379
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000380 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100381 HSuspendCheck* suspend_check_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000382 GrowableArray<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100383 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000384
385 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
386};
387
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100388static constexpr size_t kNoLifetime = -1;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100389static constexpr uint32_t kNoDexPc = -1;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100390
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000391// A block in a method. Contains the list of instructions represented
392// as a double linked list. Each block knows its predecessors and
393// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100394
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700395class HBasicBlock : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000396 public:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100397 explicit HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000398 : graph_(graph),
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000399 predecessors_(graph->GetArena(), kDefaultNumberOfPredecessors),
400 successors_(graph->GetArena(), kDefaultNumberOfSuccessors),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000401 loop_information_(nullptr),
402 dominator_(nullptr),
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100403 dominated_blocks_(graph->GetArena(), kDefaultNumberOfDominatedBlocks),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100404 block_id_(-1),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100405 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100406 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000407 lifetime_end_(kNoLifetime),
408 is_catch_block_(false) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000409
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100410 const GrowableArray<HBasicBlock*>& GetPredecessors() const {
411 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000412 }
413
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100414 const GrowableArray<HBasicBlock*>& GetSuccessors() const {
415 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000416 }
417
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100418 const GrowableArray<HBasicBlock*>& GetDominatedBlocks() const {
419 return dominated_blocks_;
420 }
421
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100422 bool IsEntryBlock() const {
423 return graph_->GetEntryBlock() == this;
424 }
425
426 bool IsExitBlock() const {
427 return graph_->GetExitBlock() == this;
428 }
429
David Brazdil46e2a392015-03-16 17:31:52 +0000430 bool IsSingleGoto() const;
431
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000432 void AddBackEdge(HBasicBlock* back_edge) {
433 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000434 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000435 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100436 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000437 loop_information_->AddBackEdge(back_edge);
438 }
439
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000440 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000441 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000442
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000443 int GetBlockId() const { return block_id_; }
444 void SetBlockId(int id) { block_id_ = id; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000445
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000446 HBasicBlock* GetDominator() const { return dominator_; }
447 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100448 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.Add(block); }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000449 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
450 for (size_t i = 0, e = dominated_blocks_.Size(); i < e; ++i) {
451 if (dominated_blocks_.Get(i) == existing) {
452 dominated_blocks_.Put(i, new_block);
453 return;
454 }
455 }
456 LOG(FATAL) << "Unreachable";
457 UNREACHABLE();
458 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000459
460 int NumberOfBackEdges() const {
461 return loop_information_ == nullptr
462 ? 0
463 : loop_information_->NumberOfBackEdges();
464 }
465
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100466 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
467 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100468 const HInstructionList& GetInstructions() const { return instructions_; }
469 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100470 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000471
472 void AddSuccessor(HBasicBlock* block) {
473 successors_.Add(block);
474 block->predecessors_.Add(this);
475 }
476
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100477 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
478 size_t successor_index = GetSuccessorIndexOf(existing);
479 DCHECK_NE(successor_index, static_cast<size_t>(-1));
480 existing->RemovePredecessor(this);
481 new_block->predecessors_.Add(this);
482 successors_.Put(successor_index, new_block);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000483 }
484
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000485 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
486 size_t predecessor_index = GetPredecessorIndexOf(existing);
487 DCHECK_NE(predecessor_index, static_cast<size_t>(-1));
488 existing->RemoveSuccessor(this);
489 new_block->successors_.Add(this);
490 predecessors_.Put(predecessor_index, new_block);
491 }
492
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100493 void RemovePredecessor(HBasicBlock* block) {
494 predecessors_.Delete(block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100495 }
496
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000497 void RemoveSuccessor(HBasicBlock* block) {
498 successors_.Delete(block);
499 }
500
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100501 void ClearAllPredecessors() {
502 predecessors_.Reset();
503 }
504
505 void AddPredecessor(HBasicBlock* block) {
506 predecessors_.Add(block);
507 block->successors_.Add(this);
508 }
509
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100510 void SwapPredecessors() {
Nicolas Geoffrayc83d4412014-09-18 16:46:20 +0100511 DCHECK_EQ(predecessors_.Size(), 2u);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100512 HBasicBlock* temp = predecessors_.Get(0);
513 predecessors_.Put(0, predecessors_.Get(1));
514 predecessors_.Put(1, temp);
515 }
516
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100517 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) {
518 for (size_t i = 0, e = predecessors_.Size(); i < e; ++i) {
519 if (predecessors_.Get(i) == predecessor) {
520 return i;
521 }
522 }
523 return -1;
524 }
525
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100526 size_t GetSuccessorIndexOf(HBasicBlock* successor) {
527 for (size_t i = 0, e = successors_.Size(); i < e; ++i) {
528 if (successors_.Get(i) == successor) {
529 return i;
530 }
531 }
532 return -1;
533 }
534
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000535 // Split the block into two blocks just after `cursor`. Returns the newly
536 // created block. Note that this method just updates raw block information,
537 // like predecessors, successors, dominators, and instruction list. It does not
538 // update the graph, reverse post order, loop information, nor make sure the
539 // blocks are consistent (for example ending with a control flow instruction).
540 HBasicBlock* SplitAfter(HInstruction* cursor);
541
542 // Merge `other` at the end of `this`. Successors and dominated blocks of
543 // `other` are changed to be successors and dominated blocks of `this`. Note
544 // that this method does not update the graph, reverse post order, loop
545 // information, nor make sure the blocks are consistent (for example ending
546 // with a control flow instruction).
547 void MergeWith(HBasicBlock* other);
548
549 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
550 // of `this` are moved to `other`.
551 // Note that this method does not update the graph, reverse post order, loop
552 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +0000553 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000554 void ReplaceWith(HBasicBlock* other);
555
David Brazdil46e2a392015-03-16 17:31:52 +0000556 // Disconnects `this` from all its predecessors, successors and the dominator.
557 // It assumes that `this` does not dominate any blocks.
558 // Note that this method does not update the graph, reverse post order, loop
559 // information, nor make sure the blocks are consistent (for example ending
560 // with a control flow instruction).
561 void DisconnectFromAll();
562
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000563 void AddInstruction(HInstruction* instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100564 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +0100565 // Replace instruction `initial` with `replacement` within this block.
566 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
567 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100568 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100569 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +0000570 // RemoveInstruction and RemovePhi delete a given instruction from the respective
571 // instruction list. With 'ensure_safety' set to true, it verifies that the
572 // instruction is not in use and removes it from the use lists of its inputs.
573 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
574 void RemovePhi(HPhi* phi, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100575
576 bool IsLoopHeader() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100577 return (loop_information_ != nullptr) && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100578 }
579
Roland Levillain6b879dd2014-09-22 17:13:44 +0100580 bool IsLoopPreHeaderFirstPredecessor() const {
581 DCHECK(IsLoopHeader());
582 DCHECK(!GetPredecessors().IsEmpty());
583 return GetPredecessors().Get(0) == GetLoopInformation()->GetPreHeader();
584 }
585
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100586 HLoopInformation* GetLoopInformation() const {
587 return loop_information_;
588 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000589
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000590 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100591 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000592 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100593 void SetInLoop(HLoopInformation* info) {
594 if (IsLoopHeader()) {
595 // Nothing to do. This just means `info` is an outer loop.
596 } else if (loop_information_ == nullptr) {
597 loop_information_ = info;
598 } else if (loop_information_->Contains(*info->GetHeader())) {
599 // Block is currently part of an outer loop. Make it part of this inner loop.
600 // Note that a non loop header having a loop information means this loop information
601 // has already been populated
602 loop_information_ = info;
603 } else {
604 // Block is part of an inner loop. Do not update the loop information.
605 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
606 // at this point, because this method is being called while populating `info`.
607 }
608 }
609
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000610 // Raw update of the loop information.
611 void SetLoopInformation(HLoopInformation* info) {
612 loop_information_ = info;
613 }
614
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100615 bool IsInLoop() const { return loop_information_ != nullptr; }
616
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100617 // Returns wheter this block dominates the blocked passed as parameter.
618 bool Dominates(HBasicBlock* block) const;
619
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100620 size_t GetLifetimeStart() const { return lifetime_start_; }
621 size_t GetLifetimeEnd() const { return lifetime_end_; }
622
623 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
624 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
625
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100626 uint32_t GetDexPc() const { return dex_pc_; }
627
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000628 bool IsCatchBlock() const { return is_catch_block_; }
629 void SetIsCatchBlock() { is_catch_block_ = true; }
630
David Brazdil8d5b8b22015-03-24 10:51:52 +0000631 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +0000632 bool EndsWithIf() const;
633 bool HasSinglePhi() const;
634
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000635 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000636 HGraph* graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000637 GrowableArray<HBasicBlock*> predecessors_;
638 GrowableArray<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100639 HInstructionList instructions_;
640 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000641 HLoopInformation* loop_information_;
642 HBasicBlock* dominator_;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100643 GrowableArray<HBasicBlock*> dominated_blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000644 int block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100645 // The dex program counter of the first instruction of this block.
646 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100647 size_t lifetime_start_;
648 size_t lifetime_end_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000649 bool is_catch_block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000650
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000651 friend class HGraph;
652 friend class HInstruction;
653
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000654 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
655};
656
David Brazdilb2bd1c52015-03-25 11:17:37 +0000657// Iterates over the LoopInformation of all loops which contain 'block'
658// from the innermost to the outermost.
659class HLoopInformationOutwardIterator : public ValueObject {
660 public:
661 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
662 : current_(block.GetLoopInformation()) {}
663
664 bool Done() const { return current_ == nullptr; }
665
666 void Advance() {
667 DCHECK(!Done());
668 current_ = current_->GetHeader()->GetDominator()->GetLoopInformation();
669 }
670
671 HLoopInformation* Current() const {
672 DCHECK(!Done());
673 return current_;
674 }
675
676 private:
677 HLoopInformation* current_;
678
679 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
680};
681
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100682#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
683 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000684 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000685 M(ArrayGet, Instruction) \
686 M(ArrayLength, Instruction) \
687 M(ArraySet, Instruction) \
David Brazdil66d126e2015-04-03 16:02:44 +0100688 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000689 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +0000690 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000691 M(CheckCast, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100692 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000693 M(Compare, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100694 M(Condition, BinaryOperation) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700695 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000696 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +0000697 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000698 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100699 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000700 M(Exit, Instruction) \
701 M(FloatConstant, Constant) \
702 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100703 M(GreaterThan, Condition) \
704 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100705 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000706 M(InstanceFieldGet, Instruction) \
707 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000708 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100709 M(IntConstant, Constant) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +0000710 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000711 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100712 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000713 M(LessThan, Condition) \
714 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000715 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000716 M(LoadException, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100717 M(LoadLocal, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000718 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100719 M(Local, Instruction) \
720 M(LongConstant, Constant) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +0000721 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000722 M(Mul, BinaryOperation) \
723 M(Neg, UnaryOperation) \
724 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100725 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +0100726 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000727 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000728 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000729 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000730 M(Or, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100731 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000732 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100733 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000734 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100735 M(Return, Instruction) \
736 M(ReturnVoid, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000737 M(Shl, BinaryOperation) \
738 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100739 M(StaticFieldGet, Instruction) \
740 M(StaticFieldSet, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100741 M(StoreLocal, Instruction) \
742 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100743 M(SuspendCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000744 M(Temporary, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000745 M(Throw, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +0000746 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000747 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000748 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000749
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100750#define FOR_EACH_INSTRUCTION(M) \
751 FOR_EACH_CONCRETE_INSTRUCTION(M) \
752 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +0100753 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +0100754 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100755 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -0700756
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100757#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000758FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
759#undef FORWARD_DECLARATION
760
Roland Levillainccc07a92014-09-16 14:48:16 +0100761#define DECLARE_INSTRUCTION(type) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000762 InstructionKind GetKind() const OVERRIDE { return k##type; } \
763 const char* DebugName() const OVERRIDE { return #type; } \
764 const H##type* As##type() const OVERRIDE { return this; } \
765 H##type* As##type() OVERRIDE { return this; } \
766 bool InstructionTypeEquals(HInstruction* other) const OVERRIDE { \
Roland Levillainccc07a92014-09-16 14:48:16 +0100767 return other->Is##type(); \
768 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000769 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000770
David Brazdiled596192015-01-23 10:39:45 +0000771template <typename T> class HUseList;
772
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100773template <typename T>
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700774class HUseListNode : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000775 public:
David Brazdiled596192015-01-23 10:39:45 +0000776 HUseListNode* GetPrevious() const { return prev_; }
777 HUseListNode* GetNext() const { return next_; }
778 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100779 size_t GetIndex() const { return index_; }
780
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000781 private:
David Brazdiled596192015-01-23 10:39:45 +0000782 HUseListNode(T user, size_t index)
783 : user_(user), index_(index), prev_(nullptr), next_(nullptr) {}
784
785 T const user_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100786 const size_t index_;
David Brazdiled596192015-01-23 10:39:45 +0000787 HUseListNode<T>* prev_;
788 HUseListNode<T>* next_;
789
790 friend class HUseList<T>;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000791
792 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
793};
794
David Brazdiled596192015-01-23 10:39:45 +0000795template <typename T>
796class HUseList : public ValueObject {
797 public:
798 HUseList() : first_(nullptr) {}
799
800 void Clear() {
801 first_ = nullptr;
802 }
803
804 // Adds a new entry at the beginning of the use list and returns
805 // the newly created node.
806 HUseListNode<T>* AddUse(T user, size_t index, ArenaAllocator* arena) {
David Brazdilea55b932015-01-27 17:12:29 +0000807 HUseListNode<T>* new_node = new (arena) HUseListNode<T>(user, index);
David Brazdiled596192015-01-23 10:39:45 +0000808 if (IsEmpty()) {
809 first_ = new_node;
810 } else {
811 first_->prev_ = new_node;
812 new_node->next_ = first_;
813 first_ = new_node;
814 }
815 return new_node;
816 }
817
818 HUseListNode<T>* GetFirst() const {
819 return first_;
820 }
821
822 void Remove(HUseListNode<T>* node) {
David Brazdil1abb4192015-02-17 18:33:36 +0000823 DCHECK(node != nullptr);
824 DCHECK(Contains(node));
825
David Brazdiled596192015-01-23 10:39:45 +0000826 if (node->prev_ != nullptr) {
827 node->prev_->next_ = node->next_;
828 }
829 if (node->next_ != nullptr) {
830 node->next_->prev_ = node->prev_;
831 }
832 if (node == first_) {
833 first_ = node->next_;
834 }
835 }
836
David Brazdil1abb4192015-02-17 18:33:36 +0000837 bool Contains(const HUseListNode<T>* node) const {
838 if (node == nullptr) {
839 return false;
840 }
841 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
842 if (current == node) {
843 return true;
844 }
845 }
846 return false;
847 }
848
David Brazdiled596192015-01-23 10:39:45 +0000849 bool IsEmpty() const {
850 return first_ == nullptr;
851 }
852
853 bool HasOnlyOneUse() const {
854 return first_ != nullptr && first_->next_ == nullptr;
855 }
856
857 private:
858 HUseListNode<T>* first_;
859};
860
861template<typename T>
862class HUseIterator : public ValueObject {
863 public:
864 explicit HUseIterator(const HUseList<T>& uses) : current_(uses.GetFirst()) {}
865
866 bool Done() const { return current_ == nullptr; }
867
868 void Advance() {
869 DCHECK(!Done());
870 current_ = current_->GetNext();
871 }
872
873 HUseListNode<T>* Current() const {
874 DCHECK(!Done());
875 return current_;
876 }
877
878 private:
879 HUseListNode<T>* current_;
880
881 friend class HValue;
882};
883
David Brazdil1abb4192015-02-17 18:33:36 +0000884// This class is used by HEnvironment and HInstruction classes to record the
885// instructions they use and pointers to the corresponding HUseListNodes kept
886// by the used instructions.
887template <typename T>
888class HUserRecord : public ValueObject {
889 public:
890 HUserRecord() : instruction_(nullptr), use_node_(nullptr) {}
891 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), use_node_(nullptr) {}
892
893 HUserRecord(const HUserRecord<T>& old_record, HUseListNode<T>* use_node)
894 : instruction_(old_record.instruction_), use_node_(use_node) {
895 DCHECK(instruction_ != nullptr);
896 DCHECK(use_node_ != nullptr);
897 DCHECK(old_record.use_node_ == nullptr);
898 }
899
900 HInstruction* GetInstruction() const { return instruction_; }
901 HUseListNode<T>* GetUseNode() const { return use_node_; }
902
903 private:
904 // Instruction used by the user.
905 HInstruction* instruction_;
906
907 // Corresponding entry in the use list kept by 'instruction_'.
908 HUseListNode<T>* use_node_;
909};
910
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100911// Represents the side effects an instruction may have.
912class SideEffects : public ValueObject {
913 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100914 SideEffects() : flags_(0) {}
915
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100916 static SideEffects None() {
917 return SideEffects(0);
918 }
919
920 static SideEffects All() {
921 return SideEffects(ChangesSomething().flags_ | DependsOnSomething().flags_);
922 }
923
924 static SideEffects ChangesSomething() {
925 return SideEffects((1 << kFlagChangesCount) - 1);
926 }
927
928 static SideEffects DependsOnSomething() {
929 int count = kFlagDependsOnCount - kFlagChangesCount;
930 return SideEffects(((1 << count) - 1) << kFlagChangesCount);
931 }
932
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100933 SideEffects Union(SideEffects other) const {
934 return SideEffects(flags_ | other.flags_);
935 }
936
Roland Levillain72bceff2014-09-15 18:29:00 +0100937 bool HasSideEffects() const {
938 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
939 return (flags_ & all_bits_set) != 0;
940 }
941
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100942 bool HasAllSideEffects() const {
943 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
944 return all_bits_set == (flags_ & all_bits_set);
945 }
946
947 bool DependsOn(SideEffects other) const {
948 size_t depends_flags = other.ComputeDependsFlags();
949 return (flags_ & depends_flags) != 0;
950 }
951
952 bool HasDependencies() const {
953 int count = kFlagDependsOnCount - kFlagChangesCount;
954 size_t all_bits_set = (1 << count) - 1;
955 return ((flags_ >> kFlagChangesCount) & all_bits_set) != 0;
956 }
957
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100958 private:
959 static constexpr int kFlagChangesSomething = 0;
960 static constexpr int kFlagChangesCount = kFlagChangesSomething + 1;
961
962 static constexpr int kFlagDependsOnSomething = kFlagChangesCount;
963 static constexpr int kFlagDependsOnCount = kFlagDependsOnSomething + 1;
964
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100965 explicit SideEffects(size_t flags) : flags_(flags) {}
966
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100967 size_t ComputeDependsFlags() const {
968 return flags_ << kFlagChangesCount;
969 }
970
971 size_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100972};
973
David Brazdiled596192015-01-23 10:39:45 +0000974// A HEnvironment object contains the values of virtual registers at a given location.
975class HEnvironment : public ArenaObject<kArenaAllocMisc> {
976 public:
977 HEnvironment(ArenaAllocator* arena, size_t number_of_vregs)
978 : vregs_(arena, number_of_vregs) {
979 vregs_.SetSize(number_of_vregs);
980 for (size_t i = 0; i < number_of_vregs; i++) {
David Brazdil1abb4192015-02-17 18:33:36 +0000981 vregs_.Put(i, HUserRecord<HEnvironment*>());
David Brazdiled596192015-01-23 10:39:45 +0000982 }
983 }
984
985 void CopyFrom(HEnvironment* env);
986
987 void SetRawEnvAt(size_t index, HInstruction* instruction) {
David Brazdil1abb4192015-02-17 18:33:36 +0000988 vregs_.Put(index, HUserRecord<HEnvironment*>(instruction));
David Brazdiled596192015-01-23 10:39:45 +0000989 }
990
991 HInstruction* GetInstructionAt(size_t index) const {
David Brazdil1abb4192015-02-17 18:33:36 +0000992 return vregs_.Get(index).GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +0000993 }
994
David Brazdil1abb4192015-02-17 18:33:36 +0000995 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +0000996
997 size_t Size() const { return vregs_.Size(); }
998
999 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001000 // Record instructions' use entries of this environment for constant-time removal.
1001 // It should only be called by HInstruction when a new environment use is added.
1002 void RecordEnvUse(HUseListNode<HEnvironment*>* env_use) {
1003 DCHECK(env_use->GetUser() == this);
1004 size_t index = env_use->GetIndex();
1005 vregs_.Put(index, HUserRecord<HEnvironment*>(vregs_.Get(index), env_use));
1006 }
David Brazdiled596192015-01-23 10:39:45 +00001007
David Brazdil1abb4192015-02-17 18:33:36 +00001008 GrowableArray<HUserRecord<HEnvironment*> > vregs_;
David Brazdiled596192015-01-23 10:39:45 +00001009
David Brazdil1abb4192015-02-17 18:33:36 +00001010 friend HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001011
1012 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1013};
1014
Calin Juravleacf735c2015-02-12 15:25:22 +00001015class ReferenceTypeInfo : ValueObject {
1016 public:
Calin Juravleb1498f62015-02-16 13:13:29 +00001017 typedef Handle<mirror::Class> TypeHandle;
1018
1019 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact)
1020 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1021 if (type_handle->IsObjectClass()) {
1022 // Override the type handle to be consistent with the case when we get to
1023 // Top but don't have the Object class available. It avoids having to guess
1024 // what value the type_handle has when it's Top.
1025 return ReferenceTypeInfo(TypeHandle(), is_exact, true);
1026 } else {
1027 return ReferenceTypeInfo(type_handle, is_exact, false);
1028 }
1029 }
1030
1031 static ReferenceTypeInfo CreateTop(bool is_exact) {
1032 return ReferenceTypeInfo(TypeHandle(), is_exact, true);
Calin Juravleacf735c2015-02-12 15:25:22 +00001033 }
1034
1035 bool IsExact() const { return is_exact_; }
1036 bool IsTop() const { return is_top_; }
1037
1038 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
1039
Calin Juravleb1498f62015-02-16 13:13:29 +00001040 bool IsSupertypeOf(ReferenceTypeInfo rti) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Calin Juravleacf735c2015-02-12 15:25:22 +00001041 if (IsTop()) {
1042 // Top (equivalent for java.lang.Object) is supertype of anything.
1043 return true;
1044 }
1045 if (rti.IsTop()) {
1046 // If we get here `this` is not Top() so it can't be a supertype.
1047 return false;
1048 }
1049 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
1050 }
1051
1052 // Returns true if the type information provide the same amount of details.
1053 // Note that it does not mean that the instructions have the same actual type
1054 // (e.g. tops are equal but they can be the result of a merge).
1055 bool IsEqual(ReferenceTypeInfo rti) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1056 if (IsExact() != rti.IsExact()) {
1057 return false;
1058 }
1059 if (IsTop() && rti.IsTop()) {
1060 // `Top` means java.lang.Object, so the types are equivalent.
1061 return true;
1062 }
1063 if (IsTop() || rti.IsTop()) {
1064 // If only one is top or object than they are not equivalent.
1065 // NB: We need this extra check because the type_handle of `Top` is invalid
1066 // and we cannot inspect its reference.
1067 return false;
1068 }
1069
1070 // Finally check the types.
1071 return GetTypeHandle().Get() == rti.GetTypeHandle().Get();
1072 }
1073
1074 private:
Calin Juravleb1498f62015-02-16 13:13:29 +00001075 ReferenceTypeInfo() : ReferenceTypeInfo(TypeHandle(), false, true) {}
1076 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact, bool is_top)
1077 : type_handle_(type_handle), is_exact_(is_exact), is_top_(is_top) {}
1078
Calin Juravleacf735c2015-02-12 15:25:22 +00001079 // The class of the object.
Calin Juravleb1498f62015-02-16 13:13:29 +00001080 TypeHandle type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001081 // Whether or not the type is exact or a superclass of the actual type.
Calin Juravleb1498f62015-02-16 13:13:29 +00001082 // Whether or not we have any information about this type.
Calin Juravleacf735c2015-02-12 15:25:22 +00001083 bool is_exact_;
1084 // A true value here means that the object type should be java.lang.Object.
1085 // We don't have access to the corresponding mirror object every time so this
1086 // flag acts as a substitute. When true, the TypeHandle refers to a null
1087 // pointer and should not be used.
1088 bool is_top_;
1089};
1090
1091std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
1092
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001093class HInstruction : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001094 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001095 explicit HInstruction(SideEffects side_effects)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001096 : previous_(nullptr),
1097 next_(nullptr),
1098 block_(nullptr),
1099 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001100 ssa_index_(-1),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001101 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001102 locations_(nullptr),
1103 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001104 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001105 side_effects_(side_effects),
1106 reference_type_info_(ReferenceTypeInfo::CreateTop(/* is_exact */ false)) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001107
Dave Allison20dfc792014-06-16 20:44:29 -07001108 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001109
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001110#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001111 enum InstructionKind {
1112 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1113 };
1114#undef DECLARE_KIND
1115
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001116 HInstruction* GetNext() const { return next_; }
1117 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001118
Calin Juravle77520bc2015-01-12 18:45:46 +00001119 HInstruction* GetNextDisregardingMoves() const;
1120 HInstruction* GetPreviousDisregardingMoves() const;
1121
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001122 HBasicBlock* GetBlock() const { return block_; }
1123 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001124 bool IsInBlock() const { return block_ != nullptr; }
1125 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +01001126 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001127
Roland Levillain6b879dd2014-09-22 17:13:44 +01001128 virtual size_t InputCount() const = 0;
David Brazdil1abb4192015-02-17 18:33:36 +00001129 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001130
1131 virtual void Accept(HGraphVisitor* visitor) = 0;
1132 virtual const char* DebugName() const = 0;
1133
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001134 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
David Brazdil1abb4192015-02-17 18:33:36 +00001135 void SetRawInputAt(size_t index, HInstruction* input) {
1136 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1137 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001138
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001139 virtual bool NeedsEnvironment() const { return false; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001140 virtual bool IsControlFlow() const { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01001141 virtual bool CanThrow() const { return false; }
Roland Levillain72bceff2014-09-15 18:29:00 +01001142 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001143
Calin Juravle61d544b2015-02-23 16:46:57 +00001144 virtual bool ActAsNullConstant() const { return false; }
1145
Calin Juravle10e244f2015-01-26 18:54:32 +00001146 // Does not apply for all instructions, but having this at top level greatly
1147 // simplifies the null check elimination.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001148 virtual bool CanBeNull() const {
1149 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1150 return true;
1151 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001152
Calin Juravle77520bc2015-01-12 18:45:46 +00001153 virtual bool CanDoImplicitNullCheck() const { return false; }
1154
Calin Juravleacf735c2015-02-12 15:25:22 +00001155 void SetReferenceTypeInfo(ReferenceTypeInfo reference_type_info) {
Calin Juravle61d544b2015-02-23 16:46:57 +00001156 DCHECK_EQ(GetType(), Primitive::kPrimNot);
Calin Juravleacf735c2015-02-12 15:25:22 +00001157 reference_type_info_ = reference_type_info;
1158 }
1159
Calin Juravle61d544b2015-02-23 16:46:57 +00001160 ReferenceTypeInfo GetReferenceTypeInfo() const {
1161 DCHECK_EQ(GetType(), Primitive::kPrimNot);
1162 return reference_type_info_;
1163 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001164
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001165 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001166 DCHECK(user != nullptr);
1167 HUseListNode<HInstruction*>* use =
1168 uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1169 user->SetRawInputRecordAt(index, HUserRecord<HInstruction*>(user->InputRecordAt(index), use));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001170 }
1171
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001172 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001173 DCHECK(user != nullptr);
David Brazdiled596192015-01-23 10:39:45 +00001174 HUseListNode<HEnvironment*>* env_use =
1175 env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1176 user->RecordEnvUse(env_use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001177 }
1178
David Brazdil1abb4192015-02-17 18:33:36 +00001179 void RemoveAsUserOfInput(size_t input) {
1180 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
1181 input_use.GetInstruction()->uses_.Remove(input_use.GetUseNode());
1182 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001183
David Brazdil1abb4192015-02-17 18:33:36 +00001184 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1185 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001186
David Brazdiled596192015-01-23 10:39:45 +00001187 bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); }
1188 bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); }
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001189 bool HasNonEnvironmentUses() const { return !uses_.IsEmpty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001190 bool HasOnlyOneNonEnvironmentUse() const {
1191 return !HasEnvironmentUses() && GetUses().HasOnlyOneUse();
1192 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001193
Roland Levillain6c82d402014-10-13 16:10:27 +01001194 // Does this instruction strictly dominate `other_instruction`?
1195 // Returns false if this instruction and `other_instruction` are the same.
1196 // Aborts if this instruction and `other_instruction` are both phis.
1197 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001198
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001199 int GetId() const { return id_; }
1200 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001201
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001202 int GetSsaIndex() const { return ssa_index_; }
1203 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1204 bool HasSsaIndex() const { return ssa_index_ != -1; }
1205
1206 bool HasEnvironment() const { return environment_ != nullptr; }
1207 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001208 // Set the `environment_` field. Raw because this method does not
1209 // update the uses lists.
1210 void SetRawEnvironment(HEnvironment* environment) { environment_ = environment; }
1211
1212 // Set the environment of this instruction, copying it from `environment`. While
1213 // copying, the uses lists are being updated.
1214 void CopyEnvironmentFrom(HEnvironment* environment) {
1215 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
1216 environment_ = new (allocator) HEnvironment(allocator, environment->Size());
1217 environment_->CopyFrom(environment);
1218 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001219
Nicolas Geoffray39468442014-09-02 15:17:15 +01001220 // Returns the number of entries in the environment. Typically, that is the
1221 // number of dex registers in a method. It could be more in case of inlining.
1222 size_t EnvironmentSize() const;
1223
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001224 LocationSummary* GetLocations() const { return locations_; }
1225 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001226
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001227 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001228 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001229
Alexandre Rames188d4312015-04-09 18:30:21 +01001230 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
1231 // uses of this instruction by `other` are *not* updated.
1232 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
1233 ReplaceWith(other);
1234 other->ReplaceInput(this, use_index);
1235 }
1236
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001237 // Move `this` instruction before `cursor`.
1238 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001239
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001240#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01001241 bool Is##type() const { return (As##type() != nullptr); } \
1242 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001243 virtual H##type* As##type() { return nullptr; }
1244
1245 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
1246#undef INSTRUCTION_TYPE_CHECK
1247
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001248 // Returns whether the instruction can be moved within the graph.
1249 virtual bool CanBeMoved() const { return false; }
1250
1251 // Returns whether the two instructions are of the same kind.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001252 virtual bool InstructionTypeEquals(HInstruction* other) const {
1253 UNUSED(other);
1254 return false;
1255 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001256
1257 // Returns whether any data encoded in the two instructions is equal.
1258 // This method does not look at the inputs. Both instructions must be
1259 // of the same type, otherwise the method has undefined behavior.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001260 virtual bool InstructionDataEquals(HInstruction* other) const {
1261 UNUSED(other);
1262 return false;
1263 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001264
1265 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00001266 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001267 // 2) Their inputs are identical.
1268 bool Equals(HInstruction* other) const;
1269
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001270 virtual InstructionKind GetKind() const = 0;
1271
1272 virtual size_t ComputeHashCode() const {
1273 size_t result = GetKind();
1274 for (size_t i = 0, e = InputCount(); i < e; ++i) {
1275 result = (result * 31) + InputAt(i)->GetId();
1276 }
1277 return result;
1278 }
1279
1280 SideEffects GetSideEffects() const { return side_effects_; }
1281
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001282 size_t GetLifetimePosition() const { return lifetime_position_; }
1283 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
1284 LiveInterval* GetLiveInterval() const { return live_interval_; }
1285 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
1286 bool HasLiveInterval() const { return live_interval_ != nullptr; }
1287
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001288 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
1289
1290 // Returns whether the code generation of the instruction will require to have access
1291 // to the current method. Such instructions are:
1292 // (1): Instructions that require an environment, as calling the runtime requires
1293 // to walk the stack and have the current method stored at a specific stack address.
1294 // (2): Object literals like classes and strings, that are loaded from the dex cache
1295 // fields of the current method.
1296 bool NeedsCurrentMethod() const {
1297 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
1298 }
1299
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001300 virtual bool NeedsDexCache() const { return false; }
1301
David Brazdil1abb4192015-02-17 18:33:36 +00001302 protected:
1303 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
1304 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
1305
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001306 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001307 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); }
1308
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001309 HInstruction* previous_;
1310 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001311 HBasicBlock* block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001312
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001313 // An instruction gets an id when it is added to the graph.
1314 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01001315 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001316 int id_;
1317
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001318 // When doing liveness analysis, instructions that have uses get an SSA index.
1319 int ssa_index_;
1320
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001321 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00001322 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001323
1324 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00001325 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001326
Nicolas Geoffray39468442014-09-02 15:17:15 +01001327 // The environment associated with this instruction. Not null if the instruction
1328 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001329 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001330
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001331 // Set by the code generator.
1332 LocationSummary* locations_;
1333
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001334 // Set by the liveness analysis.
1335 LiveInterval* live_interval_;
1336
1337 // Set by the liveness analysis, this is the position in a linear
1338 // order of blocks where this instruction's live interval start.
1339 size_t lifetime_position_;
1340
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001341 const SideEffects side_effects_;
1342
Calin Juravleacf735c2015-02-12 15:25:22 +00001343 // TODO: for primitive types this should be marked as invalid.
1344 ReferenceTypeInfo reference_type_info_;
1345
David Brazdil1abb4192015-02-17 18:33:36 +00001346 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001347 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00001348 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001349 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001350 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001351
1352 DISALLOW_COPY_AND_ASSIGN(HInstruction);
1353};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001354std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001355
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001356class HInputIterator : public ValueObject {
1357 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001358 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001359
1360 bool Done() const { return index_ == instruction_->InputCount(); }
1361 HInstruction* Current() const { return instruction_->InputAt(index_); }
1362 void Advance() { index_++; }
1363
1364 private:
1365 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001366 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001367
1368 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
1369};
1370
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001371class HInstructionIterator : public ValueObject {
1372 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001373 explicit HInstructionIterator(const HInstructionList& instructions)
1374 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001375 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001376 }
1377
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001378 bool Done() const { return instruction_ == nullptr; }
1379 HInstruction* Current() const { return instruction_; }
1380 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001381 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001382 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001383 }
1384
1385 private:
1386 HInstruction* instruction_;
1387 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001388
1389 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001390};
1391
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001392class HBackwardInstructionIterator : public ValueObject {
1393 public:
1394 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
1395 : instruction_(instructions.last_instruction_) {
1396 next_ = Done() ? nullptr : instruction_->GetPrevious();
1397 }
1398
1399 bool Done() const { return instruction_ == nullptr; }
1400 HInstruction* Current() const { return instruction_; }
1401 void Advance() {
1402 instruction_ = next_;
1403 next_ = Done() ? nullptr : instruction_->GetPrevious();
1404 }
1405
1406 private:
1407 HInstruction* instruction_;
1408 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001409
1410 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001411};
1412
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001413// An embedded container with N elements of type T. Used (with partial
1414// specialization for N=0) because embedded arrays cannot have size 0.
1415template<typename T, intptr_t N>
1416class EmbeddedArray {
1417 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001418 EmbeddedArray() : elements_() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001419
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001420 intptr_t GetLength() const { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001421
1422 const T& operator[](intptr_t i) const {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001423 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001424 return elements_[i];
1425 }
1426
1427 T& operator[](intptr_t i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001428 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001429 return elements_[i];
1430 }
1431
1432 const T& At(intptr_t i) const {
1433 return (*this)[i];
1434 }
1435
1436 void SetAt(intptr_t i, const T& val) {
1437 (*this)[i] = val;
1438 }
1439
1440 private:
1441 T elements_[N];
1442};
1443
1444template<typename T>
1445class EmbeddedArray<T, 0> {
1446 public:
1447 intptr_t length() const { return 0; }
1448 const T& operator[](intptr_t i) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001449 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001450 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001451 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001452 }
1453 T& operator[](intptr_t i) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001454 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001455 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001456 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001457 }
1458};
1459
1460template<intptr_t N>
1461class HTemplateInstruction: public HInstruction {
1462 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001463 HTemplateInstruction<N>(SideEffects side_effects)
1464 : HInstruction(side_effects), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07001465 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001466
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001467 size_t InputCount() const OVERRIDE { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001468
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001469 protected:
David Brazdil1abb4192015-02-17 18:33:36 +00001470 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_[i]; }
1471
1472 void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE {
1473 inputs_[i] = input;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001474 }
1475
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001476 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001477 EmbeddedArray<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001478
1479 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001480};
1481
Dave Allison20dfc792014-06-16 20:44:29 -07001482template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001483class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07001484 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001485 HExpression<N>(Primitive::Type type, SideEffects side_effects)
1486 : HTemplateInstruction<N>(side_effects), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07001487 virtual ~HExpression() {}
1488
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001489 Primitive::Type GetType() const OVERRIDE { return type_; }
Dave Allison20dfc792014-06-16 20:44:29 -07001490
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001491 protected:
1492 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07001493};
1494
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001495// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
1496// instruction that branches to the exit block.
1497class HReturnVoid : public HTemplateInstruction<0> {
1498 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001499 HReturnVoid() : 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(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001504
1505 private:
1506 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
1507};
1508
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001509// Represents dex's RETURN opcodes. A HReturn is a control flow
1510// instruction that branches to the exit block.
1511class HReturn : public HTemplateInstruction<1> {
1512 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001513 explicit HReturn(HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001514 SetRawInputAt(0, value);
1515 }
1516
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001517 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001518
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001519 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001520
1521 private:
1522 DISALLOW_COPY_AND_ASSIGN(HReturn);
1523};
1524
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001525// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001526// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001527// exit block.
1528class HExit : public HTemplateInstruction<0> {
1529 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001530 HExit() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001531
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001532 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001533
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001534 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001535
1536 private:
1537 DISALLOW_COPY_AND_ASSIGN(HExit);
1538};
1539
1540// Jumps from one block to another.
1541class HGoto : public HTemplateInstruction<0> {
1542 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001543 HGoto() : HTemplateInstruction(SideEffects::None()) {}
1544
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001545 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001546
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001547 HBasicBlock* GetSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001548 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001549 }
1550
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001551 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001552
1553 private:
1554 DISALLOW_COPY_AND_ASSIGN(HGoto);
1555};
1556
Dave Allison20dfc792014-06-16 20:44:29 -07001557
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001558// Conditional branch. A block ending with an HIf instruction must have
1559// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001560class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001561 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001562 explicit HIf(HInstruction* input) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001563 SetRawInputAt(0, input);
1564 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001565
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001566 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001567
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001568 HBasicBlock* IfTrueSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001569 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001570 }
1571
1572 HBasicBlock* IfFalseSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001573 return GetBlock()->GetSuccessors().Get(1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001574 }
1575
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001576 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001577
1578 private:
1579 DISALLOW_COPY_AND_ASSIGN(HIf);
1580};
1581
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001582// Deoptimize to interpreter, upon checking a condition.
1583class HDeoptimize : public HTemplateInstruction<1> {
1584 public:
1585 HDeoptimize(HInstruction* cond, uint32_t dex_pc)
1586 : HTemplateInstruction(SideEffects::None()),
1587 dex_pc_(dex_pc) {
1588 SetRawInputAt(0, cond);
1589 }
1590
1591 bool NeedsEnvironment() const OVERRIDE { return true; }
1592 bool CanThrow() const OVERRIDE { return true; }
1593 uint32_t GetDexPc() const { return dex_pc_; }
1594
1595 DECLARE_INSTRUCTION(Deoptimize);
1596
1597 private:
1598 uint32_t dex_pc_;
1599
1600 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
1601};
1602
Roland Levillain88cb1752014-10-20 16:36:47 +01001603class HUnaryOperation : public HExpression<1> {
1604 public:
1605 HUnaryOperation(Primitive::Type result_type, HInstruction* input)
1606 : HExpression(result_type, SideEffects::None()) {
1607 SetRawInputAt(0, input);
1608 }
1609
1610 HInstruction* GetInput() const { return InputAt(0); }
1611 Primitive::Type GetResultType() const { return GetType(); }
1612
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001613 bool CanBeMoved() const OVERRIDE { return true; }
1614 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001615 UNUSED(other);
1616 return true;
1617 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001618
Roland Levillain9240d6a2014-10-20 16:47:04 +01001619 // Try to statically evaluate `operation` and return a HConstant
1620 // containing the result of this evaluation. If `operation` cannot
1621 // be evaluated as a constant, return nullptr.
1622 HConstant* TryStaticEvaluation() const;
1623
1624 // Apply this operation to `x`.
1625 virtual int32_t Evaluate(int32_t x) const = 0;
1626 virtual int64_t Evaluate(int64_t x) const = 0;
1627
Roland Levillain88cb1752014-10-20 16:36:47 +01001628 DECLARE_INSTRUCTION(UnaryOperation);
1629
1630 private:
1631 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
1632};
1633
Dave Allison20dfc792014-06-16 20:44:29 -07001634class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001635 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001636 HBinaryOperation(Primitive::Type result_type,
1637 HInstruction* left,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001638 HInstruction* right) : HExpression(result_type, SideEffects::None()) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001639 SetRawInputAt(0, left);
1640 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001641 }
1642
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001643 HInstruction* GetLeft() const { return InputAt(0); }
1644 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07001645 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001646
Mingyao Yangdc5ac732015-02-25 11:28:05 -08001647 virtual bool IsCommutative() const { return false; }
1648
1649 // Put constant on the right.
1650 // Returns whether order is changed.
1651 bool OrderInputsWithConstantOnTheRight() {
1652 HInstruction* left = InputAt(0);
1653 HInstruction* right = InputAt(1);
1654 if (left->IsConstant() && !right->IsConstant()) {
1655 ReplaceInput(right, 0);
1656 ReplaceInput(left, 1);
1657 return true;
1658 }
1659 return false;
1660 }
1661
1662 // Order inputs by instruction id, but favor constant on the right side.
1663 // This helps GVN for commutative ops.
1664 void OrderInputs() {
1665 DCHECK(IsCommutative());
1666 HInstruction* left = InputAt(0);
1667 HInstruction* right = InputAt(1);
1668 if (left == right || (!left->IsConstant() && right->IsConstant())) {
1669 return;
1670 }
1671 if (OrderInputsWithConstantOnTheRight()) {
1672 return;
1673 }
1674 // Order according to instruction id.
1675 if (left->GetId() > right->GetId()) {
1676 ReplaceInput(right, 0);
1677 ReplaceInput(left, 1);
1678 }
1679 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001680
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001681 bool CanBeMoved() const OVERRIDE { return true; }
1682 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001683 UNUSED(other);
1684 return true;
1685 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001686
Roland Levillain9240d6a2014-10-20 16:47:04 +01001687 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01001688 // containing the result of this evaluation. If `operation` cannot
1689 // be evaluated as a constant, return nullptr.
Roland Levillain9240d6a2014-10-20 16:47:04 +01001690 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01001691
1692 // Apply this operation to `x` and `y`.
1693 virtual int32_t Evaluate(int32_t x, int32_t y) const = 0;
1694 virtual int64_t Evaluate(int64_t x, int64_t y) const = 0;
1695
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001696 // Returns an input that can legally be used as the right input and is
1697 // constant, or nullptr.
1698 HConstant* GetConstantRight() const;
1699
1700 // If `GetConstantRight()` returns one of the input, this returns the other
1701 // one. Otherwise it returns nullptr.
1702 HInstruction* GetLeastConstantLeft() const;
1703
Roland Levillainccc07a92014-09-16 14:48:16 +01001704 DECLARE_INSTRUCTION(BinaryOperation);
1705
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001706 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001707 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
1708};
1709
Dave Allison20dfc792014-06-16 20:44:29 -07001710class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001711 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001712 HCondition(HInstruction* first, HInstruction* second)
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001713 : HBinaryOperation(Primitive::kPrimBoolean, first, second),
1714 needs_materialization_(true) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001715
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001716 bool NeedsMaterialization() const { return needs_materialization_; }
1717 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001718
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001719 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001720 // `instruction`, and disregard moves in between.
1721 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001722
Dave Allison20dfc792014-06-16 20:44:29 -07001723 DECLARE_INSTRUCTION(Condition);
1724
1725 virtual IfCondition GetCondition() const = 0;
1726
1727 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001728 // For register allocation purposes, returns whether this instruction needs to be
1729 // materialized (that is, not just be in the processor flags).
1730 bool needs_materialization_;
1731
Dave Allison20dfc792014-06-16 20:44:29 -07001732 DISALLOW_COPY_AND_ASSIGN(HCondition);
1733};
1734
1735// Instruction to check if two inputs are equal to each other.
1736class HEqual : public HCondition {
1737 public:
1738 HEqual(HInstruction* first, HInstruction* second)
1739 : HCondition(first, second) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001740
Mingyao Yangdc5ac732015-02-25 11:28:05 -08001741 bool IsCommutative() const OVERRIDE { return true; }
1742
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001743 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001744 return x == y ? 1 : 0;
1745 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001746 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001747 return x == y ? 1 : 0;
1748 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001749
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001750 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001751
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001752 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001753 return kCondEQ;
1754 }
1755
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001756 private:
1757 DISALLOW_COPY_AND_ASSIGN(HEqual);
1758};
1759
Dave Allison20dfc792014-06-16 20:44:29 -07001760class HNotEqual : public HCondition {
1761 public:
1762 HNotEqual(HInstruction* first, HInstruction* second)
1763 : HCondition(first, second) {}
1764
Mingyao Yangdc5ac732015-02-25 11:28:05 -08001765 bool IsCommutative() const OVERRIDE { return true; }
1766
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001767 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001768 return x != y ? 1 : 0;
1769 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001770 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001771 return x != y ? 1 : 0;
1772 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001773
Dave Allison20dfc792014-06-16 20:44:29 -07001774 DECLARE_INSTRUCTION(NotEqual);
1775
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001776 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001777 return kCondNE;
1778 }
1779
1780 private:
1781 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
1782};
1783
1784class HLessThan : public HCondition {
1785 public:
1786 HLessThan(HInstruction* first, HInstruction* second)
1787 : HCondition(first, second) {}
1788
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001789 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001790 return x < y ? 1 : 0;
1791 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001792 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001793 return x < y ? 1 : 0;
1794 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001795
Dave Allison20dfc792014-06-16 20:44:29 -07001796 DECLARE_INSTRUCTION(LessThan);
1797
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001798 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001799 return kCondLT;
1800 }
1801
1802 private:
1803 DISALLOW_COPY_AND_ASSIGN(HLessThan);
1804};
1805
1806class HLessThanOrEqual : public HCondition {
1807 public:
1808 HLessThanOrEqual(HInstruction* first, HInstruction* second)
1809 : HCondition(first, second) {}
1810
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001811 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001812 return x <= y ? 1 : 0;
1813 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001814 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001815 return x <= y ? 1 : 0;
1816 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001817
Dave Allison20dfc792014-06-16 20:44:29 -07001818 DECLARE_INSTRUCTION(LessThanOrEqual);
1819
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001820 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001821 return kCondLE;
1822 }
1823
1824 private:
1825 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
1826};
1827
1828class HGreaterThan : public HCondition {
1829 public:
1830 HGreaterThan(HInstruction* first, HInstruction* second)
1831 : HCondition(first, second) {}
1832
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001833 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001834 return x > y ? 1 : 0;
1835 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001836 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001837 return x > y ? 1 : 0;
1838 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001839
Dave Allison20dfc792014-06-16 20:44:29 -07001840 DECLARE_INSTRUCTION(GreaterThan);
1841
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001842 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001843 return kCondGT;
1844 }
1845
1846 private:
1847 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
1848};
1849
1850class HGreaterThanOrEqual : public HCondition {
1851 public:
1852 HGreaterThanOrEqual(HInstruction* first, HInstruction* second)
1853 : HCondition(first, second) {}
1854
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001855 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001856 return x >= y ? 1 : 0;
1857 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001858 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001859 return x >= y ? 1 : 0;
1860 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001861
Dave Allison20dfc792014-06-16 20:44:29 -07001862 DECLARE_INSTRUCTION(GreaterThanOrEqual);
1863
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001864 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001865 return kCondGE;
1866 }
1867
1868 private:
1869 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
1870};
1871
1872
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001873// Instruction to check how two inputs compare to each other.
1874// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
1875class HCompare : public HBinaryOperation {
1876 public:
Calin Juravleddb7df22014-11-25 20:56:51 +00001877 // The bias applies for floating point operations and indicates how NaN
1878 // comparisons are treated:
1879 enum Bias {
1880 kNoBias, // bias is not applicable (i.e. for long operation)
1881 kGtBias, // return 1 for NaN comparisons
1882 kLtBias, // return -1 for NaN comparisons
1883 };
1884
1885 HCompare(Primitive::Type type, HInstruction* first, HInstruction* second, Bias bias)
1886 : HBinaryOperation(Primitive::kPrimInt, first, second), bias_(bias) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001887 DCHECK_EQ(type, first->GetType());
1888 DCHECK_EQ(type, second->GetType());
1889 }
1890
Calin Juravleddb7df22014-11-25 20:56:51 +00001891 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01001892 return
1893 x == y ? 0 :
1894 x > y ? 1 :
1895 -1;
1896 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001897
1898 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01001899 return
1900 x == y ? 0 :
1901 x > y ? 1 :
1902 -1;
1903 }
1904
Calin Juravleddb7df22014-11-25 20:56:51 +00001905 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
1906 return bias_ == other->AsCompare()->bias_;
1907 }
1908
1909 bool IsGtBias() { return bias_ == kGtBias; }
1910
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001911 DECLARE_INSTRUCTION(Compare);
1912
1913 private:
Calin Juravleddb7df22014-11-25 20:56:51 +00001914 const Bias bias_;
1915
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001916 DISALLOW_COPY_AND_ASSIGN(HCompare);
1917};
1918
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001919// A local in the graph. Corresponds to a Dex register.
1920class HLocal : public HTemplateInstruction<0> {
1921 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001922 explicit HLocal(uint16_t reg_number)
1923 : HTemplateInstruction(SideEffects::None()), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001924
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001925 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001926
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001927 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001928
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001929 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001930 // The Dex register number.
1931 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001932
1933 DISALLOW_COPY_AND_ASSIGN(HLocal);
1934};
1935
1936// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07001937class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001938 public:
Roland Levillain5799fc02014-09-25 12:15:20 +01001939 HLoadLocal(HLocal* local, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001940 : HExpression(type, SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001941 SetRawInputAt(0, local);
1942 }
1943
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001944 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1945
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001946 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001947
1948 private:
1949 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
1950};
1951
1952// Store a value in a given local. This instruction has two inputs: the value
1953// and the local.
1954class HStoreLocal : public HTemplateInstruction<2> {
1955 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001956 HStoreLocal(HLocal* local, HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001957 SetRawInputAt(0, local);
1958 SetRawInputAt(1, value);
1959 }
1960
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001961 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1962
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001963 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001964
1965 private:
1966 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
1967};
1968
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001969class HConstant : public HExpression<0> {
1970 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001971 explicit HConstant(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
1972
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001973 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001974
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001975 virtual bool IsMinusOne() const { return false; }
1976 virtual bool IsZero() const { return false; }
1977 virtual bool IsOne() const { return false; }
1978
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001979 DECLARE_INSTRUCTION(Constant);
1980
1981 private:
1982 DISALLOW_COPY_AND_ASSIGN(HConstant);
1983};
1984
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001985class HFloatConstant : public HConstant {
1986 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001987 float GetValue() const { return value_; }
1988
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001989 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001990 return bit_cast<uint32_t, float>(other->AsFloatConstant()->value_) ==
1991 bit_cast<uint32_t, float>(value_);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001992 }
1993
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001994 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001995
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001996 bool IsMinusOne() const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001997 return bit_cast<uint32_t, float>(AsFloatConstant()->GetValue()) ==
1998 bit_cast<uint32_t, float>((-1.0f));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001999 }
2000 bool IsZero() const OVERRIDE {
2001 return AsFloatConstant()->GetValue() == 0.0f;
2002 }
2003 bool IsOne() const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002004 return bit_cast<uint32_t, float>(AsFloatConstant()->GetValue()) ==
2005 bit_cast<uint32_t, float>(1.0f);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002006 }
2007
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002008 DECLARE_INSTRUCTION(FloatConstant);
2009
2010 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002011 explicit HFloatConstant(float value) : HConstant(Primitive::kPrimFloat), value_(value) {}
2012
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002013 const float value_;
2014
David Brazdil8d5b8b22015-03-24 10:51:52 +00002015 // Only the SsaBuilder can currently create floating-point constants. If we
2016 // ever need to create them later in the pipeline, we will have to handle them
2017 // the same way as integral constants.
2018 friend class SsaBuilder;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002019 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2020};
2021
2022class HDoubleConstant : public HConstant {
2023 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002024 double GetValue() const { return value_; }
2025
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002026 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002027 return bit_cast<uint64_t, double>(other->AsDoubleConstant()->value_) ==
2028 bit_cast<uint64_t, double>(value_);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002029 }
2030
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002031 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002032
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002033 bool IsMinusOne() const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002034 return bit_cast<uint64_t, double>(AsDoubleConstant()->GetValue()) ==
2035 bit_cast<uint64_t, double>((-1.0));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002036 }
2037 bool IsZero() const OVERRIDE {
2038 return AsDoubleConstant()->GetValue() == 0.0;
2039 }
2040 bool IsOne() const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002041 return bit_cast<uint64_t, double>(AsDoubleConstant()->GetValue()) ==
2042 bit_cast<uint64_t, double>(1.0);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002043 }
2044
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002045 DECLARE_INSTRUCTION(DoubleConstant);
2046
2047 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002048 explicit HDoubleConstant(double value) : HConstant(Primitive::kPrimDouble), value_(value) {}
2049
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002050 const double value_;
2051
David Brazdil8d5b8b22015-03-24 10:51:52 +00002052 // Only the SsaBuilder can currently create floating-point constants. If we
2053 // ever need to create them later in the pipeline, we will have to handle them
2054 // the same way as integral constants.
2055 friend class SsaBuilder;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002056 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
2057};
2058
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002059class HNullConstant : public HConstant {
2060 public:
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002061 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2062 return true;
2063 }
2064
2065 size_t ComputeHashCode() const OVERRIDE { return 0; }
2066
Calin Juravle61d544b2015-02-23 16:46:57 +00002067 bool ActAsNullConstant() const OVERRIDE { return true; }
2068
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002069 DECLARE_INSTRUCTION(NullConstant);
2070
2071 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002072 HNullConstant() : HConstant(Primitive::kPrimNot) {}
2073
2074 friend class HGraph;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002075 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2076};
2077
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002078// Constants of the type int. Those can be from Dex instructions, or
2079// synthesized (for example with the if-eqz instruction).
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002080class HIntConstant : public HConstant {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002081 public:
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002082 int32_t GetValue() const { return value_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002083
Calin Juravle61d544b2015-02-23 16:46:57 +00002084 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002085 return other->AsIntConstant()->value_ == value_;
2086 }
2087
Calin Juravle61d544b2015-02-23 16:46:57 +00002088 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2089
2090 // TODO: Null is represented by the `0` constant. In most cases we replace it
2091 // with a HNullConstant but we don't do it when comparing (a != null). This
2092 // method is an workaround until we fix the above.
2093 bool ActAsNullConstant() const OVERRIDE { return value_ == 0; }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002094
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002095 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2096 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2097 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2098
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002099 DECLARE_INSTRUCTION(IntConstant);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002100
2101 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002102 explicit HIntConstant(int32_t value) : HConstant(Primitive::kPrimInt), value_(value) {}
2103
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002104 const int32_t value_;
2105
David Brazdil8d5b8b22015-03-24 10:51:52 +00002106 friend class HGraph;
2107 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2108 ART_FRIEND_TEST(ParallelMoveTest, ConstantLast);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002109 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2110};
2111
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002112class HLongConstant : public HConstant {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002113 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002114 int64_t GetValue() const { return value_; }
2115
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002116 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002117 return other->AsLongConstant()->value_ == value_;
2118 }
2119
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002120 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002121
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002122 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2123 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2124 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2125
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002126 DECLARE_INSTRUCTION(LongConstant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002127
2128 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002129 explicit HLongConstant(int64_t value) : HConstant(Primitive::kPrimLong), value_(value) {}
2130
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002131 const int64_t value_;
2132
David Brazdil8d5b8b22015-03-24 10:51:52 +00002133 friend class HGraph;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002134 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2135};
2136
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002137enum class Intrinsics {
2138#define OPTIMIZING_INTRINSICS(Name, IsStatic) k ## Name,
2139#include "intrinsics_list.h"
2140 kNone,
2141 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
2142#undef INTRINSICS_LIST
2143#undef OPTIMIZING_INTRINSICS
2144};
2145std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
2146
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002147class HInvoke : public HInstruction {
2148 public:
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002149 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002150
2151 // Runtime needs to walk the stack, so Dex -> Dex calls need to
2152 // know their environment.
Calin Juravle77520bc2015-01-12 18:45:46 +00002153 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002154
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01002155 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002156 SetRawInputAt(index, argument);
2157 }
2158
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002159 Primitive::Type GetType() const OVERRIDE { return return_type_; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002160
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002161 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002162
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002163 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
2164
2165 Intrinsics GetIntrinsic() {
2166 return intrinsic_;
2167 }
2168
2169 void SetIntrinsic(Intrinsics intrinsic) {
2170 intrinsic_ = intrinsic;
2171 }
2172
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002173 DECLARE_INSTRUCTION(Invoke);
2174
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002175 protected:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002176 HInvoke(ArenaAllocator* arena,
2177 uint32_t number_of_arguments,
2178 Primitive::Type return_type,
2179 uint32_t dex_pc,
2180 uint32_t dex_method_index)
2181 : HInstruction(SideEffects::All()),
2182 inputs_(arena, number_of_arguments),
2183 return_type_(return_type),
2184 dex_pc_(dex_pc),
2185 dex_method_index_(dex_method_index),
2186 intrinsic_(Intrinsics::kNone) {
2187 inputs_.SetSize(number_of_arguments);
2188 }
2189
David Brazdil1abb4192015-02-17 18:33:36 +00002190 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
2191 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
2192 inputs_.Put(index, input);
2193 }
2194
2195 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002196 const Primitive::Type return_type_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002197 const uint32_t dex_pc_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002198 const uint32_t dex_method_index_;
2199 Intrinsics intrinsic_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002200
2201 private:
2202 DISALLOW_COPY_AND_ASSIGN(HInvoke);
2203};
2204
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002205class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002206 public:
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002207 HInvokeStaticOrDirect(ArenaAllocator* arena,
2208 uint32_t number_of_arguments,
2209 Primitive::Type return_type,
2210 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002211 uint32_t dex_method_index,
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002212 bool is_recursive,
Nicolas Geoffray79041292015-03-26 10:05:54 +00002213 InvokeType original_invoke_type,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002214 InvokeType invoke_type)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002215 : HInvoke(arena, number_of_arguments, return_type, dex_pc, dex_method_index),
Nicolas Geoffray79041292015-03-26 10:05:54 +00002216 original_invoke_type_(original_invoke_type),
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002217 invoke_type_(invoke_type),
2218 is_recursive_(is_recursive) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002219
Calin Juravle77520bc2015-01-12 18:45:46 +00002220 bool CanDoImplicitNullCheck() const OVERRIDE {
2221 // We access the method via the dex cache so we can't do an implicit null check.
2222 // TODO: for intrinsics we can generate implicit null checks.
2223 return false;
2224 }
2225
Nicolas Geoffray79041292015-03-26 10:05:54 +00002226 InvokeType GetOriginalInvokeType() const { return original_invoke_type_; }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002227 InvokeType GetInvokeType() const { return invoke_type_; }
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002228 bool IsRecursive() const { return is_recursive_; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002229 bool NeedsDexCache() const OVERRIDE { return !IsRecursive(); }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002230
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002231 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002232
2233 private:
Nicolas Geoffray79041292015-03-26 10:05:54 +00002234 const InvokeType original_invoke_type_;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002235 const InvokeType invoke_type_;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002236 const bool is_recursive_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002237
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002238 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002239};
2240
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002241class HInvokeVirtual : public HInvoke {
2242 public:
2243 HInvokeVirtual(ArenaAllocator* arena,
2244 uint32_t number_of_arguments,
2245 Primitive::Type return_type,
2246 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002247 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002248 uint32_t vtable_index)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002249 : HInvoke(arena, number_of_arguments, return_type, dex_pc, dex_method_index),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002250 vtable_index_(vtable_index) {}
2251
Calin Juravle77520bc2015-01-12 18:45:46 +00002252 bool CanDoImplicitNullCheck() const OVERRIDE {
2253 // TODO: Add implicit null checks in intrinsics.
2254 return !GetLocations()->Intrinsified();
2255 }
2256
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002257 uint32_t GetVTableIndex() const { return vtable_index_; }
2258
2259 DECLARE_INSTRUCTION(InvokeVirtual);
2260
2261 private:
2262 const uint32_t vtable_index_;
2263
2264 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
2265};
2266
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002267class HInvokeInterface : public HInvoke {
2268 public:
2269 HInvokeInterface(ArenaAllocator* arena,
2270 uint32_t number_of_arguments,
2271 Primitive::Type return_type,
2272 uint32_t dex_pc,
2273 uint32_t dex_method_index,
2274 uint32_t imt_index)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002275 : HInvoke(arena, number_of_arguments, return_type, dex_pc, dex_method_index),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002276 imt_index_(imt_index) {}
2277
Calin Juravle77520bc2015-01-12 18:45:46 +00002278 bool CanDoImplicitNullCheck() const OVERRIDE {
2279 // TODO: Add implicit null checks in intrinsics.
2280 return !GetLocations()->Intrinsified();
2281 }
2282
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002283 uint32_t GetImtIndex() const { return imt_index_; }
2284 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
2285
2286 DECLARE_INSTRUCTION(InvokeInterface);
2287
2288 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002289 const uint32_t imt_index_;
2290
2291 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
2292};
2293
Dave Allison20dfc792014-06-16 20:44:29 -07002294class HNewInstance : public HExpression<0> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002295 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002296 HNewInstance(uint32_t dex_pc, uint16_t type_index, QuickEntrypointEnum entrypoint)
Nicolas Geoffray065bf772014-09-03 14:51:22 +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 Geoffray2e7038a2014-04-03 18:49:58 +01002301
2302 uint32_t GetDexPc() const { return dex_pc_; }
2303 uint16_t GetTypeIndex() const { return type_index_; }
2304
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002305 // Calls runtime so needs an environment.
Calin Juravle92a6ed22014-12-02 18:58:03 +00002306 bool NeedsEnvironment() const OVERRIDE { return true; }
2307 // It may throw when called on:
2308 // - interfaces
2309 // - abstract/innaccessible/unknown classes
2310 // TODO: optimize when possible.
2311 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002312
Calin Juravle10e244f2015-01-26 18:54:32 +00002313 bool CanBeNull() const OVERRIDE { return false; }
2314
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002315 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
2316
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002317 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002318
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 Geoffray2e7038a2014-04-03 18:49:58 +01002323
2324 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
2325};
2326
Roland Levillain88cb1752014-10-20 16:36:47 +01002327class HNeg : public HUnaryOperation {
2328 public:
2329 explicit HNeg(Primitive::Type result_type, HInstruction* input)
2330 : HUnaryOperation(result_type, input) {}
2331
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002332 int32_t Evaluate(int32_t x) const OVERRIDE { return -x; }
2333 int64_t Evaluate(int64_t x) const OVERRIDE { return -x; }
Roland Levillain9240d6a2014-10-20 16:47:04 +01002334
Roland Levillain88cb1752014-10-20 16:36:47 +01002335 DECLARE_INSTRUCTION(Neg);
2336
2337 private:
2338 DISALLOW_COPY_AND_ASSIGN(HNeg);
2339};
2340
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002341class HNewArray : public HExpression<1> {
2342 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002343 HNewArray(HInstruction* length,
2344 uint32_t dex_pc,
2345 uint16_t type_index,
2346 QuickEntrypointEnum entrypoint)
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002347 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2348 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002349 type_index_(type_index),
2350 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002351 SetRawInputAt(0, length);
2352 }
2353
2354 uint32_t GetDexPc() const { return dex_pc_; }
2355 uint16_t GetTypeIndex() const { return type_index_; }
2356
2357 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00002358 bool NeedsEnvironment() const OVERRIDE { return true; }
2359
Mingyao Yang0c365e62015-03-31 15:09:29 -07002360 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
2361 bool CanThrow() const OVERRIDE { return true; }
2362
Calin Juravle10e244f2015-01-26 18:54:32 +00002363 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002364
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002365 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
2366
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002367 DECLARE_INSTRUCTION(NewArray);
2368
2369 private:
2370 const uint32_t dex_pc_;
2371 const uint16_t type_index_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002372 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002373
2374 DISALLOW_COPY_AND_ASSIGN(HNewArray);
2375};
2376
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002377class HAdd : public HBinaryOperation {
2378 public:
2379 HAdd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2380 : HBinaryOperation(result_type, left, right) {}
2381
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002382 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002383
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002384 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002385 return x + y;
2386 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002387 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002388 return x + y;
2389 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002390
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002391 DECLARE_INSTRUCTION(Add);
2392
2393 private:
2394 DISALLOW_COPY_AND_ASSIGN(HAdd);
2395};
2396
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002397class HSub : public HBinaryOperation {
2398 public:
2399 HSub(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2400 : HBinaryOperation(result_type, left, right) {}
2401
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002402 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002403 return x - y;
2404 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002405 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002406 return x - y;
2407 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002408
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002409 DECLARE_INSTRUCTION(Sub);
2410
2411 private:
2412 DISALLOW_COPY_AND_ASSIGN(HSub);
2413};
2414
Calin Juravle34bacdf2014-10-07 20:23:36 +01002415class HMul : public HBinaryOperation {
2416 public:
2417 HMul(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2418 : HBinaryOperation(result_type, left, right) {}
2419
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002420 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002421
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002422 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x * y; }
2423 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x * y; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002424
2425 DECLARE_INSTRUCTION(Mul);
2426
2427 private:
2428 DISALLOW_COPY_AND_ASSIGN(HMul);
2429};
2430
Calin Juravle7c4954d2014-10-28 16:57:40 +00002431class HDiv : public HBinaryOperation {
2432 public:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002433 HDiv(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
2434 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00002435
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002436 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00002437 // Our graph structure ensures we never have 0 for `y` during constant folding.
2438 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00002439 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00002440 return (y == -1) ? -x : x / y;
2441 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002442
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002443 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002444 DCHECK_NE(y, 0);
2445 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2446 return (y == -1) ? -x : x / y;
2447 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002448
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002449 uint32_t GetDexPc() const { return dex_pc_; }
2450
Calin Juravle7c4954d2014-10-28 16:57:40 +00002451 DECLARE_INSTRUCTION(Div);
2452
2453 private:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002454 const uint32_t dex_pc_;
2455
Calin Juravle7c4954d2014-10-28 16:57:40 +00002456 DISALLOW_COPY_AND_ASSIGN(HDiv);
2457};
2458
Calin Juravlebacfec32014-11-14 15:54:36 +00002459class HRem : public HBinaryOperation {
2460 public:
2461 HRem(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
2462 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
2463
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002464 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002465 DCHECK_NE(y, 0);
2466 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2467 return (y == -1) ? 0 : x % y;
2468 }
2469
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002470 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002471 DCHECK_NE(y, 0);
2472 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2473 return (y == -1) ? 0 : x % y;
2474 }
2475
2476 uint32_t GetDexPc() const { return dex_pc_; }
2477
2478 DECLARE_INSTRUCTION(Rem);
2479
2480 private:
2481 const uint32_t dex_pc_;
2482
2483 DISALLOW_COPY_AND_ASSIGN(HRem);
2484};
2485
Calin Juravled0d48522014-11-04 16:40:20 +00002486class HDivZeroCheck : public HExpression<1> {
2487 public:
2488 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
2489 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
2490 SetRawInputAt(0, value);
2491 }
2492
2493 bool CanBeMoved() const OVERRIDE { return true; }
2494
2495 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2496 UNUSED(other);
2497 return true;
2498 }
2499
2500 bool NeedsEnvironment() const OVERRIDE { return true; }
2501 bool CanThrow() const OVERRIDE { return true; }
2502
2503 uint32_t GetDexPc() const { return dex_pc_; }
2504
2505 DECLARE_INSTRUCTION(DivZeroCheck);
2506
2507 private:
2508 const uint32_t dex_pc_;
2509
2510 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
2511};
2512
Calin Juravle9aec02f2014-11-18 23:06:35 +00002513class HShl : public HBinaryOperation {
2514 public:
2515 HShl(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2516 : HBinaryOperation(result_type, left, right) {}
2517
2518 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x << (y & kMaxIntShiftValue); }
2519 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x << (y & kMaxLongShiftValue); }
2520
2521 DECLARE_INSTRUCTION(Shl);
2522
2523 private:
2524 DISALLOW_COPY_AND_ASSIGN(HShl);
2525};
2526
2527class HShr : public HBinaryOperation {
2528 public:
2529 HShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2530 : HBinaryOperation(result_type, left, right) {}
2531
2532 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x >> (y & kMaxIntShiftValue); }
2533 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x >> (y & kMaxLongShiftValue); }
2534
2535 DECLARE_INSTRUCTION(Shr);
2536
2537 private:
2538 DISALLOW_COPY_AND_ASSIGN(HShr);
2539};
2540
2541class HUShr : public HBinaryOperation {
2542 public:
2543 HUShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2544 : HBinaryOperation(result_type, left, right) {}
2545
2546 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
2547 uint32_t ux = static_cast<uint32_t>(x);
2548 uint32_t uy = static_cast<uint32_t>(y) & kMaxIntShiftValue;
2549 return static_cast<int32_t>(ux >> uy);
2550 }
2551
2552 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
2553 uint64_t ux = static_cast<uint64_t>(x);
2554 uint64_t uy = static_cast<uint64_t>(y) & kMaxLongShiftValue;
2555 return static_cast<int64_t>(ux >> uy);
2556 }
2557
2558 DECLARE_INSTRUCTION(UShr);
2559
2560 private:
2561 DISALLOW_COPY_AND_ASSIGN(HUShr);
2562};
2563
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002564class HAnd : public HBinaryOperation {
2565 public:
2566 HAnd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2567 : HBinaryOperation(result_type, left, right) {}
2568
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002569 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002570
2571 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x & y; }
2572 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x & y; }
2573
2574 DECLARE_INSTRUCTION(And);
2575
2576 private:
2577 DISALLOW_COPY_AND_ASSIGN(HAnd);
2578};
2579
2580class HOr : public HBinaryOperation {
2581 public:
2582 HOr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2583 : HBinaryOperation(result_type, left, right) {}
2584
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002585 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002586
2587 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x | y; }
2588 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x | y; }
2589
2590 DECLARE_INSTRUCTION(Or);
2591
2592 private:
2593 DISALLOW_COPY_AND_ASSIGN(HOr);
2594};
2595
2596class HXor : public HBinaryOperation {
2597 public:
2598 HXor(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2599 : HBinaryOperation(result_type, left, right) {}
2600
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002601 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002602
2603 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x ^ y; }
2604 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x ^ y; }
2605
2606 DECLARE_INSTRUCTION(Xor);
2607
2608 private:
2609 DISALLOW_COPY_AND_ASSIGN(HXor);
2610};
2611
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002612// The value of a parameter in this method. Its location depends on
2613// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07002614class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002615 public:
Calin Juravle10e244f2015-01-26 18:54:32 +00002616 HParameterValue(uint8_t index, Primitive::Type parameter_type, bool is_this = false)
2617 : HExpression(parameter_type, SideEffects::None()), index_(index), is_this_(is_this) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002618
2619 uint8_t GetIndex() const { return index_; }
2620
Calin Juravle10e244f2015-01-26 18:54:32 +00002621 bool CanBeNull() const OVERRIDE { return !is_this_; }
2622
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002623 DECLARE_INSTRUCTION(ParameterValue);
2624
2625 private:
2626 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00002627 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002628 const uint8_t index_;
2629
Calin Juravle10e244f2015-01-26 18:54:32 +00002630 // Whether or not the parameter value corresponds to 'this' argument.
2631 const bool is_this_;
2632
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002633 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
2634};
2635
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002636class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002637 public:
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002638 explicit HNot(Primitive::Type result_type, HInstruction* input)
2639 : HUnaryOperation(result_type, input) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002640
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002641 bool CanBeMoved() const OVERRIDE { return true; }
2642 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002643 UNUSED(other);
2644 return true;
2645 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002646
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002647 int32_t Evaluate(int32_t x) const OVERRIDE { return ~x; }
2648 int64_t Evaluate(int64_t x) const OVERRIDE { return ~x; }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002649
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002650 DECLARE_INSTRUCTION(Not);
2651
2652 private:
2653 DISALLOW_COPY_AND_ASSIGN(HNot);
2654};
2655
David Brazdil66d126e2015-04-03 16:02:44 +01002656class HBooleanNot : public HUnaryOperation {
2657 public:
2658 explicit HBooleanNot(HInstruction* input)
2659 : HUnaryOperation(Primitive::Type::kPrimBoolean, input) {}
2660
2661 bool CanBeMoved() const OVERRIDE { return true; }
2662 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2663 UNUSED(other);
2664 return true;
2665 }
2666
2667 int32_t Evaluate(int32_t x) const OVERRIDE {
2668 DCHECK(IsUint<1>(x));
2669 return !x;
2670 }
2671
2672 int64_t Evaluate(int64_t x ATTRIBUTE_UNUSED) const OVERRIDE {
2673 LOG(FATAL) << DebugName() << " cannot be used with 64-bit values";
2674 UNREACHABLE();
2675 }
2676
2677 DECLARE_INSTRUCTION(BooleanNot);
2678
2679 private:
2680 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
2681};
2682
Roland Levillaindff1f282014-11-05 14:15:05 +00002683class HTypeConversion : public HExpression<1> {
2684 public:
2685 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00002686 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
2687 : HExpression(result_type, SideEffects::None()), dex_pc_(dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002688 SetRawInputAt(0, input);
2689 DCHECK_NE(input->GetType(), result_type);
2690 }
2691
2692 HInstruction* GetInput() const { return InputAt(0); }
2693 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
2694 Primitive::Type GetResultType() const { return GetType(); }
2695
Roland Levillain624279f2014-12-04 11:54:28 +00002696 // Required by the x86 and ARM code generators when producing calls
2697 // to the runtime.
2698 uint32_t GetDexPc() const { return dex_pc_; }
2699
Roland Levillaindff1f282014-11-05 14:15:05 +00002700 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00002701 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00002702
2703 DECLARE_INSTRUCTION(TypeConversion);
2704
2705 private:
Roland Levillain624279f2014-12-04 11:54:28 +00002706 const uint32_t dex_pc_;
2707
Roland Levillaindff1f282014-11-05 14:15:05 +00002708 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
2709};
2710
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00002711static constexpr uint32_t kNoRegNumber = -1;
2712
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002713class HPhi : public HInstruction {
2714 public:
2715 HPhi(ArenaAllocator* arena, uint32_t reg_number, size_t number_of_inputs, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002716 : HInstruction(SideEffects::None()),
2717 inputs_(arena, number_of_inputs),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002718 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002719 type_(type),
Calin Juravle10e244f2015-01-26 18:54:32 +00002720 is_live_(false),
2721 can_be_null_(true) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002722 inputs_.SetSize(number_of_inputs);
2723 }
2724
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00002725 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
2726 static Primitive::Type ToPhiType(Primitive::Type type) {
2727 switch (type) {
2728 case Primitive::kPrimBoolean:
2729 case Primitive::kPrimByte:
2730 case Primitive::kPrimShort:
2731 case Primitive::kPrimChar:
2732 return Primitive::kPrimInt;
2733 default:
2734 return type;
2735 }
2736 }
2737
Calin Juravle10e244f2015-01-26 18:54:32 +00002738 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002739
2740 void AddInput(HInstruction* input);
2741
Calin Juravle10e244f2015-01-26 18:54:32 +00002742 Primitive::Type GetType() const OVERRIDE { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002743 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002744
Calin Juravle10e244f2015-01-26 18:54:32 +00002745 bool CanBeNull() const OVERRIDE { return can_be_null_; }
2746 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
2747
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002748 uint32_t GetRegNumber() const { return reg_number_; }
2749
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002750 void SetDead() { is_live_ = false; }
2751 void SetLive() { is_live_ = true; }
2752 bool IsDead() const { return !is_live_; }
2753 bool IsLive() const { return is_live_; }
2754
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002755 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002756
David Brazdil1abb4192015-02-17 18:33:36 +00002757 protected:
2758 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
2759
2760 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
2761 inputs_.Put(index, input);
2762 }
2763
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002764 private:
David Brazdil1abb4192015-02-17 18:33:36 +00002765 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002766 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002767 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002768 bool is_live_;
Calin Juravle10e244f2015-01-26 18:54:32 +00002769 bool can_be_null_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002770
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002771 DISALLOW_COPY_AND_ASSIGN(HPhi);
2772};
2773
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002774class HNullCheck : public HExpression<1> {
2775 public:
2776 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002777 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002778 SetRawInputAt(0, value);
2779 }
2780
Calin Juravle10e244f2015-01-26 18:54:32 +00002781 bool CanBeMoved() const OVERRIDE { return true; }
2782 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002783 UNUSED(other);
2784 return true;
2785 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002786
Calin Juravle10e244f2015-01-26 18:54:32 +00002787 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002788
Calin Juravle10e244f2015-01-26 18:54:32 +00002789 bool CanThrow() const OVERRIDE { return true; }
2790
2791 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01002792
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002793 uint32_t GetDexPc() const { return dex_pc_; }
2794
2795 DECLARE_INSTRUCTION(NullCheck);
2796
2797 private:
2798 const uint32_t dex_pc_;
2799
2800 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
2801};
2802
2803class FieldInfo : public ValueObject {
2804 public:
Calin Juravle52c48962014-12-16 17:02:57 +00002805 FieldInfo(MemberOffset field_offset, Primitive::Type field_type, bool is_volatile)
2806 : field_offset_(field_offset), field_type_(field_type), is_volatile_(is_volatile) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002807
2808 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002809 Primitive::Type GetFieldType() const { return field_type_; }
Calin Juravle52c48962014-12-16 17:02:57 +00002810 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002811
2812 private:
2813 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01002814 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00002815 const bool is_volatile_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002816};
2817
2818class HInstanceFieldGet : public HExpression<1> {
2819 public:
2820 HInstanceFieldGet(HInstruction* value,
2821 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00002822 MemberOffset field_offset,
2823 bool is_volatile)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002824 : HExpression(field_type, SideEffects::DependsOnSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00002825 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002826 SetRawInputAt(0, value);
2827 }
2828
Calin Juravle10c9cbe2014-12-19 10:50:19 +00002829 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002830
2831 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2832 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
2833 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002834 }
2835
Calin Juravle77520bc2015-01-12 18:45:46 +00002836 bool CanDoImplicitNullCheck() const OVERRIDE {
2837 return GetFieldOffset().Uint32Value() < kPageSize;
2838 }
2839
2840 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002841 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
2842 }
2843
Calin Juravle52c48962014-12-16 17:02:57 +00002844 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002845 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002846 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002847 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002848
2849 DECLARE_INSTRUCTION(InstanceFieldGet);
2850
2851 private:
2852 const FieldInfo field_info_;
2853
2854 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
2855};
2856
2857class HInstanceFieldSet : public HTemplateInstruction<2> {
2858 public:
2859 HInstanceFieldSet(HInstruction* object,
2860 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01002861 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00002862 MemberOffset field_offset,
2863 bool is_volatile)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002864 : HTemplateInstruction(SideEffects::ChangesSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00002865 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002866 SetRawInputAt(0, object);
2867 SetRawInputAt(1, value);
2868 }
2869
Calin Juravle77520bc2015-01-12 18:45:46 +00002870 bool CanDoImplicitNullCheck() const OVERRIDE {
2871 return GetFieldOffset().Uint32Value() < kPageSize;
2872 }
2873
Calin Juravle52c48962014-12-16 17:02:57 +00002874 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002875 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002876 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002877 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002878 HInstruction* GetValue() const { return InputAt(1); }
2879
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002880 DECLARE_INSTRUCTION(InstanceFieldSet);
2881
2882 private:
2883 const FieldInfo field_info_;
2884
2885 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
2886};
2887
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002888class HArrayGet : public HExpression<2> {
2889 public:
2890 HArrayGet(HInstruction* array, HInstruction* index, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002891 : HExpression(type, SideEffects::DependsOnSomething()) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002892 SetRawInputAt(0, array);
2893 SetRawInputAt(1, index);
2894 }
2895
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002896 bool CanBeMoved() const OVERRIDE { return true; }
2897 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002898 UNUSED(other);
2899 return true;
2900 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002901 bool CanDoImplicitNullCheck() const OVERRIDE {
2902 // TODO: We can be smarter here.
2903 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
2904 // which generates the implicit null check. There are cases when these can be removed
2905 // to produce better code. If we ever add optimizations to do so we should allow an
2906 // implicit check here (as long as the address falls in the first page).
2907 return false;
2908 }
2909
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002910 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002911
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002912 HInstruction* GetArray() const { return InputAt(0); }
2913 HInstruction* GetIndex() const { return InputAt(1); }
2914
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002915 DECLARE_INSTRUCTION(ArrayGet);
2916
2917 private:
2918 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
2919};
2920
2921class HArraySet : public HTemplateInstruction<3> {
2922 public:
2923 HArraySet(HInstruction* array,
2924 HInstruction* index,
2925 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002926 Primitive::Type expected_component_type,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002927 uint32_t dex_pc)
2928 : HTemplateInstruction(SideEffects::ChangesSomething()),
2929 dex_pc_(dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002930 expected_component_type_(expected_component_type),
2931 needs_type_check_(value->GetType() == Primitive::kPrimNot) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002932 SetRawInputAt(0, array);
2933 SetRawInputAt(1, index);
2934 SetRawInputAt(2, value);
2935 }
2936
Calin Juravle77520bc2015-01-12 18:45:46 +00002937 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002938 // We currently always call a runtime method to catch array store
2939 // exceptions.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002940 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002941 }
2942
Calin Juravle77520bc2015-01-12 18:45:46 +00002943 bool CanDoImplicitNullCheck() const OVERRIDE {
2944 // TODO: Same as for ArrayGet.
2945 return false;
2946 }
2947
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002948 void ClearNeedsTypeCheck() {
2949 needs_type_check_ = false;
2950 }
2951
2952 bool NeedsTypeCheck() const { return needs_type_check_; }
2953
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002954 uint32_t GetDexPc() const { return dex_pc_; }
2955
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002956 HInstruction* GetArray() const { return InputAt(0); }
2957 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002958 HInstruction* GetValue() const { return InputAt(2); }
2959
2960 Primitive::Type GetComponentType() const {
2961 // The Dex format does not type floating point index operations. Since the
2962 // `expected_component_type_` is set during building and can therefore not
2963 // be correct, we also check what is the value type. If it is a floating
2964 // point type, we must use that type.
2965 Primitive::Type value_type = GetValue()->GetType();
2966 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
2967 ? value_type
2968 : expected_component_type_;
2969 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002970
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002971 DECLARE_INSTRUCTION(ArraySet);
2972
2973 private:
2974 const uint32_t dex_pc_;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002975 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002976 bool needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002977
2978 DISALLOW_COPY_AND_ASSIGN(HArraySet);
2979};
2980
2981class HArrayLength : public HExpression<1> {
2982 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002983 explicit HArrayLength(HInstruction* array)
2984 : HExpression(Primitive::kPrimInt, SideEffects::None()) {
2985 // Note that arrays do not change length, so the instruction does not
2986 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002987 SetRawInputAt(0, array);
2988 }
2989
Calin Juravle77520bc2015-01-12 18:45:46 +00002990 bool CanBeMoved() const OVERRIDE { return true; }
2991 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002992 UNUSED(other);
2993 return true;
2994 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002995 bool CanDoImplicitNullCheck() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002996
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002997 DECLARE_INSTRUCTION(ArrayLength);
2998
2999 private:
3000 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
3001};
3002
3003class HBoundsCheck : public HExpression<2> {
3004 public:
3005 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003006 : HExpression(index->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003007 DCHECK(index->GetType() == Primitive::kPrimInt);
3008 SetRawInputAt(0, index);
3009 SetRawInputAt(1, length);
3010 }
3011
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003012 bool CanBeMoved() const OVERRIDE { return true; }
3013 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003014 UNUSED(other);
3015 return true;
3016 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003017
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003018 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003019
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003020 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01003021
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003022 uint32_t GetDexPc() const { return dex_pc_; }
3023
3024 DECLARE_INSTRUCTION(BoundsCheck);
3025
3026 private:
3027 const uint32_t dex_pc_;
3028
3029 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
3030};
3031
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003032/**
3033 * Some DEX instructions are folded into multiple HInstructions that need
3034 * to stay live until the last HInstruction. This class
3035 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00003036 * HInstruction stays live. `index` represents the stack location index of the
3037 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003038 */
3039class HTemporary : public HTemplateInstruction<0> {
3040 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003041 explicit HTemporary(size_t index) : HTemplateInstruction(SideEffects::None()), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003042
3043 size_t GetIndex() const { return index_; }
3044
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00003045 Primitive::Type GetType() const OVERRIDE {
3046 // The previous instruction is the one that will be stored in the temporary location.
3047 DCHECK(GetPrevious() != nullptr);
3048 return GetPrevious()->GetType();
3049 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00003050
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003051 DECLARE_INSTRUCTION(Temporary);
3052
3053 private:
3054 const size_t index_;
3055
3056 DISALLOW_COPY_AND_ASSIGN(HTemporary);
3057};
3058
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003059class HSuspendCheck : public HTemplateInstruction<0> {
3060 public:
3061 explicit HSuspendCheck(uint32_t dex_pc)
Nicolas Geoffray9ebc72c2014-09-25 16:33:42 +01003062 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003063
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003064 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003065 return true;
3066 }
3067
3068 uint32_t GetDexPc() const { return dex_pc_; }
3069
3070 DECLARE_INSTRUCTION(SuspendCheck);
3071
3072 private:
3073 const uint32_t dex_pc_;
3074
3075 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
3076};
3077
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003078/**
3079 * Instruction to load a Class object.
3080 */
3081class HLoadClass : public HExpression<0> {
3082 public:
3083 HLoadClass(uint16_t type_index,
3084 bool is_referrers_class,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003085 uint32_t dex_pc)
3086 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3087 type_index_(type_index),
3088 is_referrers_class_(is_referrers_class),
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003089 dex_pc_(dex_pc),
Calin Juravleb1498f62015-02-16 13:13:29 +00003090 generate_clinit_check_(false),
3091 loaded_class_rti_(ReferenceTypeInfo::CreateTop(/* is_exact */ false)) {}
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003092
3093 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003094
3095 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3096 return other->AsLoadClass()->type_index_ == type_index_;
3097 }
3098
3099 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
3100
3101 uint32_t GetDexPc() const { return dex_pc_; }
3102 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003103 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003104
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003105 bool NeedsEnvironment() const OVERRIDE {
3106 // Will call runtime and load the class if the class is not loaded yet.
3107 // TODO: finer grain decision.
3108 return !is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003109 }
3110
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003111 bool MustGenerateClinitCheck() const {
3112 return generate_clinit_check_;
3113 }
3114
3115 void SetMustGenerateClinitCheck() {
3116 generate_clinit_check_ = true;
3117 }
3118
3119 bool CanCallRuntime() const {
3120 return MustGenerateClinitCheck() || !is_referrers_class_;
3121 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003122
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003123 bool CanThrow() const OVERRIDE {
3124 // May call runtime and and therefore can throw.
3125 // TODO: finer grain decision.
3126 return !is_referrers_class_;
3127 }
3128
Calin Juravleacf735c2015-02-12 15:25:22 +00003129 ReferenceTypeInfo GetLoadedClassRTI() {
3130 return loaded_class_rti_;
3131 }
3132
3133 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
3134 // Make sure we only set exact types (the loaded class should never be merged).
3135 DCHECK(rti.IsExact());
3136 loaded_class_rti_ = rti;
3137 }
3138
3139 bool IsResolved() {
3140 return loaded_class_rti_.IsExact();
3141 }
3142
Nicolas Geoffray9437b782015-03-25 10:08:51 +00003143 bool NeedsDexCache() const OVERRIDE { return !is_referrers_class_; }
3144
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003145 DECLARE_INSTRUCTION(LoadClass);
3146
3147 private:
3148 const uint16_t type_index_;
3149 const bool is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003150 const uint32_t dex_pc_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003151 // Whether this instruction must generate the initialization check.
3152 // Used for code generation.
3153 bool generate_clinit_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003154
Calin Juravleacf735c2015-02-12 15:25:22 +00003155 ReferenceTypeInfo loaded_class_rti_;
3156
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003157 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
3158};
3159
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003160class HLoadString : public HExpression<0> {
3161 public:
3162 HLoadString(uint32_t string_index, uint32_t dex_pc)
3163 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3164 string_index_(string_index),
3165 dex_pc_(dex_pc) {}
3166
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003167 bool CanBeMoved() const OVERRIDE { return true; }
3168
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003169 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3170 return other->AsLoadString()->string_index_ == string_index_;
3171 }
3172
3173 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
3174
3175 uint32_t GetDexPc() const { return dex_pc_; }
3176 uint32_t GetStringIndex() const { return string_index_; }
3177
3178 // TODO: Can we deopt or debug when we resolve a string?
3179 bool NeedsEnvironment() const OVERRIDE { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00003180 bool NeedsDexCache() const OVERRIDE { return true; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003181
3182 DECLARE_INSTRUCTION(LoadString);
3183
3184 private:
3185 const uint32_t string_index_;
3186 const uint32_t dex_pc_;
3187
3188 DISALLOW_COPY_AND_ASSIGN(HLoadString);
3189};
3190
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003191// TODO: Pass this check to HInvokeStaticOrDirect nodes.
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003192/**
3193 * Performs an initialization check on its Class object input.
3194 */
3195class HClinitCheck : public HExpression<1> {
3196 public:
3197 explicit HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Nicolas Geoffraya0466e12015-03-27 15:00:40 +00003198 : HExpression(Primitive::kPrimNot, SideEffects::ChangesSomething()),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003199 dex_pc_(dex_pc) {
3200 SetRawInputAt(0, constant);
3201 }
3202
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003203 bool CanBeMoved() const OVERRIDE { return true; }
3204 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3205 UNUSED(other);
3206 return true;
3207 }
3208
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003209 bool NeedsEnvironment() const OVERRIDE {
3210 // May call runtime to initialize the class.
3211 return true;
3212 }
3213
3214 uint32_t GetDexPc() const { return dex_pc_; }
3215
3216 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
3217
3218 DECLARE_INSTRUCTION(ClinitCheck);
3219
3220 private:
3221 const uint32_t dex_pc_;
3222
3223 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
3224};
3225
3226class HStaticFieldGet : public HExpression<1> {
3227 public:
3228 HStaticFieldGet(HInstruction* cls,
3229 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003230 MemberOffset field_offset,
3231 bool is_volatile)
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003232 : HExpression(field_type, SideEffects::DependsOnSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00003233 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003234 SetRawInputAt(0, cls);
3235 }
3236
Calin Juravle52c48962014-12-16 17:02:57 +00003237
Calin Juravle10c9cbe2014-12-19 10:50:19 +00003238 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003239
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003240 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00003241 HStaticFieldGet* other_get = other->AsStaticFieldGet();
3242 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003243 }
3244
3245 size_t ComputeHashCode() const OVERRIDE {
3246 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
3247 }
3248
Calin Juravle52c48962014-12-16 17:02:57 +00003249 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003250 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
3251 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003252 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003253
3254 DECLARE_INSTRUCTION(StaticFieldGet);
3255
3256 private:
3257 const FieldInfo field_info_;
3258
3259 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
3260};
3261
3262class HStaticFieldSet : public HTemplateInstruction<2> {
3263 public:
3264 HStaticFieldSet(HInstruction* cls,
3265 HInstruction* value,
3266 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003267 MemberOffset field_offset,
3268 bool is_volatile)
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003269 : HTemplateInstruction(SideEffects::ChangesSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00003270 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003271 SetRawInputAt(0, cls);
3272 SetRawInputAt(1, value);
3273 }
3274
Calin Juravle52c48962014-12-16 17:02:57 +00003275 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003276 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
3277 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003278 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003279
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003280 HInstruction* GetValue() const { return InputAt(1); }
3281
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003282 DECLARE_INSTRUCTION(StaticFieldSet);
3283
3284 private:
3285 const FieldInfo field_info_;
3286
3287 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
3288};
3289
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003290// Implement the move-exception DEX instruction.
3291class HLoadException : public HExpression<0> {
3292 public:
3293 HLoadException() : HExpression(Primitive::kPrimNot, SideEffects::None()) {}
3294
3295 DECLARE_INSTRUCTION(LoadException);
3296
3297 private:
3298 DISALLOW_COPY_AND_ASSIGN(HLoadException);
3299};
3300
3301class HThrow : public HTemplateInstruction<1> {
3302 public:
3303 HThrow(HInstruction* exception, uint32_t dex_pc)
3304 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {
3305 SetRawInputAt(0, exception);
3306 }
3307
3308 bool IsControlFlow() const OVERRIDE { return true; }
3309
3310 bool NeedsEnvironment() const OVERRIDE { return true; }
3311
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003312 bool CanThrow() const OVERRIDE { return true; }
3313
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003314 uint32_t GetDexPc() const { return dex_pc_; }
3315
3316 DECLARE_INSTRUCTION(Throw);
3317
3318 private:
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003319 const uint32_t dex_pc_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003320
3321 DISALLOW_COPY_AND_ASSIGN(HThrow);
3322};
3323
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003324class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003325 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003326 HInstanceOf(HInstruction* object,
3327 HLoadClass* constant,
3328 bool class_is_final,
3329 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003330 : HExpression(Primitive::kPrimBoolean, SideEffects::None()),
3331 class_is_final_(class_is_final),
3332 dex_pc_(dex_pc) {
3333 SetRawInputAt(0, object);
3334 SetRawInputAt(1, constant);
3335 }
3336
3337 bool CanBeMoved() const OVERRIDE { return true; }
3338
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003339 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003340 return true;
3341 }
3342
3343 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003344 return false;
3345 }
3346
3347 uint32_t GetDexPc() const { return dex_pc_; }
3348
3349 bool IsClassFinal() const { return class_is_final_; }
3350
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003351 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003352
3353 private:
3354 const bool class_is_final_;
3355 const uint32_t dex_pc_;
3356
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003357 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
3358};
3359
Calin Juravleb1498f62015-02-16 13:13:29 +00003360class HBoundType : public HExpression<1> {
3361 public:
3362 HBoundType(HInstruction* input, ReferenceTypeInfo bound_type)
3363 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3364 bound_type_(bound_type) {
Calin Juravle61d544b2015-02-23 16:46:57 +00003365 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00003366 SetRawInputAt(0, input);
3367 }
3368
3369 const ReferenceTypeInfo& GetBoundType() const { return bound_type_; }
3370
3371 bool CanBeNull() const OVERRIDE {
3372 // `null instanceof ClassX` always return false so we can't be null.
3373 return false;
3374 }
3375
3376 DECLARE_INSTRUCTION(BoundType);
3377
3378 private:
3379 // Encodes the most upper class that this instruction can have. In other words
3380 // it is always the case that GetBoundType().IsSupertypeOf(GetReferenceType()).
3381 // It is used to bound the type in cases like `if (x instanceof ClassX) {}`
3382 const ReferenceTypeInfo bound_type_;
3383
3384 DISALLOW_COPY_AND_ASSIGN(HBoundType);
3385};
3386
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003387class HCheckCast : public HTemplateInstruction<2> {
3388 public:
3389 HCheckCast(HInstruction* object,
3390 HLoadClass* constant,
3391 bool class_is_final,
3392 uint32_t dex_pc)
3393 : HTemplateInstruction(SideEffects::None()),
3394 class_is_final_(class_is_final),
3395 dex_pc_(dex_pc) {
3396 SetRawInputAt(0, object);
3397 SetRawInputAt(1, constant);
3398 }
3399
3400 bool CanBeMoved() const OVERRIDE { return true; }
3401
3402 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
3403 return true;
3404 }
3405
3406 bool NeedsEnvironment() const OVERRIDE {
3407 // Instruction may throw a CheckCastError.
3408 return true;
3409 }
3410
3411 bool CanThrow() const OVERRIDE { return true; }
3412
3413 uint32_t GetDexPc() const { return dex_pc_; }
3414
3415 bool IsClassFinal() const { return class_is_final_; }
3416
3417 DECLARE_INSTRUCTION(CheckCast);
3418
3419 private:
3420 const bool class_is_final_;
3421 const uint32_t dex_pc_;
3422
3423 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003424};
3425
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003426class HMonitorOperation : public HTemplateInstruction<1> {
3427 public:
3428 enum OperationKind {
3429 kEnter,
3430 kExit,
3431 };
3432
3433 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
3434 : HTemplateInstruction(SideEffects::None()), kind_(kind), dex_pc_(dex_pc) {
3435 SetRawInputAt(0, object);
3436 }
3437
3438 // Instruction may throw a Java exception, so we need an environment.
3439 bool NeedsEnvironment() const OVERRIDE { return true; }
3440 bool CanThrow() const OVERRIDE { return true; }
3441
3442 uint32_t GetDexPc() const { return dex_pc_; }
3443
3444 bool IsEnter() const { return kind_ == kEnter; }
3445
3446 DECLARE_INSTRUCTION(MonitorOperation);
3447
Calin Juravle52c48962014-12-16 17:02:57 +00003448 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003449 const OperationKind kind_;
3450 const uint32_t dex_pc_;
3451
3452 private:
3453 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
3454};
3455
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003456class MoveOperands : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003457 public:
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003458 MoveOperands(Location source, Location destination, HInstruction* instruction)
3459 : source_(source), destination_(destination), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003460
3461 Location GetSource() const { return source_; }
3462 Location GetDestination() const { return destination_; }
3463
3464 void SetSource(Location value) { source_ = value; }
3465 void SetDestination(Location value) { destination_ = value; }
3466
3467 // The parallel move resolver marks moves as "in-progress" by clearing the
3468 // destination (but not the source).
3469 Location MarkPending() {
3470 DCHECK(!IsPending());
3471 Location dest = destination_;
3472 destination_ = Location::NoLocation();
3473 return dest;
3474 }
3475
3476 void ClearPending(Location dest) {
3477 DCHECK(IsPending());
3478 destination_ = dest;
3479 }
3480
3481 bool IsPending() const {
3482 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
3483 return destination_.IsInvalid() && !source_.IsInvalid();
3484 }
3485
3486 // True if this blocks a move from the given location.
3487 bool Blocks(Location loc) const {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003488 return !IsEliminated() && (source_.Contains(loc) || loc.Contains(source_));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003489 }
3490
3491 // A move is redundant if it's been eliminated, if its source and
3492 // destination are the same, or if its destination is unneeded.
3493 bool IsRedundant() const {
3494 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
3495 }
3496
3497 // We clear both operands to indicate move that's been eliminated.
3498 void Eliminate() {
3499 source_ = destination_ = Location::NoLocation();
3500 }
3501
3502 bool IsEliminated() const {
3503 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
3504 return source_.IsInvalid();
3505 }
3506
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003507 HInstruction* GetInstruction() const { return instruction_; }
3508
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003509 private:
3510 Location source_;
3511 Location destination_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003512 // The instruction this move is assocatied with. Null when this move is
3513 // for moving an input in the expected locations of user (including a phi user).
3514 // This is only used in debug mode, to ensure we do not connect interval siblings
3515 // in the same parallel move.
3516 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003517};
3518
3519static constexpr size_t kDefaultNumberOfMoves = 4;
3520
3521class HParallelMove : public HTemplateInstruction<0> {
3522 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003523 explicit HParallelMove(ArenaAllocator* arena)
3524 : HTemplateInstruction(SideEffects::None()), moves_(arena, kDefaultNumberOfMoves) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003525
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003526 void AddMove(Location source, Location destination, HInstruction* instruction) {
3527 DCHECK(source.IsValid());
3528 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003529 if (kIsDebugBuild) {
3530 if (instruction != nullptr) {
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00003531 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003532 if (moves_.Get(i).GetInstruction() == instruction) {
3533 // Special case the situation where the move is for the spill slot
3534 // of the instruction.
3535 if ((GetPrevious() == instruction)
3536 || ((GetPrevious() == nullptr)
3537 && instruction->IsPhi()
3538 && instruction->GetBlock() == GetBlock())) {
3539 DCHECK_NE(destination.GetKind(), moves_.Get(i).GetDestination().GetKind())
3540 << "Doing parallel moves for the same instruction.";
3541 } else {
3542 DCHECK(false) << "Doing parallel moves for the same instruction.";
3543 }
3544 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00003545 }
3546 }
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003547 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
3548 DCHECK(!destination.Equals(moves_.Get(i).GetDestination()))
3549 << "Same destination for two moves in a parallel move.";
3550 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003551 }
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003552 moves_.Add(MoveOperands(source, destination, instruction));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003553 }
3554
3555 MoveOperands* MoveOperandsAt(size_t index) const {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003556 return moves_.GetRawStorage() + index;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003557 }
3558
3559 size_t NumMoves() const { return moves_.Size(); }
3560
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003561 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003562
3563 private:
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003564 GrowableArray<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003565
3566 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
3567};
3568
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003569class HGraphVisitor : public ValueObject {
3570 public:
Dave Allison20dfc792014-06-16 20:44:29 -07003571 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
3572 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003573
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003574 virtual void VisitInstruction(HInstruction* instruction) { UNUSED(instruction); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003575 virtual void VisitBasicBlock(HBasicBlock* block);
3576
Roland Levillain633021e2014-10-01 14:12:25 +01003577 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003578 void VisitInsertionOrder();
3579
Roland Levillain633021e2014-10-01 14:12:25 +01003580 // Visit the graph following dominator tree reverse post-order.
3581 void VisitReversePostOrder();
3582
Nicolas Geoffray787c3072014-03-17 10:20:19 +00003583 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00003584
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003585 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003586#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003587 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
3588
3589 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
3590
3591#undef DECLARE_VISIT_INSTRUCTION
3592
3593 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07003594 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003595
3596 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
3597};
3598
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003599class HGraphDelegateVisitor : public HGraphVisitor {
3600 public:
3601 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
3602 virtual ~HGraphDelegateVisitor() {}
3603
3604 // Visit functions that delegate to to super class.
3605#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003606 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003607
3608 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
3609
3610#undef DECLARE_VISIT_INSTRUCTION
3611
3612 private:
3613 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
3614};
3615
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003616class HInsertionOrderIterator : public ValueObject {
3617 public:
3618 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
3619
3620 bool Done() const { return index_ == graph_.GetBlocks().Size(); }
3621 HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); }
3622 void Advance() { ++index_; }
3623
3624 private:
3625 const HGraph& graph_;
3626 size_t index_;
3627
3628 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
3629};
3630
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003631class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003632 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00003633 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
3634 // Check that reverse post order of the graph has been built.
3635 DCHECK(!graph.GetReversePostOrder().IsEmpty());
3636 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003637
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003638 bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); }
3639 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003640 void Advance() { ++index_; }
3641
3642 private:
3643 const HGraph& graph_;
3644 size_t index_;
3645
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003646 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003647};
3648
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003649class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003650 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003651 explicit HPostOrderIterator(const HGraph& graph)
David Brazdil10f56cb2015-03-24 18:49:14 +00003652 : graph_(graph), index_(graph_.GetReversePostOrder().Size()) {
3653 // Check that reverse post order of the graph has been built.
3654 DCHECK(!graph.GetReversePostOrder().IsEmpty());
3655 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003656
3657 bool Done() const { return index_ == 0; }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003658 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003659 void Advance() { --index_; }
3660
3661 private:
3662 const HGraph& graph_;
3663 size_t index_;
3664
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003665 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003666};
3667
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01003668class HLinearPostOrderIterator : public ValueObject {
3669 public:
3670 explicit HLinearPostOrderIterator(const HGraph& graph)
3671 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().Size()) {}
3672
3673 bool Done() const { return index_ == 0; }
3674
3675 HBasicBlock* Current() const { return order_.Get(index_ -1); }
3676
3677 void Advance() {
3678 --index_;
3679 DCHECK_GE(index_, 0U);
3680 }
3681
3682 private:
3683 const GrowableArray<HBasicBlock*>& order_;
3684 size_t index_;
3685
3686 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
3687};
3688
3689class HLinearOrderIterator : public ValueObject {
3690 public:
3691 explicit HLinearOrderIterator(const HGraph& graph)
3692 : order_(graph.GetLinearOrder()), index_(0) {}
3693
3694 bool Done() const { return index_ == order_.Size(); }
3695 HBasicBlock* Current() const { return order_.Get(index_); }
3696 void Advance() { ++index_; }
3697
3698 private:
3699 const GrowableArray<HBasicBlock*>& order_;
3700 size_t index_;
3701
3702 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
3703};
3704
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003705// Iterator over the blocks that art part of the loop. Includes blocks part
3706// of an inner loop. The order in which the blocks are iterated is on their
3707// block id.
3708class HBlocksInLoopIterator : public ValueObject {
3709 public:
3710 explicit HBlocksInLoopIterator(const HLoopInformation& info)
3711 : blocks_in_loop_(info.GetBlocks()),
3712 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
3713 index_(0) {
3714 if (!blocks_in_loop_.IsBitSet(index_)) {
3715 Advance();
3716 }
3717 }
3718
3719 bool Done() const { return index_ == blocks_.Size(); }
3720 HBasicBlock* Current() const { return blocks_.Get(index_); }
3721 void Advance() {
3722 ++index_;
3723 for (size_t e = blocks_.Size(); index_ < e; ++index_) {
3724 if (blocks_in_loop_.IsBitSet(index_)) {
3725 break;
3726 }
3727 }
3728 }
3729
3730 private:
3731 const BitVector& blocks_in_loop_;
3732 const GrowableArray<HBasicBlock*>& blocks_;
3733 size_t index_;
3734
3735 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
3736};
3737
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003738inline int64_t Int64FromConstant(HConstant* constant) {
3739 DCHECK(constant->IsIntConstant() || constant->IsLongConstant());
3740 return constant->IsIntConstant() ? constant->AsIntConstant()->GetValue()
3741 : constant->AsLongConstant()->GetValue();
3742}
3743
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003744} // namespace art
3745
3746#endif // ART_COMPILER_OPTIMIZING_NODES_H_