blob: b89487f4f60097edadca63a31ec092adfbd68ea3 [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
David Brazdilc3d743f2015-04-22 13:40:50 +010078 // Insert `instruction` before/after an existing instruction `cursor`.
79 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
80 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
81
Roland Levillain6b469232014-09-25 10:10:38 +010082 // Return true if this list contains `instruction`.
83 bool Contains(HInstruction* instruction) const;
84
Roland Levillainccc07a92014-09-16 14:48:16 +010085 // Return true if `instruction1` is found before `instruction2` in
86 // this instruction list and false otherwise. Abort if none
87 // of these instructions is found.
88 bool FoundBefore(const HInstruction* instruction1,
89 const HInstruction* instruction2) const;
90
Nicolas Geoffray276d9da2015-02-02 18:24:11 +000091 bool IsEmpty() const { return first_instruction_ == nullptr; }
92 void Clear() { first_instruction_ = last_instruction_ = nullptr; }
93
94 // Update the block of all instructions to be `block`.
95 void SetBlockOfInstructions(HBasicBlock* block) const;
96
97 void AddAfter(HInstruction* cursor, const HInstructionList& instruction_list);
98 void Add(const HInstructionList& instruction_list);
99
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100100 private:
101 HInstruction* first_instruction_;
102 HInstruction* last_instruction_;
103
104 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000105 friend class HGraph;
106 friend class HInstruction;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100107 friend class HInstructionIterator;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100108 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100109
110 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
111};
112
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000113// Control-flow graph of a method. Contains a list of basic blocks.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700114class HGraph : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000115 public:
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000116 HGraph(ArenaAllocator* arena, bool debuggable = false, int start_instruction_id = 0)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000117 : arena_(arena),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000118 blocks_(arena, kDefaultNumberOfBlocks),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100119 reverse_post_order_(arena, kDefaultNumberOfBlocks),
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100120 linear_order_(arena, kDefaultNumberOfBlocks),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700121 entry_block_(nullptr),
122 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100123 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100124 number_of_vregs_(0),
125 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000126 temporaries_vreg_slots_(0),
Mingyao Yange4335eb2015-03-02 15:14:13 -0800127 has_array_accesses_(false),
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000128 debuggable_(debuggable),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000129 current_instruction_id_(start_instruction_id),
130 cached_null_constant_(nullptr),
131 cached_int_constants_(std::less<int32_t>(), arena->Adapter()),
132 cached_long_constants_(std::less<int64_t>(), arena->Adapter()) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000133
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000134 ArenaAllocator* GetArena() const { return arena_; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100135 const GrowableArray<HBasicBlock*>& GetBlocks() const { return blocks_; }
Roland Levillain93445682014-10-06 19:24:02 +0100136 HBasicBlock* GetBlock(size_t id) const { return blocks_.Get(id); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000137
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000138 HBasicBlock* GetEntryBlock() const { return entry_block_; }
139 HBasicBlock* GetExitBlock() const { return exit_block_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000140
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000141 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
142 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000143
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000144 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100145
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000146 // Try building the SSA form of this graph, with dominance computation and loop
147 // recognition. Returns whether it was successful in doing all these steps.
148 bool TryBuildingSsa() {
149 BuildDominatorTree();
Nicolas Geoffrayd3350832015-03-12 11:16:23 +0000150 // The SSA builder requires loops to all be natural. Specifically, the dead phi
151 // elimination phase checks the consistency of the graph when doing a post-order
152 // visit for eliminating dead phis: a dead phi can only have loop header phi
153 // users remaining when being visited.
154 if (!AnalyzeNaturalLoops()) return false;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000155 TransformToSsa();
Nicolas Geoffrayd3350832015-03-12 11:16:23 +0000156 return true;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000157 }
158
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000159 void BuildDominatorTree();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000160 void TransformToSsa();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100161 void SimplifyCFG();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000162
Nicolas Geoffrayf5370122014-12-02 11:51:19 +0000163 // Analyze all natural loops in this graph. Returns false if one
164 // loop is not natural, that is the header does not dominate the
165 // back edge.
166 bool AnalyzeNaturalLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100167
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000168 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
169 void InlineInto(HGraph* outer_graph, HInvoke* invoke);
170
David Brazdil46e2a392015-03-16 17:31:52 +0000171 void MergeEmptyBranches(HBasicBlock* start_block, HBasicBlock* end_block);
172
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100173 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
174 void SimplifyLoop(HBasicBlock* header);
175
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000176 int32_t GetNextInstructionId() {
177 DCHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000178 return current_instruction_id_++;
179 }
180
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000181 int32_t GetCurrentInstructionId() const {
182 return current_instruction_id_;
183 }
184
185 void SetCurrentInstructionId(int32_t id) {
186 current_instruction_id_ = id;
187 }
188
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100189 uint16_t GetMaximumNumberOfOutVRegs() const {
190 return maximum_number_of_out_vregs_;
191 }
192
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000193 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
194 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100195 }
196
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000197 void UpdateTemporariesVRegSlots(size_t slots) {
198 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100199 }
200
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000201 size_t GetTemporariesVRegSlots() const {
202 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100203 }
204
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100205 void SetNumberOfVRegs(uint16_t number_of_vregs) {
206 number_of_vregs_ = number_of_vregs;
207 }
208
209 uint16_t GetNumberOfVRegs() const {
210 return number_of_vregs_;
211 }
212
213 void SetNumberOfInVRegs(uint16_t value) {
214 number_of_in_vregs_ = value;
215 }
216
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100217 uint16_t GetNumberOfLocalVRegs() const {
218 return number_of_vregs_ - number_of_in_vregs_;
219 }
220
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100221 const GrowableArray<HBasicBlock*>& GetReversePostOrder() const {
222 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100223 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100224
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100225 const GrowableArray<HBasicBlock*>& GetLinearOrder() const {
226 return linear_order_;
227 }
228
Mingyao Yange4335eb2015-03-02 15:14:13 -0800229 bool HasArrayAccesses() const {
230 return has_array_accesses_;
231 }
232
233 void SetHasArrayAccesses(bool value) {
234 has_array_accesses_ = value;
235 }
236
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000237 bool IsDebuggable() const { return debuggable_; }
238
David Brazdil8d5b8b22015-03-24 10:51:52 +0000239 // Returns a constant of the given type and value. If it does not exist
240 // already, it is created and inserted into the graph. Only integral types
241 // are currently supported.
242 HConstant* GetConstant(Primitive::Type type, int64_t value);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000243 HNullConstant* GetNullConstant();
David Brazdil8d5b8b22015-03-24 10:51:52 +0000244 HIntConstant* GetIntConstant(int32_t value) {
245 return CreateConstant(value, &cached_int_constants_);
246 }
247 HLongConstant* GetLongConstant(int64_t value) {
248 return CreateConstant(value, &cached_long_constants_);
249 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000250
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000251 private:
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000252 HBasicBlock* FindCommonDominator(HBasicBlock* first, HBasicBlock* second) const;
253 void VisitBlockForDominatorTree(HBasicBlock* block,
254 HBasicBlock* predecessor,
255 GrowableArray<size_t>* visits);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100256 void FindBackEdges(ArenaBitVector* visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000257 void VisitBlockForBackEdges(HBasicBlock* block,
258 ArenaBitVector* visited,
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100259 ArenaBitVector* visiting);
Roland Levillainfc600dc2014-12-02 17:16:31 +0000260 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100261 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000262
David Brazdil8d5b8b22015-03-24 10:51:52 +0000263 template <class InstType, typename ValueType>
264 InstType* CreateConstant(ValueType value, ArenaSafeMap<ValueType, InstType*>* cache);
265 void InsertConstant(HConstant* instruction);
266
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000267 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000268
269 // List of blocks in insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000270 GrowableArray<HBasicBlock*> blocks_;
271
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100272 // List of blocks to perform a reverse post order tree traversal.
273 GrowableArray<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000274
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100275 // List of blocks to perform a linear order tree traversal.
276 GrowableArray<HBasicBlock*> linear_order_;
277
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000278 HBasicBlock* entry_block_;
279 HBasicBlock* exit_block_;
280
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100281 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100282 uint16_t maximum_number_of_out_vregs_;
283
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100284 // The number of virtual registers in this method. Contains the parameters.
285 uint16_t number_of_vregs_;
286
287 // The number of virtual registers used by parameters of this method.
288 uint16_t number_of_in_vregs_;
289
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000290 // Number of vreg size slots that the temporaries use (used in baseline compiler).
291 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100292
Mingyao Yange4335eb2015-03-02 15:14:13 -0800293 // Has array accesses. We can totally skip BCE if it's false.
294 bool has_array_accesses_;
295
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000296 // Indicates whether the graph should be compiled in a way that
297 // ensures full debuggability. If false, we can apply more
298 // aggressive optimizations that may limit the level of debugging.
299 const bool debuggable_;
300
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000301 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000302 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000303
David Brazdil46e2a392015-03-16 17:31:52 +0000304 // Cached common constants often needed by optimization passes.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000305 HNullConstant* cached_null_constant_;
306 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
307 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000308
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100309 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000310 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000311 DISALLOW_COPY_AND_ASSIGN(HGraph);
312};
313
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700314class HLoopInformation : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000315 public:
316 HLoopInformation(HBasicBlock* header, HGraph* graph)
317 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100318 suspend_check_(nullptr),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100319 back_edges_(graph->GetArena(), kDefaultNumberOfBackEdges),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100320 // Make bit vector growable, as the number of blocks may change.
321 blocks_(graph->GetArena(), graph->GetBlocks().Size(), true) {}
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100322
323 HBasicBlock* GetHeader() const {
324 return header_;
325 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000326
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000327 void SetHeader(HBasicBlock* block) {
328 header_ = block;
329 }
330
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100331 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
332 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
333 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
334
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000335 void AddBackEdge(HBasicBlock* back_edge) {
336 back_edges_.Add(back_edge);
337 }
338
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100339 void RemoveBackEdge(HBasicBlock* back_edge) {
340 back_edges_.Delete(back_edge);
341 }
342
David Brazdil46e2a392015-03-16 17:31:52 +0000343 bool IsBackEdge(const HBasicBlock& block) const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100344 for (size_t i = 0, e = back_edges_.Size(); i < e; ++i) {
David Brazdil46e2a392015-03-16 17:31:52 +0000345 if (back_edges_.Get(i) == &block) return true;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100346 }
347 return false;
348 }
349
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000350 size_t NumberOfBackEdges() const {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000351 return back_edges_.Size();
352 }
353
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100354 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100355
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100356 const GrowableArray<HBasicBlock*>& GetBackEdges() const {
357 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100358 }
359
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100360 void ClearBackEdges() {
361 back_edges_.Reset();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100362 }
363
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100364 // Find blocks that are part of this loop. Returns whether the loop is a natural loop,
365 // that is the header dominates the back edge.
366 bool Populate();
367
368 // Returns whether this loop information contains `block`.
369 // Note that this loop information *must* be populated before entering this function.
370 bool Contains(const HBasicBlock& block) const;
371
372 // Returns whether this loop information is an inner loop of `other`.
373 // Note that `other` *must* be populated before entering this function.
374 bool IsIn(const HLoopInformation& other) const;
375
376 const ArenaBitVector& GetBlocks() const { return blocks_; }
377
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000378 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000379 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000380
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000381 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100382 // Internal recursive implementation of `Populate`.
383 void PopulateRecursive(HBasicBlock* block);
384
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000385 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100386 HSuspendCheck* suspend_check_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000387 GrowableArray<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100388 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000389
390 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
391};
392
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100393static constexpr size_t kNoLifetime = -1;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100394static constexpr uint32_t kNoDexPc = -1;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100395
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000396// A block in a method. Contains the list of instructions represented
397// as a double linked list. Each block knows its predecessors and
398// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100399
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700400class HBasicBlock : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000401 public:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100402 explicit HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000403 : graph_(graph),
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000404 predecessors_(graph->GetArena(), kDefaultNumberOfPredecessors),
405 successors_(graph->GetArena(), kDefaultNumberOfSuccessors),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000406 loop_information_(nullptr),
407 dominator_(nullptr),
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100408 dominated_blocks_(graph->GetArena(), kDefaultNumberOfDominatedBlocks),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100409 block_id_(-1),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100410 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100411 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000412 lifetime_end_(kNoLifetime),
413 is_catch_block_(false) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000414
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100415 const GrowableArray<HBasicBlock*>& GetPredecessors() const {
416 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000417 }
418
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100419 const GrowableArray<HBasicBlock*>& GetSuccessors() const {
420 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000421 }
422
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100423 const GrowableArray<HBasicBlock*>& GetDominatedBlocks() const {
424 return dominated_blocks_;
425 }
426
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100427 bool IsEntryBlock() const {
428 return graph_->GetEntryBlock() == this;
429 }
430
431 bool IsExitBlock() const {
432 return graph_->GetExitBlock() == this;
433 }
434
David Brazdil46e2a392015-03-16 17:31:52 +0000435 bool IsSingleGoto() const;
436
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000437 void AddBackEdge(HBasicBlock* back_edge) {
438 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000439 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000440 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100441 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000442 loop_information_->AddBackEdge(back_edge);
443 }
444
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000445 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000446 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000447
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000448 int GetBlockId() const { return block_id_; }
449 void SetBlockId(int id) { block_id_ = id; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000450
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000451 HBasicBlock* GetDominator() const { return dominator_; }
452 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100453 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.Add(block); }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000454 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
455 for (size_t i = 0, e = dominated_blocks_.Size(); i < e; ++i) {
456 if (dominated_blocks_.Get(i) == existing) {
457 dominated_blocks_.Put(i, new_block);
458 return;
459 }
460 }
461 LOG(FATAL) << "Unreachable";
462 UNREACHABLE();
463 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000464
465 int NumberOfBackEdges() const {
466 return loop_information_ == nullptr
467 ? 0
468 : loop_information_->NumberOfBackEdges();
469 }
470
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100471 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
472 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100473 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100474 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +0100475 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
476 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000477
478 void AddSuccessor(HBasicBlock* block) {
479 successors_.Add(block);
480 block->predecessors_.Add(this);
481 }
482
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100483 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
484 size_t successor_index = GetSuccessorIndexOf(existing);
485 DCHECK_NE(successor_index, static_cast<size_t>(-1));
486 existing->RemovePredecessor(this);
487 new_block->predecessors_.Add(this);
488 successors_.Put(successor_index, new_block);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000489 }
490
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000491 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
492 size_t predecessor_index = GetPredecessorIndexOf(existing);
493 DCHECK_NE(predecessor_index, static_cast<size_t>(-1));
494 existing->RemoveSuccessor(this);
495 new_block->successors_.Add(this);
496 predecessors_.Put(predecessor_index, new_block);
497 }
498
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100499 void RemovePredecessor(HBasicBlock* block) {
500 predecessors_.Delete(block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100501 }
502
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000503 void RemoveSuccessor(HBasicBlock* block) {
504 successors_.Delete(block);
505 }
506
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100507 void ClearAllPredecessors() {
508 predecessors_.Reset();
509 }
510
511 void AddPredecessor(HBasicBlock* block) {
512 predecessors_.Add(block);
513 block->successors_.Add(this);
514 }
515
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100516 void SwapPredecessors() {
Nicolas Geoffrayc83d4412014-09-18 16:46:20 +0100517 DCHECK_EQ(predecessors_.Size(), 2u);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100518 HBasicBlock* temp = predecessors_.Get(0);
519 predecessors_.Put(0, predecessors_.Get(1));
520 predecessors_.Put(1, temp);
521 }
522
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100523 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) {
524 for (size_t i = 0, e = predecessors_.Size(); i < e; ++i) {
525 if (predecessors_.Get(i) == predecessor) {
526 return i;
527 }
528 }
529 return -1;
530 }
531
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100532 size_t GetSuccessorIndexOf(HBasicBlock* successor) {
533 for (size_t i = 0, e = successors_.Size(); i < e; ++i) {
534 if (successors_.Get(i) == successor) {
535 return i;
536 }
537 }
538 return -1;
539 }
540
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000541 // Split the block into two blocks just after `cursor`. Returns the newly
542 // created block. Note that this method just updates raw block information,
543 // like predecessors, successors, dominators, and instruction list. It does not
544 // update the graph, reverse post order, loop information, nor make sure the
545 // blocks are consistent (for example ending with a control flow instruction).
546 HBasicBlock* SplitAfter(HInstruction* cursor);
547
548 // Merge `other` at the end of `this`. Successors and dominated blocks of
549 // `other` are changed to be successors and dominated blocks of `this`. Note
550 // that this method does not update the graph, reverse post order, loop
551 // information, nor make sure the blocks are consistent (for example ending
552 // with a control flow instruction).
553 void MergeWith(HBasicBlock* other);
554
555 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
556 // of `this` are moved to `other`.
557 // Note that this method does not update the graph, reverse post order, loop
558 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +0000559 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000560 void ReplaceWith(HBasicBlock* other);
561
David Brazdil46e2a392015-03-16 17:31:52 +0000562 // Disconnects `this` from all its predecessors, successors and the dominator.
563 // It assumes that `this` does not dominate any blocks.
564 // Note that this method does not update the graph, reverse post order, loop
565 // information, nor make sure the blocks are consistent (for example ending
566 // with a control flow instruction).
567 void DisconnectFromAll();
568
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000569 void AddInstruction(HInstruction* instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100570 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +0100571 // Replace instruction `initial` with `replacement` within this block.
572 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
573 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100574 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100575 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +0000576 // RemoveInstruction and RemovePhi delete a given instruction from the respective
577 // instruction list. With 'ensure_safety' set to true, it verifies that the
578 // instruction is not in use and removes it from the use lists of its inputs.
579 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
580 void RemovePhi(HPhi* phi, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100581
582 bool IsLoopHeader() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100583 return (loop_information_ != nullptr) && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100584 }
585
Roland Levillain6b879dd2014-09-22 17:13:44 +0100586 bool IsLoopPreHeaderFirstPredecessor() const {
587 DCHECK(IsLoopHeader());
588 DCHECK(!GetPredecessors().IsEmpty());
589 return GetPredecessors().Get(0) == GetLoopInformation()->GetPreHeader();
590 }
591
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100592 HLoopInformation* GetLoopInformation() const {
593 return loop_information_;
594 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000595
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000596 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100597 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000598 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100599 void SetInLoop(HLoopInformation* info) {
600 if (IsLoopHeader()) {
601 // Nothing to do. This just means `info` is an outer loop.
602 } else if (loop_information_ == nullptr) {
603 loop_information_ = info;
604 } else if (loop_information_->Contains(*info->GetHeader())) {
605 // Block is currently part of an outer loop. Make it part of this inner loop.
606 // Note that a non loop header having a loop information means this loop information
607 // has already been populated
608 loop_information_ = info;
609 } else {
610 // Block is part of an inner loop. Do not update the loop information.
611 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
612 // at this point, because this method is being called while populating `info`.
613 }
614 }
615
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000616 // Raw update of the loop information.
617 void SetLoopInformation(HLoopInformation* info) {
618 loop_information_ = info;
619 }
620
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100621 bool IsInLoop() const { return loop_information_ != nullptr; }
622
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100623 // Returns wheter this block dominates the blocked passed as parameter.
624 bool Dominates(HBasicBlock* block) const;
625
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100626 size_t GetLifetimeStart() const { return lifetime_start_; }
627 size_t GetLifetimeEnd() const { return lifetime_end_; }
628
629 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
630 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
631
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100632 uint32_t GetDexPc() const { return dex_pc_; }
633
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000634 bool IsCatchBlock() const { return is_catch_block_; }
635 void SetIsCatchBlock() { is_catch_block_ = true; }
636
David Brazdil8d5b8b22015-03-24 10:51:52 +0000637 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +0000638 bool EndsWithIf() const;
639 bool HasSinglePhi() const;
640
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000641 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000642 HGraph* graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000643 GrowableArray<HBasicBlock*> predecessors_;
644 GrowableArray<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100645 HInstructionList instructions_;
646 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000647 HLoopInformation* loop_information_;
648 HBasicBlock* dominator_;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100649 GrowableArray<HBasicBlock*> dominated_blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000650 int block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100651 // The dex program counter of the first instruction of this block.
652 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100653 size_t lifetime_start_;
654 size_t lifetime_end_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000655 bool is_catch_block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000656
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000657 friend class HGraph;
658 friend class HInstruction;
659
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000660 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
661};
662
David Brazdilb2bd1c52015-03-25 11:17:37 +0000663// Iterates over the LoopInformation of all loops which contain 'block'
664// from the innermost to the outermost.
665class HLoopInformationOutwardIterator : public ValueObject {
666 public:
667 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
668 : current_(block.GetLoopInformation()) {}
669
670 bool Done() const { return current_ == nullptr; }
671
672 void Advance() {
673 DCHECK(!Done());
674 current_ = current_->GetHeader()->GetDominator()->GetLoopInformation();
675 }
676
677 HLoopInformation* Current() const {
678 DCHECK(!Done());
679 return current_;
680 }
681
682 private:
683 HLoopInformation* current_;
684
685 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
686};
687
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100688#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
689 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000690 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000691 M(ArrayGet, Instruction) \
692 M(ArrayLength, Instruction) \
693 M(ArraySet, Instruction) \
David Brazdil66d126e2015-04-03 16:02:44 +0100694 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000695 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +0000696 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000697 M(CheckCast, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100698 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000699 M(Compare, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100700 M(Condition, BinaryOperation) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700701 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000702 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +0000703 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000704 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100705 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000706 M(Exit, Instruction) \
707 M(FloatConstant, Constant) \
708 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100709 M(GreaterThan, Condition) \
710 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100711 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000712 M(InstanceFieldGet, Instruction) \
713 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000714 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100715 M(IntConstant, Constant) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +0000716 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000717 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100718 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000719 M(LessThan, Condition) \
720 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000721 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000722 M(LoadException, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100723 M(LoadLocal, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000724 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100725 M(Local, Instruction) \
726 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +0100727 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +0000728 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000729 M(Mul, BinaryOperation) \
730 M(Neg, UnaryOperation) \
731 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100732 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +0100733 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000734 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000735 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000736 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000737 M(Or, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100738 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000739 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100740 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000741 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100742 M(Return, Instruction) \
743 M(ReturnVoid, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000744 M(Shl, BinaryOperation) \
745 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100746 M(StaticFieldGet, Instruction) \
747 M(StaticFieldSet, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100748 M(StoreLocal, Instruction) \
749 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100750 M(SuspendCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000751 M(Temporary, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000752 M(Throw, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +0000753 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000754 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000755 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000756
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100757#define FOR_EACH_INSTRUCTION(M) \
758 FOR_EACH_CONCRETE_INSTRUCTION(M) \
759 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +0100760 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +0100761 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100762 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -0700763
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100764#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000765FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
766#undef FORWARD_DECLARATION
767
Roland Levillainccc07a92014-09-16 14:48:16 +0100768#define DECLARE_INSTRUCTION(type) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000769 InstructionKind GetKind() const OVERRIDE { return k##type; } \
770 const char* DebugName() const OVERRIDE { return #type; } \
771 const H##type* As##type() const OVERRIDE { return this; } \
772 H##type* As##type() OVERRIDE { return this; } \
773 bool InstructionTypeEquals(HInstruction* other) const OVERRIDE { \
Roland Levillainccc07a92014-09-16 14:48:16 +0100774 return other->Is##type(); \
775 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000776 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000777
David Brazdiled596192015-01-23 10:39:45 +0000778template <typename T> class HUseList;
779
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100780template <typename T>
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700781class HUseListNode : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000782 public:
David Brazdiled596192015-01-23 10:39:45 +0000783 HUseListNode* GetPrevious() const { return prev_; }
784 HUseListNode* GetNext() const { return next_; }
785 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100786 size_t GetIndex() const { return index_; }
787
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000788 private:
David Brazdiled596192015-01-23 10:39:45 +0000789 HUseListNode(T user, size_t index)
790 : user_(user), index_(index), prev_(nullptr), next_(nullptr) {}
791
792 T const user_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100793 const size_t index_;
David Brazdiled596192015-01-23 10:39:45 +0000794 HUseListNode<T>* prev_;
795 HUseListNode<T>* next_;
796
797 friend class HUseList<T>;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000798
799 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
800};
801
David Brazdiled596192015-01-23 10:39:45 +0000802template <typename T>
803class HUseList : public ValueObject {
804 public:
805 HUseList() : first_(nullptr) {}
806
807 void Clear() {
808 first_ = nullptr;
809 }
810
811 // Adds a new entry at the beginning of the use list and returns
812 // the newly created node.
813 HUseListNode<T>* AddUse(T user, size_t index, ArenaAllocator* arena) {
David Brazdilea55b932015-01-27 17:12:29 +0000814 HUseListNode<T>* new_node = new (arena) HUseListNode<T>(user, index);
David Brazdiled596192015-01-23 10:39:45 +0000815 if (IsEmpty()) {
816 first_ = new_node;
817 } else {
818 first_->prev_ = new_node;
819 new_node->next_ = first_;
820 first_ = new_node;
821 }
822 return new_node;
823 }
824
825 HUseListNode<T>* GetFirst() const {
826 return first_;
827 }
828
829 void Remove(HUseListNode<T>* node) {
David Brazdil1abb4192015-02-17 18:33:36 +0000830 DCHECK(node != nullptr);
831 DCHECK(Contains(node));
832
David Brazdiled596192015-01-23 10:39:45 +0000833 if (node->prev_ != nullptr) {
834 node->prev_->next_ = node->next_;
835 }
836 if (node->next_ != nullptr) {
837 node->next_->prev_ = node->prev_;
838 }
839 if (node == first_) {
840 first_ = node->next_;
841 }
842 }
843
David Brazdil1abb4192015-02-17 18:33:36 +0000844 bool Contains(const HUseListNode<T>* node) const {
845 if (node == nullptr) {
846 return false;
847 }
848 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
849 if (current == node) {
850 return true;
851 }
852 }
853 return false;
854 }
855
David Brazdiled596192015-01-23 10:39:45 +0000856 bool IsEmpty() const {
857 return first_ == nullptr;
858 }
859
860 bool HasOnlyOneUse() const {
861 return first_ != nullptr && first_->next_ == nullptr;
862 }
863
864 private:
865 HUseListNode<T>* first_;
866};
867
868template<typename T>
869class HUseIterator : public ValueObject {
870 public:
871 explicit HUseIterator(const HUseList<T>& uses) : current_(uses.GetFirst()) {}
872
873 bool Done() const { return current_ == nullptr; }
874
875 void Advance() {
876 DCHECK(!Done());
877 current_ = current_->GetNext();
878 }
879
880 HUseListNode<T>* Current() const {
881 DCHECK(!Done());
882 return current_;
883 }
884
885 private:
886 HUseListNode<T>* current_;
887
888 friend class HValue;
889};
890
David Brazdil1abb4192015-02-17 18:33:36 +0000891// This class is used by HEnvironment and HInstruction classes to record the
892// instructions they use and pointers to the corresponding HUseListNodes kept
893// by the used instructions.
894template <typename T>
895class HUserRecord : public ValueObject {
896 public:
897 HUserRecord() : instruction_(nullptr), use_node_(nullptr) {}
898 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), use_node_(nullptr) {}
899
900 HUserRecord(const HUserRecord<T>& old_record, HUseListNode<T>* use_node)
901 : instruction_(old_record.instruction_), use_node_(use_node) {
902 DCHECK(instruction_ != nullptr);
903 DCHECK(use_node_ != nullptr);
904 DCHECK(old_record.use_node_ == nullptr);
905 }
906
907 HInstruction* GetInstruction() const { return instruction_; }
908 HUseListNode<T>* GetUseNode() const { return use_node_; }
909
910 private:
911 // Instruction used by the user.
912 HInstruction* instruction_;
913
914 // Corresponding entry in the use list kept by 'instruction_'.
915 HUseListNode<T>* use_node_;
916};
917
Calin Juravle27df7582015-04-17 19:12:31 +0100918// TODO: Add better documentation to this class and maybe refactor with more suggestive names.
919// - Has(All)SideEffects suggests that all the side effects are present but only ChangesSomething
920// flag is consider.
921// - DependsOn suggests that there is a real dependency between side effects but it only
922// checks DependendsOnSomething flag.
923//
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100924// Represents the side effects an instruction may have.
925class SideEffects : public ValueObject {
926 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100927 SideEffects() : flags_(0) {}
928
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100929 static SideEffects None() {
930 return SideEffects(0);
931 }
932
933 static SideEffects All() {
934 return SideEffects(ChangesSomething().flags_ | DependsOnSomething().flags_);
935 }
936
937 static SideEffects ChangesSomething() {
938 return SideEffects((1 << kFlagChangesCount) - 1);
939 }
940
941 static SideEffects DependsOnSomething() {
942 int count = kFlagDependsOnCount - kFlagChangesCount;
943 return SideEffects(((1 << count) - 1) << kFlagChangesCount);
944 }
945
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100946 SideEffects Union(SideEffects other) const {
947 return SideEffects(flags_ | other.flags_);
948 }
949
Roland Levillain72bceff2014-09-15 18:29:00 +0100950 bool HasSideEffects() const {
951 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
952 return (flags_ & all_bits_set) != 0;
953 }
954
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100955 bool HasAllSideEffects() const {
956 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
957 return all_bits_set == (flags_ & all_bits_set);
958 }
959
960 bool DependsOn(SideEffects other) const {
961 size_t depends_flags = other.ComputeDependsFlags();
962 return (flags_ & depends_flags) != 0;
963 }
964
965 bool HasDependencies() const {
966 int count = kFlagDependsOnCount - kFlagChangesCount;
967 size_t all_bits_set = (1 << count) - 1;
968 return ((flags_ >> kFlagChangesCount) & all_bits_set) != 0;
969 }
970
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100971 private:
972 static constexpr int kFlagChangesSomething = 0;
973 static constexpr int kFlagChangesCount = kFlagChangesSomething + 1;
974
975 static constexpr int kFlagDependsOnSomething = kFlagChangesCount;
976 static constexpr int kFlagDependsOnCount = kFlagDependsOnSomething + 1;
977
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100978 explicit SideEffects(size_t flags) : flags_(flags) {}
979
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100980 size_t ComputeDependsFlags() const {
981 return flags_ << kFlagChangesCount;
982 }
983
984 size_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100985};
986
David Brazdiled596192015-01-23 10:39:45 +0000987// A HEnvironment object contains the values of virtual registers at a given location.
988class HEnvironment : public ArenaObject<kArenaAllocMisc> {
989 public:
990 HEnvironment(ArenaAllocator* arena, size_t number_of_vregs)
991 : vregs_(arena, number_of_vregs) {
992 vregs_.SetSize(number_of_vregs);
993 for (size_t i = 0; i < number_of_vregs; i++) {
David Brazdil1abb4192015-02-17 18:33:36 +0000994 vregs_.Put(i, HUserRecord<HEnvironment*>());
David Brazdiled596192015-01-23 10:39:45 +0000995 }
996 }
997
998 void CopyFrom(HEnvironment* env);
999
1000 void SetRawEnvAt(size_t index, HInstruction* instruction) {
David Brazdil1abb4192015-02-17 18:33:36 +00001001 vregs_.Put(index, HUserRecord<HEnvironment*>(instruction));
David Brazdiled596192015-01-23 10:39:45 +00001002 }
1003
1004 HInstruction* GetInstructionAt(size_t index) const {
David Brazdil1abb4192015-02-17 18:33:36 +00001005 return vregs_.Get(index).GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001006 }
1007
David Brazdil1abb4192015-02-17 18:33:36 +00001008 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001009
1010 size_t Size() const { return vregs_.Size(); }
1011
1012 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001013 // Record instructions' use entries of this environment for constant-time removal.
1014 // It should only be called by HInstruction when a new environment use is added.
1015 void RecordEnvUse(HUseListNode<HEnvironment*>* env_use) {
1016 DCHECK(env_use->GetUser() == this);
1017 size_t index = env_use->GetIndex();
1018 vregs_.Put(index, HUserRecord<HEnvironment*>(vregs_.Get(index), env_use));
1019 }
David Brazdiled596192015-01-23 10:39:45 +00001020
David Brazdil1abb4192015-02-17 18:33:36 +00001021 GrowableArray<HUserRecord<HEnvironment*> > vregs_;
David Brazdiled596192015-01-23 10:39:45 +00001022
David Brazdil1abb4192015-02-17 18:33:36 +00001023 friend HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001024
1025 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1026};
1027
Calin Juravleacf735c2015-02-12 15:25:22 +00001028class ReferenceTypeInfo : ValueObject {
1029 public:
Calin Juravleb1498f62015-02-16 13:13:29 +00001030 typedef Handle<mirror::Class> TypeHandle;
1031
1032 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact)
1033 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1034 if (type_handle->IsObjectClass()) {
1035 // Override the type handle to be consistent with the case when we get to
1036 // Top but don't have the Object class available. It avoids having to guess
1037 // what value the type_handle has when it's Top.
1038 return ReferenceTypeInfo(TypeHandle(), is_exact, true);
1039 } else {
1040 return ReferenceTypeInfo(type_handle, is_exact, false);
1041 }
1042 }
1043
1044 static ReferenceTypeInfo CreateTop(bool is_exact) {
1045 return ReferenceTypeInfo(TypeHandle(), is_exact, true);
Calin Juravleacf735c2015-02-12 15:25:22 +00001046 }
1047
1048 bool IsExact() const { return is_exact_; }
1049 bool IsTop() const { return is_top_; }
1050
1051 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
1052
Calin Juravleb1498f62015-02-16 13:13:29 +00001053 bool IsSupertypeOf(ReferenceTypeInfo rti) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Calin Juravleacf735c2015-02-12 15:25:22 +00001054 if (IsTop()) {
1055 // Top (equivalent for java.lang.Object) is supertype of anything.
1056 return true;
1057 }
1058 if (rti.IsTop()) {
1059 // If we get here `this` is not Top() so it can't be a supertype.
1060 return false;
1061 }
1062 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
1063 }
1064
1065 // Returns true if the type information provide the same amount of details.
1066 // Note that it does not mean that the instructions have the same actual type
1067 // (e.g. tops are equal but they can be the result of a merge).
1068 bool IsEqual(ReferenceTypeInfo rti) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1069 if (IsExact() != rti.IsExact()) {
1070 return false;
1071 }
1072 if (IsTop() && rti.IsTop()) {
1073 // `Top` means java.lang.Object, so the types are equivalent.
1074 return true;
1075 }
1076 if (IsTop() || rti.IsTop()) {
1077 // If only one is top or object than they are not equivalent.
1078 // NB: We need this extra check because the type_handle of `Top` is invalid
1079 // and we cannot inspect its reference.
1080 return false;
1081 }
1082
1083 // Finally check the types.
1084 return GetTypeHandle().Get() == rti.GetTypeHandle().Get();
1085 }
1086
1087 private:
Calin Juravleb1498f62015-02-16 13:13:29 +00001088 ReferenceTypeInfo() : ReferenceTypeInfo(TypeHandle(), false, true) {}
1089 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact, bool is_top)
1090 : type_handle_(type_handle), is_exact_(is_exact), is_top_(is_top) {}
1091
Calin Juravleacf735c2015-02-12 15:25:22 +00001092 // The class of the object.
Calin Juravleb1498f62015-02-16 13:13:29 +00001093 TypeHandle type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001094 // Whether or not the type is exact or a superclass of the actual type.
Calin Juravleb1498f62015-02-16 13:13:29 +00001095 // Whether or not we have any information about this type.
Calin Juravleacf735c2015-02-12 15:25:22 +00001096 bool is_exact_;
1097 // A true value here means that the object type should be java.lang.Object.
1098 // We don't have access to the corresponding mirror object every time so this
1099 // flag acts as a substitute. When true, the TypeHandle refers to a null
1100 // pointer and should not be used.
1101 bool is_top_;
1102};
1103
1104std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
1105
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001106class HInstruction : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001107 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001108 explicit HInstruction(SideEffects side_effects)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001109 : previous_(nullptr),
1110 next_(nullptr),
1111 block_(nullptr),
1112 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001113 ssa_index_(-1),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001114 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001115 locations_(nullptr),
1116 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001117 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001118 side_effects_(side_effects),
1119 reference_type_info_(ReferenceTypeInfo::CreateTop(/* is_exact */ false)) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001120
Dave Allison20dfc792014-06-16 20:44:29 -07001121 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001122
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001123#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001124 enum InstructionKind {
1125 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1126 };
1127#undef DECLARE_KIND
1128
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001129 HInstruction* GetNext() const { return next_; }
1130 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001131
Calin Juravle77520bc2015-01-12 18:45:46 +00001132 HInstruction* GetNextDisregardingMoves() const;
1133 HInstruction* GetPreviousDisregardingMoves() const;
1134
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001135 HBasicBlock* GetBlock() const { return block_; }
1136 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001137 bool IsInBlock() const { return block_ != nullptr; }
1138 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +01001139 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001140
Roland Levillain6b879dd2014-09-22 17:13:44 +01001141 virtual size_t InputCount() const = 0;
David Brazdil1abb4192015-02-17 18:33:36 +00001142 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001143
1144 virtual void Accept(HGraphVisitor* visitor) = 0;
1145 virtual const char* DebugName() const = 0;
1146
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001147 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
David Brazdil1abb4192015-02-17 18:33:36 +00001148 void SetRawInputAt(size_t index, HInstruction* input) {
1149 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1150 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001151
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001152 virtual bool NeedsEnvironment() const { return false; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001153 virtual bool IsControlFlow() const { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01001154 virtual bool CanThrow() const { return false; }
Roland Levillain72bceff2014-09-15 18:29:00 +01001155 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001156
Calin Juravle10e244f2015-01-26 18:54:32 +00001157 // Does not apply for all instructions, but having this at top level greatly
1158 // simplifies the null check elimination.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001159 virtual bool CanBeNull() const {
1160 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1161 return true;
1162 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001163
Calin Juravle641547a2015-04-21 22:08:51 +01001164 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj) const {
1165 UNUSED(obj);
1166 return false;
1167 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001168
Calin Juravleacf735c2015-02-12 15:25:22 +00001169 void SetReferenceTypeInfo(ReferenceTypeInfo reference_type_info) {
Calin Juravle61d544b2015-02-23 16:46:57 +00001170 DCHECK_EQ(GetType(), Primitive::kPrimNot);
Calin Juravleacf735c2015-02-12 15:25:22 +00001171 reference_type_info_ = reference_type_info;
1172 }
1173
Calin Juravle61d544b2015-02-23 16:46:57 +00001174 ReferenceTypeInfo GetReferenceTypeInfo() const {
1175 DCHECK_EQ(GetType(), Primitive::kPrimNot);
1176 return reference_type_info_;
1177 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001178
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001179 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001180 DCHECK(user != nullptr);
1181 HUseListNode<HInstruction*>* use =
1182 uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1183 user->SetRawInputRecordAt(index, HUserRecord<HInstruction*>(user->InputRecordAt(index), use));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001184 }
1185
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001186 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001187 DCHECK(user != nullptr);
David Brazdiled596192015-01-23 10:39:45 +00001188 HUseListNode<HEnvironment*>* env_use =
1189 env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1190 user->RecordEnvUse(env_use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001191 }
1192
David Brazdil1abb4192015-02-17 18:33:36 +00001193 void RemoveAsUserOfInput(size_t input) {
1194 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
1195 input_use.GetInstruction()->uses_.Remove(input_use.GetUseNode());
1196 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001197
David Brazdil1abb4192015-02-17 18:33:36 +00001198 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1199 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001200
David Brazdiled596192015-01-23 10:39:45 +00001201 bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); }
1202 bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); }
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001203 bool HasNonEnvironmentUses() const { return !uses_.IsEmpty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001204 bool HasOnlyOneNonEnvironmentUse() const {
1205 return !HasEnvironmentUses() && GetUses().HasOnlyOneUse();
1206 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001207
Roland Levillain6c82d402014-10-13 16:10:27 +01001208 // Does this instruction strictly dominate `other_instruction`?
1209 // Returns false if this instruction and `other_instruction` are the same.
1210 // Aborts if this instruction and `other_instruction` are both phis.
1211 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001212
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001213 int GetId() const { return id_; }
1214 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001215
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001216 int GetSsaIndex() const { return ssa_index_; }
1217 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1218 bool HasSsaIndex() const { return ssa_index_ != -1; }
1219
1220 bool HasEnvironment() const { return environment_ != nullptr; }
1221 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001222 // Set the `environment_` field. Raw because this method does not
1223 // update the uses lists.
1224 void SetRawEnvironment(HEnvironment* environment) { environment_ = environment; }
1225
1226 // Set the environment of this instruction, copying it from `environment`. While
1227 // copying, the uses lists are being updated.
1228 void CopyEnvironmentFrom(HEnvironment* environment) {
1229 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
1230 environment_ = new (allocator) HEnvironment(allocator, environment->Size());
1231 environment_->CopyFrom(environment);
1232 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001233
Nicolas Geoffray39468442014-09-02 15:17:15 +01001234 // Returns the number of entries in the environment. Typically, that is the
1235 // number of dex registers in a method. It could be more in case of inlining.
1236 size_t EnvironmentSize() const;
1237
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001238 LocationSummary* GetLocations() const { return locations_; }
1239 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001240
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001241 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001242 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001243
Alexandre Rames188d4312015-04-09 18:30:21 +01001244 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
1245 // uses of this instruction by `other` are *not* updated.
1246 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
1247 ReplaceWith(other);
1248 other->ReplaceInput(this, use_index);
1249 }
1250
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001251 // Move `this` instruction before `cursor`.
1252 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001253
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001254#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01001255 bool Is##type() const { return (As##type() != nullptr); } \
1256 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001257 virtual H##type* As##type() { return nullptr; }
1258
1259 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
1260#undef INSTRUCTION_TYPE_CHECK
1261
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001262 // Returns whether the instruction can be moved within the graph.
1263 virtual bool CanBeMoved() const { return false; }
1264
1265 // Returns whether the two instructions are of the same kind.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001266 virtual bool InstructionTypeEquals(HInstruction* other) const {
1267 UNUSED(other);
1268 return false;
1269 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001270
1271 // Returns whether any data encoded in the two instructions is equal.
1272 // This method does not look at the inputs. Both instructions must be
1273 // of the same type, otherwise the method has undefined behavior.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001274 virtual bool InstructionDataEquals(HInstruction* other) const {
1275 UNUSED(other);
1276 return false;
1277 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001278
1279 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00001280 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001281 // 2) Their inputs are identical.
1282 bool Equals(HInstruction* other) const;
1283
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001284 virtual InstructionKind GetKind() const = 0;
1285
1286 virtual size_t ComputeHashCode() const {
1287 size_t result = GetKind();
1288 for (size_t i = 0, e = InputCount(); i < e; ++i) {
1289 result = (result * 31) + InputAt(i)->GetId();
1290 }
1291 return result;
1292 }
1293
1294 SideEffects GetSideEffects() const { return side_effects_; }
1295
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001296 size_t GetLifetimePosition() const { return lifetime_position_; }
1297 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
1298 LiveInterval* GetLiveInterval() const { return live_interval_; }
1299 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
1300 bool HasLiveInterval() const { return live_interval_ != nullptr; }
1301
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001302 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
1303
1304 // Returns whether the code generation of the instruction will require to have access
1305 // to the current method. Such instructions are:
1306 // (1): Instructions that require an environment, as calling the runtime requires
1307 // to walk the stack and have the current method stored at a specific stack address.
1308 // (2): Object literals like classes and strings, that are loaded from the dex cache
1309 // fields of the current method.
1310 bool NeedsCurrentMethod() const {
1311 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
1312 }
1313
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001314 virtual bool NeedsDexCache() const { return false; }
1315
David Brazdil1abb4192015-02-17 18:33:36 +00001316 protected:
1317 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
1318 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
1319
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001320 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001321 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); }
1322
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001323 HInstruction* previous_;
1324 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001325 HBasicBlock* block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001326
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001327 // An instruction gets an id when it is added to the graph.
1328 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01001329 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001330 int id_;
1331
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001332 // When doing liveness analysis, instructions that have uses get an SSA index.
1333 int ssa_index_;
1334
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001335 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00001336 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001337
1338 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00001339 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001340
Nicolas Geoffray39468442014-09-02 15:17:15 +01001341 // The environment associated with this instruction. Not null if the instruction
1342 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001343 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001344
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001345 // Set by the code generator.
1346 LocationSummary* locations_;
1347
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001348 // Set by the liveness analysis.
1349 LiveInterval* live_interval_;
1350
1351 // Set by the liveness analysis, this is the position in a linear
1352 // order of blocks where this instruction's live interval start.
1353 size_t lifetime_position_;
1354
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001355 const SideEffects side_effects_;
1356
Calin Juravleacf735c2015-02-12 15:25:22 +00001357 // TODO: for primitive types this should be marked as invalid.
1358 ReferenceTypeInfo reference_type_info_;
1359
David Brazdil1abb4192015-02-17 18:33:36 +00001360 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001361 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00001362 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001363 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001364 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001365
1366 DISALLOW_COPY_AND_ASSIGN(HInstruction);
1367};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001368std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001369
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001370class HInputIterator : public ValueObject {
1371 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001372 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001373
1374 bool Done() const { return index_ == instruction_->InputCount(); }
1375 HInstruction* Current() const { return instruction_->InputAt(index_); }
1376 void Advance() { index_++; }
1377
1378 private:
1379 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001380 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001381
1382 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
1383};
1384
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001385class HInstructionIterator : public ValueObject {
1386 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001387 explicit HInstructionIterator(const HInstructionList& instructions)
1388 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001389 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001390 }
1391
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001392 bool Done() const { return instruction_ == nullptr; }
1393 HInstruction* Current() const { return instruction_; }
1394 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001395 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001396 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001397 }
1398
1399 private:
1400 HInstruction* instruction_;
1401 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001402
1403 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001404};
1405
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001406class HBackwardInstructionIterator : public ValueObject {
1407 public:
1408 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
1409 : instruction_(instructions.last_instruction_) {
1410 next_ = Done() ? nullptr : instruction_->GetPrevious();
1411 }
1412
1413 bool Done() const { return instruction_ == nullptr; }
1414 HInstruction* Current() const { return instruction_; }
1415 void Advance() {
1416 instruction_ = next_;
1417 next_ = Done() ? nullptr : instruction_->GetPrevious();
1418 }
1419
1420 private:
1421 HInstruction* instruction_;
1422 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001423
1424 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001425};
1426
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001427// An embedded container with N elements of type T. Used (with partial
1428// specialization for N=0) because embedded arrays cannot have size 0.
1429template<typename T, intptr_t N>
1430class EmbeddedArray {
1431 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001432 EmbeddedArray() : elements_() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001433
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001434 intptr_t GetLength() const { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001435
1436 const T& operator[](intptr_t i) const {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001437 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001438 return elements_[i];
1439 }
1440
1441 T& operator[](intptr_t i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001442 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001443 return elements_[i];
1444 }
1445
1446 const T& At(intptr_t i) const {
1447 return (*this)[i];
1448 }
1449
1450 void SetAt(intptr_t i, const T& val) {
1451 (*this)[i] = val;
1452 }
1453
1454 private:
1455 T elements_[N];
1456};
1457
1458template<typename T>
1459class EmbeddedArray<T, 0> {
1460 public:
1461 intptr_t length() const { return 0; }
1462 const T& operator[](intptr_t i) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001463 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001464 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001465 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001466 }
1467 T& operator[](intptr_t i) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001468 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001469 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001470 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001471 }
1472};
1473
1474template<intptr_t N>
1475class HTemplateInstruction: public HInstruction {
1476 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001477 HTemplateInstruction<N>(SideEffects side_effects)
1478 : HInstruction(side_effects), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07001479 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001480
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001481 size_t InputCount() const OVERRIDE { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001482
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001483 protected:
David Brazdil1abb4192015-02-17 18:33:36 +00001484 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_[i]; }
1485
1486 void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE {
1487 inputs_[i] = input;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001488 }
1489
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001490 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001491 EmbeddedArray<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001492
1493 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001494};
1495
Dave Allison20dfc792014-06-16 20:44:29 -07001496template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001497class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07001498 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001499 HExpression<N>(Primitive::Type type, SideEffects side_effects)
1500 : HTemplateInstruction<N>(side_effects), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07001501 virtual ~HExpression() {}
1502
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001503 Primitive::Type GetType() const OVERRIDE { return type_; }
Dave Allison20dfc792014-06-16 20:44:29 -07001504
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001505 protected:
1506 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07001507};
1508
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001509// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
1510// instruction that branches to the exit block.
1511class HReturnVoid : public HTemplateInstruction<0> {
1512 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001513 HReturnVoid() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001514
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001515 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001516
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001517 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001518
1519 private:
1520 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
1521};
1522
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001523// Represents dex's RETURN opcodes. A HReturn is a control flow
1524// instruction that branches to the exit block.
1525class HReturn : public HTemplateInstruction<1> {
1526 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001527 explicit HReturn(HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001528 SetRawInputAt(0, value);
1529 }
1530
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001531 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001532
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001533 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001534
1535 private:
1536 DISALLOW_COPY_AND_ASSIGN(HReturn);
1537};
1538
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001539// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001540// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001541// exit block.
1542class HExit : public HTemplateInstruction<0> {
1543 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001544 HExit() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001545
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001546 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001547
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001548 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001549
1550 private:
1551 DISALLOW_COPY_AND_ASSIGN(HExit);
1552};
1553
1554// Jumps from one block to another.
1555class HGoto : public HTemplateInstruction<0> {
1556 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001557 HGoto() : HTemplateInstruction(SideEffects::None()) {}
1558
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001559 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001560
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001561 HBasicBlock* GetSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001562 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001563 }
1564
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001565 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001566
1567 private:
1568 DISALLOW_COPY_AND_ASSIGN(HGoto);
1569};
1570
Dave Allison20dfc792014-06-16 20:44:29 -07001571
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001572// Conditional branch. A block ending with an HIf instruction must have
1573// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001574class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001575 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001576 explicit HIf(HInstruction* input) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001577 SetRawInputAt(0, input);
1578 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001579
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001580 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001581
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001582 HBasicBlock* IfTrueSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001583 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001584 }
1585
1586 HBasicBlock* IfFalseSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001587 return GetBlock()->GetSuccessors().Get(1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001588 }
1589
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001590 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001591
1592 private:
1593 DISALLOW_COPY_AND_ASSIGN(HIf);
1594};
1595
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001596// Deoptimize to interpreter, upon checking a condition.
1597class HDeoptimize : public HTemplateInstruction<1> {
1598 public:
1599 HDeoptimize(HInstruction* cond, uint32_t dex_pc)
1600 : HTemplateInstruction(SideEffects::None()),
1601 dex_pc_(dex_pc) {
1602 SetRawInputAt(0, cond);
1603 }
1604
1605 bool NeedsEnvironment() const OVERRIDE { return true; }
1606 bool CanThrow() const OVERRIDE { return true; }
1607 uint32_t GetDexPc() const { return dex_pc_; }
1608
1609 DECLARE_INSTRUCTION(Deoptimize);
1610
1611 private:
1612 uint32_t dex_pc_;
1613
1614 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
1615};
1616
Roland Levillain88cb1752014-10-20 16:36:47 +01001617class HUnaryOperation : public HExpression<1> {
1618 public:
1619 HUnaryOperation(Primitive::Type result_type, HInstruction* input)
1620 : HExpression(result_type, SideEffects::None()) {
1621 SetRawInputAt(0, input);
1622 }
1623
1624 HInstruction* GetInput() const { return InputAt(0); }
1625 Primitive::Type GetResultType() const { return GetType(); }
1626
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001627 bool CanBeMoved() const OVERRIDE { return true; }
1628 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001629 UNUSED(other);
1630 return true;
1631 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001632
Roland Levillain9240d6a2014-10-20 16:47:04 +01001633 // Try to statically evaluate `operation` and return a HConstant
1634 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001635 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01001636 HConstant* TryStaticEvaluation() const;
1637
1638 // Apply this operation to `x`.
1639 virtual int32_t Evaluate(int32_t x) const = 0;
1640 virtual int64_t Evaluate(int64_t x) const = 0;
1641
Roland Levillain88cb1752014-10-20 16:36:47 +01001642 DECLARE_INSTRUCTION(UnaryOperation);
1643
1644 private:
1645 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
1646};
1647
Dave Allison20dfc792014-06-16 20:44:29 -07001648class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001649 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001650 HBinaryOperation(Primitive::Type result_type,
1651 HInstruction* left,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001652 HInstruction* right) : HExpression(result_type, SideEffects::None()) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001653 SetRawInputAt(0, left);
1654 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001655 }
1656
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001657 HInstruction* GetLeft() const { return InputAt(0); }
1658 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07001659 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001660
Mingyao Yangdc5ac732015-02-25 11:28:05 -08001661 virtual bool IsCommutative() const { return false; }
1662
1663 // Put constant on the right.
1664 // Returns whether order is changed.
1665 bool OrderInputsWithConstantOnTheRight() {
1666 HInstruction* left = InputAt(0);
1667 HInstruction* right = InputAt(1);
1668 if (left->IsConstant() && !right->IsConstant()) {
1669 ReplaceInput(right, 0);
1670 ReplaceInput(left, 1);
1671 return true;
1672 }
1673 return false;
1674 }
1675
1676 // Order inputs by instruction id, but favor constant on the right side.
1677 // This helps GVN for commutative ops.
1678 void OrderInputs() {
1679 DCHECK(IsCommutative());
1680 HInstruction* left = InputAt(0);
1681 HInstruction* right = InputAt(1);
1682 if (left == right || (!left->IsConstant() && right->IsConstant())) {
1683 return;
1684 }
1685 if (OrderInputsWithConstantOnTheRight()) {
1686 return;
1687 }
1688 // Order according to instruction id.
1689 if (left->GetId() > right->GetId()) {
1690 ReplaceInput(right, 0);
1691 ReplaceInput(left, 1);
1692 }
1693 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001694
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001695 bool CanBeMoved() const OVERRIDE { return true; }
1696 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001697 UNUSED(other);
1698 return true;
1699 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001700
Roland Levillain9240d6a2014-10-20 16:47:04 +01001701 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01001702 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001703 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01001704 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01001705
1706 // Apply this operation to `x` and `y`.
1707 virtual int32_t Evaluate(int32_t x, int32_t y) const = 0;
1708 virtual int64_t Evaluate(int64_t x, int64_t y) const = 0;
1709
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001710 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001711 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001712 HConstant* GetConstantRight() const;
1713
1714 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001715 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001716 HInstruction* GetLeastConstantLeft() const;
1717
Roland Levillainccc07a92014-09-16 14:48:16 +01001718 DECLARE_INSTRUCTION(BinaryOperation);
1719
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001720 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001721 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
1722};
1723
Dave Allison20dfc792014-06-16 20:44:29 -07001724class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001725 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001726 HCondition(HInstruction* first, HInstruction* second)
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001727 : HBinaryOperation(Primitive::kPrimBoolean, first, second),
1728 needs_materialization_(true) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001729
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001730 bool NeedsMaterialization() const { return needs_materialization_; }
1731 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001732
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001733 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001734 // `instruction`, and disregard moves in between.
1735 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001736
Dave Allison20dfc792014-06-16 20:44:29 -07001737 DECLARE_INSTRUCTION(Condition);
1738
1739 virtual IfCondition GetCondition() const = 0;
1740
1741 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001742 // For register allocation purposes, returns whether this instruction needs to be
1743 // materialized (that is, not just be in the processor flags).
1744 bool needs_materialization_;
1745
Dave Allison20dfc792014-06-16 20:44:29 -07001746 DISALLOW_COPY_AND_ASSIGN(HCondition);
1747};
1748
1749// Instruction to check if two inputs are equal to each other.
1750class HEqual : public HCondition {
1751 public:
1752 HEqual(HInstruction* first, HInstruction* second)
1753 : HCondition(first, second) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001754
Mingyao Yangdc5ac732015-02-25 11:28:05 -08001755 bool IsCommutative() const OVERRIDE { return true; }
1756
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001757 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001758 return x == y ? 1 : 0;
1759 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001760 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001761 return x == y ? 1 : 0;
1762 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001763
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001764 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001765
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001766 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001767 return kCondEQ;
1768 }
1769
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001770 private:
1771 DISALLOW_COPY_AND_ASSIGN(HEqual);
1772};
1773
Dave Allison20dfc792014-06-16 20:44:29 -07001774class HNotEqual : public HCondition {
1775 public:
1776 HNotEqual(HInstruction* first, HInstruction* second)
1777 : HCondition(first, second) {}
1778
Mingyao Yangdc5ac732015-02-25 11:28:05 -08001779 bool IsCommutative() const OVERRIDE { return true; }
1780
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001781 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001782 return x != y ? 1 : 0;
1783 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001784 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001785 return x != y ? 1 : 0;
1786 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001787
Dave Allison20dfc792014-06-16 20:44:29 -07001788 DECLARE_INSTRUCTION(NotEqual);
1789
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001790 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001791 return kCondNE;
1792 }
1793
1794 private:
1795 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
1796};
1797
1798class HLessThan : public HCondition {
1799 public:
1800 HLessThan(HInstruction* first, HInstruction* second)
1801 : HCondition(first, second) {}
1802
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001803 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001804 return x < y ? 1 : 0;
1805 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001806 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001807 return x < y ? 1 : 0;
1808 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001809
Dave Allison20dfc792014-06-16 20:44:29 -07001810 DECLARE_INSTRUCTION(LessThan);
1811
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001812 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001813 return kCondLT;
1814 }
1815
1816 private:
1817 DISALLOW_COPY_AND_ASSIGN(HLessThan);
1818};
1819
1820class HLessThanOrEqual : public HCondition {
1821 public:
1822 HLessThanOrEqual(HInstruction* first, HInstruction* second)
1823 : HCondition(first, second) {}
1824
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001825 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001826 return x <= y ? 1 : 0;
1827 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001828 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001829 return x <= y ? 1 : 0;
1830 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001831
Dave Allison20dfc792014-06-16 20:44:29 -07001832 DECLARE_INSTRUCTION(LessThanOrEqual);
1833
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001834 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001835 return kCondLE;
1836 }
1837
1838 private:
1839 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
1840};
1841
1842class HGreaterThan : public HCondition {
1843 public:
1844 HGreaterThan(HInstruction* first, HInstruction* second)
1845 : HCondition(first, second) {}
1846
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001847 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001848 return x > y ? 1 : 0;
1849 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001850 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001851 return x > y ? 1 : 0;
1852 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001853
Dave Allison20dfc792014-06-16 20:44:29 -07001854 DECLARE_INSTRUCTION(GreaterThan);
1855
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001856 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001857 return kCondGT;
1858 }
1859
1860 private:
1861 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
1862};
1863
1864class HGreaterThanOrEqual : public HCondition {
1865 public:
1866 HGreaterThanOrEqual(HInstruction* first, HInstruction* second)
1867 : HCondition(first, second) {}
1868
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001869 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001870 return x >= y ? 1 : 0;
1871 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001872 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001873 return x >= y ? 1 : 0;
1874 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001875
Dave Allison20dfc792014-06-16 20:44:29 -07001876 DECLARE_INSTRUCTION(GreaterThanOrEqual);
1877
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001878 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001879 return kCondGE;
1880 }
1881
1882 private:
1883 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
1884};
1885
1886
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001887// Instruction to check how two inputs compare to each other.
1888// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
1889class HCompare : public HBinaryOperation {
1890 public:
Calin Juravleddb7df22014-11-25 20:56:51 +00001891 // The bias applies for floating point operations and indicates how NaN
1892 // comparisons are treated:
1893 enum Bias {
1894 kNoBias, // bias is not applicable (i.e. for long operation)
1895 kGtBias, // return 1 for NaN comparisons
1896 kLtBias, // return -1 for NaN comparisons
1897 };
1898
1899 HCompare(Primitive::Type type, HInstruction* first, HInstruction* second, Bias bias)
1900 : HBinaryOperation(Primitive::kPrimInt, first, second), bias_(bias) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001901 DCHECK_EQ(type, first->GetType());
1902 DCHECK_EQ(type, second->GetType());
1903 }
1904
Calin Juravleddb7df22014-11-25 20:56:51 +00001905 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01001906 return
1907 x == y ? 0 :
1908 x > y ? 1 :
1909 -1;
1910 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001911
1912 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01001913 return
1914 x == y ? 0 :
1915 x > y ? 1 :
1916 -1;
1917 }
1918
Calin Juravleddb7df22014-11-25 20:56:51 +00001919 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
1920 return bias_ == other->AsCompare()->bias_;
1921 }
1922
1923 bool IsGtBias() { return bias_ == kGtBias; }
1924
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001925 DECLARE_INSTRUCTION(Compare);
1926
1927 private:
Calin Juravleddb7df22014-11-25 20:56:51 +00001928 const Bias bias_;
1929
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001930 DISALLOW_COPY_AND_ASSIGN(HCompare);
1931};
1932
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001933// A local in the graph. Corresponds to a Dex register.
1934class HLocal : public HTemplateInstruction<0> {
1935 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001936 explicit HLocal(uint16_t reg_number)
1937 : HTemplateInstruction(SideEffects::None()), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001938
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001939 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001940
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001941 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001942
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001943 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001944 // The Dex register number.
1945 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001946
1947 DISALLOW_COPY_AND_ASSIGN(HLocal);
1948};
1949
1950// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07001951class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001952 public:
Roland Levillain5799fc02014-09-25 12:15:20 +01001953 HLoadLocal(HLocal* local, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001954 : HExpression(type, SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001955 SetRawInputAt(0, local);
1956 }
1957
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001958 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1959
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001960 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001961
1962 private:
1963 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
1964};
1965
1966// Store a value in a given local. This instruction has two inputs: the value
1967// and the local.
1968class HStoreLocal : public HTemplateInstruction<2> {
1969 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001970 HStoreLocal(HLocal* local, HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001971 SetRawInputAt(0, local);
1972 SetRawInputAt(1, value);
1973 }
1974
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001975 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1976
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001977 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001978
1979 private:
1980 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
1981};
1982
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001983class HConstant : public HExpression<0> {
1984 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001985 explicit HConstant(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
1986
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001987 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001988
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001989 virtual bool IsMinusOne() const { return false; }
1990 virtual bool IsZero() const { return false; }
1991 virtual bool IsOne() const { return false; }
1992
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001993 DECLARE_INSTRUCTION(Constant);
1994
1995 private:
1996 DISALLOW_COPY_AND_ASSIGN(HConstant);
1997};
1998
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001999class HFloatConstant : public HConstant {
2000 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002001 float GetValue() const { return value_; }
2002
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002003 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002004 return bit_cast<uint32_t, float>(other->AsFloatConstant()->value_) ==
2005 bit_cast<uint32_t, float>(value_);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002006 }
2007
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002008 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002009
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002010 bool IsMinusOne() const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002011 return bit_cast<uint32_t, float>(AsFloatConstant()->GetValue()) ==
2012 bit_cast<uint32_t, float>((-1.0f));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002013 }
2014 bool IsZero() const OVERRIDE {
2015 return AsFloatConstant()->GetValue() == 0.0f;
2016 }
2017 bool IsOne() const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002018 return bit_cast<uint32_t, float>(AsFloatConstant()->GetValue()) ==
2019 bit_cast<uint32_t, float>(1.0f);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002020 }
2021
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002022 DECLARE_INSTRUCTION(FloatConstant);
2023
2024 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002025 explicit HFloatConstant(float value) : HConstant(Primitive::kPrimFloat), value_(value) {}
2026
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002027 const float value_;
2028
David Brazdil8d5b8b22015-03-24 10:51:52 +00002029 // Only the SsaBuilder can currently create floating-point constants. If we
2030 // ever need to create them later in the pipeline, we will have to handle them
2031 // the same way as integral constants.
2032 friend class SsaBuilder;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002033 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2034};
2035
2036class HDoubleConstant : public HConstant {
2037 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002038 double GetValue() const { return value_; }
2039
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002040 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002041 return bit_cast<uint64_t, double>(other->AsDoubleConstant()->value_) ==
2042 bit_cast<uint64_t, double>(value_);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002043 }
2044
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002045 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002046
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002047 bool IsMinusOne() const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002048 return bit_cast<uint64_t, double>(AsDoubleConstant()->GetValue()) ==
2049 bit_cast<uint64_t, double>((-1.0));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002050 }
2051 bool IsZero() const OVERRIDE {
2052 return AsDoubleConstant()->GetValue() == 0.0;
2053 }
2054 bool IsOne() const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002055 return bit_cast<uint64_t, double>(AsDoubleConstant()->GetValue()) ==
2056 bit_cast<uint64_t, double>(1.0);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002057 }
2058
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002059 DECLARE_INSTRUCTION(DoubleConstant);
2060
2061 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002062 explicit HDoubleConstant(double value) : HConstant(Primitive::kPrimDouble), value_(value) {}
2063
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002064 const double value_;
2065
David Brazdil8d5b8b22015-03-24 10:51:52 +00002066 // Only the SsaBuilder can currently create floating-point constants. If we
2067 // ever need to create them later in the pipeline, we will have to handle them
2068 // the same way as integral constants.
2069 friend class SsaBuilder;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002070 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
2071};
2072
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002073class HNullConstant : public HConstant {
2074 public:
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002075 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2076 return true;
2077 }
2078
2079 size_t ComputeHashCode() const OVERRIDE { return 0; }
2080
2081 DECLARE_INSTRUCTION(NullConstant);
2082
2083 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002084 HNullConstant() : HConstant(Primitive::kPrimNot) {}
2085
2086 friend class HGraph;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002087 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2088};
2089
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002090// Constants of the type int. Those can be from Dex instructions, or
2091// synthesized (for example with the if-eqz instruction).
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002092class HIntConstant : public HConstant {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002093 public:
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002094 int32_t GetValue() const { return value_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002095
Calin Juravle61d544b2015-02-23 16:46:57 +00002096 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002097 return other->AsIntConstant()->value_ == value_;
2098 }
2099
Calin Juravle61d544b2015-02-23 16:46:57 +00002100 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2101
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002102 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2103 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2104 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2105
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002106 DECLARE_INSTRUCTION(IntConstant);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002107
2108 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002109 explicit HIntConstant(int32_t value) : HConstant(Primitive::kPrimInt), value_(value) {}
2110
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002111 const int32_t value_;
2112
David Brazdil8d5b8b22015-03-24 10:51:52 +00002113 friend class HGraph;
2114 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
Zheng Xuad4450e2015-04-17 18:48:56 +08002115 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002116 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2117};
2118
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002119class HLongConstant : public HConstant {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002120 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002121 int64_t GetValue() const { return value_; }
2122
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002123 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002124 return other->AsLongConstant()->value_ == value_;
2125 }
2126
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002127 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002128
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002129 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2130 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2131 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2132
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002133 DECLARE_INSTRUCTION(LongConstant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002134
2135 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002136 explicit HLongConstant(int64_t value) : HConstant(Primitive::kPrimLong), value_(value) {}
2137
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002138 const int64_t value_;
2139
David Brazdil8d5b8b22015-03-24 10:51:52 +00002140 friend class HGraph;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002141 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2142};
2143
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002144enum class Intrinsics {
2145#define OPTIMIZING_INTRINSICS(Name, IsStatic) k ## Name,
2146#include "intrinsics_list.h"
2147 kNone,
2148 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
2149#undef INTRINSICS_LIST
2150#undef OPTIMIZING_INTRINSICS
2151};
2152std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
2153
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002154class HInvoke : public HInstruction {
2155 public:
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002156 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002157
2158 // Runtime needs to walk the stack, so Dex -> Dex calls need to
2159 // know their environment.
Calin Juravle77520bc2015-01-12 18:45:46 +00002160 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002161
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01002162 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002163 SetRawInputAt(index, argument);
2164 }
2165
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002166 Primitive::Type GetType() const OVERRIDE { return return_type_; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002167
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002168 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002169
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002170 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
2171
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01002172 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002173 return intrinsic_;
2174 }
2175
2176 void SetIntrinsic(Intrinsics intrinsic) {
2177 intrinsic_ = intrinsic;
2178 }
2179
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002180 DECLARE_INSTRUCTION(Invoke);
2181
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002182 protected:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002183 HInvoke(ArenaAllocator* arena,
2184 uint32_t number_of_arguments,
2185 Primitive::Type return_type,
2186 uint32_t dex_pc,
2187 uint32_t dex_method_index)
2188 : HInstruction(SideEffects::All()),
2189 inputs_(arena, number_of_arguments),
2190 return_type_(return_type),
2191 dex_pc_(dex_pc),
2192 dex_method_index_(dex_method_index),
2193 intrinsic_(Intrinsics::kNone) {
2194 inputs_.SetSize(number_of_arguments);
2195 }
2196
David Brazdil1abb4192015-02-17 18:33:36 +00002197 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
2198 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
2199 inputs_.Put(index, input);
2200 }
2201
2202 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002203 const Primitive::Type return_type_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002204 const uint32_t dex_pc_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002205 const uint32_t dex_method_index_;
2206 Intrinsics intrinsic_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002207
2208 private:
2209 DISALLOW_COPY_AND_ASSIGN(HInvoke);
2210};
2211
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002212class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002213 public:
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002214 HInvokeStaticOrDirect(ArenaAllocator* arena,
2215 uint32_t number_of_arguments,
2216 Primitive::Type return_type,
2217 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002218 uint32_t dex_method_index,
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002219 bool is_recursive,
Nicolas Geoffray79041292015-03-26 10:05:54 +00002220 InvokeType original_invoke_type,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002221 InvokeType invoke_type)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002222 : HInvoke(arena, number_of_arguments, return_type, dex_pc, dex_method_index),
Nicolas Geoffray79041292015-03-26 10:05:54 +00002223 original_invoke_type_(original_invoke_type),
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002224 invoke_type_(invoke_type),
2225 is_recursive_(is_recursive) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002226
Calin Juravle641547a2015-04-21 22:08:51 +01002227 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
2228 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00002229 // We access the method via the dex cache so we can't do an implicit null check.
2230 // TODO: for intrinsics we can generate implicit null checks.
2231 return false;
2232 }
2233
Nicolas Geoffray79041292015-03-26 10:05:54 +00002234 InvokeType GetOriginalInvokeType() const { return original_invoke_type_; }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002235 InvokeType GetInvokeType() const { return invoke_type_; }
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002236 bool IsRecursive() const { return is_recursive_; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002237 bool NeedsDexCache() const OVERRIDE { return !IsRecursive(); }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002238
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002239 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002240
2241 private:
Nicolas Geoffray79041292015-03-26 10:05:54 +00002242 const InvokeType original_invoke_type_;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002243 const InvokeType invoke_type_;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002244 const bool is_recursive_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002245
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002246 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002247};
2248
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002249class HInvokeVirtual : public HInvoke {
2250 public:
2251 HInvokeVirtual(ArenaAllocator* arena,
2252 uint32_t number_of_arguments,
2253 Primitive::Type return_type,
2254 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002255 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002256 uint32_t vtable_index)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002257 : HInvoke(arena, number_of_arguments, return_type, dex_pc, dex_method_index),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002258 vtable_index_(vtable_index) {}
2259
Calin Juravle641547a2015-04-21 22:08:51 +01002260 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00002261 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01002262 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00002263 }
2264
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002265 uint32_t GetVTableIndex() const { return vtable_index_; }
2266
2267 DECLARE_INSTRUCTION(InvokeVirtual);
2268
2269 private:
2270 const uint32_t vtable_index_;
2271
2272 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
2273};
2274
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002275class HInvokeInterface : public HInvoke {
2276 public:
2277 HInvokeInterface(ArenaAllocator* arena,
2278 uint32_t number_of_arguments,
2279 Primitive::Type return_type,
2280 uint32_t dex_pc,
2281 uint32_t dex_method_index,
2282 uint32_t imt_index)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002283 : HInvoke(arena, number_of_arguments, return_type, dex_pc, dex_method_index),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002284 imt_index_(imt_index) {}
2285
Calin Juravle641547a2015-04-21 22:08:51 +01002286 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00002287 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01002288 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00002289 }
2290
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002291 uint32_t GetImtIndex() const { return imt_index_; }
2292 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
2293
2294 DECLARE_INSTRUCTION(InvokeInterface);
2295
2296 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002297 const uint32_t imt_index_;
2298
2299 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
2300};
2301
Dave Allison20dfc792014-06-16 20:44:29 -07002302class HNewInstance : public HExpression<0> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002303 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002304 HNewInstance(uint32_t dex_pc, uint16_t type_index, QuickEntrypointEnum entrypoint)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002305 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2306 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002307 type_index_(type_index),
2308 entrypoint_(entrypoint) {}
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002309
2310 uint32_t GetDexPc() const { return dex_pc_; }
2311 uint16_t GetTypeIndex() const { return type_index_; }
2312
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002313 // Calls runtime so needs an environment.
Calin Juravle92a6ed22014-12-02 18:58:03 +00002314 bool NeedsEnvironment() const OVERRIDE { return true; }
2315 // It may throw when called on:
2316 // - interfaces
2317 // - abstract/innaccessible/unknown classes
2318 // TODO: optimize when possible.
2319 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002320
Calin Juravle10e244f2015-01-26 18:54:32 +00002321 bool CanBeNull() const OVERRIDE { return false; }
2322
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002323 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
2324
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002325 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002326
2327 private:
2328 const uint32_t dex_pc_;
2329 const uint16_t type_index_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002330 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002331
2332 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
2333};
2334
Roland Levillain88cb1752014-10-20 16:36:47 +01002335class HNeg : public HUnaryOperation {
2336 public:
2337 explicit HNeg(Primitive::Type result_type, HInstruction* input)
2338 : HUnaryOperation(result_type, input) {}
2339
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002340 int32_t Evaluate(int32_t x) const OVERRIDE { return -x; }
2341 int64_t Evaluate(int64_t x) const OVERRIDE { return -x; }
Roland Levillain9240d6a2014-10-20 16:47:04 +01002342
Roland Levillain88cb1752014-10-20 16:36:47 +01002343 DECLARE_INSTRUCTION(Neg);
2344
2345 private:
2346 DISALLOW_COPY_AND_ASSIGN(HNeg);
2347};
2348
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002349class HNewArray : public HExpression<1> {
2350 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002351 HNewArray(HInstruction* length,
2352 uint32_t dex_pc,
2353 uint16_t type_index,
2354 QuickEntrypointEnum entrypoint)
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002355 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2356 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002357 type_index_(type_index),
2358 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002359 SetRawInputAt(0, length);
2360 }
2361
2362 uint32_t GetDexPc() const { return dex_pc_; }
2363 uint16_t GetTypeIndex() const { return type_index_; }
2364
2365 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00002366 bool NeedsEnvironment() const OVERRIDE { return true; }
2367
Mingyao Yang0c365e62015-03-31 15:09:29 -07002368 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
2369 bool CanThrow() const OVERRIDE { return true; }
2370
Calin Juravle10e244f2015-01-26 18:54:32 +00002371 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002372
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002373 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
2374
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002375 DECLARE_INSTRUCTION(NewArray);
2376
2377 private:
2378 const uint32_t dex_pc_;
2379 const uint16_t type_index_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002380 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002381
2382 DISALLOW_COPY_AND_ASSIGN(HNewArray);
2383};
2384
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002385class HAdd : public HBinaryOperation {
2386 public:
2387 HAdd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2388 : HBinaryOperation(result_type, left, right) {}
2389
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002390 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002391
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002392 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002393 return x + y;
2394 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002395 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002396 return x + y;
2397 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002398
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002399 DECLARE_INSTRUCTION(Add);
2400
2401 private:
2402 DISALLOW_COPY_AND_ASSIGN(HAdd);
2403};
2404
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002405class HSub : public HBinaryOperation {
2406 public:
2407 HSub(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2408 : HBinaryOperation(result_type, left, right) {}
2409
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002410 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002411 return x - y;
2412 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002413 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002414 return x - y;
2415 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002416
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002417 DECLARE_INSTRUCTION(Sub);
2418
2419 private:
2420 DISALLOW_COPY_AND_ASSIGN(HSub);
2421};
2422
Calin Juravle34bacdf2014-10-07 20:23:36 +01002423class HMul : public HBinaryOperation {
2424 public:
2425 HMul(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2426 : HBinaryOperation(result_type, left, right) {}
2427
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002428 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002429
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002430 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x * y; }
2431 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x * y; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002432
2433 DECLARE_INSTRUCTION(Mul);
2434
2435 private:
2436 DISALLOW_COPY_AND_ASSIGN(HMul);
2437};
2438
Calin Juravle7c4954d2014-10-28 16:57:40 +00002439class HDiv : public HBinaryOperation {
2440 public:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002441 HDiv(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
2442 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00002443
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002444 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00002445 // Our graph structure ensures we never have 0 for `y` during constant folding.
2446 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00002447 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00002448 return (y == -1) ? -x : x / y;
2449 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002450
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002451 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002452 DCHECK_NE(y, 0);
2453 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2454 return (y == -1) ? -x : x / y;
2455 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002456
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002457 uint32_t GetDexPc() const { return dex_pc_; }
2458
Calin Juravle7c4954d2014-10-28 16:57:40 +00002459 DECLARE_INSTRUCTION(Div);
2460
2461 private:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002462 const uint32_t dex_pc_;
2463
Calin Juravle7c4954d2014-10-28 16:57:40 +00002464 DISALLOW_COPY_AND_ASSIGN(HDiv);
2465};
2466
Calin Juravlebacfec32014-11-14 15:54:36 +00002467class HRem : public HBinaryOperation {
2468 public:
2469 HRem(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
2470 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
2471
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002472 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002473 DCHECK_NE(y, 0);
2474 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2475 return (y == -1) ? 0 : x % y;
2476 }
2477
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002478 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002479 DCHECK_NE(y, 0);
2480 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2481 return (y == -1) ? 0 : x % y;
2482 }
2483
2484 uint32_t GetDexPc() const { return dex_pc_; }
2485
2486 DECLARE_INSTRUCTION(Rem);
2487
2488 private:
2489 const uint32_t dex_pc_;
2490
2491 DISALLOW_COPY_AND_ASSIGN(HRem);
2492};
2493
Calin Juravled0d48522014-11-04 16:40:20 +00002494class HDivZeroCheck : public HExpression<1> {
2495 public:
2496 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
2497 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
2498 SetRawInputAt(0, value);
2499 }
2500
2501 bool CanBeMoved() const OVERRIDE { return true; }
2502
2503 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2504 UNUSED(other);
2505 return true;
2506 }
2507
2508 bool NeedsEnvironment() const OVERRIDE { return true; }
2509 bool CanThrow() const OVERRIDE { return true; }
2510
2511 uint32_t GetDexPc() const { return dex_pc_; }
2512
2513 DECLARE_INSTRUCTION(DivZeroCheck);
2514
2515 private:
2516 const uint32_t dex_pc_;
2517
2518 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
2519};
2520
Calin Juravle9aec02f2014-11-18 23:06:35 +00002521class HShl : public HBinaryOperation {
2522 public:
2523 HShl(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2524 : HBinaryOperation(result_type, left, right) {}
2525
2526 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x << (y & kMaxIntShiftValue); }
2527 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x << (y & kMaxLongShiftValue); }
2528
2529 DECLARE_INSTRUCTION(Shl);
2530
2531 private:
2532 DISALLOW_COPY_AND_ASSIGN(HShl);
2533};
2534
2535class HShr : public HBinaryOperation {
2536 public:
2537 HShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2538 : HBinaryOperation(result_type, left, right) {}
2539
2540 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x >> (y & kMaxIntShiftValue); }
2541 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x >> (y & kMaxLongShiftValue); }
2542
2543 DECLARE_INSTRUCTION(Shr);
2544
2545 private:
2546 DISALLOW_COPY_AND_ASSIGN(HShr);
2547};
2548
2549class HUShr : public HBinaryOperation {
2550 public:
2551 HUShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2552 : HBinaryOperation(result_type, left, right) {}
2553
2554 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
2555 uint32_t ux = static_cast<uint32_t>(x);
2556 uint32_t uy = static_cast<uint32_t>(y) & kMaxIntShiftValue;
2557 return static_cast<int32_t>(ux >> uy);
2558 }
2559
2560 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
2561 uint64_t ux = static_cast<uint64_t>(x);
2562 uint64_t uy = static_cast<uint64_t>(y) & kMaxLongShiftValue;
2563 return static_cast<int64_t>(ux >> uy);
2564 }
2565
2566 DECLARE_INSTRUCTION(UShr);
2567
2568 private:
2569 DISALLOW_COPY_AND_ASSIGN(HUShr);
2570};
2571
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002572class HAnd : public HBinaryOperation {
2573 public:
2574 HAnd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2575 : HBinaryOperation(result_type, left, right) {}
2576
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002577 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002578
2579 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x & y; }
2580 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x & y; }
2581
2582 DECLARE_INSTRUCTION(And);
2583
2584 private:
2585 DISALLOW_COPY_AND_ASSIGN(HAnd);
2586};
2587
2588class HOr : public HBinaryOperation {
2589 public:
2590 HOr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2591 : HBinaryOperation(result_type, left, right) {}
2592
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002593 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002594
2595 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x | y; }
2596 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x | y; }
2597
2598 DECLARE_INSTRUCTION(Or);
2599
2600 private:
2601 DISALLOW_COPY_AND_ASSIGN(HOr);
2602};
2603
2604class HXor : public HBinaryOperation {
2605 public:
2606 HXor(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2607 : HBinaryOperation(result_type, left, right) {}
2608
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002609 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002610
2611 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x ^ y; }
2612 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x ^ y; }
2613
2614 DECLARE_INSTRUCTION(Xor);
2615
2616 private:
2617 DISALLOW_COPY_AND_ASSIGN(HXor);
2618};
2619
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002620// The value of a parameter in this method. Its location depends on
2621// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07002622class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002623 public:
Calin Juravle10e244f2015-01-26 18:54:32 +00002624 HParameterValue(uint8_t index, Primitive::Type parameter_type, bool is_this = false)
2625 : HExpression(parameter_type, SideEffects::None()), index_(index), is_this_(is_this) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002626
2627 uint8_t GetIndex() const { return index_; }
2628
Calin Juravle10e244f2015-01-26 18:54:32 +00002629 bool CanBeNull() const OVERRIDE { return !is_this_; }
2630
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002631 DECLARE_INSTRUCTION(ParameterValue);
2632
2633 private:
2634 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00002635 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002636 const uint8_t index_;
2637
Calin Juravle10e244f2015-01-26 18:54:32 +00002638 // Whether or not the parameter value corresponds to 'this' argument.
2639 const bool is_this_;
2640
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002641 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
2642};
2643
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002644class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002645 public:
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002646 explicit HNot(Primitive::Type result_type, HInstruction* input)
2647 : HUnaryOperation(result_type, input) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002648
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002649 bool CanBeMoved() const OVERRIDE { return true; }
2650 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002651 UNUSED(other);
2652 return true;
2653 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002654
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002655 int32_t Evaluate(int32_t x) const OVERRIDE { return ~x; }
2656 int64_t Evaluate(int64_t x) const OVERRIDE { return ~x; }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002657
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002658 DECLARE_INSTRUCTION(Not);
2659
2660 private:
2661 DISALLOW_COPY_AND_ASSIGN(HNot);
2662};
2663
David Brazdil66d126e2015-04-03 16:02:44 +01002664class HBooleanNot : public HUnaryOperation {
2665 public:
2666 explicit HBooleanNot(HInstruction* input)
2667 : HUnaryOperation(Primitive::Type::kPrimBoolean, input) {}
2668
2669 bool CanBeMoved() const OVERRIDE { return true; }
2670 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2671 UNUSED(other);
2672 return true;
2673 }
2674
2675 int32_t Evaluate(int32_t x) const OVERRIDE {
2676 DCHECK(IsUint<1>(x));
2677 return !x;
2678 }
2679
2680 int64_t Evaluate(int64_t x ATTRIBUTE_UNUSED) const OVERRIDE {
2681 LOG(FATAL) << DebugName() << " cannot be used with 64-bit values";
2682 UNREACHABLE();
2683 }
2684
2685 DECLARE_INSTRUCTION(BooleanNot);
2686
2687 private:
2688 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
2689};
2690
Roland Levillaindff1f282014-11-05 14:15:05 +00002691class HTypeConversion : public HExpression<1> {
2692 public:
2693 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00002694 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
2695 : HExpression(result_type, SideEffects::None()), dex_pc_(dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002696 SetRawInputAt(0, input);
2697 DCHECK_NE(input->GetType(), result_type);
2698 }
2699
2700 HInstruction* GetInput() const { return InputAt(0); }
2701 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
2702 Primitive::Type GetResultType() const { return GetType(); }
2703
Roland Levillain624279f2014-12-04 11:54:28 +00002704 // Required by the x86 and ARM code generators when producing calls
2705 // to the runtime.
2706 uint32_t GetDexPc() const { return dex_pc_; }
2707
Roland Levillaindff1f282014-11-05 14:15:05 +00002708 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00002709 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00002710
2711 DECLARE_INSTRUCTION(TypeConversion);
2712
2713 private:
Roland Levillain624279f2014-12-04 11:54:28 +00002714 const uint32_t dex_pc_;
2715
Roland Levillaindff1f282014-11-05 14:15:05 +00002716 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
2717};
2718
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00002719static constexpr uint32_t kNoRegNumber = -1;
2720
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002721class HPhi : public HInstruction {
2722 public:
2723 HPhi(ArenaAllocator* arena, uint32_t reg_number, size_t number_of_inputs, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002724 : HInstruction(SideEffects::None()),
2725 inputs_(arena, number_of_inputs),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002726 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002727 type_(type),
Calin Juravle10e244f2015-01-26 18:54:32 +00002728 is_live_(false),
2729 can_be_null_(true) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002730 inputs_.SetSize(number_of_inputs);
2731 }
2732
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00002733 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
2734 static Primitive::Type ToPhiType(Primitive::Type type) {
2735 switch (type) {
2736 case Primitive::kPrimBoolean:
2737 case Primitive::kPrimByte:
2738 case Primitive::kPrimShort:
2739 case Primitive::kPrimChar:
2740 return Primitive::kPrimInt;
2741 default:
2742 return type;
2743 }
2744 }
2745
Calin Juravle10e244f2015-01-26 18:54:32 +00002746 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002747
2748 void AddInput(HInstruction* input);
2749
Calin Juravle10e244f2015-01-26 18:54:32 +00002750 Primitive::Type GetType() const OVERRIDE { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002751 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002752
Calin Juravle10e244f2015-01-26 18:54:32 +00002753 bool CanBeNull() const OVERRIDE { return can_be_null_; }
2754 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
2755
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002756 uint32_t GetRegNumber() const { return reg_number_; }
2757
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002758 void SetDead() { is_live_ = false; }
2759 void SetLive() { is_live_ = true; }
2760 bool IsDead() const { return !is_live_; }
2761 bool IsLive() const { return is_live_; }
2762
Calin Juravlea4f88312015-04-16 12:57:19 +01002763 // Returns the next equivalent phi (starting from the current one) or null if there is none.
2764 // An equivalent phi is a phi having the same dex register and type.
2765 // It assumes that phis with the same dex register are adjacent.
2766 HPhi* GetNextEquivalentPhiWithSameType() {
2767 HInstruction* next = GetNext();
2768 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
2769 if (next->GetType() == GetType()) {
2770 return next->AsPhi();
2771 }
2772 next = next->GetNext();
2773 }
2774 return nullptr;
2775 }
2776
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002777 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002778
David Brazdil1abb4192015-02-17 18:33:36 +00002779 protected:
2780 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
2781
2782 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
2783 inputs_.Put(index, input);
2784 }
2785
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002786 private:
David Brazdil1abb4192015-02-17 18:33:36 +00002787 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002788 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002789 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002790 bool is_live_;
Calin Juravle10e244f2015-01-26 18:54:32 +00002791 bool can_be_null_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002792
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002793 DISALLOW_COPY_AND_ASSIGN(HPhi);
2794};
2795
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002796class HNullCheck : public HExpression<1> {
2797 public:
2798 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002799 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002800 SetRawInputAt(0, value);
2801 }
2802
Calin Juravle10e244f2015-01-26 18:54:32 +00002803 bool CanBeMoved() const OVERRIDE { return true; }
2804 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002805 UNUSED(other);
2806 return true;
2807 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002808
Calin Juravle10e244f2015-01-26 18:54:32 +00002809 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002810
Calin Juravle10e244f2015-01-26 18:54:32 +00002811 bool CanThrow() const OVERRIDE { return true; }
2812
2813 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01002814
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002815 uint32_t GetDexPc() const { return dex_pc_; }
2816
2817 DECLARE_INSTRUCTION(NullCheck);
2818
2819 private:
2820 const uint32_t dex_pc_;
2821
2822 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
2823};
2824
2825class FieldInfo : public ValueObject {
2826 public:
Calin Juravle52c48962014-12-16 17:02:57 +00002827 FieldInfo(MemberOffset field_offset, Primitive::Type field_type, bool is_volatile)
2828 : field_offset_(field_offset), field_type_(field_type), is_volatile_(is_volatile) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002829
2830 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002831 Primitive::Type GetFieldType() const { return field_type_; }
Calin Juravle52c48962014-12-16 17:02:57 +00002832 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002833
2834 private:
2835 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01002836 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00002837 const bool is_volatile_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002838};
2839
2840class HInstanceFieldGet : public HExpression<1> {
2841 public:
2842 HInstanceFieldGet(HInstruction* value,
2843 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00002844 MemberOffset field_offset,
2845 bool is_volatile)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002846 : HExpression(field_type, SideEffects::DependsOnSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00002847 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002848 SetRawInputAt(0, value);
2849 }
2850
Calin Juravle10c9cbe2014-12-19 10:50:19 +00002851 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002852
2853 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2854 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
2855 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002856 }
2857
Calin Juravle641547a2015-04-21 22:08:51 +01002858 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
2859 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00002860 }
2861
2862 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002863 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
2864 }
2865
Calin Juravle52c48962014-12-16 17:02:57 +00002866 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002867 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002868 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002869 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002870
2871 DECLARE_INSTRUCTION(InstanceFieldGet);
2872
2873 private:
2874 const FieldInfo field_info_;
2875
2876 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
2877};
2878
2879class HInstanceFieldSet : public HTemplateInstruction<2> {
2880 public:
2881 HInstanceFieldSet(HInstruction* object,
2882 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01002883 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00002884 MemberOffset field_offset,
2885 bool is_volatile)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002886 : HTemplateInstruction(SideEffects::ChangesSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00002887 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002888 SetRawInputAt(0, object);
2889 SetRawInputAt(1, value);
2890 }
2891
Calin Juravle641547a2015-04-21 22:08:51 +01002892 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
2893 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00002894 }
2895
Calin Juravle52c48962014-12-16 17:02:57 +00002896 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002897 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002898 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002899 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002900 HInstruction* GetValue() const { return InputAt(1); }
2901
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002902 DECLARE_INSTRUCTION(InstanceFieldSet);
2903
2904 private:
2905 const FieldInfo field_info_;
2906
2907 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
2908};
2909
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002910class HArrayGet : public HExpression<2> {
2911 public:
2912 HArrayGet(HInstruction* array, HInstruction* index, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002913 : HExpression(type, SideEffects::DependsOnSomething()) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002914 SetRawInputAt(0, array);
2915 SetRawInputAt(1, index);
2916 }
2917
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002918 bool CanBeMoved() const OVERRIDE { return true; }
2919 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002920 UNUSED(other);
2921 return true;
2922 }
Calin Juravle641547a2015-04-21 22:08:51 +01002923 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
2924 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00002925 // TODO: We can be smarter here.
2926 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
2927 // which generates the implicit null check. There are cases when these can be removed
2928 // to produce better code. If we ever add optimizations to do so we should allow an
2929 // implicit check here (as long as the address falls in the first page).
2930 return false;
2931 }
2932
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002933 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002934
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002935 HInstruction* GetArray() const { return InputAt(0); }
2936 HInstruction* GetIndex() const { return InputAt(1); }
2937
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002938 DECLARE_INSTRUCTION(ArrayGet);
2939
2940 private:
2941 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
2942};
2943
2944class HArraySet : public HTemplateInstruction<3> {
2945 public:
2946 HArraySet(HInstruction* array,
2947 HInstruction* index,
2948 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002949 Primitive::Type expected_component_type,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002950 uint32_t dex_pc)
2951 : HTemplateInstruction(SideEffects::ChangesSomething()),
2952 dex_pc_(dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002953 expected_component_type_(expected_component_type),
2954 needs_type_check_(value->GetType() == Primitive::kPrimNot) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002955 SetRawInputAt(0, array);
2956 SetRawInputAt(1, index);
2957 SetRawInputAt(2, value);
2958 }
2959
Calin Juravle77520bc2015-01-12 18:45:46 +00002960 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002961 // We currently always call a runtime method to catch array store
2962 // exceptions.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002963 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002964 }
2965
Calin Juravle641547a2015-04-21 22:08:51 +01002966 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
2967 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00002968 // TODO: Same as for ArrayGet.
2969 return false;
2970 }
2971
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002972 void ClearNeedsTypeCheck() {
2973 needs_type_check_ = false;
2974 }
2975
2976 bool NeedsTypeCheck() const { return needs_type_check_; }
2977
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002978 uint32_t GetDexPc() const { return dex_pc_; }
2979
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002980 HInstruction* GetArray() const { return InputAt(0); }
2981 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002982 HInstruction* GetValue() const { return InputAt(2); }
2983
2984 Primitive::Type GetComponentType() const {
2985 // The Dex format does not type floating point index operations. Since the
2986 // `expected_component_type_` is set during building and can therefore not
2987 // be correct, we also check what is the value type. If it is a floating
2988 // point type, we must use that type.
2989 Primitive::Type value_type = GetValue()->GetType();
2990 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
2991 ? value_type
2992 : expected_component_type_;
2993 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002994
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002995 DECLARE_INSTRUCTION(ArraySet);
2996
2997 private:
2998 const uint32_t dex_pc_;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002999 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003000 bool needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003001
3002 DISALLOW_COPY_AND_ASSIGN(HArraySet);
3003};
3004
3005class HArrayLength : public HExpression<1> {
3006 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003007 explicit HArrayLength(HInstruction* array)
3008 : HExpression(Primitive::kPrimInt, SideEffects::None()) {
3009 // Note that arrays do not change length, so the instruction does not
3010 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003011 SetRawInputAt(0, array);
3012 }
3013
Calin Juravle77520bc2015-01-12 18:45:46 +00003014 bool CanBeMoved() const OVERRIDE { return true; }
3015 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003016 UNUSED(other);
3017 return true;
3018 }
Calin Juravle641547a2015-04-21 22:08:51 +01003019 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3020 return obj == InputAt(0);
3021 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003022
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003023 DECLARE_INSTRUCTION(ArrayLength);
3024
3025 private:
3026 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
3027};
3028
3029class HBoundsCheck : public HExpression<2> {
3030 public:
3031 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003032 : HExpression(index->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003033 DCHECK(index->GetType() == Primitive::kPrimInt);
3034 SetRawInputAt(0, index);
3035 SetRawInputAt(1, length);
3036 }
3037
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003038 bool CanBeMoved() const OVERRIDE { return true; }
3039 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003040 UNUSED(other);
3041 return true;
3042 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003043
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003044 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003045
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003046 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01003047
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003048 uint32_t GetDexPc() const { return dex_pc_; }
3049
3050 DECLARE_INSTRUCTION(BoundsCheck);
3051
3052 private:
3053 const uint32_t dex_pc_;
3054
3055 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
3056};
3057
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003058/**
3059 * Some DEX instructions are folded into multiple HInstructions that need
3060 * to stay live until the last HInstruction. This class
3061 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00003062 * HInstruction stays live. `index` represents the stack location index of the
3063 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003064 */
3065class HTemporary : public HTemplateInstruction<0> {
3066 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003067 explicit HTemporary(size_t index) : HTemplateInstruction(SideEffects::None()), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003068
3069 size_t GetIndex() const { return index_; }
3070
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00003071 Primitive::Type GetType() const OVERRIDE {
3072 // The previous instruction is the one that will be stored in the temporary location.
3073 DCHECK(GetPrevious() != nullptr);
3074 return GetPrevious()->GetType();
3075 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00003076
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003077 DECLARE_INSTRUCTION(Temporary);
3078
3079 private:
3080 const size_t index_;
3081
3082 DISALLOW_COPY_AND_ASSIGN(HTemporary);
3083};
3084
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003085class HSuspendCheck : public HTemplateInstruction<0> {
3086 public:
3087 explicit HSuspendCheck(uint32_t dex_pc)
Nicolas Geoffray9ebc72c2014-09-25 16:33:42 +01003088 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003089
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003090 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003091 return true;
3092 }
3093
3094 uint32_t GetDexPc() const { return dex_pc_; }
3095
3096 DECLARE_INSTRUCTION(SuspendCheck);
3097
3098 private:
3099 const uint32_t dex_pc_;
3100
3101 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
3102};
3103
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003104/**
3105 * Instruction to load a Class object.
3106 */
3107class HLoadClass : public HExpression<0> {
3108 public:
3109 HLoadClass(uint16_t type_index,
3110 bool is_referrers_class,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003111 uint32_t dex_pc)
3112 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3113 type_index_(type_index),
3114 is_referrers_class_(is_referrers_class),
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003115 dex_pc_(dex_pc),
Calin Juravleb1498f62015-02-16 13:13:29 +00003116 generate_clinit_check_(false),
3117 loaded_class_rti_(ReferenceTypeInfo::CreateTop(/* is_exact */ false)) {}
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003118
3119 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003120
3121 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3122 return other->AsLoadClass()->type_index_ == type_index_;
3123 }
3124
3125 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
3126
3127 uint32_t GetDexPc() const { return dex_pc_; }
3128 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003129 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003130
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003131 bool NeedsEnvironment() const OVERRIDE {
3132 // Will call runtime and load the class if the class is not loaded yet.
3133 // TODO: finer grain decision.
3134 return !is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003135 }
3136
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003137 bool MustGenerateClinitCheck() const {
3138 return generate_clinit_check_;
3139 }
3140
3141 void SetMustGenerateClinitCheck() {
3142 generate_clinit_check_ = true;
3143 }
3144
3145 bool CanCallRuntime() const {
3146 return MustGenerateClinitCheck() || !is_referrers_class_;
3147 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003148
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003149 bool CanThrow() const OVERRIDE {
3150 // May call runtime and and therefore can throw.
3151 // TODO: finer grain decision.
3152 return !is_referrers_class_;
3153 }
3154
Calin Juravleacf735c2015-02-12 15:25:22 +00003155 ReferenceTypeInfo GetLoadedClassRTI() {
3156 return loaded_class_rti_;
3157 }
3158
3159 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
3160 // Make sure we only set exact types (the loaded class should never be merged).
3161 DCHECK(rti.IsExact());
3162 loaded_class_rti_ = rti;
3163 }
3164
3165 bool IsResolved() {
3166 return loaded_class_rti_.IsExact();
3167 }
3168
Nicolas Geoffray9437b782015-03-25 10:08:51 +00003169 bool NeedsDexCache() const OVERRIDE { return !is_referrers_class_; }
3170
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003171 DECLARE_INSTRUCTION(LoadClass);
3172
3173 private:
3174 const uint16_t type_index_;
3175 const bool is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003176 const uint32_t dex_pc_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003177 // Whether this instruction must generate the initialization check.
3178 // Used for code generation.
3179 bool generate_clinit_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003180
Calin Juravleacf735c2015-02-12 15:25:22 +00003181 ReferenceTypeInfo loaded_class_rti_;
3182
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003183 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
3184};
3185
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003186class HLoadString : public HExpression<0> {
3187 public:
3188 HLoadString(uint32_t string_index, uint32_t dex_pc)
3189 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3190 string_index_(string_index),
3191 dex_pc_(dex_pc) {}
3192
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003193 bool CanBeMoved() const OVERRIDE { return true; }
3194
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003195 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3196 return other->AsLoadString()->string_index_ == string_index_;
3197 }
3198
3199 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
3200
3201 uint32_t GetDexPc() const { return dex_pc_; }
3202 uint32_t GetStringIndex() const { return string_index_; }
3203
3204 // TODO: Can we deopt or debug when we resolve a string?
3205 bool NeedsEnvironment() const OVERRIDE { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00003206 bool NeedsDexCache() const OVERRIDE { return true; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003207
3208 DECLARE_INSTRUCTION(LoadString);
3209
3210 private:
3211 const uint32_t string_index_;
3212 const uint32_t dex_pc_;
3213
3214 DISALLOW_COPY_AND_ASSIGN(HLoadString);
3215};
3216
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003217// TODO: Pass this check to HInvokeStaticOrDirect nodes.
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003218/**
3219 * Performs an initialization check on its Class object input.
3220 */
3221class HClinitCheck : public HExpression<1> {
3222 public:
3223 explicit HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Nicolas Geoffraya0466e12015-03-27 15:00:40 +00003224 : HExpression(Primitive::kPrimNot, SideEffects::ChangesSomething()),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003225 dex_pc_(dex_pc) {
3226 SetRawInputAt(0, constant);
3227 }
3228
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003229 bool CanBeMoved() const OVERRIDE { return true; }
3230 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3231 UNUSED(other);
3232 return true;
3233 }
3234
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003235 bool NeedsEnvironment() const OVERRIDE {
3236 // May call runtime to initialize the class.
3237 return true;
3238 }
3239
3240 uint32_t GetDexPc() const { return dex_pc_; }
3241
3242 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
3243
3244 DECLARE_INSTRUCTION(ClinitCheck);
3245
3246 private:
3247 const uint32_t dex_pc_;
3248
3249 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
3250};
3251
3252class HStaticFieldGet : public HExpression<1> {
3253 public:
3254 HStaticFieldGet(HInstruction* cls,
3255 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003256 MemberOffset field_offset,
3257 bool is_volatile)
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003258 : HExpression(field_type, SideEffects::DependsOnSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00003259 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003260 SetRawInputAt(0, cls);
3261 }
3262
Calin Juravle52c48962014-12-16 17:02:57 +00003263
Calin Juravle10c9cbe2014-12-19 10:50:19 +00003264 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003265
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003266 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00003267 HStaticFieldGet* other_get = other->AsStaticFieldGet();
3268 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003269 }
3270
3271 size_t ComputeHashCode() const OVERRIDE {
3272 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
3273 }
3274
Calin Juravle52c48962014-12-16 17:02:57 +00003275 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003276 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
3277 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003278 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003279
3280 DECLARE_INSTRUCTION(StaticFieldGet);
3281
3282 private:
3283 const FieldInfo field_info_;
3284
3285 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
3286};
3287
3288class HStaticFieldSet : public HTemplateInstruction<2> {
3289 public:
3290 HStaticFieldSet(HInstruction* cls,
3291 HInstruction* value,
3292 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003293 MemberOffset field_offset,
3294 bool is_volatile)
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003295 : HTemplateInstruction(SideEffects::ChangesSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00003296 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003297 SetRawInputAt(0, cls);
3298 SetRawInputAt(1, value);
3299 }
3300
Calin Juravle52c48962014-12-16 17:02:57 +00003301 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003302 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
3303 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003304 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003305
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003306 HInstruction* GetValue() const { return InputAt(1); }
3307
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003308 DECLARE_INSTRUCTION(StaticFieldSet);
3309
3310 private:
3311 const FieldInfo field_info_;
3312
3313 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
3314};
3315
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003316// Implement the move-exception DEX instruction.
3317class HLoadException : public HExpression<0> {
3318 public:
3319 HLoadException() : HExpression(Primitive::kPrimNot, SideEffects::None()) {}
3320
3321 DECLARE_INSTRUCTION(LoadException);
3322
3323 private:
3324 DISALLOW_COPY_AND_ASSIGN(HLoadException);
3325};
3326
3327class HThrow : public HTemplateInstruction<1> {
3328 public:
3329 HThrow(HInstruction* exception, uint32_t dex_pc)
3330 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {
3331 SetRawInputAt(0, exception);
3332 }
3333
3334 bool IsControlFlow() const OVERRIDE { return true; }
3335
3336 bool NeedsEnvironment() const OVERRIDE { return true; }
3337
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003338 bool CanThrow() const OVERRIDE { return true; }
3339
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003340 uint32_t GetDexPc() const { return dex_pc_; }
3341
3342 DECLARE_INSTRUCTION(Throw);
3343
3344 private:
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003345 const uint32_t dex_pc_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003346
3347 DISALLOW_COPY_AND_ASSIGN(HThrow);
3348};
3349
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003350class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003351 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003352 HInstanceOf(HInstruction* object,
3353 HLoadClass* constant,
3354 bool class_is_final,
3355 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003356 : HExpression(Primitive::kPrimBoolean, SideEffects::None()),
3357 class_is_final_(class_is_final),
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003358 must_do_null_check_(true),
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003359 dex_pc_(dex_pc) {
3360 SetRawInputAt(0, object);
3361 SetRawInputAt(1, constant);
3362 }
3363
3364 bool CanBeMoved() const OVERRIDE { return true; }
3365
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003366 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003367 return true;
3368 }
3369
3370 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003371 return false;
3372 }
3373
3374 uint32_t GetDexPc() const { return dex_pc_; }
3375
3376 bool IsClassFinal() const { return class_is_final_; }
3377
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003378 // Used only in code generation.
3379 bool MustDoNullCheck() const { return must_do_null_check_; }
3380 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
3381
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003382 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003383
3384 private:
3385 const bool class_is_final_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003386 bool must_do_null_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003387 const uint32_t dex_pc_;
3388
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003389 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
3390};
3391
Calin Juravleb1498f62015-02-16 13:13:29 +00003392class HBoundType : public HExpression<1> {
3393 public:
3394 HBoundType(HInstruction* input, ReferenceTypeInfo bound_type)
3395 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3396 bound_type_(bound_type) {
Calin Juravle61d544b2015-02-23 16:46:57 +00003397 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00003398 SetRawInputAt(0, input);
3399 }
3400
3401 const ReferenceTypeInfo& GetBoundType() const { return bound_type_; }
3402
3403 bool CanBeNull() const OVERRIDE {
3404 // `null instanceof ClassX` always return false so we can't be null.
3405 return false;
3406 }
3407
3408 DECLARE_INSTRUCTION(BoundType);
3409
3410 private:
3411 // Encodes the most upper class that this instruction can have. In other words
3412 // it is always the case that GetBoundType().IsSupertypeOf(GetReferenceType()).
3413 // It is used to bound the type in cases like `if (x instanceof ClassX) {}`
3414 const ReferenceTypeInfo bound_type_;
3415
3416 DISALLOW_COPY_AND_ASSIGN(HBoundType);
3417};
3418
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003419class HCheckCast : public HTemplateInstruction<2> {
3420 public:
3421 HCheckCast(HInstruction* object,
3422 HLoadClass* constant,
3423 bool class_is_final,
3424 uint32_t dex_pc)
3425 : HTemplateInstruction(SideEffects::None()),
3426 class_is_final_(class_is_final),
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003427 must_do_null_check_(true),
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003428 dex_pc_(dex_pc) {
3429 SetRawInputAt(0, object);
3430 SetRawInputAt(1, constant);
3431 }
3432
3433 bool CanBeMoved() const OVERRIDE { return true; }
3434
3435 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
3436 return true;
3437 }
3438
3439 bool NeedsEnvironment() const OVERRIDE {
3440 // Instruction may throw a CheckCastError.
3441 return true;
3442 }
3443
3444 bool CanThrow() const OVERRIDE { return true; }
3445
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003446 bool MustDoNullCheck() const { return must_do_null_check_; }
3447 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
3448
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003449 uint32_t GetDexPc() const { return dex_pc_; }
3450
3451 bool IsClassFinal() const { return class_is_final_; }
3452
3453 DECLARE_INSTRUCTION(CheckCast);
3454
3455 private:
3456 const bool class_is_final_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003457 bool must_do_null_check_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003458 const uint32_t dex_pc_;
3459
3460 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003461};
3462
Calin Juravle27df7582015-04-17 19:12:31 +01003463class HMemoryBarrier : public HTemplateInstruction<0> {
3464 public:
3465 explicit HMemoryBarrier(MemBarrierKind barrier_kind)
3466 : HTemplateInstruction(SideEffects::None()),
3467 barrier_kind_(barrier_kind) {}
3468
3469 MemBarrierKind GetBarrierKind() { return barrier_kind_; }
3470
3471 DECLARE_INSTRUCTION(MemoryBarrier);
3472
3473 private:
3474 const MemBarrierKind barrier_kind_;
3475
3476 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
3477};
3478
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003479class HMonitorOperation : public HTemplateInstruction<1> {
3480 public:
3481 enum OperationKind {
3482 kEnter,
3483 kExit,
3484 };
3485
3486 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
3487 : HTemplateInstruction(SideEffects::None()), kind_(kind), dex_pc_(dex_pc) {
3488 SetRawInputAt(0, object);
3489 }
3490
3491 // Instruction may throw a Java exception, so we need an environment.
3492 bool NeedsEnvironment() const OVERRIDE { return true; }
3493 bool CanThrow() const OVERRIDE { return true; }
3494
3495 uint32_t GetDexPc() const { return dex_pc_; }
3496
3497 bool IsEnter() const { return kind_ == kEnter; }
3498
3499 DECLARE_INSTRUCTION(MonitorOperation);
3500
Calin Juravle52c48962014-12-16 17:02:57 +00003501 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003502 const OperationKind kind_;
3503 const uint32_t dex_pc_;
3504
3505 private:
3506 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
3507};
3508
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003509class MoveOperands : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003510 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01003511 MoveOperands(Location source,
3512 Location destination,
3513 Primitive::Type type,
3514 HInstruction* instruction)
3515 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003516
3517 Location GetSource() const { return source_; }
3518 Location GetDestination() const { return destination_; }
3519
3520 void SetSource(Location value) { source_ = value; }
3521 void SetDestination(Location value) { destination_ = value; }
3522
3523 // The parallel move resolver marks moves as "in-progress" by clearing the
3524 // destination (but not the source).
3525 Location MarkPending() {
3526 DCHECK(!IsPending());
3527 Location dest = destination_;
3528 destination_ = Location::NoLocation();
3529 return dest;
3530 }
3531
3532 void ClearPending(Location dest) {
3533 DCHECK(IsPending());
3534 destination_ = dest;
3535 }
3536
3537 bool IsPending() const {
3538 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
3539 return destination_.IsInvalid() && !source_.IsInvalid();
3540 }
3541
3542 // True if this blocks a move from the given location.
3543 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08003544 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003545 }
3546
3547 // A move is redundant if it's been eliminated, if its source and
3548 // destination are the same, or if its destination is unneeded.
3549 bool IsRedundant() const {
3550 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
3551 }
3552
3553 // We clear both operands to indicate move that's been eliminated.
3554 void Eliminate() {
3555 source_ = destination_ = Location::NoLocation();
3556 }
3557
3558 bool IsEliminated() const {
3559 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
3560 return source_.IsInvalid();
3561 }
3562
Nicolas Geoffray90218252015-04-15 11:56:51 +01003563 bool Is64BitMove() const {
3564 return Primitive::Is64BitType(type_);
3565 }
3566
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003567 HInstruction* GetInstruction() const { return instruction_; }
3568
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003569 private:
3570 Location source_;
3571 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01003572 // The type this move is for.
3573 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003574 // The instruction this move is assocatied with. Null when this move is
3575 // for moving an input in the expected locations of user (including a phi user).
3576 // This is only used in debug mode, to ensure we do not connect interval siblings
3577 // in the same parallel move.
3578 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003579};
3580
3581static constexpr size_t kDefaultNumberOfMoves = 4;
3582
3583class HParallelMove : public HTemplateInstruction<0> {
3584 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003585 explicit HParallelMove(ArenaAllocator* arena)
3586 : HTemplateInstruction(SideEffects::None()), moves_(arena, kDefaultNumberOfMoves) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003587
Nicolas Geoffray90218252015-04-15 11:56:51 +01003588 void AddMove(Location source,
3589 Location destination,
3590 Primitive::Type type,
3591 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003592 DCHECK(source.IsValid());
3593 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003594 if (kIsDebugBuild) {
3595 if (instruction != nullptr) {
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00003596 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003597 if (moves_.Get(i).GetInstruction() == instruction) {
3598 // Special case the situation where the move is for the spill slot
3599 // of the instruction.
3600 if ((GetPrevious() == instruction)
3601 || ((GetPrevious() == nullptr)
3602 && instruction->IsPhi()
3603 && instruction->GetBlock() == GetBlock())) {
3604 DCHECK_NE(destination.GetKind(), moves_.Get(i).GetDestination().GetKind())
3605 << "Doing parallel moves for the same instruction.";
3606 } else {
3607 DCHECK(false) << "Doing parallel moves for the same instruction.";
3608 }
3609 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00003610 }
3611 }
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003612 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Zheng Xuad4450e2015-04-17 18:48:56 +08003613 DCHECK(!destination.OverlapsWith(moves_.Get(i).GetDestination()))
3614 << "Overlapped destination for two moves in a parallel move.";
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003615 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003616 }
Nicolas Geoffray90218252015-04-15 11:56:51 +01003617 moves_.Add(MoveOperands(source, destination, type, instruction));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003618 }
3619
3620 MoveOperands* MoveOperandsAt(size_t index) const {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003621 return moves_.GetRawStorage() + index;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003622 }
3623
3624 size_t NumMoves() const { return moves_.Size(); }
3625
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003626 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003627
3628 private:
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003629 GrowableArray<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003630
3631 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
3632};
3633
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003634class HGraphVisitor : public ValueObject {
3635 public:
Dave Allison20dfc792014-06-16 20:44:29 -07003636 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
3637 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003638
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003639 virtual void VisitInstruction(HInstruction* instruction) { UNUSED(instruction); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003640 virtual void VisitBasicBlock(HBasicBlock* block);
3641
Roland Levillain633021e2014-10-01 14:12:25 +01003642 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003643 void VisitInsertionOrder();
3644
Roland Levillain633021e2014-10-01 14:12:25 +01003645 // Visit the graph following dominator tree reverse post-order.
3646 void VisitReversePostOrder();
3647
Nicolas Geoffray787c3072014-03-17 10:20:19 +00003648 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00003649
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003650 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003651#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003652 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
3653
3654 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
3655
3656#undef DECLARE_VISIT_INSTRUCTION
3657
3658 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07003659 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003660
3661 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
3662};
3663
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003664class HGraphDelegateVisitor : public HGraphVisitor {
3665 public:
3666 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
3667 virtual ~HGraphDelegateVisitor() {}
3668
3669 // Visit functions that delegate to to super class.
3670#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003671 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003672
3673 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
3674
3675#undef DECLARE_VISIT_INSTRUCTION
3676
3677 private:
3678 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
3679};
3680
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003681class HInsertionOrderIterator : public ValueObject {
3682 public:
3683 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
3684
3685 bool Done() const { return index_ == graph_.GetBlocks().Size(); }
3686 HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); }
3687 void Advance() { ++index_; }
3688
3689 private:
3690 const HGraph& graph_;
3691 size_t index_;
3692
3693 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
3694};
3695
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003696class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003697 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00003698 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
3699 // Check that reverse post order of the graph has been built.
3700 DCHECK(!graph.GetReversePostOrder().IsEmpty());
3701 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003702
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003703 bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); }
3704 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003705 void Advance() { ++index_; }
3706
3707 private:
3708 const HGraph& graph_;
3709 size_t index_;
3710
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003711 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003712};
3713
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003714class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003715 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003716 explicit HPostOrderIterator(const HGraph& graph)
David Brazdil10f56cb2015-03-24 18:49:14 +00003717 : graph_(graph), index_(graph_.GetReversePostOrder().Size()) {
3718 // Check that reverse post order of the graph has been built.
3719 DCHECK(!graph.GetReversePostOrder().IsEmpty());
3720 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003721
3722 bool Done() const { return index_ == 0; }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003723 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003724 void Advance() { --index_; }
3725
3726 private:
3727 const HGraph& graph_;
3728 size_t index_;
3729
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003730 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003731};
3732
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01003733class HLinearPostOrderIterator : public ValueObject {
3734 public:
3735 explicit HLinearPostOrderIterator(const HGraph& graph)
3736 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().Size()) {}
3737
3738 bool Done() const { return index_ == 0; }
3739
3740 HBasicBlock* Current() const { return order_.Get(index_ -1); }
3741
3742 void Advance() {
3743 --index_;
3744 DCHECK_GE(index_, 0U);
3745 }
3746
3747 private:
3748 const GrowableArray<HBasicBlock*>& order_;
3749 size_t index_;
3750
3751 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
3752};
3753
3754class HLinearOrderIterator : public ValueObject {
3755 public:
3756 explicit HLinearOrderIterator(const HGraph& graph)
3757 : order_(graph.GetLinearOrder()), index_(0) {}
3758
3759 bool Done() const { return index_ == order_.Size(); }
3760 HBasicBlock* Current() const { return order_.Get(index_); }
3761 void Advance() { ++index_; }
3762
3763 private:
3764 const GrowableArray<HBasicBlock*>& order_;
3765 size_t index_;
3766
3767 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
3768};
3769
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003770// Iterator over the blocks that art part of the loop. Includes blocks part
3771// of an inner loop. The order in which the blocks are iterated is on their
3772// block id.
3773class HBlocksInLoopIterator : public ValueObject {
3774 public:
3775 explicit HBlocksInLoopIterator(const HLoopInformation& info)
3776 : blocks_in_loop_(info.GetBlocks()),
3777 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
3778 index_(0) {
3779 if (!blocks_in_loop_.IsBitSet(index_)) {
3780 Advance();
3781 }
3782 }
3783
3784 bool Done() const { return index_ == blocks_.Size(); }
3785 HBasicBlock* Current() const { return blocks_.Get(index_); }
3786 void Advance() {
3787 ++index_;
3788 for (size_t e = blocks_.Size(); index_ < e; ++index_) {
3789 if (blocks_in_loop_.IsBitSet(index_)) {
3790 break;
3791 }
3792 }
3793 }
3794
3795 private:
3796 const BitVector& blocks_in_loop_;
3797 const GrowableArray<HBasicBlock*>& blocks_;
3798 size_t index_;
3799
3800 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
3801};
3802
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003803inline int64_t Int64FromConstant(HConstant* constant) {
3804 DCHECK(constant->IsIntConstant() || constant->IsLongConstant());
3805 return constant->IsIntConstant() ? constant->AsIntConstant()->GetValue()
3806 : constant->AsLongConstant()->GetValue();
3807}
3808
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003809} // namespace art
3810
3811#endif // ART_COMPILER_OPTIMIZING_NODES_H_