blob: 9e496dfc1b65ecc51a138f20870b4ad9dbb02f2a [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"
Calin Juravle27df7582015-04-17 19:12:31 +010022#include "dex/compiler_enums.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000023#include "entrypoints/quick/quick_entrypoints_enum.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000024#include "handle.h"
25#include "handle_scope.h"
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000026#include "invoke_type.h"
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +010027#include "locations.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000028#include "mirror/class.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010029#include "offsets.h"
Ian Rogerse63db272014-07-15 15:36:11 -070030#include "primitive.h"
Nicolas Geoffray0e336432014-02-26 18:24:38 +000031#include "utils/arena_bit_vector.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000032#include "utils/growable_array.h"
33
34namespace art {
35
David Brazdil1abb4192015-02-17 18:33:36 +000036class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000037class HBasicBlock;
David Brazdil8d5b8b22015-03-24 10:51:52 +000038class HDoubleConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010039class HEnvironment;
David Brazdil8d5b8b22015-03-24 10:51:52 +000040class HFloatConstant;
41class HGraphVisitor;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000042class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000043class HIntConstant;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000044class HInvoke;
David Brazdil8d5b8b22015-03-24 10:51:52 +000045class HLongConstant;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +000046class HNullConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010047class HPhi;
Nicolas Geoffray3c049742014-09-24 18:10:46 +010048class HSuspendCheck;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010049class LiveInterval;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000050class LocationSummary;
David Brazdil8d5b8b22015-03-24 10:51:52 +000051class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000052
53static const int kDefaultNumberOfBlocks = 8;
54static const int kDefaultNumberOfSuccessors = 2;
55static const int kDefaultNumberOfPredecessors = 2;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010056static const int kDefaultNumberOfDominatedBlocks = 1;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000057static const int kDefaultNumberOfBackEdges = 1;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000058
Calin Juravle9aec02f2014-11-18 23:06:35 +000059static constexpr uint32_t kMaxIntShiftValue = 0x1f;
60static constexpr uint64_t kMaxLongShiftValue = 0x3f;
61
Dave Allison20dfc792014-06-16 20:44:29 -070062enum IfCondition {
63 kCondEQ,
64 kCondNE,
65 kCondLT,
66 kCondLE,
67 kCondGT,
68 kCondGE,
69};
70
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010071class HInstructionList {
72 public:
73 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
74
75 void AddInstruction(HInstruction* instruction);
76 void RemoveInstruction(HInstruction* instruction);
77
Roland Levillain6b469232014-09-25 10:10:38 +010078 // Return true if this list contains `instruction`.
79 bool Contains(HInstruction* instruction) const;
80
Roland Levillainccc07a92014-09-16 14:48:16 +010081 // Return true if `instruction1` is found before `instruction2` in
82 // this instruction list and false otherwise. Abort if none
83 // of these instructions is found.
84 bool FoundBefore(const HInstruction* instruction1,
85 const HInstruction* instruction2) const;
86
Nicolas Geoffray276d9da2015-02-02 18:24:11 +000087 bool IsEmpty() const { return first_instruction_ == nullptr; }
88 void Clear() { first_instruction_ = last_instruction_ = nullptr; }
89
90 // Update the block of all instructions to be `block`.
91 void SetBlockOfInstructions(HBasicBlock* block) const;
92
93 void AddAfter(HInstruction* cursor, const HInstructionList& instruction_list);
94 void Add(const HInstructionList& instruction_list);
95
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010096 private:
97 HInstruction* first_instruction_;
98 HInstruction* last_instruction_;
99
100 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000101 friend class HGraph;
102 friend class HInstruction;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100103 friend class HInstructionIterator;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100104 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100105
106 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
107};
108
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000109// Control-flow graph of a method. Contains a list of basic blocks.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700110class HGraph : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000111 public:
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000112 HGraph(ArenaAllocator* arena, bool debuggable = false, int start_instruction_id = 0)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000113 : arena_(arena),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000114 blocks_(arena, kDefaultNumberOfBlocks),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100115 reverse_post_order_(arena, kDefaultNumberOfBlocks),
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100116 linear_order_(arena, kDefaultNumberOfBlocks),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700117 entry_block_(nullptr),
118 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100119 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100120 number_of_vregs_(0),
121 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000122 temporaries_vreg_slots_(0),
Mingyao Yange4335eb2015-03-02 15:14:13 -0800123 has_array_accesses_(false),
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000124 debuggable_(debuggable),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000125 current_instruction_id_(start_instruction_id),
126 cached_null_constant_(nullptr),
127 cached_int_constants_(std::less<int32_t>(), arena->Adapter()),
128 cached_long_constants_(std::less<int64_t>(), arena->Adapter()) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000129
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000130 ArenaAllocator* GetArena() const { return arena_; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100131 const GrowableArray<HBasicBlock*>& GetBlocks() const { return blocks_; }
Roland Levillain93445682014-10-06 19:24:02 +0100132 HBasicBlock* GetBlock(size_t id) const { return blocks_.Get(id); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000133
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000134 HBasicBlock* GetEntryBlock() const { return entry_block_; }
135 HBasicBlock* GetExitBlock() const { return exit_block_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000136
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000137 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
138 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000139
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000140 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100141
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000142 // Try building the SSA form of this graph, with dominance computation and loop
143 // recognition. Returns whether it was successful in doing all these steps.
144 bool TryBuildingSsa() {
145 BuildDominatorTree();
Nicolas Geoffrayd3350832015-03-12 11:16:23 +0000146 // The SSA builder requires loops to all be natural. Specifically, the dead phi
147 // elimination phase checks the consistency of the graph when doing a post-order
148 // visit for eliminating dead phis: a dead phi can only have loop header phi
149 // users remaining when being visited.
150 if (!AnalyzeNaturalLoops()) return false;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000151 TransformToSsa();
Nicolas Geoffrayd3350832015-03-12 11:16:23 +0000152 return true;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000153 }
154
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000155 void BuildDominatorTree();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000156 void TransformToSsa();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100157 void SimplifyCFG();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000158
Nicolas Geoffrayf5370122014-12-02 11:51:19 +0000159 // Analyze all natural loops in this graph. Returns false if one
160 // loop is not natural, that is the header does not dominate the
161 // back edge.
162 bool AnalyzeNaturalLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100163
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000164 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
165 void InlineInto(HGraph* outer_graph, HInvoke* invoke);
166
David Brazdil46e2a392015-03-16 17:31:52 +0000167 void MergeEmptyBranches(HBasicBlock* start_block, HBasicBlock* end_block);
168
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100169 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
170 void SimplifyLoop(HBasicBlock* header);
171
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000172 int32_t GetNextInstructionId() {
173 DCHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000174 return current_instruction_id_++;
175 }
176
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000177 int32_t GetCurrentInstructionId() const {
178 return current_instruction_id_;
179 }
180
181 void SetCurrentInstructionId(int32_t id) {
182 current_instruction_id_ = id;
183 }
184
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100185 uint16_t GetMaximumNumberOfOutVRegs() const {
186 return maximum_number_of_out_vregs_;
187 }
188
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000189 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
190 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100191 }
192
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000193 void UpdateTemporariesVRegSlots(size_t slots) {
194 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100195 }
196
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000197 size_t GetTemporariesVRegSlots() const {
198 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100199 }
200
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100201 void SetNumberOfVRegs(uint16_t number_of_vregs) {
202 number_of_vregs_ = number_of_vregs;
203 }
204
205 uint16_t GetNumberOfVRegs() const {
206 return number_of_vregs_;
207 }
208
209 void SetNumberOfInVRegs(uint16_t value) {
210 number_of_in_vregs_ = value;
211 }
212
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100213 uint16_t GetNumberOfLocalVRegs() const {
214 return number_of_vregs_ - number_of_in_vregs_;
215 }
216
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100217 const GrowableArray<HBasicBlock*>& GetReversePostOrder() const {
218 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100219 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100220
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100221 const GrowableArray<HBasicBlock*>& GetLinearOrder() const {
222 return linear_order_;
223 }
224
Mingyao Yange4335eb2015-03-02 15:14:13 -0800225 bool HasArrayAccesses() const {
226 return has_array_accesses_;
227 }
228
229 void SetHasArrayAccesses(bool value) {
230 has_array_accesses_ = value;
231 }
232
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000233 bool IsDebuggable() const { return debuggable_; }
234
David Brazdil8d5b8b22015-03-24 10:51:52 +0000235 // Returns a constant of the given type and value. If it does not exist
236 // already, it is created and inserted into the graph. Only integral types
237 // are currently supported.
238 HConstant* GetConstant(Primitive::Type type, int64_t value);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000239 HNullConstant* GetNullConstant();
David Brazdil8d5b8b22015-03-24 10:51:52 +0000240 HIntConstant* GetIntConstant(int32_t value) {
241 return CreateConstant(value, &cached_int_constants_);
242 }
243 HLongConstant* GetLongConstant(int64_t value) {
244 return CreateConstant(value, &cached_long_constants_);
245 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000246
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000247 private:
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000248 HBasicBlock* FindCommonDominator(HBasicBlock* first, HBasicBlock* second) const;
249 void VisitBlockForDominatorTree(HBasicBlock* block,
250 HBasicBlock* predecessor,
251 GrowableArray<size_t>* visits);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100252 void FindBackEdges(ArenaBitVector* visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000253 void VisitBlockForBackEdges(HBasicBlock* block,
254 ArenaBitVector* visited,
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100255 ArenaBitVector* visiting);
Roland Levillainfc600dc2014-12-02 17:16:31 +0000256 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100257 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000258
David Brazdil8d5b8b22015-03-24 10:51:52 +0000259 template <class InstType, typename ValueType>
260 InstType* CreateConstant(ValueType value, ArenaSafeMap<ValueType, InstType*>* cache);
261 void InsertConstant(HConstant* instruction);
262
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000263 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000264
265 // List of blocks in insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000266 GrowableArray<HBasicBlock*> blocks_;
267
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100268 // List of blocks to perform a reverse post order tree traversal.
269 GrowableArray<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000270
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100271 // List of blocks to perform a linear order tree traversal.
272 GrowableArray<HBasicBlock*> linear_order_;
273
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000274 HBasicBlock* entry_block_;
275 HBasicBlock* exit_block_;
276
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100277 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100278 uint16_t maximum_number_of_out_vregs_;
279
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100280 // The number of virtual registers in this method. Contains the parameters.
281 uint16_t number_of_vregs_;
282
283 // The number of virtual registers used by parameters of this method.
284 uint16_t number_of_in_vregs_;
285
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000286 // Number of vreg size slots that the temporaries use (used in baseline compiler).
287 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100288
Mingyao Yange4335eb2015-03-02 15:14:13 -0800289 // Has array accesses. We can totally skip BCE if it's false.
290 bool has_array_accesses_;
291
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000292 // Indicates whether the graph should be compiled in a way that
293 // ensures full debuggability. If false, we can apply more
294 // aggressive optimizations that may limit the level of debugging.
295 const bool debuggable_;
296
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000297 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000298 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000299
David Brazdil46e2a392015-03-16 17:31:52 +0000300 // Cached common constants often needed by optimization passes.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000301 HNullConstant* cached_null_constant_;
302 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
303 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000304
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100305 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000306 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000307 DISALLOW_COPY_AND_ASSIGN(HGraph);
308};
309
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700310class HLoopInformation : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000311 public:
312 HLoopInformation(HBasicBlock* header, HGraph* graph)
313 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100314 suspend_check_(nullptr),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100315 back_edges_(graph->GetArena(), kDefaultNumberOfBackEdges),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100316 // Make bit vector growable, as the number of blocks may change.
317 blocks_(graph->GetArena(), graph->GetBlocks().Size(), true) {}
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100318
319 HBasicBlock* GetHeader() const {
320 return header_;
321 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000322
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000323 void SetHeader(HBasicBlock* block) {
324 header_ = block;
325 }
326
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100327 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
328 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
329 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
330
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000331 void AddBackEdge(HBasicBlock* back_edge) {
332 back_edges_.Add(back_edge);
333 }
334
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100335 void RemoveBackEdge(HBasicBlock* back_edge) {
336 back_edges_.Delete(back_edge);
337 }
338
David Brazdil46e2a392015-03-16 17:31:52 +0000339 bool IsBackEdge(const HBasicBlock& block) const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100340 for (size_t i = 0, e = back_edges_.Size(); i < e; ++i) {
David Brazdil46e2a392015-03-16 17:31:52 +0000341 if (back_edges_.Get(i) == &block) return true;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100342 }
343 return false;
344 }
345
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000346 size_t NumberOfBackEdges() const {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000347 return back_edges_.Size();
348 }
349
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100350 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100351
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100352 const GrowableArray<HBasicBlock*>& GetBackEdges() const {
353 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100354 }
355
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100356 void ClearBackEdges() {
357 back_edges_.Reset();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100358 }
359
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100360 // Find blocks that are part of this loop. Returns whether the loop is a natural loop,
361 // that is the header dominates the back edge.
362 bool Populate();
363
364 // Returns whether this loop information contains `block`.
365 // Note that this loop information *must* be populated before entering this function.
366 bool Contains(const HBasicBlock& block) const;
367
368 // Returns whether this loop information is an inner loop of `other`.
369 // Note that `other` *must* be populated before entering this function.
370 bool IsIn(const HLoopInformation& other) const;
371
372 const ArenaBitVector& GetBlocks() const { return blocks_; }
373
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000374 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000375 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000376
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000377 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100378 // Internal recursive implementation of `Populate`.
379 void PopulateRecursive(HBasicBlock* block);
380
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000381 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100382 HSuspendCheck* suspend_check_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000383 GrowableArray<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100384 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000385
386 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
387};
388
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100389static constexpr size_t kNoLifetime = -1;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100390static constexpr uint32_t kNoDexPc = -1;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100391
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000392// A block in a method. Contains the list of instructions represented
393// as a double linked list. Each block knows its predecessors and
394// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100395
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700396class HBasicBlock : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000397 public:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100398 explicit HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000399 : graph_(graph),
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000400 predecessors_(graph->GetArena(), kDefaultNumberOfPredecessors),
401 successors_(graph->GetArena(), kDefaultNumberOfSuccessors),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000402 loop_information_(nullptr),
403 dominator_(nullptr),
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100404 dominated_blocks_(graph->GetArena(), kDefaultNumberOfDominatedBlocks),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100405 block_id_(-1),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100406 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100407 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000408 lifetime_end_(kNoLifetime),
409 is_catch_block_(false) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000410
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100411 const GrowableArray<HBasicBlock*>& GetPredecessors() const {
412 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000413 }
414
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100415 const GrowableArray<HBasicBlock*>& GetSuccessors() const {
416 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000417 }
418
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100419 const GrowableArray<HBasicBlock*>& GetDominatedBlocks() const {
420 return dominated_blocks_;
421 }
422
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100423 bool IsEntryBlock() const {
424 return graph_->GetEntryBlock() == this;
425 }
426
427 bool IsExitBlock() const {
428 return graph_->GetExitBlock() == this;
429 }
430
David Brazdil46e2a392015-03-16 17:31:52 +0000431 bool IsSingleGoto() const;
432
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000433 void AddBackEdge(HBasicBlock* back_edge) {
434 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000435 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000436 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100437 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000438 loop_information_->AddBackEdge(back_edge);
439 }
440
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000441 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000442 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000443
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000444 int GetBlockId() const { return block_id_; }
445 void SetBlockId(int id) { block_id_ = id; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000446
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000447 HBasicBlock* GetDominator() const { return dominator_; }
448 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100449 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.Add(block); }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000450 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
451 for (size_t i = 0, e = dominated_blocks_.Size(); i < e; ++i) {
452 if (dominated_blocks_.Get(i) == existing) {
453 dominated_blocks_.Put(i, new_block);
454 return;
455 }
456 }
457 LOG(FATAL) << "Unreachable";
458 UNREACHABLE();
459 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000460
461 int NumberOfBackEdges() const {
462 return loop_information_ == nullptr
463 ? 0
464 : loop_information_->NumberOfBackEdges();
465 }
466
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100467 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
468 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100469 const HInstructionList& GetInstructions() const { return instructions_; }
470 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100471 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000472
473 void AddSuccessor(HBasicBlock* block) {
474 successors_.Add(block);
475 block->predecessors_.Add(this);
476 }
477
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100478 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
479 size_t successor_index = GetSuccessorIndexOf(existing);
480 DCHECK_NE(successor_index, static_cast<size_t>(-1));
481 existing->RemovePredecessor(this);
482 new_block->predecessors_.Add(this);
483 successors_.Put(successor_index, new_block);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000484 }
485
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000486 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
487 size_t predecessor_index = GetPredecessorIndexOf(existing);
488 DCHECK_NE(predecessor_index, static_cast<size_t>(-1));
489 existing->RemoveSuccessor(this);
490 new_block->successors_.Add(this);
491 predecessors_.Put(predecessor_index, new_block);
492 }
493
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100494 void RemovePredecessor(HBasicBlock* block) {
495 predecessors_.Delete(block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100496 }
497
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000498 void RemoveSuccessor(HBasicBlock* block) {
499 successors_.Delete(block);
500 }
501
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100502 void ClearAllPredecessors() {
503 predecessors_.Reset();
504 }
505
506 void AddPredecessor(HBasicBlock* block) {
507 predecessors_.Add(block);
508 block->successors_.Add(this);
509 }
510
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100511 void SwapPredecessors() {
Nicolas Geoffrayc83d4412014-09-18 16:46:20 +0100512 DCHECK_EQ(predecessors_.Size(), 2u);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100513 HBasicBlock* temp = predecessors_.Get(0);
514 predecessors_.Put(0, predecessors_.Get(1));
515 predecessors_.Put(1, temp);
516 }
517
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100518 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) {
519 for (size_t i = 0, e = predecessors_.Size(); i < e; ++i) {
520 if (predecessors_.Get(i) == predecessor) {
521 return i;
522 }
523 }
524 return -1;
525 }
526
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100527 size_t GetSuccessorIndexOf(HBasicBlock* successor) {
528 for (size_t i = 0, e = successors_.Size(); i < e; ++i) {
529 if (successors_.Get(i) == successor) {
530 return i;
531 }
532 }
533 return -1;
534 }
535
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000536 // Split the block into two blocks just after `cursor`. Returns the newly
537 // created block. Note that this method just updates raw block information,
538 // like predecessors, successors, dominators, and instruction list. It does not
539 // update the graph, reverse post order, loop information, nor make sure the
540 // blocks are consistent (for example ending with a control flow instruction).
541 HBasicBlock* SplitAfter(HInstruction* cursor);
542
543 // Merge `other` at the end of `this`. Successors and dominated blocks of
544 // `other` are changed to be successors and dominated blocks of `this`. Note
545 // that this method does not update the graph, reverse post order, loop
546 // information, nor make sure the blocks are consistent (for example ending
547 // with a control flow instruction).
548 void MergeWith(HBasicBlock* other);
549
550 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
551 // of `this` are moved to `other`.
552 // Note that this method does not update the graph, reverse post order, loop
553 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +0000554 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000555 void ReplaceWith(HBasicBlock* other);
556
David Brazdil46e2a392015-03-16 17:31:52 +0000557 // Disconnects `this` from all its predecessors, successors and the dominator.
558 // It assumes that `this` does not dominate any blocks.
559 // Note that this method does not update the graph, reverse post order, loop
560 // information, nor make sure the blocks are consistent (for example ending
561 // with a control flow instruction).
562 void DisconnectFromAll();
563
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000564 void AddInstruction(HInstruction* instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100565 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +0100566 // Replace instruction `initial` with `replacement` within this block.
567 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
568 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100569 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100570 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +0000571 // RemoveInstruction and RemovePhi delete a given instruction from the respective
572 // instruction list. With 'ensure_safety' set to true, it verifies that the
573 // instruction is not in use and removes it from the use lists of its inputs.
574 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
575 void RemovePhi(HPhi* phi, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100576
577 bool IsLoopHeader() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100578 return (loop_information_ != nullptr) && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100579 }
580
Roland Levillain6b879dd2014-09-22 17:13:44 +0100581 bool IsLoopPreHeaderFirstPredecessor() const {
582 DCHECK(IsLoopHeader());
583 DCHECK(!GetPredecessors().IsEmpty());
584 return GetPredecessors().Get(0) == GetLoopInformation()->GetPreHeader();
585 }
586
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100587 HLoopInformation* GetLoopInformation() const {
588 return loop_information_;
589 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000590
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000591 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100592 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000593 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100594 void SetInLoop(HLoopInformation* info) {
595 if (IsLoopHeader()) {
596 // Nothing to do. This just means `info` is an outer loop.
597 } else if (loop_information_ == nullptr) {
598 loop_information_ = info;
599 } else if (loop_information_->Contains(*info->GetHeader())) {
600 // Block is currently part of an outer loop. Make it part of this inner loop.
601 // Note that a non loop header having a loop information means this loop information
602 // has already been populated
603 loop_information_ = info;
604 } else {
605 // Block is part of an inner loop. Do not update the loop information.
606 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
607 // at this point, because this method is being called while populating `info`.
608 }
609 }
610
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000611 // Raw update of the loop information.
612 void SetLoopInformation(HLoopInformation* info) {
613 loop_information_ = info;
614 }
615
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100616 bool IsInLoop() const { return loop_information_ != nullptr; }
617
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100618 // Returns wheter this block dominates the blocked passed as parameter.
619 bool Dominates(HBasicBlock* block) const;
620
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100621 size_t GetLifetimeStart() const { return lifetime_start_; }
622 size_t GetLifetimeEnd() const { return lifetime_end_; }
623
624 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
625 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
626
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100627 uint32_t GetDexPc() const { return dex_pc_; }
628
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000629 bool IsCatchBlock() const { return is_catch_block_; }
630 void SetIsCatchBlock() { is_catch_block_ = true; }
631
David Brazdil8d5b8b22015-03-24 10:51:52 +0000632 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +0000633 bool EndsWithIf() const;
634 bool HasSinglePhi() const;
635
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000636 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000637 HGraph* graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000638 GrowableArray<HBasicBlock*> predecessors_;
639 GrowableArray<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100640 HInstructionList instructions_;
641 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000642 HLoopInformation* loop_information_;
643 HBasicBlock* dominator_;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100644 GrowableArray<HBasicBlock*> dominated_blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000645 int block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100646 // The dex program counter of the first instruction of this block.
647 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100648 size_t lifetime_start_;
649 size_t lifetime_end_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000650 bool is_catch_block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000651
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000652 friend class HGraph;
653 friend class HInstruction;
654
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000655 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
656};
657
David Brazdilb2bd1c52015-03-25 11:17:37 +0000658// Iterates over the LoopInformation of all loops which contain 'block'
659// from the innermost to the outermost.
660class HLoopInformationOutwardIterator : public ValueObject {
661 public:
662 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
663 : current_(block.GetLoopInformation()) {}
664
665 bool Done() const { return current_ == nullptr; }
666
667 void Advance() {
668 DCHECK(!Done());
669 current_ = current_->GetHeader()->GetDominator()->GetLoopInformation();
670 }
671
672 HLoopInformation* Current() const {
673 DCHECK(!Done());
674 return current_;
675 }
676
677 private:
678 HLoopInformation* current_;
679
680 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
681};
682
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100683#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
684 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000685 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000686 M(ArrayGet, Instruction) \
687 M(ArrayLength, Instruction) \
688 M(ArraySet, Instruction) \
David Brazdil66d126e2015-04-03 16:02:44 +0100689 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000690 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +0000691 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000692 M(CheckCast, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100693 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000694 M(Compare, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100695 M(Condition, BinaryOperation) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700696 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000697 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +0000698 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000699 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100700 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000701 M(Exit, Instruction) \
702 M(FloatConstant, Constant) \
703 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100704 M(GreaterThan, Condition) \
705 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100706 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000707 M(InstanceFieldGet, Instruction) \
708 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000709 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100710 M(IntConstant, Constant) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +0000711 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000712 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100713 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000714 M(LessThan, Condition) \
715 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000716 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000717 M(LoadException, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100718 M(LoadLocal, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000719 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100720 M(Local, Instruction) \
721 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +0100722 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +0000723 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000724 M(Mul, BinaryOperation) \
725 M(Neg, UnaryOperation) \
726 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100727 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +0100728 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000729 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000730 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000731 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000732 M(Or, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100733 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000734 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100735 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000736 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100737 M(Return, Instruction) \
738 M(ReturnVoid, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000739 M(Shl, BinaryOperation) \
740 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100741 M(StaticFieldGet, Instruction) \
742 M(StaticFieldSet, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100743 M(StoreLocal, Instruction) \
744 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100745 M(SuspendCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000746 M(Temporary, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000747 M(Throw, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +0000748 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000749 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000750 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000751
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100752#define FOR_EACH_INSTRUCTION(M) \
753 FOR_EACH_CONCRETE_INSTRUCTION(M) \
754 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +0100755 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +0100756 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100757 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -0700758
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100759#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000760FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
761#undef FORWARD_DECLARATION
762
Roland Levillainccc07a92014-09-16 14:48:16 +0100763#define DECLARE_INSTRUCTION(type) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000764 InstructionKind GetKind() const OVERRIDE { return k##type; } \
765 const char* DebugName() const OVERRIDE { return #type; } \
766 const H##type* As##type() const OVERRIDE { return this; } \
767 H##type* As##type() OVERRIDE { return this; } \
768 bool InstructionTypeEquals(HInstruction* other) const OVERRIDE { \
Roland Levillainccc07a92014-09-16 14:48:16 +0100769 return other->Is##type(); \
770 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000771 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000772
David Brazdiled596192015-01-23 10:39:45 +0000773template <typename T> class HUseList;
774
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100775template <typename T>
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700776class HUseListNode : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000777 public:
David Brazdiled596192015-01-23 10:39:45 +0000778 HUseListNode* GetPrevious() const { return prev_; }
779 HUseListNode* GetNext() const { return next_; }
780 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100781 size_t GetIndex() const { return index_; }
782
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000783 private:
David Brazdiled596192015-01-23 10:39:45 +0000784 HUseListNode(T user, size_t index)
785 : user_(user), index_(index), prev_(nullptr), next_(nullptr) {}
786
787 T const user_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100788 const size_t index_;
David Brazdiled596192015-01-23 10:39:45 +0000789 HUseListNode<T>* prev_;
790 HUseListNode<T>* next_;
791
792 friend class HUseList<T>;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000793
794 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
795};
796
David Brazdiled596192015-01-23 10:39:45 +0000797template <typename T>
798class HUseList : public ValueObject {
799 public:
800 HUseList() : first_(nullptr) {}
801
802 void Clear() {
803 first_ = nullptr;
804 }
805
806 // Adds a new entry at the beginning of the use list and returns
807 // the newly created node.
808 HUseListNode<T>* AddUse(T user, size_t index, ArenaAllocator* arena) {
David Brazdilea55b932015-01-27 17:12:29 +0000809 HUseListNode<T>* new_node = new (arena) HUseListNode<T>(user, index);
David Brazdiled596192015-01-23 10:39:45 +0000810 if (IsEmpty()) {
811 first_ = new_node;
812 } else {
813 first_->prev_ = new_node;
814 new_node->next_ = first_;
815 first_ = new_node;
816 }
817 return new_node;
818 }
819
820 HUseListNode<T>* GetFirst() const {
821 return first_;
822 }
823
824 void Remove(HUseListNode<T>* node) {
David Brazdil1abb4192015-02-17 18:33:36 +0000825 DCHECK(node != nullptr);
826 DCHECK(Contains(node));
827
David Brazdiled596192015-01-23 10:39:45 +0000828 if (node->prev_ != nullptr) {
829 node->prev_->next_ = node->next_;
830 }
831 if (node->next_ != nullptr) {
832 node->next_->prev_ = node->prev_;
833 }
834 if (node == first_) {
835 first_ = node->next_;
836 }
837 }
838
David Brazdil1abb4192015-02-17 18:33:36 +0000839 bool Contains(const HUseListNode<T>* node) const {
840 if (node == nullptr) {
841 return false;
842 }
843 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
844 if (current == node) {
845 return true;
846 }
847 }
848 return false;
849 }
850
David Brazdiled596192015-01-23 10:39:45 +0000851 bool IsEmpty() const {
852 return first_ == nullptr;
853 }
854
855 bool HasOnlyOneUse() const {
856 return first_ != nullptr && first_->next_ == nullptr;
857 }
858
859 private:
860 HUseListNode<T>* first_;
861};
862
863template<typename T>
864class HUseIterator : public ValueObject {
865 public:
866 explicit HUseIterator(const HUseList<T>& uses) : current_(uses.GetFirst()) {}
867
868 bool Done() const { return current_ == nullptr; }
869
870 void Advance() {
871 DCHECK(!Done());
872 current_ = current_->GetNext();
873 }
874
875 HUseListNode<T>* Current() const {
876 DCHECK(!Done());
877 return current_;
878 }
879
880 private:
881 HUseListNode<T>* current_;
882
883 friend class HValue;
884};
885
David Brazdil1abb4192015-02-17 18:33:36 +0000886// This class is used by HEnvironment and HInstruction classes to record the
887// instructions they use and pointers to the corresponding HUseListNodes kept
888// by the used instructions.
889template <typename T>
890class HUserRecord : public ValueObject {
891 public:
892 HUserRecord() : instruction_(nullptr), use_node_(nullptr) {}
893 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), use_node_(nullptr) {}
894
895 HUserRecord(const HUserRecord<T>& old_record, HUseListNode<T>* use_node)
896 : instruction_(old_record.instruction_), use_node_(use_node) {
897 DCHECK(instruction_ != nullptr);
898 DCHECK(use_node_ != nullptr);
899 DCHECK(old_record.use_node_ == nullptr);
900 }
901
902 HInstruction* GetInstruction() const { return instruction_; }
903 HUseListNode<T>* GetUseNode() const { return use_node_; }
904
905 private:
906 // Instruction used by the user.
907 HInstruction* instruction_;
908
909 // Corresponding entry in the use list kept by 'instruction_'.
910 HUseListNode<T>* use_node_;
911};
912
Calin Juravle27df7582015-04-17 19:12:31 +0100913// TODO: Add better documentation to this class and maybe refactor with more suggestive names.
914// - Has(All)SideEffects suggests that all the side effects are present but only ChangesSomething
915// flag is consider.
916// - DependsOn suggests that there is a real dependency between side effects but it only
917// checks DependendsOnSomething flag.
918//
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100919// Represents the side effects an instruction may have.
920class SideEffects : public ValueObject {
921 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100922 SideEffects() : flags_(0) {}
923
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100924 static SideEffects None() {
925 return SideEffects(0);
926 }
927
928 static SideEffects All() {
929 return SideEffects(ChangesSomething().flags_ | DependsOnSomething().flags_);
930 }
931
932 static SideEffects ChangesSomething() {
933 return SideEffects((1 << kFlagChangesCount) - 1);
934 }
935
936 static SideEffects DependsOnSomething() {
937 int count = kFlagDependsOnCount - kFlagChangesCount;
938 return SideEffects(((1 << count) - 1) << kFlagChangesCount);
939 }
940
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100941 SideEffects Union(SideEffects other) const {
942 return SideEffects(flags_ | other.flags_);
943 }
944
Roland Levillain72bceff2014-09-15 18:29:00 +0100945 bool HasSideEffects() const {
946 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
947 return (flags_ & all_bits_set) != 0;
948 }
949
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100950 bool HasAllSideEffects() const {
951 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
952 return all_bits_set == (flags_ & all_bits_set);
953 }
954
955 bool DependsOn(SideEffects other) const {
956 size_t depends_flags = other.ComputeDependsFlags();
957 return (flags_ & depends_flags) != 0;
958 }
959
960 bool HasDependencies() const {
961 int count = kFlagDependsOnCount - kFlagChangesCount;
962 size_t all_bits_set = (1 << count) - 1;
963 return ((flags_ >> kFlagChangesCount) & all_bits_set) != 0;
964 }
965
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100966 private:
967 static constexpr int kFlagChangesSomething = 0;
968 static constexpr int kFlagChangesCount = kFlagChangesSomething + 1;
969
970 static constexpr int kFlagDependsOnSomething = kFlagChangesCount;
971 static constexpr int kFlagDependsOnCount = kFlagDependsOnSomething + 1;
972
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100973 explicit SideEffects(size_t flags) : flags_(flags) {}
974
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100975 size_t ComputeDependsFlags() const {
976 return flags_ << kFlagChangesCount;
977 }
978
979 size_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100980};
981
David Brazdiled596192015-01-23 10:39:45 +0000982// A HEnvironment object contains the values of virtual registers at a given location.
983class HEnvironment : public ArenaObject<kArenaAllocMisc> {
984 public:
985 HEnvironment(ArenaAllocator* arena, size_t number_of_vregs)
986 : vregs_(arena, number_of_vregs) {
987 vregs_.SetSize(number_of_vregs);
988 for (size_t i = 0; i < number_of_vregs; i++) {
David Brazdil1abb4192015-02-17 18:33:36 +0000989 vregs_.Put(i, HUserRecord<HEnvironment*>());
David Brazdiled596192015-01-23 10:39:45 +0000990 }
991 }
992
993 void CopyFrom(HEnvironment* env);
994
995 void SetRawEnvAt(size_t index, HInstruction* instruction) {
David Brazdil1abb4192015-02-17 18:33:36 +0000996 vregs_.Put(index, HUserRecord<HEnvironment*>(instruction));
David Brazdiled596192015-01-23 10:39:45 +0000997 }
998
999 HInstruction* GetInstructionAt(size_t index) const {
David Brazdil1abb4192015-02-17 18:33:36 +00001000 return vregs_.Get(index).GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001001 }
1002
David Brazdil1abb4192015-02-17 18:33:36 +00001003 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001004
1005 size_t Size() const { return vregs_.Size(); }
1006
1007 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001008 // Record instructions' use entries of this environment for constant-time removal.
1009 // It should only be called by HInstruction when a new environment use is added.
1010 void RecordEnvUse(HUseListNode<HEnvironment*>* env_use) {
1011 DCHECK(env_use->GetUser() == this);
1012 size_t index = env_use->GetIndex();
1013 vregs_.Put(index, HUserRecord<HEnvironment*>(vregs_.Get(index), env_use));
1014 }
David Brazdiled596192015-01-23 10:39:45 +00001015
David Brazdil1abb4192015-02-17 18:33:36 +00001016 GrowableArray<HUserRecord<HEnvironment*> > vregs_;
David Brazdiled596192015-01-23 10:39:45 +00001017
David Brazdil1abb4192015-02-17 18:33:36 +00001018 friend HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001019
1020 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1021};
1022
Calin Juravleacf735c2015-02-12 15:25:22 +00001023class ReferenceTypeInfo : ValueObject {
1024 public:
Calin Juravleb1498f62015-02-16 13:13:29 +00001025 typedef Handle<mirror::Class> TypeHandle;
1026
1027 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact)
1028 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1029 if (type_handle->IsObjectClass()) {
1030 // Override the type handle to be consistent with the case when we get to
1031 // Top but don't have the Object class available. It avoids having to guess
1032 // what value the type_handle has when it's Top.
1033 return ReferenceTypeInfo(TypeHandle(), is_exact, true);
1034 } else {
1035 return ReferenceTypeInfo(type_handle, is_exact, false);
1036 }
1037 }
1038
1039 static ReferenceTypeInfo CreateTop(bool is_exact) {
1040 return ReferenceTypeInfo(TypeHandle(), is_exact, true);
Calin Juravleacf735c2015-02-12 15:25:22 +00001041 }
1042
1043 bool IsExact() const { return is_exact_; }
1044 bool IsTop() const { return is_top_; }
1045
1046 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
1047
Calin Juravleb1498f62015-02-16 13:13:29 +00001048 bool IsSupertypeOf(ReferenceTypeInfo rti) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Calin Juravleacf735c2015-02-12 15:25:22 +00001049 if (IsTop()) {
1050 // Top (equivalent for java.lang.Object) is supertype of anything.
1051 return true;
1052 }
1053 if (rti.IsTop()) {
1054 // If we get here `this` is not Top() so it can't be a supertype.
1055 return false;
1056 }
1057 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
1058 }
1059
1060 // Returns true if the type information provide the same amount of details.
1061 // Note that it does not mean that the instructions have the same actual type
1062 // (e.g. tops are equal but they can be the result of a merge).
1063 bool IsEqual(ReferenceTypeInfo rti) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1064 if (IsExact() != rti.IsExact()) {
1065 return false;
1066 }
1067 if (IsTop() && rti.IsTop()) {
1068 // `Top` means java.lang.Object, so the types are equivalent.
1069 return true;
1070 }
1071 if (IsTop() || rti.IsTop()) {
1072 // If only one is top or object than they are not equivalent.
1073 // NB: We need this extra check because the type_handle of `Top` is invalid
1074 // and we cannot inspect its reference.
1075 return false;
1076 }
1077
1078 // Finally check the types.
1079 return GetTypeHandle().Get() == rti.GetTypeHandle().Get();
1080 }
1081
1082 private:
Calin Juravleb1498f62015-02-16 13:13:29 +00001083 ReferenceTypeInfo() : ReferenceTypeInfo(TypeHandle(), false, true) {}
1084 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact, bool is_top)
1085 : type_handle_(type_handle), is_exact_(is_exact), is_top_(is_top) {}
1086
Calin Juravleacf735c2015-02-12 15:25:22 +00001087 // The class of the object.
Calin Juravleb1498f62015-02-16 13:13:29 +00001088 TypeHandle type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001089 // Whether or not the type is exact or a superclass of the actual type.
Calin Juravleb1498f62015-02-16 13:13:29 +00001090 // Whether or not we have any information about this type.
Calin Juravleacf735c2015-02-12 15:25:22 +00001091 bool is_exact_;
1092 // A true value here means that the object type should be java.lang.Object.
1093 // We don't have access to the corresponding mirror object every time so this
1094 // flag acts as a substitute. When true, the TypeHandle refers to a null
1095 // pointer and should not be used.
1096 bool is_top_;
1097};
1098
1099std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
1100
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001101class HInstruction : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001102 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001103 explicit HInstruction(SideEffects side_effects)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001104 : previous_(nullptr),
1105 next_(nullptr),
1106 block_(nullptr),
1107 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001108 ssa_index_(-1),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001109 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001110 locations_(nullptr),
1111 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001112 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001113 side_effects_(side_effects),
1114 reference_type_info_(ReferenceTypeInfo::CreateTop(/* is_exact */ false)) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001115
Dave Allison20dfc792014-06-16 20:44:29 -07001116 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001117
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001118#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001119 enum InstructionKind {
1120 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1121 };
1122#undef DECLARE_KIND
1123
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001124 HInstruction* GetNext() const { return next_; }
1125 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001126
Calin Juravle77520bc2015-01-12 18:45:46 +00001127 HInstruction* GetNextDisregardingMoves() const;
1128 HInstruction* GetPreviousDisregardingMoves() const;
1129
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001130 HBasicBlock* GetBlock() const { return block_; }
1131 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001132 bool IsInBlock() const { return block_ != nullptr; }
1133 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +01001134 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001135
Roland Levillain6b879dd2014-09-22 17:13:44 +01001136 virtual size_t InputCount() const = 0;
David Brazdil1abb4192015-02-17 18:33:36 +00001137 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001138
1139 virtual void Accept(HGraphVisitor* visitor) = 0;
1140 virtual const char* DebugName() const = 0;
1141
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001142 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
David Brazdil1abb4192015-02-17 18:33:36 +00001143 void SetRawInputAt(size_t index, HInstruction* input) {
1144 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1145 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001146
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001147 virtual bool NeedsEnvironment() const { return false; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001148 virtual bool IsControlFlow() const { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01001149 virtual bool CanThrow() const { return false; }
Roland Levillain72bceff2014-09-15 18:29:00 +01001150 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001151
Calin Juravle61d544b2015-02-23 16:46:57 +00001152 virtual bool ActAsNullConstant() const { return false; }
1153
Calin Juravle10e244f2015-01-26 18:54:32 +00001154 // Does not apply for all instructions, but having this at top level greatly
1155 // simplifies the null check elimination.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001156 virtual bool CanBeNull() const {
1157 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1158 return true;
1159 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001160
Calin Juravle77520bc2015-01-12 18:45:46 +00001161 virtual bool CanDoImplicitNullCheck() const { return false; }
1162
Calin Juravleacf735c2015-02-12 15:25:22 +00001163 void SetReferenceTypeInfo(ReferenceTypeInfo reference_type_info) {
Calin Juravle61d544b2015-02-23 16:46:57 +00001164 DCHECK_EQ(GetType(), Primitive::kPrimNot);
Calin Juravleacf735c2015-02-12 15:25:22 +00001165 reference_type_info_ = reference_type_info;
1166 }
1167
Calin Juravle61d544b2015-02-23 16:46:57 +00001168 ReferenceTypeInfo GetReferenceTypeInfo() const {
1169 DCHECK_EQ(GetType(), Primitive::kPrimNot);
1170 return reference_type_info_;
1171 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001172
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001173 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001174 DCHECK(user != nullptr);
1175 HUseListNode<HInstruction*>* use =
1176 uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1177 user->SetRawInputRecordAt(index, HUserRecord<HInstruction*>(user->InputRecordAt(index), use));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001178 }
1179
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001180 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001181 DCHECK(user != nullptr);
David Brazdiled596192015-01-23 10:39:45 +00001182 HUseListNode<HEnvironment*>* env_use =
1183 env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1184 user->RecordEnvUse(env_use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001185 }
1186
David Brazdil1abb4192015-02-17 18:33:36 +00001187 void RemoveAsUserOfInput(size_t input) {
1188 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
1189 input_use.GetInstruction()->uses_.Remove(input_use.GetUseNode());
1190 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001191
David Brazdil1abb4192015-02-17 18:33:36 +00001192 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1193 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001194
David Brazdiled596192015-01-23 10:39:45 +00001195 bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); }
1196 bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); }
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001197 bool HasNonEnvironmentUses() const { return !uses_.IsEmpty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001198 bool HasOnlyOneNonEnvironmentUse() const {
1199 return !HasEnvironmentUses() && GetUses().HasOnlyOneUse();
1200 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001201
Roland Levillain6c82d402014-10-13 16:10:27 +01001202 // Does this instruction strictly dominate `other_instruction`?
1203 // Returns false if this instruction and `other_instruction` are the same.
1204 // Aborts if this instruction and `other_instruction` are both phis.
1205 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001206
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001207 int GetId() const { return id_; }
1208 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001209
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001210 int GetSsaIndex() const { return ssa_index_; }
1211 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1212 bool HasSsaIndex() const { return ssa_index_ != -1; }
1213
1214 bool HasEnvironment() const { return environment_ != nullptr; }
1215 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001216 // Set the `environment_` field. Raw because this method does not
1217 // update the uses lists.
1218 void SetRawEnvironment(HEnvironment* environment) { environment_ = environment; }
1219
1220 // Set the environment of this instruction, copying it from `environment`. While
1221 // copying, the uses lists are being updated.
1222 void CopyEnvironmentFrom(HEnvironment* environment) {
1223 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
1224 environment_ = new (allocator) HEnvironment(allocator, environment->Size());
1225 environment_->CopyFrom(environment);
1226 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001227
Nicolas Geoffray39468442014-09-02 15:17:15 +01001228 // Returns the number of entries in the environment. Typically, that is the
1229 // number of dex registers in a method. It could be more in case of inlining.
1230 size_t EnvironmentSize() const;
1231
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001232 LocationSummary* GetLocations() const { return locations_; }
1233 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001234
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001235 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001236 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001237
Alexandre Rames188d4312015-04-09 18:30:21 +01001238 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
1239 // uses of this instruction by `other` are *not* updated.
1240 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
1241 ReplaceWith(other);
1242 other->ReplaceInput(this, use_index);
1243 }
1244
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001245 // Move `this` instruction before `cursor`.
1246 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001247
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001248#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01001249 bool Is##type() const { return (As##type() != nullptr); } \
1250 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001251 virtual H##type* As##type() { return nullptr; }
1252
1253 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
1254#undef INSTRUCTION_TYPE_CHECK
1255
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001256 // Returns whether the instruction can be moved within the graph.
1257 virtual bool CanBeMoved() const { return false; }
1258
1259 // Returns whether the two instructions are of the same kind.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001260 virtual bool InstructionTypeEquals(HInstruction* other) const {
1261 UNUSED(other);
1262 return false;
1263 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001264
1265 // Returns whether any data encoded in the two instructions is equal.
1266 // This method does not look at the inputs. Both instructions must be
1267 // of the same type, otherwise the method has undefined behavior.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001268 virtual bool InstructionDataEquals(HInstruction* other) const {
1269 UNUSED(other);
1270 return false;
1271 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001272
1273 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00001274 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001275 // 2) Their inputs are identical.
1276 bool Equals(HInstruction* other) const;
1277
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001278 virtual InstructionKind GetKind() const = 0;
1279
1280 virtual size_t ComputeHashCode() const {
1281 size_t result = GetKind();
1282 for (size_t i = 0, e = InputCount(); i < e; ++i) {
1283 result = (result * 31) + InputAt(i)->GetId();
1284 }
1285 return result;
1286 }
1287
1288 SideEffects GetSideEffects() const { return side_effects_; }
1289
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001290 size_t GetLifetimePosition() const { return lifetime_position_; }
1291 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
1292 LiveInterval* GetLiveInterval() const { return live_interval_; }
1293 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
1294 bool HasLiveInterval() const { return live_interval_ != nullptr; }
1295
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001296 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
1297
1298 // Returns whether the code generation of the instruction will require to have access
1299 // to the current method. Such instructions are:
1300 // (1): Instructions that require an environment, as calling the runtime requires
1301 // to walk the stack and have the current method stored at a specific stack address.
1302 // (2): Object literals like classes and strings, that are loaded from the dex cache
1303 // fields of the current method.
1304 bool NeedsCurrentMethod() const {
1305 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
1306 }
1307
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001308 virtual bool NeedsDexCache() const { return false; }
1309
David Brazdil1abb4192015-02-17 18:33:36 +00001310 protected:
1311 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
1312 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
1313
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001314 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001315 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); }
1316
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001317 HInstruction* previous_;
1318 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001319 HBasicBlock* block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001320
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001321 // An instruction gets an id when it is added to the graph.
1322 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01001323 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001324 int id_;
1325
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001326 // When doing liveness analysis, instructions that have uses get an SSA index.
1327 int ssa_index_;
1328
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001329 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00001330 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001331
1332 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00001333 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001334
Nicolas Geoffray39468442014-09-02 15:17:15 +01001335 // The environment associated with this instruction. Not null if the instruction
1336 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001337 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001338
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001339 // Set by the code generator.
1340 LocationSummary* locations_;
1341
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001342 // Set by the liveness analysis.
1343 LiveInterval* live_interval_;
1344
1345 // Set by the liveness analysis, this is the position in a linear
1346 // order of blocks where this instruction's live interval start.
1347 size_t lifetime_position_;
1348
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001349 const SideEffects side_effects_;
1350
Calin Juravleacf735c2015-02-12 15:25:22 +00001351 // TODO: for primitive types this should be marked as invalid.
1352 ReferenceTypeInfo reference_type_info_;
1353
David Brazdil1abb4192015-02-17 18:33:36 +00001354 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001355 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00001356 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001357 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001358 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001359
1360 DISALLOW_COPY_AND_ASSIGN(HInstruction);
1361};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001362std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001363
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001364class HInputIterator : public ValueObject {
1365 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001366 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001367
1368 bool Done() const { return index_ == instruction_->InputCount(); }
1369 HInstruction* Current() const { return instruction_->InputAt(index_); }
1370 void Advance() { index_++; }
1371
1372 private:
1373 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001374 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001375
1376 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
1377};
1378
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001379class HInstructionIterator : public ValueObject {
1380 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001381 explicit HInstructionIterator(const HInstructionList& instructions)
1382 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001383 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001384 }
1385
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001386 bool Done() const { return instruction_ == nullptr; }
1387 HInstruction* Current() const { return instruction_; }
1388 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001389 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001390 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001391 }
1392
1393 private:
1394 HInstruction* instruction_;
1395 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001396
1397 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001398};
1399
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001400class HBackwardInstructionIterator : public ValueObject {
1401 public:
1402 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
1403 : instruction_(instructions.last_instruction_) {
1404 next_ = Done() ? nullptr : instruction_->GetPrevious();
1405 }
1406
1407 bool Done() const { return instruction_ == nullptr; }
1408 HInstruction* Current() const { return instruction_; }
1409 void Advance() {
1410 instruction_ = next_;
1411 next_ = Done() ? nullptr : instruction_->GetPrevious();
1412 }
1413
1414 private:
1415 HInstruction* instruction_;
1416 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001417
1418 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001419};
1420
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001421// An embedded container with N elements of type T. Used (with partial
1422// specialization for N=0) because embedded arrays cannot have size 0.
1423template<typename T, intptr_t N>
1424class EmbeddedArray {
1425 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001426 EmbeddedArray() : elements_() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001427
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001428 intptr_t GetLength() const { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001429
1430 const T& operator[](intptr_t i) const {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001431 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001432 return elements_[i];
1433 }
1434
1435 T& operator[](intptr_t i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001436 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001437 return elements_[i];
1438 }
1439
1440 const T& At(intptr_t i) const {
1441 return (*this)[i];
1442 }
1443
1444 void SetAt(intptr_t i, const T& val) {
1445 (*this)[i] = val;
1446 }
1447
1448 private:
1449 T elements_[N];
1450};
1451
1452template<typename T>
1453class EmbeddedArray<T, 0> {
1454 public:
1455 intptr_t length() const { return 0; }
1456 const T& operator[](intptr_t i) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001457 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001458 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001459 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001460 }
1461 T& operator[](intptr_t i) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001462 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001463 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001464 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001465 }
1466};
1467
1468template<intptr_t N>
1469class HTemplateInstruction: public HInstruction {
1470 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001471 HTemplateInstruction<N>(SideEffects side_effects)
1472 : HInstruction(side_effects), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07001473 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001474
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001475 size_t InputCount() const OVERRIDE { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001476
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001477 protected:
David Brazdil1abb4192015-02-17 18:33:36 +00001478 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_[i]; }
1479
1480 void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE {
1481 inputs_[i] = input;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001482 }
1483
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001484 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001485 EmbeddedArray<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001486
1487 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001488};
1489
Dave Allison20dfc792014-06-16 20:44:29 -07001490template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001491class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07001492 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001493 HExpression<N>(Primitive::Type type, SideEffects side_effects)
1494 : HTemplateInstruction<N>(side_effects), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07001495 virtual ~HExpression() {}
1496
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001497 Primitive::Type GetType() const OVERRIDE { return type_; }
Dave Allison20dfc792014-06-16 20:44:29 -07001498
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001499 protected:
1500 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07001501};
1502
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001503// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
1504// instruction that branches to the exit block.
1505class HReturnVoid : public HTemplateInstruction<0> {
1506 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001507 HReturnVoid() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001508
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001509 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001510
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001511 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001512
1513 private:
1514 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
1515};
1516
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001517// Represents dex's RETURN opcodes. A HReturn is a control flow
1518// instruction that branches to the exit block.
1519class HReturn : public HTemplateInstruction<1> {
1520 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001521 explicit HReturn(HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001522 SetRawInputAt(0, value);
1523 }
1524
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001525 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001526
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001527 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001528
1529 private:
1530 DISALLOW_COPY_AND_ASSIGN(HReturn);
1531};
1532
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001533// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001534// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001535// exit block.
1536class HExit : public HTemplateInstruction<0> {
1537 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001538 HExit() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001539
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001540 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001541
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001542 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001543
1544 private:
1545 DISALLOW_COPY_AND_ASSIGN(HExit);
1546};
1547
1548// Jumps from one block to another.
1549class HGoto : public HTemplateInstruction<0> {
1550 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001551 HGoto() : HTemplateInstruction(SideEffects::None()) {}
1552
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001553 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001554
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001555 HBasicBlock* GetSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001556 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001557 }
1558
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001559 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001560
1561 private:
1562 DISALLOW_COPY_AND_ASSIGN(HGoto);
1563};
1564
Dave Allison20dfc792014-06-16 20:44:29 -07001565
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001566// Conditional branch. A block ending with an HIf instruction must have
1567// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001568class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001569 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001570 explicit HIf(HInstruction* input) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001571 SetRawInputAt(0, input);
1572 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001573
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001574 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001575
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001576 HBasicBlock* IfTrueSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001577 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001578 }
1579
1580 HBasicBlock* IfFalseSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001581 return GetBlock()->GetSuccessors().Get(1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001582 }
1583
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001584 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001585
1586 private:
1587 DISALLOW_COPY_AND_ASSIGN(HIf);
1588};
1589
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001590// Deoptimize to interpreter, upon checking a condition.
1591class HDeoptimize : public HTemplateInstruction<1> {
1592 public:
1593 HDeoptimize(HInstruction* cond, uint32_t dex_pc)
1594 : HTemplateInstruction(SideEffects::None()),
1595 dex_pc_(dex_pc) {
1596 SetRawInputAt(0, cond);
1597 }
1598
1599 bool NeedsEnvironment() const OVERRIDE { return true; }
1600 bool CanThrow() const OVERRIDE { return true; }
1601 uint32_t GetDexPc() const { return dex_pc_; }
1602
1603 DECLARE_INSTRUCTION(Deoptimize);
1604
1605 private:
1606 uint32_t dex_pc_;
1607
1608 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
1609};
1610
Roland Levillain88cb1752014-10-20 16:36:47 +01001611class HUnaryOperation : public HExpression<1> {
1612 public:
1613 HUnaryOperation(Primitive::Type result_type, HInstruction* input)
1614 : HExpression(result_type, SideEffects::None()) {
1615 SetRawInputAt(0, input);
1616 }
1617
1618 HInstruction* GetInput() const { return InputAt(0); }
1619 Primitive::Type GetResultType() const { return GetType(); }
1620
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001621 bool CanBeMoved() const OVERRIDE { return true; }
1622 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001623 UNUSED(other);
1624 return true;
1625 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001626
Roland Levillain9240d6a2014-10-20 16:47:04 +01001627 // Try to statically evaluate `operation` and return a HConstant
1628 // containing the result of this evaluation. If `operation` cannot
1629 // be evaluated as a constant, return nullptr.
1630 HConstant* TryStaticEvaluation() const;
1631
1632 // Apply this operation to `x`.
1633 virtual int32_t Evaluate(int32_t x) const = 0;
1634 virtual int64_t Evaluate(int64_t x) const = 0;
1635
Roland Levillain88cb1752014-10-20 16:36:47 +01001636 DECLARE_INSTRUCTION(UnaryOperation);
1637
1638 private:
1639 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
1640};
1641
Dave Allison20dfc792014-06-16 20:44:29 -07001642class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001643 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001644 HBinaryOperation(Primitive::Type result_type,
1645 HInstruction* left,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001646 HInstruction* right) : HExpression(result_type, SideEffects::None()) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001647 SetRawInputAt(0, left);
1648 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001649 }
1650
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001651 HInstruction* GetLeft() const { return InputAt(0); }
1652 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07001653 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001654
Mingyao Yangdc5ac732015-02-25 11:28:05 -08001655 virtual bool IsCommutative() const { return false; }
1656
1657 // Put constant on the right.
1658 // Returns whether order is changed.
1659 bool OrderInputsWithConstantOnTheRight() {
1660 HInstruction* left = InputAt(0);
1661 HInstruction* right = InputAt(1);
1662 if (left->IsConstant() && !right->IsConstant()) {
1663 ReplaceInput(right, 0);
1664 ReplaceInput(left, 1);
1665 return true;
1666 }
1667 return false;
1668 }
1669
1670 // Order inputs by instruction id, but favor constant on the right side.
1671 // This helps GVN for commutative ops.
1672 void OrderInputs() {
1673 DCHECK(IsCommutative());
1674 HInstruction* left = InputAt(0);
1675 HInstruction* right = InputAt(1);
1676 if (left == right || (!left->IsConstant() && right->IsConstant())) {
1677 return;
1678 }
1679 if (OrderInputsWithConstantOnTheRight()) {
1680 return;
1681 }
1682 // Order according to instruction id.
1683 if (left->GetId() > right->GetId()) {
1684 ReplaceInput(right, 0);
1685 ReplaceInput(left, 1);
1686 }
1687 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001688
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001689 bool CanBeMoved() const OVERRIDE { return true; }
1690 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001691 UNUSED(other);
1692 return true;
1693 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001694
Roland Levillain9240d6a2014-10-20 16:47:04 +01001695 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01001696 // containing the result of this evaluation. If `operation` cannot
1697 // be evaluated as a constant, return nullptr.
Roland Levillain9240d6a2014-10-20 16:47:04 +01001698 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01001699
1700 // Apply this operation to `x` and `y`.
1701 virtual int32_t Evaluate(int32_t x, int32_t y) const = 0;
1702 virtual int64_t Evaluate(int64_t x, int64_t y) const = 0;
1703
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001704 // Returns an input that can legally be used as the right input and is
1705 // constant, or nullptr.
1706 HConstant* GetConstantRight() const;
1707
1708 // If `GetConstantRight()` returns one of the input, this returns the other
1709 // one. Otherwise it returns nullptr.
1710 HInstruction* GetLeastConstantLeft() const;
1711
Roland Levillainccc07a92014-09-16 14:48:16 +01001712 DECLARE_INSTRUCTION(BinaryOperation);
1713
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001714 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001715 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
1716};
1717
Dave Allison20dfc792014-06-16 20:44:29 -07001718class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001719 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001720 HCondition(HInstruction* first, HInstruction* second)
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001721 : HBinaryOperation(Primitive::kPrimBoolean, first, second),
1722 needs_materialization_(true) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001723
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001724 bool NeedsMaterialization() const { return needs_materialization_; }
1725 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001726
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001727 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001728 // `instruction`, and disregard moves in between.
1729 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001730
Dave Allison20dfc792014-06-16 20:44:29 -07001731 DECLARE_INSTRUCTION(Condition);
1732
1733 virtual IfCondition GetCondition() const = 0;
1734
1735 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001736 // For register allocation purposes, returns whether this instruction needs to be
1737 // materialized (that is, not just be in the processor flags).
1738 bool needs_materialization_;
1739
Dave Allison20dfc792014-06-16 20:44:29 -07001740 DISALLOW_COPY_AND_ASSIGN(HCondition);
1741};
1742
1743// Instruction to check if two inputs are equal to each other.
1744class HEqual : public HCondition {
1745 public:
1746 HEqual(HInstruction* first, HInstruction* second)
1747 : HCondition(first, second) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001748
Mingyao Yangdc5ac732015-02-25 11:28:05 -08001749 bool IsCommutative() const OVERRIDE { return true; }
1750
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001751 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001752 return x == y ? 1 : 0;
1753 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001754 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001755 return x == y ? 1 : 0;
1756 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001757
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001758 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001759
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001760 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001761 return kCondEQ;
1762 }
1763
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001764 private:
1765 DISALLOW_COPY_AND_ASSIGN(HEqual);
1766};
1767
Dave Allison20dfc792014-06-16 20:44:29 -07001768class HNotEqual : public HCondition {
1769 public:
1770 HNotEqual(HInstruction* first, HInstruction* second)
1771 : HCondition(first, second) {}
1772
Mingyao Yangdc5ac732015-02-25 11:28:05 -08001773 bool IsCommutative() const OVERRIDE { return true; }
1774
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001775 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001776 return x != y ? 1 : 0;
1777 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001778 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001779 return x != y ? 1 : 0;
1780 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001781
Dave Allison20dfc792014-06-16 20:44:29 -07001782 DECLARE_INSTRUCTION(NotEqual);
1783
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001784 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001785 return kCondNE;
1786 }
1787
1788 private:
1789 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
1790};
1791
1792class HLessThan : public HCondition {
1793 public:
1794 HLessThan(HInstruction* first, HInstruction* second)
1795 : HCondition(first, second) {}
1796
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001797 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001798 return x < y ? 1 : 0;
1799 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001800 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001801 return x < y ? 1 : 0;
1802 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001803
Dave Allison20dfc792014-06-16 20:44:29 -07001804 DECLARE_INSTRUCTION(LessThan);
1805
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001806 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001807 return kCondLT;
1808 }
1809
1810 private:
1811 DISALLOW_COPY_AND_ASSIGN(HLessThan);
1812};
1813
1814class HLessThanOrEqual : public HCondition {
1815 public:
1816 HLessThanOrEqual(HInstruction* first, HInstruction* second)
1817 : HCondition(first, second) {}
1818
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001819 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001820 return x <= y ? 1 : 0;
1821 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001822 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001823 return x <= y ? 1 : 0;
1824 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001825
Dave Allison20dfc792014-06-16 20:44:29 -07001826 DECLARE_INSTRUCTION(LessThanOrEqual);
1827
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001828 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001829 return kCondLE;
1830 }
1831
1832 private:
1833 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
1834};
1835
1836class HGreaterThan : public HCondition {
1837 public:
1838 HGreaterThan(HInstruction* first, HInstruction* second)
1839 : HCondition(first, second) {}
1840
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001841 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001842 return x > y ? 1 : 0;
1843 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001844 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001845 return x > y ? 1 : 0;
1846 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001847
Dave Allison20dfc792014-06-16 20:44:29 -07001848 DECLARE_INSTRUCTION(GreaterThan);
1849
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001850 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001851 return kCondGT;
1852 }
1853
1854 private:
1855 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
1856};
1857
1858class HGreaterThanOrEqual : public HCondition {
1859 public:
1860 HGreaterThanOrEqual(HInstruction* first, HInstruction* second)
1861 : HCondition(first, second) {}
1862
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001863 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001864 return x >= y ? 1 : 0;
1865 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001866 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001867 return x >= y ? 1 : 0;
1868 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001869
Dave Allison20dfc792014-06-16 20:44:29 -07001870 DECLARE_INSTRUCTION(GreaterThanOrEqual);
1871
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001872 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001873 return kCondGE;
1874 }
1875
1876 private:
1877 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
1878};
1879
1880
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001881// Instruction to check how two inputs compare to each other.
1882// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
1883class HCompare : public HBinaryOperation {
1884 public:
Calin Juravleddb7df22014-11-25 20:56:51 +00001885 // The bias applies for floating point operations and indicates how NaN
1886 // comparisons are treated:
1887 enum Bias {
1888 kNoBias, // bias is not applicable (i.e. for long operation)
1889 kGtBias, // return 1 for NaN comparisons
1890 kLtBias, // return -1 for NaN comparisons
1891 };
1892
1893 HCompare(Primitive::Type type, HInstruction* first, HInstruction* second, Bias bias)
1894 : HBinaryOperation(Primitive::kPrimInt, first, second), bias_(bias) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001895 DCHECK_EQ(type, first->GetType());
1896 DCHECK_EQ(type, second->GetType());
1897 }
1898
Calin Juravleddb7df22014-11-25 20:56:51 +00001899 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01001900 return
1901 x == y ? 0 :
1902 x > y ? 1 :
1903 -1;
1904 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001905
1906 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01001907 return
1908 x == y ? 0 :
1909 x > y ? 1 :
1910 -1;
1911 }
1912
Calin Juravleddb7df22014-11-25 20:56:51 +00001913 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
1914 return bias_ == other->AsCompare()->bias_;
1915 }
1916
1917 bool IsGtBias() { return bias_ == kGtBias; }
1918
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001919 DECLARE_INSTRUCTION(Compare);
1920
1921 private:
Calin Juravleddb7df22014-11-25 20:56:51 +00001922 const Bias bias_;
1923
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001924 DISALLOW_COPY_AND_ASSIGN(HCompare);
1925};
1926
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001927// A local in the graph. Corresponds to a Dex register.
1928class HLocal : public HTemplateInstruction<0> {
1929 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001930 explicit HLocal(uint16_t reg_number)
1931 : HTemplateInstruction(SideEffects::None()), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001932
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001933 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001934
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001935 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001936
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001937 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001938 // The Dex register number.
1939 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001940
1941 DISALLOW_COPY_AND_ASSIGN(HLocal);
1942};
1943
1944// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07001945class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001946 public:
Roland Levillain5799fc02014-09-25 12:15:20 +01001947 HLoadLocal(HLocal* local, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001948 : HExpression(type, SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001949 SetRawInputAt(0, local);
1950 }
1951
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001952 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1953
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001954 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001955
1956 private:
1957 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
1958};
1959
1960// Store a value in a given local. This instruction has two inputs: the value
1961// and the local.
1962class HStoreLocal : public HTemplateInstruction<2> {
1963 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001964 HStoreLocal(HLocal* local, HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001965 SetRawInputAt(0, local);
1966 SetRawInputAt(1, value);
1967 }
1968
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001969 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1970
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001971 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001972
1973 private:
1974 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
1975};
1976
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001977class HConstant : public HExpression<0> {
1978 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001979 explicit HConstant(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
1980
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001981 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001982
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001983 virtual bool IsMinusOne() const { return false; }
1984 virtual bool IsZero() const { return false; }
1985 virtual bool IsOne() const { return false; }
1986
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001987 DECLARE_INSTRUCTION(Constant);
1988
1989 private:
1990 DISALLOW_COPY_AND_ASSIGN(HConstant);
1991};
1992
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001993class HFloatConstant : public HConstant {
1994 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001995 float GetValue() const { return value_; }
1996
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001997 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001998 return bit_cast<uint32_t, float>(other->AsFloatConstant()->value_) ==
1999 bit_cast<uint32_t, float>(value_);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002000 }
2001
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002002 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002003
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002004 bool IsMinusOne() const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002005 return bit_cast<uint32_t, float>(AsFloatConstant()->GetValue()) ==
2006 bit_cast<uint32_t, float>((-1.0f));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002007 }
2008 bool IsZero() const OVERRIDE {
2009 return AsFloatConstant()->GetValue() == 0.0f;
2010 }
2011 bool IsOne() const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002012 return bit_cast<uint32_t, float>(AsFloatConstant()->GetValue()) ==
2013 bit_cast<uint32_t, float>(1.0f);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002014 }
2015
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002016 DECLARE_INSTRUCTION(FloatConstant);
2017
2018 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002019 explicit HFloatConstant(float value) : HConstant(Primitive::kPrimFloat), value_(value) {}
2020
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002021 const float value_;
2022
David Brazdil8d5b8b22015-03-24 10:51:52 +00002023 // Only the SsaBuilder can currently create floating-point constants. If we
2024 // ever need to create them later in the pipeline, we will have to handle them
2025 // the same way as integral constants.
2026 friend class SsaBuilder;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002027 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2028};
2029
2030class HDoubleConstant : public HConstant {
2031 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002032 double GetValue() const { return value_; }
2033
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002034 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002035 return bit_cast<uint64_t, double>(other->AsDoubleConstant()->value_) ==
2036 bit_cast<uint64_t, double>(value_);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002037 }
2038
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002039 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002040
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002041 bool IsMinusOne() const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002042 return bit_cast<uint64_t, double>(AsDoubleConstant()->GetValue()) ==
2043 bit_cast<uint64_t, double>((-1.0));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002044 }
2045 bool IsZero() const OVERRIDE {
2046 return AsDoubleConstant()->GetValue() == 0.0;
2047 }
2048 bool IsOne() const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002049 return bit_cast<uint64_t, double>(AsDoubleConstant()->GetValue()) ==
2050 bit_cast<uint64_t, double>(1.0);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002051 }
2052
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002053 DECLARE_INSTRUCTION(DoubleConstant);
2054
2055 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002056 explicit HDoubleConstant(double value) : HConstant(Primitive::kPrimDouble), value_(value) {}
2057
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002058 const double value_;
2059
David Brazdil8d5b8b22015-03-24 10:51:52 +00002060 // Only the SsaBuilder can currently create floating-point constants. If we
2061 // ever need to create them later in the pipeline, we will have to handle them
2062 // the same way as integral constants.
2063 friend class SsaBuilder;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002064 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
2065};
2066
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002067class HNullConstant : public HConstant {
2068 public:
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002069 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2070 return true;
2071 }
2072
2073 size_t ComputeHashCode() const OVERRIDE { return 0; }
2074
Calin Juravle61d544b2015-02-23 16:46:57 +00002075 bool ActAsNullConstant() const OVERRIDE { return true; }
2076
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002077 DECLARE_INSTRUCTION(NullConstant);
2078
2079 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002080 HNullConstant() : HConstant(Primitive::kPrimNot) {}
2081
2082 friend class HGraph;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002083 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2084};
2085
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002086// Constants of the type int. Those can be from Dex instructions, or
2087// synthesized (for example with the if-eqz instruction).
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002088class HIntConstant : public HConstant {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002089 public:
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002090 int32_t GetValue() const { return value_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002091
Calin Juravle61d544b2015-02-23 16:46:57 +00002092 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002093 return other->AsIntConstant()->value_ == value_;
2094 }
2095
Calin Juravle61d544b2015-02-23 16:46:57 +00002096 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2097
2098 // TODO: Null is represented by the `0` constant. In most cases we replace it
2099 // with a HNullConstant but we don't do it when comparing (a != null). This
2100 // method is an workaround until we fix the above.
2101 bool ActAsNullConstant() const OVERRIDE { return value_ == 0; }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002102
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002103 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2104 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2105 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2106
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002107 DECLARE_INSTRUCTION(IntConstant);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002108
2109 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002110 explicit HIntConstant(int32_t value) : HConstant(Primitive::kPrimInt), value_(value) {}
2111
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002112 const int32_t value_;
2113
David Brazdil8d5b8b22015-03-24 10:51:52 +00002114 friend class HGraph;
2115 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
Zheng Xuad4450e2015-04-17 18:48:56 +08002116 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002117 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2118};
2119
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002120class HLongConstant : public HConstant {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002121 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002122 int64_t GetValue() const { return value_; }
2123
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002124 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002125 return other->AsLongConstant()->value_ == value_;
2126 }
2127
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002128 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002129
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002130 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2131 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2132 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2133
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002134 DECLARE_INSTRUCTION(LongConstant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002135
2136 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002137 explicit HLongConstant(int64_t value) : HConstant(Primitive::kPrimLong), value_(value) {}
2138
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002139 const int64_t value_;
2140
David Brazdil8d5b8b22015-03-24 10:51:52 +00002141 friend class HGraph;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002142 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2143};
2144
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002145enum class Intrinsics {
2146#define OPTIMIZING_INTRINSICS(Name, IsStatic) k ## Name,
2147#include "intrinsics_list.h"
2148 kNone,
2149 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
2150#undef INTRINSICS_LIST
2151#undef OPTIMIZING_INTRINSICS
2152};
2153std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
2154
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002155class HInvoke : public HInstruction {
2156 public:
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002157 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002158
2159 // Runtime needs to walk the stack, so Dex -> Dex calls need to
2160 // know their environment.
Calin Juravle77520bc2015-01-12 18:45:46 +00002161 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002162
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01002163 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002164 SetRawInputAt(index, argument);
2165 }
2166
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002167 Primitive::Type GetType() const OVERRIDE { return return_type_; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002168
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002169 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002170
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002171 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
2172
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01002173 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002174 return intrinsic_;
2175 }
2176
2177 void SetIntrinsic(Intrinsics intrinsic) {
2178 intrinsic_ = intrinsic;
2179 }
2180
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002181 DECLARE_INSTRUCTION(Invoke);
2182
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002183 protected:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002184 HInvoke(ArenaAllocator* arena,
2185 uint32_t number_of_arguments,
2186 Primitive::Type return_type,
2187 uint32_t dex_pc,
2188 uint32_t dex_method_index)
2189 : HInstruction(SideEffects::All()),
2190 inputs_(arena, number_of_arguments),
2191 return_type_(return_type),
2192 dex_pc_(dex_pc),
2193 dex_method_index_(dex_method_index),
2194 intrinsic_(Intrinsics::kNone) {
2195 inputs_.SetSize(number_of_arguments);
2196 }
2197
David Brazdil1abb4192015-02-17 18:33:36 +00002198 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
2199 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
2200 inputs_.Put(index, input);
2201 }
2202
2203 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002204 const Primitive::Type return_type_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002205 const uint32_t dex_pc_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002206 const uint32_t dex_method_index_;
2207 Intrinsics intrinsic_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002208
2209 private:
2210 DISALLOW_COPY_AND_ASSIGN(HInvoke);
2211};
2212
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002213class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002214 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01002215 // Requirements of this method call regarding the class
2216 // initialization (clinit) check of its declaring class.
2217 enum class ClinitCheckRequirement {
2218 kNone, // Class already initialized.
2219 kExplicit, // Static call having explicit clinit check as last input.
2220 kImplicit, // Static call implicitly requiring a clinit check.
2221 };
2222
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002223 HInvokeStaticOrDirect(ArenaAllocator* arena,
2224 uint32_t number_of_arguments,
2225 Primitive::Type return_type,
2226 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002227 uint32_t dex_method_index,
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002228 bool is_recursive,
Nicolas Geoffray79041292015-03-26 10:05:54 +00002229 InvokeType original_invoke_type,
Roland Levillain4c0eb422015-04-24 16:43:49 +01002230 InvokeType invoke_type,
2231 ClinitCheckRequirement clinit_check_requirement)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002232 : HInvoke(arena, number_of_arguments, return_type, dex_pc, dex_method_index),
Nicolas Geoffray79041292015-03-26 10:05:54 +00002233 original_invoke_type_(original_invoke_type),
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002234 invoke_type_(invoke_type),
Roland Levillain4c0eb422015-04-24 16:43:49 +01002235 is_recursive_(is_recursive),
2236 clinit_check_requirement_(clinit_check_requirement) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002237
Calin Juravle77520bc2015-01-12 18:45:46 +00002238 bool CanDoImplicitNullCheck() const OVERRIDE {
2239 // We access the method via the dex cache so we can't do an implicit null check.
2240 // TODO: for intrinsics we can generate implicit null checks.
2241 return false;
2242 }
2243
Nicolas Geoffray79041292015-03-26 10:05:54 +00002244 InvokeType GetOriginalInvokeType() const { return original_invoke_type_; }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002245 InvokeType GetInvokeType() const { return invoke_type_; }
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002246 bool IsRecursive() const { return is_recursive_; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002247 bool NeedsDexCache() const OVERRIDE { return !IsRecursive(); }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002248
Roland Levillain4c0eb422015-04-24 16:43:49 +01002249 // Is this instruction a call to a static method?
2250 bool IsStatic() const {
2251 return GetInvokeType() == kStatic;
2252 }
2253
2254 // Remove the art::HLoadClass instruction set as last input by
2255 // art::PrepareForRegisterAllocation::VisitClinitCheck in lieu of
2256 // the initial art::HClinitCheck instruction (only relevant for
2257 // static calls with explicit clinit check).
2258 void RemoveLoadClassAsLastInput() {
2259 DCHECK(IsStaticWithExplicitClinitCheck());
2260 size_t last_input_index = InputCount() - 1;
2261 HInstruction* last_input = InputAt(last_input_index);
2262 DCHECK(last_input != nullptr);
2263 DCHECK(last_input->IsLoadClass()) << last_input->DebugName();
2264 RemoveAsUserOfInput(last_input_index);
2265 inputs_.DeleteAt(last_input_index);
2266 clinit_check_requirement_ = ClinitCheckRequirement::kImplicit;
2267 DCHECK(IsStaticWithImplicitClinitCheck());
2268 }
2269
2270 // Is this a call to a static method whose declaring class has an
2271 // explicit intialization check in the graph?
2272 bool IsStaticWithExplicitClinitCheck() const {
2273 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kExplicit);
2274 }
2275
2276 // Is this a call to a static method whose declaring class has an
2277 // implicit intialization check requirement?
2278 bool IsStaticWithImplicitClinitCheck() const {
2279 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kImplicit);
2280 }
2281
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002282 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002283
Roland Levillain4c0eb422015-04-24 16:43:49 +01002284 protected:
2285 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
2286 const HUserRecord<HInstruction*> input_record = HInvoke::InputRecordAt(i);
2287 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck() && (i == InputCount() - 1)) {
2288 HInstruction* input = input_record.GetInstruction();
2289 // `input` is the last input of a static invoke marked as having
2290 // an explicit clinit check. It must either be:
2291 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
2292 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
2293 DCHECK(input != nullptr);
2294 DCHECK(input->IsClinitCheck() || input->IsLoadClass()) << input->DebugName();
2295 }
2296 return input_record;
2297 }
2298
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002299 private:
Nicolas Geoffray79041292015-03-26 10:05:54 +00002300 const InvokeType original_invoke_type_;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002301 const InvokeType invoke_type_;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002302 const bool is_recursive_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01002303 ClinitCheckRequirement clinit_check_requirement_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002304
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002305 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002306};
2307
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002308class HInvokeVirtual : public HInvoke {
2309 public:
2310 HInvokeVirtual(ArenaAllocator* arena,
2311 uint32_t number_of_arguments,
2312 Primitive::Type return_type,
2313 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002314 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002315 uint32_t vtable_index)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002316 : HInvoke(arena, number_of_arguments, return_type, dex_pc, dex_method_index),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002317 vtable_index_(vtable_index) {}
2318
Calin Juravle77520bc2015-01-12 18:45:46 +00002319 bool CanDoImplicitNullCheck() const OVERRIDE {
2320 // TODO: Add implicit null checks in intrinsics.
2321 return !GetLocations()->Intrinsified();
2322 }
2323
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002324 uint32_t GetVTableIndex() const { return vtable_index_; }
2325
2326 DECLARE_INSTRUCTION(InvokeVirtual);
2327
2328 private:
2329 const uint32_t vtable_index_;
2330
2331 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
2332};
2333
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002334class HInvokeInterface : public HInvoke {
2335 public:
2336 HInvokeInterface(ArenaAllocator* arena,
2337 uint32_t number_of_arguments,
2338 Primitive::Type return_type,
2339 uint32_t dex_pc,
2340 uint32_t dex_method_index,
2341 uint32_t imt_index)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002342 : HInvoke(arena, number_of_arguments, return_type, dex_pc, dex_method_index),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002343 imt_index_(imt_index) {}
2344
Calin Juravle77520bc2015-01-12 18:45:46 +00002345 bool CanDoImplicitNullCheck() const OVERRIDE {
2346 // TODO: Add implicit null checks in intrinsics.
2347 return !GetLocations()->Intrinsified();
2348 }
2349
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002350 uint32_t GetImtIndex() const { return imt_index_; }
2351 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
2352
2353 DECLARE_INSTRUCTION(InvokeInterface);
2354
2355 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002356 const uint32_t imt_index_;
2357
2358 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
2359};
2360
Dave Allison20dfc792014-06-16 20:44:29 -07002361class HNewInstance : public HExpression<0> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002362 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002363 HNewInstance(uint32_t dex_pc, uint16_t type_index, QuickEntrypointEnum entrypoint)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002364 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2365 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002366 type_index_(type_index),
2367 entrypoint_(entrypoint) {}
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002368
2369 uint32_t GetDexPc() const { return dex_pc_; }
2370 uint16_t GetTypeIndex() const { return type_index_; }
2371
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002372 // Calls runtime so needs an environment.
Calin Juravle92a6ed22014-12-02 18:58:03 +00002373 bool NeedsEnvironment() const OVERRIDE { return true; }
2374 // It may throw when called on:
2375 // - interfaces
2376 // - abstract/innaccessible/unknown classes
2377 // TODO: optimize when possible.
2378 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002379
Calin Juravle10e244f2015-01-26 18:54:32 +00002380 bool CanBeNull() const OVERRIDE { return false; }
2381
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002382 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
2383
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002384 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002385
2386 private:
2387 const uint32_t dex_pc_;
2388 const uint16_t type_index_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002389 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002390
2391 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
2392};
2393
Roland Levillain88cb1752014-10-20 16:36:47 +01002394class HNeg : public HUnaryOperation {
2395 public:
2396 explicit HNeg(Primitive::Type result_type, HInstruction* input)
2397 : HUnaryOperation(result_type, input) {}
2398
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002399 int32_t Evaluate(int32_t x) const OVERRIDE { return -x; }
2400 int64_t Evaluate(int64_t x) const OVERRIDE { return -x; }
Roland Levillain9240d6a2014-10-20 16:47:04 +01002401
Roland Levillain88cb1752014-10-20 16:36:47 +01002402 DECLARE_INSTRUCTION(Neg);
2403
2404 private:
2405 DISALLOW_COPY_AND_ASSIGN(HNeg);
2406};
2407
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002408class HNewArray : public HExpression<1> {
2409 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002410 HNewArray(HInstruction* length,
2411 uint32_t dex_pc,
2412 uint16_t type_index,
2413 QuickEntrypointEnum entrypoint)
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002414 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2415 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002416 type_index_(type_index),
2417 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002418 SetRawInputAt(0, length);
2419 }
2420
2421 uint32_t GetDexPc() const { return dex_pc_; }
2422 uint16_t GetTypeIndex() const { return type_index_; }
2423
2424 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00002425 bool NeedsEnvironment() const OVERRIDE { return true; }
2426
Mingyao Yang0c365e62015-03-31 15:09:29 -07002427 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
2428 bool CanThrow() const OVERRIDE { return true; }
2429
Calin Juravle10e244f2015-01-26 18:54:32 +00002430 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002431
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002432 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
2433
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002434 DECLARE_INSTRUCTION(NewArray);
2435
2436 private:
2437 const uint32_t dex_pc_;
2438 const uint16_t type_index_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002439 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002440
2441 DISALLOW_COPY_AND_ASSIGN(HNewArray);
2442};
2443
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002444class HAdd : public HBinaryOperation {
2445 public:
2446 HAdd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2447 : HBinaryOperation(result_type, left, right) {}
2448
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002449 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002450
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002451 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002452 return x + y;
2453 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002454 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002455 return x + y;
2456 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002457
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002458 DECLARE_INSTRUCTION(Add);
2459
2460 private:
2461 DISALLOW_COPY_AND_ASSIGN(HAdd);
2462};
2463
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002464class HSub : public HBinaryOperation {
2465 public:
2466 HSub(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2467 : HBinaryOperation(result_type, left, right) {}
2468
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002469 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002470 return x - y;
2471 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002472 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002473 return x - y;
2474 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002475
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002476 DECLARE_INSTRUCTION(Sub);
2477
2478 private:
2479 DISALLOW_COPY_AND_ASSIGN(HSub);
2480};
2481
Calin Juravle34bacdf2014-10-07 20:23:36 +01002482class HMul : public HBinaryOperation {
2483 public:
2484 HMul(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2485 : HBinaryOperation(result_type, left, right) {}
2486
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002487 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002488
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002489 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x * y; }
2490 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x * y; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002491
2492 DECLARE_INSTRUCTION(Mul);
2493
2494 private:
2495 DISALLOW_COPY_AND_ASSIGN(HMul);
2496};
2497
Calin Juravle7c4954d2014-10-28 16:57:40 +00002498class HDiv : public HBinaryOperation {
2499 public:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002500 HDiv(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
2501 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00002502
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002503 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00002504 // Our graph structure ensures we never have 0 for `y` during constant folding.
2505 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00002506 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00002507 return (y == -1) ? -x : x / y;
2508 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002509
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002510 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002511 DCHECK_NE(y, 0);
2512 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2513 return (y == -1) ? -x : x / y;
2514 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002515
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002516 uint32_t GetDexPc() const { return dex_pc_; }
2517
Calin Juravle7c4954d2014-10-28 16:57:40 +00002518 DECLARE_INSTRUCTION(Div);
2519
2520 private:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002521 const uint32_t dex_pc_;
2522
Calin Juravle7c4954d2014-10-28 16:57:40 +00002523 DISALLOW_COPY_AND_ASSIGN(HDiv);
2524};
2525
Calin Juravlebacfec32014-11-14 15:54:36 +00002526class HRem : public HBinaryOperation {
2527 public:
2528 HRem(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
2529 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
2530
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002531 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002532 DCHECK_NE(y, 0);
2533 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2534 return (y == -1) ? 0 : x % y;
2535 }
2536
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002537 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002538 DCHECK_NE(y, 0);
2539 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2540 return (y == -1) ? 0 : x % y;
2541 }
2542
2543 uint32_t GetDexPc() const { return dex_pc_; }
2544
2545 DECLARE_INSTRUCTION(Rem);
2546
2547 private:
2548 const uint32_t dex_pc_;
2549
2550 DISALLOW_COPY_AND_ASSIGN(HRem);
2551};
2552
Calin Juravled0d48522014-11-04 16:40:20 +00002553class HDivZeroCheck : public HExpression<1> {
2554 public:
2555 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
2556 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
2557 SetRawInputAt(0, value);
2558 }
2559
2560 bool CanBeMoved() const OVERRIDE { return true; }
2561
2562 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2563 UNUSED(other);
2564 return true;
2565 }
2566
2567 bool NeedsEnvironment() const OVERRIDE { return true; }
2568 bool CanThrow() const OVERRIDE { return true; }
2569
2570 uint32_t GetDexPc() const { return dex_pc_; }
2571
2572 DECLARE_INSTRUCTION(DivZeroCheck);
2573
2574 private:
2575 const uint32_t dex_pc_;
2576
2577 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
2578};
2579
Calin Juravle9aec02f2014-11-18 23:06:35 +00002580class HShl : public HBinaryOperation {
2581 public:
2582 HShl(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2583 : HBinaryOperation(result_type, left, right) {}
2584
2585 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x << (y & kMaxIntShiftValue); }
2586 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x << (y & kMaxLongShiftValue); }
2587
2588 DECLARE_INSTRUCTION(Shl);
2589
2590 private:
2591 DISALLOW_COPY_AND_ASSIGN(HShl);
2592};
2593
2594class HShr : public HBinaryOperation {
2595 public:
2596 HShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2597 : HBinaryOperation(result_type, left, right) {}
2598
2599 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x >> (y & kMaxIntShiftValue); }
2600 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x >> (y & kMaxLongShiftValue); }
2601
2602 DECLARE_INSTRUCTION(Shr);
2603
2604 private:
2605 DISALLOW_COPY_AND_ASSIGN(HShr);
2606};
2607
2608class HUShr : public HBinaryOperation {
2609 public:
2610 HUShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2611 : HBinaryOperation(result_type, left, right) {}
2612
2613 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
2614 uint32_t ux = static_cast<uint32_t>(x);
2615 uint32_t uy = static_cast<uint32_t>(y) & kMaxIntShiftValue;
2616 return static_cast<int32_t>(ux >> uy);
2617 }
2618
2619 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
2620 uint64_t ux = static_cast<uint64_t>(x);
2621 uint64_t uy = static_cast<uint64_t>(y) & kMaxLongShiftValue;
2622 return static_cast<int64_t>(ux >> uy);
2623 }
2624
2625 DECLARE_INSTRUCTION(UShr);
2626
2627 private:
2628 DISALLOW_COPY_AND_ASSIGN(HUShr);
2629};
2630
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002631class HAnd : public HBinaryOperation {
2632 public:
2633 HAnd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2634 : HBinaryOperation(result_type, left, right) {}
2635
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002636 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002637
2638 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x & y; }
2639 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x & y; }
2640
2641 DECLARE_INSTRUCTION(And);
2642
2643 private:
2644 DISALLOW_COPY_AND_ASSIGN(HAnd);
2645};
2646
2647class HOr : public HBinaryOperation {
2648 public:
2649 HOr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2650 : HBinaryOperation(result_type, left, right) {}
2651
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002652 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002653
2654 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x | y; }
2655 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x | y; }
2656
2657 DECLARE_INSTRUCTION(Or);
2658
2659 private:
2660 DISALLOW_COPY_AND_ASSIGN(HOr);
2661};
2662
2663class HXor : public HBinaryOperation {
2664 public:
2665 HXor(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2666 : HBinaryOperation(result_type, left, right) {}
2667
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002668 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002669
2670 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x ^ y; }
2671 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x ^ y; }
2672
2673 DECLARE_INSTRUCTION(Xor);
2674
2675 private:
2676 DISALLOW_COPY_AND_ASSIGN(HXor);
2677};
2678
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002679// The value of a parameter in this method. Its location depends on
2680// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07002681class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002682 public:
Calin Juravle10e244f2015-01-26 18:54:32 +00002683 HParameterValue(uint8_t index, Primitive::Type parameter_type, bool is_this = false)
2684 : HExpression(parameter_type, SideEffects::None()), index_(index), is_this_(is_this) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002685
2686 uint8_t GetIndex() const { return index_; }
2687
Calin Juravle10e244f2015-01-26 18:54:32 +00002688 bool CanBeNull() const OVERRIDE { return !is_this_; }
2689
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002690 DECLARE_INSTRUCTION(ParameterValue);
2691
2692 private:
2693 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00002694 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002695 const uint8_t index_;
2696
Calin Juravle10e244f2015-01-26 18:54:32 +00002697 // Whether or not the parameter value corresponds to 'this' argument.
2698 const bool is_this_;
2699
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002700 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
2701};
2702
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002703class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002704 public:
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002705 explicit HNot(Primitive::Type result_type, HInstruction* input)
2706 : HUnaryOperation(result_type, input) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002707
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002708 bool CanBeMoved() const OVERRIDE { return true; }
2709 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002710 UNUSED(other);
2711 return true;
2712 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002713
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002714 int32_t Evaluate(int32_t x) const OVERRIDE { return ~x; }
2715 int64_t Evaluate(int64_t x) const OVERRIDE { return ~x; }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002716
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002717 DECLARE_INSTRUCTION(Not);
2718
2719 private:
2720 DISALLOW_COPY_AND_ASSIGN(HNot);
2721};
2722
David Brazdil66d126e2015-04-03 16:02:44 +01002723class HBooleanNot : public HUnaryOperation {
2724 public:
2725 explicit HBooleanNot(HInstruction* input)
2726 : HUnaryOperation(Primitive::Type::kPrimBoolean, input) {}
2727
2728 bool CanBeMoved() const OVERRIDE { return true; }
2729 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2730 UNUSED(other);
2731 return true;
2732 }
2733
2734 int32_t Evaluate(int32_t x) const OVERRIDE {
2735 DCHECK(IsUint<1>(x));
2736 return !x;
2737 }
2738
2739 int64_t Evaluate(int64_t x ATTRIBUTE_UNUSED) const OVERRIDE {
2740 LOG(FATAL) << DebugName() << " cannot be used with 64-bit values";
2741 UNREACHABLE();
2742 }
2743
2744 DECLARE_INSTRUCTION(BooleanNot);
2745
2746 private:
2747 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
2748};
2749
Roland Levillaindff1f282014-11-05 14:15:05 +00002750class HTypeConversion : public HExpression<1> {
2751 public:
2752 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00002753 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
2754 : HExpression(result_type, SideEffects::None()), dex_pc_(dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002755 SetRawInputAt(0, input);
2756 DCHECK_NE(input->GetType(), result_type);
2757 }
2758
2759 HInstruction* GetInput() const { return InputAt(0); }
2760 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
2761 Primitive::Type GetResultType() const { return GetType(); }
2762
Roland Levillain624279f2014-12-04 11:54:28 +00002763 // Required by the x86 and ARM code generators when producing calls
2764 // to the runtime.
2765 uint32_t GetDexPc() const { return dex_pc_; }
2766
Roland Levillaindff1f282014-11-05 14:15:05 +00002767 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00002768 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00002769
2770 DECLARE_INSTRUCTION(TypeConversion);
2771
2772 private:
Roland Levillain624279f2014-12-04 11:54:28 +00002773 const uint32_t dex_pc_;
2774
Roland Levillaindff1f282014-11-05 14:15:05 +00002775 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
2776};
2777
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00002778static constexpr uint32_t kNoRegNumber = -1;
2779
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002780class HPhi : public HInstruction {
2781 public:
2782 HPhi(ArenaAllocator* arena, uint32_t reg_number, size_t number_of_inputs, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002783 : HInstruction(SideEffects::None()),
2784 inputs_(arena, number_of_inputs),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002785 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002786 type_(type),
Calin Juravle10e244f2015-01-26 18:54:32 +00002787 is_live_(false),
2788 can_be_null_(true) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002789 inputs_.SetSize(number_of_inputs);
2790 }
2791
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00002792 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
2793 static Primitive::Type ToPhiType(Primitive::Type type) {
2794 switch (type) {
2795 case Primitive::kPrimBoolean:
2796 case Primitive::kPrimByte:
2797 case Primitive::kPrimShort:
2798 case Primitive::kPrimChar:
2799 return Primitive::kPrimInt;
2800 default:
2801 return type;
2802 }
2803 }
2804
Calin Juravle10e244f2015-01-26 18:54:32 +00002805 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002806
2807 void AddInput(HInstruction* input);
2808
Calin Juravle10e244f2015-01-26 18:54:32 +00002809 Primitive::Type GetType() const OVERRIDE { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002810 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002811
Calin Juravle10e244f2015-01-26 18:54:32 +00002812 bool CanBeNull() const OVERRIDE { return can_be_null_; }
2813 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
2814
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002815 uint32_t GetRegNumber() const { return reg_number_; }
2816
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002817 void SetDead() { is_live_ = false; }
2818 void SetLive() { is_live_ = true; }
2819 bool IsDead() const { return !is_live_; }
2820 bool IsLive() const { return is_live_; }
2821
Calin Juravlea4f88312015-04-16 12:57:19 +01002822 // Returns the next equivalent phi (starting from the current one) or null if there is none.
2823 // An equivalent phi is a phi having the same dex register and type.
2824 // It assumes that phis with the same dex register are adjacent.
2825 HPhi* GetNextEquivalentPhiWithSameType() {
2826 HInstruction* next = GetNext();
2827 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
2828 if (next->GetType() == GetType()) {
2829 return next->AsPhi();
2830 }
2831 next = next->GetNext();
2832 }
2833 return nullptr;
2834 }
2835
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002836 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002837
David Brazdil1abb4192015-02-17 18:33:36 +00002838 protected:
2839 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
2840
2841 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
2842 inputs_.Put(index, input);
2843 }
2844
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002845 private:
David Brazdil1abb4192015-02-17 18:33:36 +00002846 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002847 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002848 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002849 bool is_live_;
Calin Juravle10e244f2015-01-26 18:54:32 +00002850 bool can_be_null_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002851
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002852 DISALLOW_COPY_AND_ASSIGN(HPhi);
2853};
2854
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002855class HNullCheck : public HExpression<1> {
2856 public:
2857 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002858 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002859 SetRawInputAt(0, value);
2860 }
2861
Calin Juravle10e244f2015-01-26 18:54:32 +00002862 bool CanBeMoved() const OVERRIDE { return true; }
2863 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002864 UNUSED(other);
2865 return true;
2866 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002867
Calin Juravle10e244f2015-01-26 18:54:32 +00002868 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002869
Calin Juravle10e244f2015-01-26 18:54:32 +00002870 bool CanThrow() const OVERRIDE { return true; }
2871
2872 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01002873
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002874 uint32_t GetDexPc() const { return dex_pc_; }
2875
2876 DECLARE_INSTRUCTION(NullCheck);
2877
2878 private:
2879 const uint32_t dex_pc_;
2880
2881 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
2882};
2883
2884class FieldInfo : public ValueObject {
2885 public:
Calin Juravle52c48962014-12-16 17:02:57 +00002886 FieldInfo(MemberOffset field_offset, Primitive::Type field_type, bool is_volatile)
2887 : field_offset_(field_offset), field_type_(field_type), is_volatile_(is_volatile) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002888
2889 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002890 Primitive::Type GetFieldType() const { return field_type_; }
Calin Juravle52c48962014-12-16 17:02:57 +00002891 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002892
2893 private:
2894 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01002895 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00002896 const bool is_volatile_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002897};
2898
2899class HInstanceFieldGet : public HExpression<1> {
2900 public:
2901 HInstanceFieldGet(HInstruction* value,
2902 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00002903 MemberOffset field_offset,
2904 bool is_volatile)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002905 : HExpression(field_type, SideEffects::DependsOnSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00002906 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002907 SetRawInputAt(0, value);
2908 }
2909
Calin Juravle10c9cbe2014-12-19 10:50:19 +00002910 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002911
2912 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2913 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
2914 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002915 }
2916
Calin Juravle77520bc2015-01-12 18:45:46 +00002917 bool CanDoImplicitNullCheck() const OVERRIDE {
2918 return GetFieldOffset().Uint32Value() < kPageSize;
2919 }
2920
2921 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002922 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
2923 }
2924
Calin Juravle52c48962014-12-16 17:02:57 +00002925 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002926 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002927 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002928 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002929
2930 DECLARE_INSTRUCTION(InstanceFieldGet);
2931
2932 private:
2933 const FieldInfo field_info_;
2934
2935 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
2936};
2937
2938class HInstanceFieldSet : public HTemplateInstruction<2> {
2939 public:
2940 HInstanceFieldSet(HInstruction* object,
2941 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01002942 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00002943 MemberOffset field_offset,
2944 bool is_volatile)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002945 : HTemplateInstruction(SideEffects::ChangesSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00002946 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002947 SetRawInputAt(0, object);
2948 SetRawInputAt(1, value);
2949 }
2950
Calin Juravle77520bc2015-01-12 18:45:46 +00002951 bool CanDoImplicitNullCheck() const OVERRIDE {
2952 return GetFieldOffset().Uint32Value() < kPageSize;
2953 }
2954
Calin Juravle52c48962014-12-16 17:02:57 +00002955 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002956 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002957 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002958 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002959 HInstruction* GetValue() const { return InputAt(1); }
2960
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002961 DECLARE_INSTRUCTION(InstanceFieldSet);
2962
2963 private:
2964 const FieldInfo field_info_;
2965
2966 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
2967};
2968
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002969class HArrayGet : public HExpression<2> {
2970 public:
2971 HArrayGet(HInstruction* array, HInstruction* index, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002972 : HExpression(type, SideEffects::DependsOnSomething()) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002973 SetRawInputAt(0, array);
2974 SetRawInputAt(1, index);
2975 }
2976
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002977 bool CanBeMoved() const OVERRIDE { return true; }
2978 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002979 UNUSED(other);
2980 return true;
2981 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002982 bool CanDoImplicitNullCheck() const OVERRIDE {
2983 // TODO: We can be smarter here.
2984 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
2985 // which generates the implicit null check. There are cases when these can be removed
2986 // to produce better code. If we ever add optimizations to do so we should allow an
2987 // implicit check here (as long as the address falls in the first page).
2988 return false;
2989 }
2990
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002991 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002992
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002993 HInstruction* GetArray() const { return InputAt(0); }
2994 HInstruction* GetIndex() const { return InputAt(1); }
2995
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002996 DECLARE_INSTRUCTION(ArrayGet);
2997
2998 private:
2999 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
3000};
3001
3002class HArraySet : public HTemplateInstruction<3> {
3003 public:
3004 HArraySet(HInstruction* array,
3005 HInstruction* index,
3006 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003007 Primitive::Type expected_component_type,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003008 uint32_t dex_pc)
3009 : HTemplateInstruction(SideEffects::ChangesSomething()),
3010 dex_pc_(dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003011 expected_component_type_(expected_component_type),
3012 needs_type_check_(value->GetType() == Primitive::kPrimNot) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003013 SetRawInputAt(0, array);
3014 SetRawInputAt(1, index);
3015 SetRawInputAt(2, value);
3016 }
3017
Calin Juravle77520bc2015-01-12 18:45:46 +00003018 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003019 // We currently always call a runtime method to catch array store
3020 // exceptions.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003021 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003022 }
3023
Calin Juravle77520bc2015-01-12 18:45:46 +00003024 bool CanDoImplicitNullCheck() const OVERRIDE {
3025 // TODO: Same as for ArrayGet.
3026 return false;
3027 }
3028
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003029 void ClearNeedsTypeCheck() {
3030 needs_type_check_ = false;
3031 }
3032
3033 bool NeedsTypeCheck() const { return needs_type_check_; }
3034
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003035 uint32_t GetDexPc() const { return dex_pc_; }
3036
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003037 HInstruction* GetArray() const { return InputAt(0); }
3038 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003039 HInstruction* GetValue() const { return InputAt(2); }
3040
3041 Primitive::Type GetComponentType() const {
3042 // The Dex format does not type floating point index operations. Since the
3043 // `expected_component_type_` is set during building and can therefore not
3044 // be correct, we also check what is the value type. If it is a floating
3045 // point type, we must use that type.
3046 Primitive::Type value_type = GetValue()->GetType();
3047 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
3048 ? value_type
3049 : expected_component_type_;
3050 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003051
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003052 DECLARE_INSTRUCTION(ArraySet);
3053
3054 private:
3055 const uint32_t dex_pc_;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003056 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003057 bool needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003058
3059 DISALLOW_COPY_AND_ASSIGN(HArraySet);
3060};
3061
3062class HArrayLength : public HExpression<1> {
3063 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003064 explicit HArrayLength(HInstruction* array)
3065 : HExpression(Primitive::kPrimInt, SideEffects::None()) {
3066 // Note that arrays do not change length, so the instruction does not
3067 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003068 SetRawInputAt(0, array);
3069 }
3070
Calin Juravle77520bc2015-01-12 18:45:46 +00003071 bool CanBeMoved() const OVERRIDE { return true; }
3072 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003073 UNUSED(other);
3074 return true;
3075 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003076 bool CanDoImplicitNullCheck() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003077
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003078 DECLARE_INSTRUCTION(ArrayLength);
3079
3080 private:
3081 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
3082};
3083
3084class HBoundsCheck : public HExpression<2> {
3085 public:
3086 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003087 : HExpression(index->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003088 DCHECK(index->GetType() == Primitive::kPrimInt);
3089 SetRawInputAt(0, index);
3090 SetRawInputAt(1, length);
3091 }
3092
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003093 bool CanBeMoved() const OVERRIDE { return true; }
3094 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003095 UNUSED(other);
3096 return true;
3097 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003098
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003099 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003100
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003101 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01003102
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003103 uint32_t GetDexPc() const { return dex_pc_; }
3104
3105 DECLARE_INSTRUCTION(BoundsCheck);
3106
3107 private:
3108 const uint32_t dex_pc_;
3109
3110 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
3111};
3112
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003113/**
3114 * Some DEX instructions are folded into multiple HInstructions that need
3115 * to stay live until the last HInstruction. This class
3116 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00003117 * HInstruction stays live. `index` represents the stack location index of the
3118 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003119 */
3120class HTemporary : public HTemplateInstruction<0> {
3121 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003122 explicit HTemporary(size_t index) : HTemplateInstruction(SideEffects::None()), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003123
3124 size_t GetIndex() const { return index_; }
3125
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00003126 Primitive::Type GetType() const OVERRIDE {
3127 // The previous instruction is the one that will be stored in the temporary location.
3128 DCHECK(GetPrevious() != nullptr);
3129 return GetPrevious()->GetType();
3130 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00003131
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003132 DECLARE_INSTRUCTION(Temporary);
3133
3134 private:
3135 const size_t index_;
3136
3137 DISALLOW_COPY_AND_ASSIGN(HTemporary);
3138};
3139
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003140class HSuspendCheck : public HTemplateInstruction<0> {
3141 public:
3142 explicit HSuspendCheck(uint32_t dex_pc)
Nicolas Geoffray9ebc72c2014-09-25 16:33:42 +01003143 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003144
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003145 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003146 return true;
3147 }
3148
3149 uint32_t GetDexPc() const { return dex_pc_; }
3150
3151 DECLARE_INSTRUCTION(SuspendCheck);
3152
3153 private:
3154 const uint32_t dex_pc_;
3155
3156 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
3157};
3158
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003159/**
3160 * Instruction to load a Class object.
3161 */
3162class HLoadClass : public HExpression<0> {
3163 public:
3164 HLoadClass(uint16_t type_index,
3165 bool is_referrers_class,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003166 uint32_t dex_pc)
3167 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3168 type_index_(type_index),
3169 is_referrers_class_(is_referrers_class),
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003170 dex_pc_(dex_pc),
Calin Juravleb1498f62015-02-16 13:13:29 +00003171 generate_clinit_check_(false),
3172 loaded_class_rti_(ReferenceTypeInfo::CreateTop(/* is_exact */ false)) {}
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003173
3174 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003175
3176 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3177 return other->AsLoadClass()->type_index_ == type_index_;
3178 }
3179
3180 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
3181
3182 uint32_t GetDexPc() const { return dex_pc_; }
3183 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003184 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003185
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003186 bool NeedsEnvironment() const OVERRIDE {
3187 // Will call runtime and load the class if the class is not loaded yet.
3188 // TODO: finer grain decision.
3189 return !is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003190 }
3191
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003192 bool MustGenerateClinitCheck() const {
3193 return generate_clinit_check_;
3194 }
3195
3196 void SetMustGenerateClinitCheck() {
3197 generate_clinit_check_ = true;
3198 }
3199
3200 bool CanCallRuntime() const {
3201 return MustGenerateClinitCheck() || !is_referrers_class_;
3202 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003203
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003204 bool CanThrow() const OVERRIDE {
3205 // May call runtime and and therefore can throw.
3206 // TODO: finer grain decision.
3207 return !is_referrers_class_;
3208 }
3209
Calin Juravleacf735c2015-02-12 15:25:22 +00003210 ReferenceTypeInfo GetLoadedClassRTI() {
3211 return loaded_class_rti_;
3212 }
3213
3214 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
3215 // Make sure we only set exact types (the loaded class should never be merged).
3216 DCHECK(rti.IsExact());
3217 loaded_class_rti_ = rti;
3218 }
3219
3220 bool IsResolved() {
3221 return loaded_class_rti_.IsExact();
3222 }
3223
Nicolas Geoffray9437b782015-03-25 10:08:51 +00003224 bool NeedsDexCache() const OVERRIDE { return !is_referrers_class_; }
3225
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003226 DECLARE_INSTRUCTION(LoadClass);
3227
3228 private:
3229 const uint16_t type_index_;
3230 const bool is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003231 const uint32_t dex_pc_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003232 // Whether this instruction must generate the initialization check.
3233 // Used for code generation.
3234 bool generate_clinit_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003235
Calin Juravleacf735c2015-02-12 15:25:22 +00003236 ReferenceTypeInfo loaded_class_rti_;
3237
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003238 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
3239};
3240
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003241class HLoadString : public HExpression<0> {
3242 public:
3243 HLoadString(uint32_t string_index, uint32_t dex_pc)
3244 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3245 string_index_(string_index),
3246 dex_pc_(dex_pc) {}
3247
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003248 bool CanBeMoved() const OVERRIDE { return true; }
3249
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003250 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3251 return other->AsLoadString()->string_index_ == string_index_;
3252 }
3253
3254 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
3255
3256 uint32_t GetDexPc() const { return dex_pc_; }
3257 uint32_t GetStringIndex() const { return string_index_; }
3258
3259 // TODO: Can we deopt or debug when we resolve a string?
3260 bool NeedsEnvironment() const OVERRIDE { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00003261 bool NeedsDexCache() const OVERRIDE { return true; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003262
3263 DECLARE_INSTRUCTION(LoadString);
3264
3265 private:
3266 const uint32_t string_index_;
3267 const uint32_t dex_pc_;
3268
3269 DISALLOW_COPY_AND_ASSIGN(HLoadString);
3270};
3271
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003272/**
3273 * Performs an initialization check on its Class object input.
3274 */
3275class HClinitCheck : public HExpression<1> {
3276 public:
3277 explicit HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Nicolas Geoffraya0466e12015-03-27 15:00:40 +00003278 : HExpression(Primitive::kPrimNot, SideEffects::ChangesSomething()),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003279 dex_pc_(dex_pc) {
3280 SetRawInputAt(0, constant);
3281 }
3282
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003283 bool CanBeMoved() const OVERRIDE { return true; }
3284 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3285 UNUSED(other);
3286 return true;
3287 }
3288
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003289 bool NeedsEnvironment() const OVERRIDE {
3290 // May call runtime to initialize the class.
3291 return true;
3292 }
3293
3294 uint32_t GetDexPc() const { return dex_pc_; }
3295
3296 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
3297
3298 DECLARE_INSTRUCTION(ClinitCheck);
3299
3300 private:
3301 const uint32_t dex_pc_;
3302
3303 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
3304};
3305
3306class HStaticFieldGet : public HExpression<1> {
3307 public:
3308 HStaticFieldGet(HInstruction* cls,
3309 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003310 MemberOffset field_offset,
3311 bool is_volatile)
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003312 : HExpression(field_type, SideEffects::DependsOnSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00003313 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003314 SetRawInputAt(0, cls);
3315 }
3316
Calin Juravle52c48962014-12-16 17:02:57 +00003317
Calin Juravle10c9cbe2014-12-19 10:50:19 +00003318 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003319
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003320 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00003321 HStaticFieldGet* other_get = other->AsStaticFieldGet();
3322 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003323 }
3324
3325 size_t ComputeHashCode() const OVERRIDE {
3326 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
3327 }
3328
Calin Juravle52c48962014-12-16 17:02:57 +00003329 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003330 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
3331 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003332 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003333
3334 DECLARE_INSTRUCTION(StaticFieldGet);
3335
3336 private:
3337 const FieldInfo field_info_;
3338
3339 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
3340};
3341
3342class HStaticFieldSet : public HTemplateInstruction<2> {
3343 public:
3344 HStaticFieldSet(HInstruction* cls,
3345 HInstruction* value,
3346 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003347 MemberOffset field_offset,
3348 bool is_volatile)
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003349 : HTemplateInstruction(SideEffects::ChangesSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00003350 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003351 SetRawInputAt(0, cls);
3352 SetRawInputAt(1, value);
3353 }
3354
Calin Juravle52c48962014-12-16 17:02:57 +00003355 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003356 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
3357 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003358 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003359
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003360 HInstruction* GetValue() const { return InputAt(1); }
3361
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003362 DECLARE_INSTRUCTION(StaticFieldSet);
3363
3364 private:
3365 const FieldInfo field_info_;
3366
3367 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
3368};
3369
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003370// Implement the move-exception DEX instruction.
3371class HLoadException : public HExpression<0> {
3372 public:
3373 HLoadException() : HExpression(Primitive::kPrimNot, SideEffects::None()) {}
3374
3375 DECLARE_INSTRUCTION(LoadException);
3376
3377 private:
3378 DISALLOW_COPY_AND_ASSIGN(HLoadException);
3379};
3380
3381class HThrow : public HTemplateInstruction<1> {
3382 public:
3383 HThrow(HInstruction* exception, uint32_t dex_pc)
3384 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {
3385 SetRawInputAt(0, exception);
3386 }
3387
3388 bool IsControlFlow() const OVERRIDE { return true; }
3389
3390 bool NeedsEnvironment() const OVERRIDE { return true; }
3391
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003392 bool CanThrow() const OVERRIDE { return true; }
3393
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003394 uint32_t GetDexPc() const { return dex_pc_; }
3395
3396 DECLARE_INSTRUCTION(Throw);
3397
3398 private:
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003399 const uint32_t dex_pc_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003400
3401 DISALLOW_COPY_AND_ASSIGN(HThrow);
3402};
3403
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003404class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003405 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003406 HInstanceOf(HInstruction* object,
3407 HLoadClass* constant,
3408 bool class_is_final,
3409 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003410 : HExpression(Primitive::kPrimBoolean, SideEffects::None()),
3411 class_is_final_(class_is_final),
3412 dex_pc_(dex_pc) {
3413 SetRawInputAt(0, object);
3414 SetRawInputAt(1, constant);
3415 }
3416
3417 bool CanBeMoved() const OVERRIDE { return true; }
3418
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003419 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003420 return true;
3421 }
3422
3423 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003424 return false;
3425 }
3426
3427 uint32_t GetDexPc() const { return dex_pc_; }
3428
3429 bool IsClassFinal() const { return class_is_final_; }
3430
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003431 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003432
3433 private:
3434 const bool class_is_final_;
3435 const uint32_t dex_pc_;
3436
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003437 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
3438};
3439
Calin Juravleb1498f62015-02-16 13:13:29 +00003440class HBoundType : public HExpression<1> {
3441 public:
3442 HBoundType(HInstruction* input, ReferenceTypeInfo bound_type)
3443 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3444 bound_type_(bound_type) {
Calin Juravle61d544b2015-02-23 16:46:57 +00003445 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00003446 SetRawInputAt(0, input);
3447 }
3448
3449 const ReferenceTypeInfo& GetBoundType() const { return bound_type_; }
3450
3451 bool CanBeNull() const OVERRIDE {
3452 // `null instanceof ClassX` always return false so we can't be null.
3453 return false;
3454 }
3455
3456 DECLARE_INSTRUCTION(BoundType);
3457
3458 private:
3459 // Encodes the most upper class that this instruction can have. In other words
3460 // it is always the case that GetBoundType().IsSupertypeOf(GetReferenceType()).
3461 // It is used to bound the type in cases like `if (x instanceof ClassX) {}`
3462 const ReferenceTypeInfo bound_type_;
3463
3464 DISALLOW_COPY_AND_ASSIGN(HBoundType);
3465};
3466
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003467class HCheckCast : public HTemplateInstruction<2> {
3468 public:
3469 HCheckCast(HInstruction* object,
3470 HLoadClass* constant,
3471 bool class_is_final,
3472 uint32_t dex_pc)
3473 : HTemplateInstruction(SideEffects::None()),
3474 class_is_final_(class_is_final),
3475 dex_pc_(dex_pc) {
3476 SetRawInputAt(0, object);
3477 SetRawInputAt(1, constant);
3478 }
3479
3480 bool CanBeMoved() const OVERRIDE { return true; }
3481
3482 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
3483 return true;
3484 }
3485
3486 bool NeedsEnvironment() const OVERRIDE {
3487 // Instruction may throw a CheckCastError.
3488 return true;
3489 }
3490
3491 bool CanThrow() const OVERRIDE { return true; }
3492
3493 uint32_t GetDexPc() const { return dex_pc_; }
3494
3495 bool IsClassFinal() const { return class_is_final_; }
3496
3497 DECLARE_INSTRUCTION(CheckCast);
3498
3499 private:
3500 const bool class_is_final_;
3501 const uint32_t dex_pc_;
3502
3503 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003504};
3505
Calin Juravle27df7582015-04-17 19:12:31 +01003506class HMemoryBarrier : public HTemplateInstruction<0> {
3507 public:
3508 explicit HMemoryBarrier(MemBarrierKind barrier_kind)
3509 : HTemplateInstruction(SideEffects::None()),
3510 barrier_kind_(barrier_kind) {}
3511
3512 MemBarrierKind GetBarrierKind() { return barrier_kind_; }
3513
3514 DECLARE_INSTRUCTION(MemoryBarrier);
3515
3516 private:
3517 const MemBarrierKind barrier_kind_;
3518
3519 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
3520};
3521
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003522class HMonitorOperation : public HTemplateInstruction<1> {
3523 public:
3524 enum OperationKind {
3525 kEnter,
3526 kExit,
3527 };
3528
3529 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
3530 : HTemplateInstruction(SideEffects::None()), kind_(kind), dex_pc_(dex_pc) {
3531 SetRawInputAt(0, object);
3532 }
3533
3534 // Instruction may throw a Java exception, so we need an environment.
3535 bool NeedsEnvironment() const OVERRIDE { return true; }
3536 bool CanThrow() const OVERRIDE { return true; }
3537
3538 uint32_t GetDexPc() const { return dex_pc_; }
3539
3540 bool IsEnter() const { return kind_ == kEnter; }
3541
3542 DECLARE_INSTRUCTION(MonitorOperation);
3543
Calin Juravle52c48962014-12-16 17:02:57 +00003544 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003545 const OperationKind kind_;
3546 const uint32_t dex_pc_;
3547
3548 private:
3549 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
3550};
3551
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003552class MoveOperands : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003553 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01003554 MoveOperands(Location source,
3555 Location destination,
3556 Primitive::Type type,
3557 HInstruction* instruction)
3558 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003559
3560 Location GetSource() const { return source_; }
3561 Location GetDestination() const { return destination_; }
3562
3563 void SetSource(Location value) { source_ = value; }
3564 void SetDestination(Location value) { destination_ = value; }
3565
3566 // The parallel move resolver marks moves as "in-progress" by clearing the
3567 // destination (but not the source).
3568 Location MarkPending() {
3569 DCHECK(!IsPending());
3570 Location dest = destination_;
3571 destination_ = Location::NoLocation();
3572 return dest;
3573 }
3574
3575 void ClearPending(Location dest) {
3576 DCHECK(IsPending());
3577 destination_ = dest;
3578 }
3579
3580 bool IsPending() const {
3581 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
3582 return destination_.IsInvalid() && !source_.IsInvalid();
3583 }
3584
3585 // True if this blocks a move from the given location.
3586 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08003587 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003588 }
3589
3590 // A move is redundant if it's been eliminated, if its source and
3591 // destination are the same, or if its destination is unneeded.
3592 bool IsRedundant() const {
3593 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
3594 }
3595
3596 // We clear both operands to indicate move that's been eliminated.
3597 void Eliminate() {
3598 source_ = destination_ = Location::NoLocation();
3599 }
3600
3601 bool IsEliminated() const {
3602 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
3603 return source_.IsInvalid();
3604 }
3605
Nicolas Geoffray90218252015-04-15 11:56:51 +01003606 bool Is64BitMove() const {
3607 return Primitive::Is64BitType(type_);
3608 }
3609
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003610 HInstruction* GetInstruction() const { return instruction_; }
3611
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003612 private:
3613 Location source_;
3614 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01003615 // The type this move is for.
3616 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003617 // The instruction this move is assocatied with. Null when this move is
3618 // for moving an input in the expected locations of user (including a phi user).
3619 // This is only used in debug mode, to ensure we do not connect interval siblings
3620 // in the same parallel move.
3621 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003622};
3623
3624static constexpr size_t kDefaultNumberOfMoves = 4;
3625
3626class HParallelMove : public HTemplateInstruction<0> {
3627 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003628 explicit HParallelMove(ArenaAllocator* arena)
3629 : HTemplateInstruction(SideEffects::None()), moves_(arena, kDefaultNumberOfMoves) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003630
Nicolas Geoffray90218252015-04-15 11:56:51 +01003631 void AddMove(Location source,
3632 Location destination,
3633 Primitive::Type type,
3634 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003635 DCHECK(source.IsValid());
3636 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003637 if (kIsDebugBuild) {
3638 if (instruction != nullptr) {
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00003639 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003640 if (moves_.Get(i).GetInstruction() == instruction) {
3641 // Special case the situation where the move is for the spill slot
3642 // of the instruction.
3643 if ((GetPrevious() == instruction)
3644 || ((GetPrevious() == nullptr)
3645 && instruction->IsPhi()
3646 && instruction->GetBlock() == GetBlock())) {
3647 DCHECK_NE(destination.GetKind(), moves_.Get(i).GetDestination().GetKind())
3648 << "Doing parallel moves for the same instruction.";
3649 } else {
3650 DCHECK(false) << "Doing parallel moves for the same instruction.";
3651 }
3652 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00003653 }
3654 }
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003655 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Zheng Xuad4450e2015-04-17 18:48:56 +08003656 DCHECK(!destination.OverlapsWith(moves_.Get(i).GetDestination()))
3657 << "Overlapped destination for two moves in a parallel move.";
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003658 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003659 }
Nicolas Geoffray90218252015-04-15 11:56:51 +01003660 moves_.Add(MoveOperands(source, destination, type, instruction));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003661 }
3662
3663 MoveOperands* MoveOperandsAt(size_t index) const {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003664 return moves_.GetRawStorage() + index;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003665 }
3666
3667 size_t NumMoves() const { return moves_.Size(); }
3668
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003669 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003670
3671 private:
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003672 GrowableArray<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003673
3674 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
3675};
3676
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003677class HGraphVisitor : public ValueObject {
3678 public:
Dave Allison20dfc792014-06-16 20:44:29 -07003679 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
3680 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003681
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003682 virtual void VisitInstruction(HInstruction* instruction) { UNUSED(instruction); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003683 virtual void VisitBasicBlock(HBasicBlock* block);
3684
Roland Levillain633021e2014-10-01 14:12:25 +01003685 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003686 void VisitInsertionOrder();
3687
Roland Levillain633021e2014-10-01 14:12:25 +01003688 // Visit the graph following dominator tree reverse post-order.
3689 void VisitReversePostOrder();
3690
Nicolas Geoffray787c3072014-03-17 10:20:19 +00003691 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00003692
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003693 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003694#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003695 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
3696
3697 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
3698
3699#undef DECLARE_VISIT_INSTRUCTION
3700
3701 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07003702 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003703
3704 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
3705};
3706
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003707class HGraphDelegateVisitor : public HGraphVisitor {
3708 public:
3709 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
3710 virtual ~HGraphDelegateVisitor() {}
3711
3712 // Visit functions that delegate to to super class.
3713#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003714 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003715
3716 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
3717
3718#undef DECLARE_VISIT_INSTRUCTION
3719
3720 private:
3721 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
3722};
3723
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003724class HInsertionOrderIterator : public ValueObject {
3725 public:
3726 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
3727
3728 bool Done() const { return index_ == graph_.GetBlocks().Size(); }
3729 HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); }
3730 void Advance() { ++index_; }
3731
3732 private:
3733 const HGraph& graph_;
3734 size_t index_;
3735
3736 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
3737};
3738
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003739class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003740 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00003741 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
3742 // Check that reverse post order of the graph has been built.
3743 DCHECK(!graph.GetReversePostOrder().IsEmpty());
3744 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003745
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003746 bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); }
3747 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003748 void Advance() { ++index_; }
3749
3750 private:
3751 const HGraph& graph_;
3752 size_t index_;
3753
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003754 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003755};
3756
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003757class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003758 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003759 explicit HPostOrderIterator(const HGraph& graph)
David Brazdil10f56cb2015-03-24 18:49:14 +00003760 : graph_(graph), index_(graph_.GetReversePostOrder().Size()) {
3761 // Check that reverse post order of the graph has been built.
3762 DCHECK(!graph.GetReversePostOrder().IsEmpty());
3763 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003764
3765 bool Done() const { return index_ == 0; }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003766 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003767 void Advance() { --index_; }
3768
3769 private:
3770 const HGraph& graph_;
3771 size_t index_;
3772
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003773 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003774};
3775
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01003776class HLinearPostOrderIterator : public ValueObject {
3777 public:
3778 explicit HLinearPostOrderIterator(const HGraph& graph)
3779 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().Size()) {}
3780
3781 bool Done() const { return index_ == 0; }
3782
3783 HBasicBlock* Current() const { return order_.Get(index_ -1); }
3784
3785 void Advance() {
3786 --index_;
3787 DCHECK_GE(index_, 0U);
3788 }
3789
3790 private:
3791 const GrowableArray<HBasicBlock*>& order_;
3792 size_t index_;
3793
3794 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
3795};
3796
3797class HLinearOrderIterator : public ValueObject {
3798 public:
3799 explicit HLinearOrderIterator(const HGraph& graph)
3800 : order_(graph.GetLinearOrder()), index_(0) {}
3801
3802 bool Done() const { return index_ == order_.Size(); }
3803 HBasicBlock* Current() const { return order_.Get(index_); }
3804 void Advance() { ++index_; }
3805
3806 private:
3807 const GrowableArray<HBasicBlock*>& order_;
3808 size_t index_;
3809
3810 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
3811};
3812
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003813// Iterator over the blocks that art part of the loop. Includes blocks part
3814// of an inner loop. The order in which the blocks are iterated is on their
3815// block id.
3816class HBlocksInLoopIterator : public ValueObject {
3817 public:
3818 explicit HBlocksInLoopIterator(const HLoopInformation& info)
3819 : blocks_in_loop_(info.GetBlocks()),
3820 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
3821 index_(0) {
3822 if (!blocks_in_loop_.IsBitSet(index_)) {
3823 Advance();
3824 }
3825 }
3826
3827 bool Done() const { return index_ == blocks_.Size(); }
3828 HBasicBlock* Current() const { return blocks_.Get(index_); }
3829 void Advance() {
3830 ++index_;
3831 for (size_t e = blocks_.Size(); index_ < e; ++index_) {
3832 if (blocks_in_loop_.IsBitSet(index_)) {
3833 break;
3834 }
3835 }
3836 }
3837
3838 private:
3839 const BitVector& blocks_in_loop_;
3840 const GrowableArray<HBasicBlock*>& blocks_;
3841 size_t index_;
3842
3843 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
3844};
3845
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003846inline int64_t Int64FromConstant(HConstant* constant) {
3847 DCHECK(constant->IsIntConstant() || constant->IsLongConstant());
3848 return constant->IsIntConstant() ? constant->AsIntConstant()->GetValue()
3849 : constant->AsLongConstant()->GetValue();
3850}
3851
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003852} // namespace art
3853
3854#endif // ART_COMPILER_OPTIMIZING_NODES_H_