blob: cba64c5552b9d720fff29382327dfd760cfb6217 [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);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +0100570 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100571 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +0100572 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +0100573 // Replace instruction `initial` with `replacement` within this block.
574 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
575 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100576 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100577 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +0000578 // RemoveInstruction and RemovePhi delete a given instruction from the respective
579 // instruction list. With 'ensure_safety' set to true, it verifies that the
580 // instruction is not in use and removes it from the use lists of its inputs.
581 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
582 void RemovePhi(HPhi* phi, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100583
584 bool IsLoopHeader() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100585 return (loop_information_ != nullptr) && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100586 }
587
Roland Levillain6b879dd2014-09-22 17:13:44 +0100588 bool IsLoopPreHeaderFirstPredecessor() const {
589 DCHECK(IsLoopHeader());
590 DCHECK(!GetPredecessors().IsEmpty());
591 return GetPredecessors().Get(0) == GetLoopInformation()->GetPreHeader();
592 }
593
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100594 HLoopInformation* GetLoopInformation() const {
595 return loop_information_;
596 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000597
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000598 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100599 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000600 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100601 void SetInLoop(HLoopInformation* info) {
602 if (IsLoopHeader()) {
603 // Nothing to do. This just means `info` is an outer loop.
604 } else if (loop_information_ == nullptr) {
605 loop_information_ = info;
606 } else if (loop_information_->Contains(*info->GetHeader())) {
607 // Block is currently part of an outer loop. Make it part of this inner loop.
608 // Note that a non loop header having a loop information means this loop information
609 // has already been populated
610 loop_information_ = info;
611 } else {
612 // Block is part of an inner loop. Do not update the loop information.
613 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
614 // at this point, because this method is being called while populating `info`.
615 }
616 }
617
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000618 // Raw update of the loop information.
619 void SetLoopInformation(HLoopInformation* info) {
620 loop_information_ = info;
621 }
622
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100623 bool IsInLoop() const { return loop_information_ != nullptr; }
624
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100625 // Returns wheter this block dominates the blocked passed as parameter.
626 bool Dominates(HBasicBlock* block) const;
627
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100628 size_t GetLifetimeStart() const { return lifetime_start_; }
629 size_t GetLifetimeEnd() const { return lifetime_end_; }
630
631 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
632 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
633
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100634 uint32_t GetDexPc() const { return dex_pc_; }
635
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000636 bool IsCatchBlock() const { return is_catch_block_; }
637 void SetIsCatchBlock() { is_catch_block_ = true; }
638
David Brazdil8d5b8b22015-03-24 10:51:52 +0000639 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +0000640 bool EndsWithIf() const;
641 bool HasSinglePhi() const;
642
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000643 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000644 HGraph* graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000645 GrowableArray<HBasicBlock*> predecessors_;
646 GrowableArray<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100647 HInstructionList instructions_;
648 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000649 HLoopInformation* loop_information_;
650 HBasicBlock* dominator_;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100651 GrowableArray<HBasicBlock*> dominated_blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000652 int block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100653 // The dex program counter of the first instruction of this block.
654 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100655 size_t lifetime_start_;
656 size_t lifetime_end_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000657 bool is_catch_block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000658
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000659 friend class HGraph;
660 friend class HInstruction;
661
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000662 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
663};
664
David Brazdilb2bd1c52015-03-25 11:17:37 +0000665// Iterates over the LoopInformation of all loops which contain 'block'
666// from the innermost to the outermost.
667class HLoopInformationOutwardIterator : public ValueObject {
668 public:
669 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
670 : current_(block.GetLoopInformation()) {}
671
672 bool Done() const { return current_ == nullptr; }
673
674 void Advance() {
675 DCHECK(!Done());
676 current_ = current_->GetHeader()->GetDominator()->GetLoopInformation();
677 }
678
679 HLoopInformation* Current() const {
680 DCHECK(!Done());
681 return current_;
682 }
683
684 private:
685 HLoopInformation* current_;
686
687 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
688};
689
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100690#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
691 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000692 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000693 M(ArrayGet, Instruction) \
694 M(ArrayLength, Instruction) \
695 M(ArraySet, Instruction) \
David Brazdil66d126e2015-04-03 16:02:44 +0100696 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000697 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +0000698 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000699 M(CheckCast, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100700 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000701 M(Compare, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100702 M(Condition, BinaryOperation) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700703 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000704 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +0000705 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000706 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100707 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000708 M(Exit, Instruction) \
709 M(FloatConstant, Constant) \
710 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100711 M(GreaterThan, Condition) \
712 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100713 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000714 M(InstanceFieldGet, Instruction) \
715 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000716 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100717 M(IntConstant, Constant) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +0000718 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000719 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100720 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000721 M(LessThan, Condition) \
722 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000723 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000724 M(LoadException, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100725 M(LoadLocal, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000726 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100727 M(Local, Instruction) \
728 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +0100729 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +0000730 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000731 M(Mul, BinaryOperation) \
732 M(Neg, UnaryOperation) \
733 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100734 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +0100735 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000736 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000737 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000738 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000739 M(Or, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100740 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000741 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100742 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000743 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100744 M(Return, Instruction) \
745 M(ReturnVoid, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000746 M(Shl, BinaryOperation) \
747 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100748 M(StaticFieldGet, Instruction) \
749 M(StaticFieldSet, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100750 M(StoreLocal, Instruction) \
751 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100752 M(SuspendCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000753 M(Temporary, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000754 M(Throw, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +0000755 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000756 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000757 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000758
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100759#define FOR_EACH_INSTRUCTION(M) \
760 FOR_EACH_CONCRETE_INSTRUCTION(M) \
761 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +0100762 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +0100763 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100764 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -0700765
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100766#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000767FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
768#undef FORWARD_DECLARATION
769
Roland Levillainccc07a92014-09-16 14:48:16 +0100770#define DECLARE_INSTRUCTION(type) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000771 InstructionKind GetKind() const OVERRIDE { return k##type; } \
772 const char* DebugName() const OVERRIDE { return #type; } \
773 const H##type* As##type() const OVERRIDE { return this; } \
774 H##type* As##type() OVERRIDE { return this; } \
775 bool InstructionTypeEquals(HInstruction* other) const OVERRIDE { \
Roland Levillainccc07a92014-09-16 14:48:16 +0100776 return other->Is##type(); \
777 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000778 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000779
David Brazdiled596192015-01-23 10:39:45 +0000780template <typename T> class HUseList;
781
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100782template <typename T>
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700783class HUseListNode : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000784 public:
David Brazdiled596192015-01-23 10:39:45 +0000785 HUseListNode* GetPrevious() const { return prev_; }
786 HUseListNode* GetNext() const { return next_; }
787 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100788 size_t GetIndex() const { return index_; }
789
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000790 private:
David Brazdiled596192015-01-23 10:39:45 +0000791 HUseListNode(T user, size_t index)
792 : user_(user), index_(index), prev_(nullptr), next_(nullptr) {}
793
794 T const user_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100795 const size_t index_;
David Brazdiled596192015-01-23 10:39:45 +0000796 HUseListNode<T>* prev_;
797 HUseListNode<T>* next_;
798
799 friend class HUseList<T>;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000800
801 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
802};
803
David Brazdiled596192015-01-23 10:39:45 +0000804template <typename T>
805class HUseList : public ValueObject {
806 public:
807 HUseList() : first_(nullptr) {}
808
809 void Clear() {
810 first_ = nullptr;
811 }
812
813 // Adds a new entry at the beginning of the use list and returns
814 // the newly created node.
815 HUseListNode<T>* AddUse(T user, size_t index, ArenaAllocator* arena) {
David Brazdilea55b932015-01-27 17:12:29 +0000816 HUseListNode<T>* new_node = new (arena) HUseListNode<T>(user, index);
David Brazdiled596192015-01-23 10:39:45 +0000817 if (IsEmpty()) {
818 first_ = new_node;
819 } else {
820 first_->prev_ = new_node;
821 new_node->next_ = first_;
822 first_ = new_node;
823 }
824 return new_node;
825 }
826
827 HUseListNode<T>* GetFirst() const {
828 return first_;
829 }
830
831 void Remove(HUseListNode<T>* node) {
David Brazdil1abb4192015-02-17 18:33:36 +0000832 DCHECK(node != nullptr);
833 DCHECK(Contains(node));
834
David Brazdiled596192015-01-23 10:39:45 +0000835 if (node->prev_ != nullptr) {
836 node->prev_->next_ = node->next_;
837 }
838 if (node->next_ != nullptr) {
839 node->next_->prev_ = node->prev_;
840 }
841 if (node == first_) {
842 first_ = node->next_;
843 }
844 }
845
David Brazdil1abb4192015-02-17 18:33:36 +0000846 bool Contains(const HUseListNode<T>* node) const {
847 if (node == nullptr) {
848 return false;
849 }
850 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
851 if (current == node) {
852 return true;
853 }
854 }
855 return false;
856 }
857
David Brazdiled596192015-01-23 10:39:45 +0000858 bool IsEmpty() const {
859 return first_ == nullptr;
860 }
861
862 bool HasOnlyOneUse() const {
863 return first_ != nullptr && first_->next_ == nullptr;
864 }
865
866 private:
867 HUseListNode<T>* first_;
868};
869
870template<typename T>
871class HUseIterator : public ValueObject {
872 public:
873 explicit HUseIterator(const HUseList<T>& uses) : current_(uses.GetFirst()) {}
874
875 bool Done() const { return current_ == nullptr; }
876
877 void Advance() {
878 DCHECK(!Done());
879 current_ = current_->GetNext();
880 }
881
882 HUseListNode<T>* Current() const {
883 DCHECK(!Done());
884 return current_;
885 }
886
887 private:
888 HUseListNode<T>* current_;
889
890 friend class HValue;
891};
892
David Brazdil1abb4192015-02-17 18:33:36 +0000893// This class is used by HEnvironment and HInstruction classes to record the
894// instructions they use and pointers to the corresponding HUseListNodes kept
895// by the used instructions.
896template <typename T>
897class HUserRecord : public ValueObject {
898 public:
899 HUserRecord() : instruction_(nullptr), use_node_(nullptr) {}
900 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), use_node_(nullptr) {}
901
902 HUserRecord(const HUserRecord<T>& old_record, HUseListNode<T>* use_node)
903 : instruction_(old_record.instruction_), use_node_(use_node) {
904 DCHECK(instruction_ != nullptr);
905 DCHECK(use_node_ != nullptr);
906 DCHECK(old_record.use_node_ == nullptr);
907 }
908
909 HInstruction* GetInstruction() const { return instruction_; }
910 HUseListNode<T>* GetUseNode() const { return use_node_; }
911
912 private:
913 // Instruction used by the user.
914 HInstruction* instruction_;
915
916 // Corresponding entry in the use list kept by 'instruction_'.
917 HUseListNode<T>* use_node_;
918};
919
Calin Juravle27df7582015-04-17 19:12:31 +0100920// TODO: Add better documentation to this class and maybe refactor with more suggestive names.
921// - Has(All)SideEffects suggests that all the side effects are present but only ChangesSomething
922// flag is consider.
923// - DependsOn suggests that there is a real dependency between side effects but it only
924// checks DependendsOnSomething flag.
925//
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100926// Represents the side effects an instruction may have.
927class SideEffects : public ValueObject {
928 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100929 SideEffects() : flags_(0) {}
930
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100931 static SideEffects None() {
932 return SideEffects(0);
933 }
934
935 static SideEffects All() {
936 return SideEffects(ChangesSomething().flags_ | DependsOnSomething().flags_);
937 }
938
939 static SideEffects ChangesSomething() {
940 return SideEffects((1 << kFlagChangesCount) - 1);
941 }
942
943 static SideEffects DependsOnSomething() {
944 int count = kFlagDependsOnCount - kFlagChangesCount;
945 return SideEffects(((1 << count) - 1) << kFlagChangesCount);
946 }
947
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100948 SideEffects Union(SideEffects other) const {
949 return SideEffects(flags_ | other.flags_);
950 }
951
Roland Levillain72bceff2014-09-15 18:29:00 +0100952 bool HasSideEffects() const {
953 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
954 return (flags_ & all_bits_set) != 0;
955 }
956
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100957 bool HasAllSideEffects() const {
958 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
959 return all_bits_set == (flags_ & all_bits_set);
960 }
961
962 bool DependsOn(SideEffects other) const {
963 size_t depends_flags = other.ComputeDependsFlags();
964 return (flags_ & depends_flags) != 0;
965 }
966
967 bool HasDependencies() const {
968 int count = kFlagDependsOnCount - kFlagChangesCount;
969 size_t all_bits_set = (1 << count) - 1;
970 return ((flags_ >> kFlagChangesCount) & all_bits_set) != 0;
971 }
972
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100973 private:
974 static constexpr int kFlagChangesSomething = 0;
975 static constexpr int kFlagChangesCount = kFlagChangesSomething + 1;
976
977 static constexpr int kFlagDependsOnSomething = kFlagChangesCount;
978 static constexpr int kFlagDependsOnCount = kFlagDependsOnSomething + 1;
979
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100980 explicit SideEffects(size_t flags) : flags_(flags) {}
981
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100982 size_t ComputeDependsFlags() const {
983 return flags_ << kFlagChangesCount;
984 }
985
986 size_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100987};
988
David Brazdiled596192015-01-23 10:39:45 +0000989// A HEnvironment object contains the values of virtual registers at a given location.
990class HEnvironment : public ArenaObject<kArenaAllocMisc> {
991 public:
992 HEnvironment(ArenaAllocator* arena, size_t number_of_vregs)
993 : vregs_(arena, number_of_vregs) {
994 vregs_.SetSize(number_of_vregs);
995 for (size_t i = 0; i < number_of_vregs; i++) {
David Brazdil1abb4192015-02-17 18:33:36 +0000996 vregs_.Put(i, HUserRecord<HEnvironment*>());
David Brazdiled596192015-01-23 10:39:45 +0000997 }
998 }
999
1000 void CopyFrom(HEnvironment* env);
1001
1002 void SetRawEnvAt(size_t index, HInstruction* instruction) {
David Brazdil1abb4192015-02-17 18:33:36 +00001003 vregs_.Put(index, HUserRecord<HEnvironment*>(instruction));
David Brazdiled596192015-01-23 10:39:45 +00001004 }
1005
1006 HInstruction* GetInstructionAt(size_t index) const {
David Brazdil1abb4192015-02-17 18:33:36 +00001007 return vregs_.Get(index).GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001008 }
1009
David Brazdil1abb4192015-02-17 18:33:36 +00001010 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001011
1012 size_t Size() const { return vregs_.Size(); }
1013
1014 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001015 // Record instructions' use entries of this environment for constant-time removal.
1016 // It should only be called by HInstruction when a new environment use is added.
1017 void RecordEnvUse(HUseListNode<HEnvironment*>* env_use) {
1018 DCHECK(env_use->GetUser() == this);
1019 size_t index = env_use->GetIndex();
1020 vregs_.Put(index, HUserRecord<HEnvironment*>(vregs_.Get(index), env_use));
1021 }
David Brazdiled596192015-01-23 10:39:45 +00001022
David Brazdil1abb4192015-02-17 18:33:36 +00001023 GrowableArray<HUserRecord<HEnvironment*> > vregs_;
David Brazdiled596192015-01-23 10:39:45 +00001024
David Brazdil1abb4192015-02-17 18:33:36 +00001025 friend HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001026
1027 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1028};
1029
Calin Juravleacf735c2015-02-12 15:25:22 +00001030class ReferenceTypeInfo : ValueObject {
1031 public:
Calin Juravleb1498f62015-02-16 13:13:29 +00001032 typedef Handle<mirror::Class> TypeHandle;
1033
1034 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact)
1035 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1036 if (type_handle->IsObjectClass()) {
1037 // Override the type handle to be consistent with the case when we get to
1038 // Top but don't have the Object class available. It avoids having to guess
1039 // what value the type_handle has when it's Top.
1040 return ReferenceTypeInfo(TypeHandle(), is_exact, true);
1041 } else {
1042 return ReferenceTypeInfo(type_handle, is_exact, false);
1043 }
1044 }
1045
1046 static ReferenceTypeInfo CreateTop(bool is_exact) {
1047 return ReferenceTypeInfo(TypeHandle(), is_exact, true);
Calin Juravleacf735c2015-02-12 15:25:22 +00001048 }
1049
1050 bool IsExact() const { return is_exact_; }
1051 bool IsTop() const { return is_top_; }
1052
1053 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
1054
Calin Juravleb1498f62015-02-16 13:13:29 +00001055 bool IsSupertypeOf(ReferenceTypeInfo rti) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Calin Juravleacf735c2015-02-12 15:25:22 +00001056 if (IsTop()) {
1057 // Top (equivalent for java.lang.Object) is supertype of anything.
1058 return true;
1059 }
1060 if (rti.IsTop()) {
1061 // If we get here `this` is not Top() so it can't be a supertype.
1062 return false;
1063 }
1064 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
1065 }
1066
1067 // Returns true if the type information provide the same amount of details.
1068 // Note that it does not mean that the instructions have the same actual type
1069 // (e.g. tops are equal but they can be the result of a merge).
1070 bool IsEqual(ReferenceTypeInfo rti) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1071 if (IsExact() != rti.IsExact()) {
1072 return false;
1073 }
1074 if (IsTop() && rti.IsTop()) {
1075 // `Top` means java.lang.Object, so the types are equivalent.
1076 return true;
1077 }
1078 if (IsTop() || rti.IsTop()) {
1079 // If only one is top or object than they are not equivalent.
1080 // NB: We need this extra check because the type_handle of `Top` is invalid
1081 // and we cannot inspect its reference.
1082 return false;
1083 }
1084
1085 // Finally check the types.
1086 return GetTypeHandle().Get() == rti.GetTypeHandle().Get();
1087 }
1088
1089 private:
Calin Juravleb1498f62015-02-16 13:13:29 +00001090 ReferenceTypeInfo() : ReferenceTypeInfo(TypeHandle(), false, true) {}
1091 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact, bool is_top)
1092 : type_handle_(type_handle), is_exact_(is_exact), is_top_(is_top) {}
1093
Calin Juravleacf735c2015-02-12 15:25:22 +00001094 // The class of the object.
Calin Juravleb1498f62015-02-16 13:13:29 +00001095 TypeHandle type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001096 // Whether or not the type is exact or a superclass of the actual type.
Calin Juravleb1498f62015-02-16 13:13:29 +00001097 // Whether or not we have any information about this type.
Calin Juravleacf735c2015-02-12 15:25:22 +00001098 bool is_exact_;
1099 // A true value here means that the object type should be java.lang.Object.
1100 // We don't have access to the corresponding mirror object every time so this
1101 // flag acts as a substitute. When true, the TypeHandle refers to a null
1102 // pointer and should not be used.
1103 bool is_top_;
1104};
1105
1106std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
1107
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001108class HInstruction : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001109 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001110 explicit HInstruction(SideEffects side_effects)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001111 : previous_(nullptr),
1112 next_(nullptr),
1113 block_(nullptr),
1114 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001115 ssa_index_(-1),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001116 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001117 locations_(nullptr),
1118 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001119 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001120 side_effects_(side_effects),
1121 reference_type_info_(ReferenceTypeInfo::CreateTop(/* is_exact */ false)) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001122
Dave Allison20dfc792014-06-16 20:44:29 -07001123 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001124
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001125#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001126 enum InstructionKind {
1127 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1128 };
1129#undef DECLARE_KIND
1130
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001131 HInstruction* GetNext() const { return next_; }
1132 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001133
Calin Juravle77520bc2015-01-12 18:45:46 +00001134 HInstruction* GetNextDisregardingMoves() const;
1135 HInstruction* GetPreviousDisregardingMoves() const;
1136
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001137 HBasicBlock* GetBlock() const { return block_; }
1138 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001139 bool IsInBlock() const { return block_ != nullptr; }
1140 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +01001141 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001142
Roland Levillain6b879dd2014-09-22 17:13:44 +01001143 virtual size_t InputCount() const = 0;
David Brazdil1abb4192015-02-17 18:33:36 +00001144 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001145
1146 virtual void Accept(HGraphVisitor* visitor) = 0;
1147 virtual const char* DebugName() const = 0;
1148
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001149 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
David Brazdil1abb4192015-02-17 18:33:36 +00001150 void SetRawInputAt(size_t index, HInstruction* input) {
1151 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1152 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001153
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001154 virtual bool NeedsEnvironment() const { return false; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001155 virtual bool IsControlFlow() const { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01001156 virtual bool CanThrow() const { return false; }
Roland Levillain72bceff2014-09-15 18:29:00 +01001157 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001158
Calin Juravle10e244f2015-01-26 18:54:32 +00001159 // Does not apply for all instructions, but having this at top level greatly
1160 // simplifies the null check elimination.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001161 virtual bool CanBeNull() const {
1162 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1163 return true;
1164 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001165
Calin Juravle641547a2015-04-21 22:08:51 +01001166 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj) const {
1167 UNUSED(obj);
1168 return false;
1169 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001170
Calin Juravleacf735c2015-02-12 15:25:22 +00001171 void SetReferenceTypeInfo(ReferenceTypeInfo reference_type_info) {
Calin Juravle61d544b2015-02-23 16:46:57 +00001172 DCHECK_EQ(GetType(), Primitive::kPrimNot);
Calin Juravleacf735c2015-02-12 15:25:22 +00001173 reference_type_info_ = reference_type_info;
1174 }
1175
Calin Juravle61d544b2015-02-23 16:46:57 +00001176 ReferenceTypeInfo GetReferenceTypeInfo() const {
1177 DCHECK_EQ(GetType(), Primitive::kPrimNot);
1178 return reference_type_info_;
1179 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001180
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001181 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001182 DCHECK(user != nullptr);
1183 HUseListNode<HInstruction*>* use =
1184 uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1185 user->SetRawInputRecordAt(index, HUserRecord<HInstruction*>(user->InputRecordAt(index), use));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001186 }
1187
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001188 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001189 DCHECK(user != nullptr);
David Brazdiled596192015-01-23 10:39:45 +00001190 HUseListNode<HEnvironment*>* env_use =
1191 env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1192 user->RecordEnvUse(env_use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001193 }
1194
David Brazdil1abb4192015-02-17 18:33:36 +00001195 void RemoveAsUserOfInput(size_t input) {
1196 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
1197 input_use.GetInstruction()->uses_.Remove(input_use.GetUseNode());
1198 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001199
David Brazdil1abb4192015-02-17 18:33:36 +00001200 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1201 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001202
David Brazdiled596192015-01-23 10:39:45 +00001203 bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); }
1204 bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); }
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001205 bool HasNonEnvironmentUses() const { return !uses_.IsEmpty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001206 bool HasOnlyOneNonEnvironmentUse() const {
1207 return !HasEnvironmentUses() && GetUses().HasOnlyOneUse();
1208 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001209
Roland Levillain6c82d402014-10-13 16:10:27 +01001210 // Does this instruction strictly dominate `other_instruction`?
1211 // Returns false if this instruction and `other_instruction` are the same.
1212 // Aborts if this instruction and `other_instruction` are both phis.
1213 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001214
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001215 int GetId() const { return id_; }
1216 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001217
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001218 int GetSsaIndex() const { return ssa_index_; }
1219 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1220 bool HasSsaIndex() const { return ssa_index_ != -1; }
1221
1222 bool HasEnvironment() const { return environment_ != nullptr; }
1223 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001224 // Set the `environment_` field. Raw because this method does not
1225 // update the uses lists.
1226 void SetRawEnvironment(HEnvironment* environment) { environment_ = environment; }
1227
1228 // Set the environment of this instruction, copying it from `environment`. While
1229 // copying, the uses lists are being updated.
1230 void CopyEnvironmentFrom(HEnvironment* environment) {
1231 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
1232 environment_ = new (allocator) HEnvironment(allocator, environment->Size());
1233 environment_->CopyFrom(environment);
1234 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001235
Nicolas Geoffray39468442014-09-02 15:17:15 +01001236 // Returns the number of entries in the environment. Typically, that is the
1237 // number of dex registers in a method. It could be more in case of inlining.
1238 size_t EnvironmentSize() const;
1239
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001240 LocationSummary* GetLocations() const { return locations_; }
1241 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001242
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001243 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001244 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001245
Alexandre Rames188d4312015-04-09 18:30:21 +01001246 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
1247 // uses of this instruction by `other` are *not* updated.
1248 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
1249 ReplaceWith(other);
1250 other->ReplaceInput(this, use_index);
1251 }
1252
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001253 // Move `this` instruction before `cursor`.
1254 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001255
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001256#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01001257 bool Is##type() const { return (As##type() != nullptr); } \
1258 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001259 virtual H##type* As##type() { return nullptr; }
1260
1261 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
1262#undef INSTRUCTION_TYPE_CHECK
1263
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001264 // Returns whether the instruction can be moved within the graph.
1265 virtual bool CanBeMoved() const { return false; }
1266
1267 // Returns whether the two instructions are of the same kind.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001268 virtual bool InstructionTypeEquals(HInstruction* other) const {
1269 UNUSED(other);
1270 return false;
1271 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001272
1273 // Returns whether any data encoded in the two instructions is equal.
1274 // This method does not look at the inputs. Both instructions must be
1275 // of the same type, otherwise the method has undefined behavior.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001276 virtual bool InstructionDataEquals(HInstruction* other) const {
1277 UNUSED(other);
1278 return false;
1279 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001280
1281 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00001282 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001283 // 2) Their inputs are identical.
1284 bool Equals(HInstruction* other) const;
1285
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001286 virtual InstructionKind GetKind() const = 0;
1287
1288 virtual size_t ComputeHashCode() const {
1289 size_t result = GetKind();
1290 for (size_t i = 0, e = InputCount(); i < e; ++i) {
1291 result = (result * 31) + InputAt(i)->GetId();
1292 }
1293 return result;
1294 }
1295
1296 SideEffects GetSideEffects() const { return side_effects_; }
1297
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001298 size_t GetLifetimePosition() const { return lifetime_position_; }
1299 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
1300 LiveInterval* GetLiveInterval() const { return live_interval_; }
1301 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
1302 bool HasLiveInterval() const { return live_interval_ != nullptr; }
1303
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001304 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
1305
1306 // Returns whether the code generation of the instruction will require to have access
1307 // to the current method. Such instructions are:
1308 // (1): Instructions that require an environment, as calling the runtime requires
1309 // to walk the stack and have the current method stored at a specific stack address.
1310 // (2): Object literals like classes and strings, that are loaded from the dex cache
1311 // fields of the current method.
1312 bool NeedsCurrentMethod() const {
1313 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
1314 }
1315
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001316 virtual bool NeedsDexCache() const { return false; }
1317
David Brazdil1abb4192015-02-17 18:33:36 +00001318 protected:
1319 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
1320 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
1321
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001322 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001323 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); }
1324
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001325 HInstruction* previous_;
1326 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001327 HBasicBlock* block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001328
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001329 // An instruction gets an id when it is added to the graph.
1330 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01001331 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001332 int id_;
1333
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001334 // When doing liveness analysis, instructions that have uses get an SSA index.
1335 int ssa_index_;
1336
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001337 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00001338 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001339
1340 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00001341 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001342
Nicolas Geoffray39468442014-09-02 15:17:15 +01001343 // The environment associated with this instruction. Not null if the instruction
1344 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001345 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001346
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001347 // Set by the code generator.
1348 LocationSummary* locations_;
1349
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001350 // Set by the liveness analysis.
1351 LiveInterval* live_interval_;
1352
1353 // Set by the liveness analysis, this is the position in a linear
1354 // order of blocks where this instruction's live interval start.
1355 size_t lifetime_position_;
1356
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001357 const SideEffects side_effects_;
1358
Calin Juravleacf735c2015-02-12 15:25:22 +00001359 // TODO: for primitive types this should be marked as invalid.
1360 ReferenceTypeInfo reference_type_info_;
1361
David Brazdil1abb4192015-02-17 18:33:36 +00001362 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001363 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00001364 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001365 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001366 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001367
1368 DISALLOW_COPY_AND_ASSIGN(HInstruction);
1369};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001370std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001371
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001372class HInputIterator : public ValueObject {
1373 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001374 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001375
1376 bool Done() const { return index_ == instruction_->InputCount(); }
1377 HInstruction* Current() const { return instruction_->InputAt(index_); }
1378 void Advance() { index_++; }
1379
1380 private:
1381 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001382 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001383
1384 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
1385};
1386
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001387class HInstructionIterator : public ValueObject {
1388 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001389 explicit HInstructionIterator(const HInstructionList& instructions)
1390 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001391 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001392 }
1393
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001394 bool Done() const { return instruction_ == nullptr; }
1395 HInstruction* Current() const { return instruction_; }
1396 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001397 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001398 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001399 }
1400
1401 private:
1402 HInstruction* instruction_;
1403 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001404
1405 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001406};
1407
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001408class HBackwardInstructionIterator : public ValueObject {
1409 public:
1410 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
1411 : instruction_(instructions.last_instruction_) {
1412 next_ = Done() ? nullptr : instruction_->GetPrevious();
1413 }
1414
1415 bool Done() const { return instruction_ == nullptr; }
1416 HInstruction* Current() const { return instruction_; }
1417 void Advance() {
1418 instruction_ = next_;
1419 next_ = Done() ? nullptr : instruction_->GetPrevious();
1420 }
1421
1422 private:
1423 HInstruction* instruction_;
1424 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001425
1426 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001427};
1428
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001429// An embedded container with N elements of type T. Used (with partial
1430// specialization for N=0) because embedded arrays cannot have size 0.
1431template<typename T, intptr_t N>
1432class EmbeddedArray {
1433 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001434 EmbeddedArray() : elements_() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001435
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001436 intptr_t GetLength() const { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001437
1438 const T& operator[](intptr_t i) const {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001439 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001440 return elements_[i];
1441 }
1442
1443 T& operator[](intptr_t i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001444 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001445 return elements_[i];
1446 }
1447
1448 const T& At(intptr_t i) const {
1449 return (*this)[i];
1450 }
1451
1452 void SetAt(intptr_t i, const T& val) {
1453 (*this)[i] = val;
1454 }
1455
1456 private:
1457 T elements_[N];
1458};
1459
1460template<typename T>
1461class EmbeddedArray<T, 0> {
1462 public:
1463 intptr_t length() const { return 0; }
1464 const T& operator[](intptr_t i) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001465 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001466 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001467 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001468 }
1469 T& operator[](intptr_t i) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001470 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001471 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001472 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001473 }
1474};
1475
1476template<intptr_t N>
1477class HTemplateInstruction: public HInstruction {
1478 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001479 HTemplateInstruction<N>(SideEffects side_effects)
1480 : HInstruction(side_effects), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07001481 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001482
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001483 size_t InputCount() const OVERRIDE { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001484
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001485 protected:
David Brazdil1abb4192015-02-17 18:33:36 +00001486 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_[i]; }
1487
1488 void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE {
1489 inputs_[i] = input;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001490 }
1491
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001492 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001493 EmbeddedArray<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001494
1495 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001496};
1497
Dave Allison20dfc792014-06-16 20:44:29 -07001498template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001499class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07001500 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001501 HExpression<N>(Primitive::Type type, SideEffects side_effects)
1502 : HTemplateInstruction<N>(side_effects), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07001503 virtual ~HExpression() {}
1504
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001505 Primitive::Type GetType() const OVERRIDE { return type_; }
Dave Allison20dfc792014-06-16 20:44:29 -07001506
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001507 protected:
1508 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07001509};
1510
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001511// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
1512// instruction that branches to the exit block.
1513class HReturnVoid : public HTemplateInstruction<0> {
1514 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001515 HReturnVoid() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001516
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001517 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001518
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001519 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001520
1521 private:
1522 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
1523};
1524
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001525// Represents dex's RETURN opcodes. A HReturn is a control flow
1526// instruction that branches to the exit block.
1527class HReturn : public HTemplateInstruction<1> {
1528 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001529 explicit HReturn(HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001530 SetRawInputAt(0, value);
1531 }
1532
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001533 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001534
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001535 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001536
1537 private:
1538 DISALLOW_COPY_AND_ASSIGN(HReturn);
1539};
1540
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001541// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001542// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001543// exit block.
1544class HExit : public HTemplateInstruction<0> {
1545 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001546 HExit() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001547
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001548 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001549
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001550 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001551
1552 private:
1553 DISALLOW_COPY_AND_ASSIGN(HExit);
1554};
1555
1556// Jumps from one block to another.
1557class HGoto : public HTemplateInstruction<0> {
1558 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001559 HGoto() : HTemplateInstruction(SideEffects::None()) {}
1560
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001561 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001562
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001563 HBasicBlock* GetSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001564 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001565 }
1566
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001567 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001568
1569 private:
1570 DISALLOW_COPY_AND_ASSIGN(HGoto);
1571};
1572
Dave Allison20dfc792014-06-16 20:44:29 -07001573
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001574// Conditional branch. A block ending with an HIf instruction must have
1575// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001576class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001577 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001578 explicit HIf(HInstruction* input) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001579 SetRawInputAt(0, input);
1580 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001581
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001582 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001583
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001584 HBasicBlock* IfTrueSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001585 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001586 }
1587
1588 HBasicBlock* IfFalseSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001589 return GetBlock()->GetSuccessors().Get(1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001590 }
1591
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001592 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001593
1594 private:
1595 DISALLOW_COPY_AND_ASSIGN(HIf);
1596};
1597
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001598// Deoptimize to interpreter, upon checking a condition.
1599class HDeoptimize : public HTemplateInstruction<1> {
1600 public:
1601 HDeoptimize(HInstruction* cond, uint32_t dex_pc)
1602 : HTemplateInstruction(SideEffects::None()),
1603 dex_pc_(dex_pc) {
1604 SetRawInputAt(0, cond);
1605 }
1606
1607 bool NeedsEnvironment() const OVERRIDE { return true; }
1608 bool CanThrow() const OVERRIDE { return true; }
1609 uint32_t GetDexPc() const { return dex_pc_; }
1610
1611 DECLARE_INSTRUCTION(Deoptimize);
1612
1613 private:
1614 uint32_t dex_pc_;
1615
1616 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
1617};
1618
Roland Levillain88cb1752014-10-20 16:36:47 +01001619class HUnaryOperation : public HExpression<1> {
1620 public:
1621 HUnaryOperation(Primitive::Type result_type, HInstruction* input)
1622 : HExpression(result_type, SideEffects::None()) {
1623 SetRawInputAt(0, input);
1624 }
1625
1626 HInstruction* GetInput() const { return InputAt(0); }
1627 Primitive::Type GetResultType() const { return GetType(); }
1628
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001629 bool CanBeMoved() const OVERRIDE { return true; }
1630 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001631 UNUSED(other);
1632 return true;
1633 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001634
Roland Levillain9240d6a2014-10-20 16:47:04 +01001635 // Try to statically evaluate `operation` and return a HConstant
1636 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001637 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01001638 HConstant* TryStaticEvaluation() const;
1639
1640 // Apply this operation to `x`.
1641 virtual int32_t Evaluate(int32_t x) const = 0;
1642 virtual int64_t Evaluate(int64_t x) const = 0;
1643
Roland Levillain88cb1752014-10-20 16:36:47 +01001644 DECLARE_INSTRUCTION(UnaryOperation);
1645
1646 private:
1647 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
1648};
1649
Dave Allison20dfc792014-06-16 20:44:29 -07001650class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001651 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001652 HBinaryOperation(Primitive::Type result_type,
1653 HInstruction* left,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001654 HInstruction* right) : HExpression(result_type, SideEffects::None()) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001655 SetRawInputAt(0, left);
1656 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001657 }
1658
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001659 HInstruction* GetLeft() const { return InputAt(0); }
1660 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07001661 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001662
Mingyao Yangdc5ac732015-02-25 11:28:05 -08001663 virtual bool IsCommutative() const { return false; }
1664
1665 // Put constant on the right.
1666 // Returns whether order is changed.
1667 bool OrderInputsWithConstantOnTheRight() {
1668 HInstruction* left = InputAt(0);
1669 HInstruction* right = InputAt(1);
1670 if (left->IsConstant() && !right->IsConstant()) {
1671 ReplaceInput(right, 0);
1672 ReplaceInput(left, 1);
1673 return true;
1674 }
1675 return false;
1676 }
1677
1678 // Order inputs by instruction id, but favor constant on the right side.
1679 // This helps GVN for commutative ops.
1680 void OrderInputs() {
1681 DCHECK(IsCommutative());
1682 HInstruction* left = InputAt(0);
1683 HInstruction* right = InputAt(1);
1684 if (left == right || (!left->IsConstant() && right->IsConstant())) {
1685 return;
1686 }
1687 if (OrderInputsWithConstantOnTheRight()) {
1688 return;
1689 }
1690 // Order according to instruction id.
1691 if (left->GetId() > right->GetId()) {
1692 ReplaceInput(right, 0);
1693 ReplaceInput(left, 1);
1694 }
1695 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001696
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001697 bool CanBeMoved() const OVERRIDE { return true; }
1698 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001699 UNUSED(other);
1700 return true;
1701 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001702
Roland Levillain9240d6a2014-10-20 16:47:04 +01001703 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01001704 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001705 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01001706 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01001707
1708 // Apply this operation to `x` and `y`.
1709 virtual int32_t Evaluate(int32_t x, int32_t y) const = 0;
1710 virtual int64_t Evaluate(int64_t x, int64_t y) const = 0;
1711
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001712 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001713 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001714 HConstant* GetConstantRight() const;
1715
1716 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001717 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001718 HInstruction* GetLeastConstantLeft() const;
1719
Roland Levillainccc07a92014-09-16 14:48:16 +01001720 DECLARE_INSTRUCTION(BinaryOperation);
1721
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001722 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001723 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
1724};
1725
Dave Allison20dfc792014-06-16 20:44:29 -07001726class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001727 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001728 HCondition(HInstruction* first, HInstruction* second)
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001729 : HBinaryOperation(Primitive::kPrimBoolean, first, second),
1730 needs_materialization_(true) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001731
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001732 bool NeedsMaterialization() const { return needs_materialization_; }
1733 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001734
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001735 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001736 // `instruction`, and disregard moves in between.
1737 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001738
Dave Allison20dfc792014-06-16 20:44:29 -07001739 DECLARE_INSTRUCTION(Condition);
1740
1741 virtual IfCondition GetCondition() const = 0;
1742
1743 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001744 // For register allocation purposes, returns whether this instruction needs to be
1745 // materialized (that is, not just be in the processor flags).
1746 bool needs_materialization_;
1747
Dave Allison20dfc792014-06-16 20:44:29 -07001748 DISALLOW_COPY_AND_ASSIGN(HCondition);
1749};
1750
1751// Instruction to check if two inputs are equal to each other.
1752class HEqual : public HCondition {
1753 public:
1754 HEqual(HInstruction* first, HInstruction* second)
1755 : HCondition(first, second) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001756
Mingyao Yangdc5ac732015-02-25 11:28:05 -08001757 bool IsCommutative() const OVERRIDE { return true; }
1758
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001759 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001760 return x == y ? 1 : 0;
1761 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001762 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001763 return x == y ? 1 : 0;
1764 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001765
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001766 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001767
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001768 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001769 return kCondEQ;
1770 }
1771
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001772 private:
1773 DISALLOW_COPY_AND_ASSIGN(HEqual);
1774};
1775
Dave Allison20dfc792014-06-16 20:44:29 -07001776class HNotEqual : public HCondition {
1777 public:
1778 HNotEqual(HInstruction* first, HInstruction* second)
1779 : HCondition(first, second) {}
1780
Mingyao Yangdc5ac732015-02-25 11:28:05 -08001781 bool IsCommutative() const OVERRIDE { return true; }
1782
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001783 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001784 return x != y ? 1 : 0;
1785 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001786 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001787 return x != y ? 1 : 0;
1788 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001789
Dave Allison20dfc792014-06-16 20:44:29 -07001790 DECLARE_INSTRUCTION(NotEqual);
1791
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001792 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001793 return kCondNE;
1794 }
1795
1796 private:
1797 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
1798};
1799
1800class HLessThan : public HCondition {
1801 public:
1802 HLessThan(HInstruction* first, HInstruction* second)
1803 : HCondition(first, second) {}
1804
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001805 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001806 return x < y ? 1 : 0;
1807 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001808 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001809 return x < y ? 1 : 0;
1810 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001811
Dave Allison20dfc792014-06-16 20:44:29 -07001812 DECLARE_INSTRUCTION(LessThan);
1813
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001814 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001815 return kCondLT;
1816 }
1817
1818 private:
1819 DISALLOW_COPY_AND_ASSIGN(HLessThan);
1820};
1821
1822class HLessThanOrEqual : public HCondition {
1823 public:
1824 HLessThanOrEqual(HInstruction* first, HInstruction* second)
1825 : HCondition(first, second) {}
1826
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001827 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001828 return x <= y ? 1 : 0;
1829 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001830 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001831 return x <= y ? 1 : 0;
1832 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001833
Dave Allison20dfc792014-06-16 20:44:29 -07001834 DECLARE_INSTRUCTION(LessThanOrEqual);
1835
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001836 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001837 return kCondLE;
1838 }
1839
1840 private:
1841 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
1842};
1843
1844class HGreaterThan : public HCondition {
1845 public:
1846 HGreaterThan(HInstruction* first, HInstruction* second)
1847 : HCondition(first, second) {}
1848
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001849 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001850 return x > y ? 1 : 0;
1851 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001852 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001853 return x > y ? 1 : 0;
1854 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001855
Dave Allison20dfc792014-06-16 20:44:29 -07001856 DECLARE_INSTRUCTION(GreaterThan);
1857
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001858 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001859 return kCondGT;
1860 }
1861
1862 private:
1863 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
1864};
1865
1866class HGreaterThanOrEqual : public HCondition {
1867 public:
1868 HGreaterThanOrEqual(HInstruction* first, HInstruction* second)
1869 : HCondition(first, second) {}
1870
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001871 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001872 return x >= y ? 1 : 0;
1873 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001874 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001875 return x >= y ? 1 : 0;
1876 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001877
Dave Allison20dfc792014-06-16 20:44:29 -07001878 DECLARE_INSTRUCTION(GreaterThanOrEqual);
1879
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001880 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001881 return kCondGE;
1882 }
1883
1884 private:
1885 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
1886};
1887
1888
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001889// Instruction to check how two inputs compare to each other.
1890// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
1891class HCompare : public HBinaryOperation {
1892 public:
Calin Juravleddb7df22014-11-25 20:56:51 +00001893 // The bias applies for floating point operations and indicates how NaN
1894 // comparisons are treated:
1895 enum Bias {
1896 kNoBias, // bias is not applicable (i.e. for long operation)
1897 kGtBias, // return 1 for NaN comparisons
1898 kLtBias, // return -1 for NaN comparisons
1899 };
1900
1901 HCompare(Primitive::Type type, HInstruction* first, HInstruction* second, Bias bias)
1902 : HBinaryOperation(Primitive::kPrimInt, first, second), bias_(bias) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001903 DCHECK_EQ(type, first->GetType());
1904 DCHECK_EQ(type, second->GetType());
1905 }
1906
Calin Juravleddb7df22014-11-25 20:56:51 +00001907 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01001908 return
1909 x == y ? 0 :
1910 x > y ? 1 :
1911 -1;
1912 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001913
1914 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01001915 return
1916 x == y ? 0 :
1917 x > y ? 1 :
1918 -1;
1919 }
1920
Calin Juravleddb7df22014-11-25 20:56:51 +00001921 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
1922 return bias_ == other->AsCompare()->bias_;
1923 }
1924
1925 bool IsGtBias() { return bias_ == kGtBias; }
1926
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001927 DECLARE_INSTRUCTION(Compare);
1928
1929 private:
Calin Juravleddb7df22014-11-25 20:56:51 +00001930 const Bias bias_;
1931
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001932 DISALLOW_COPY_AND_ASSIGN(HCompare);
1933};
1934
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001935// A local in the graph. Corresponds to a Dex register.
1936class HLocal : public HTemplateInstruction<0> {
1937 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001938 explicit HLocal(uint16_t reg_number)
1939 : HTemplateInstruction(SideEffects::None()), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001940
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001941 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001942
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001943 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001944
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001945 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001946 // The Dex register number.
1947 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001948
1949 DISALLOW_COPY_AND_ASSIGN(HLocal);
1950};
1951
1952// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07001953class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001954 public:
Roland Levillain5799fc02014-09-25 12:15:20 +01001955 HLoadLocal(HLocal* local, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001956 : HExpression(type, SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001957 SetRawInputAt(0, local);
1958 }
1959
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001960 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1961
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001962 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001963
1964 private:
1965 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
1966};
1967
1968// Store a value in a given local. This instruction has two inputs: the value
1969// and the local.
1970class HStoreLocal : public HTemplateInstruction<2> {
1971 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001972 HStoreLocal(HLocal* local, HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001973 SetRawInputAt(0, local);
1974 SetRawInputAt(1, value);
1975 }
1976
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001977 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1978
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001979 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001980
1981 private:
1982 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
1983};
1984
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001985class HConstant : public HExpression<0> {
1986 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001987 explicit HConstant(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
1988
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001989 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001990
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001991 virtual bool IsMinusOne() const { return false; }
1992 virtual bool IsZero() const { return false; }
1993 virtual bool IsOne() const { return false; }
1994
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001995 DECLARE_INSTRUCTION(Constant);
1996
1997 private:
1998 DISALLOW_COPY_AND_ASSIGN(HConstant);
1999};
2000
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002001class HFloatConstant : public HConstant {
2002 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002003 float GetValue() const { return value_; }
2004
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002005 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002006 return bit_cast<uint32_t, float>(other->AsFloatConstant()->value_) ==
2007 bit_cast<uint32_t, float>(value_);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002008 }
2009
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002010 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002011
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002012 bool IsMinusOne() const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002013 return bit_cast<uint32_t, float>(AsFloatConstant()->GetValue()) ==
2014 bit_cast<uint32_t, float>((-1.0f));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002015 }
2016 bool IsZero() const OVERRIDE {
2017 return AsFloatConstant()->GetValue() == 0.0f;
2018 }
2019 bool IsOne() const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002020 return bit_cast<uint32_t, float>(AsFloatConstant()->GetValue()) ==
2021 bit_cast<uint32_t, float>(1.0f);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002022 }
2023
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002024 DECLARE_INSTRUCTION(FloatConstant);
2025
2026 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002027 explicit HFloatConstant(float value) : HConstant(Primitive::kPrimFloat), value_(value) {}
2028
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002029 const float value_;
2030
David Brazdil8d5b8b22015-03-24 10:51:52 +00002031 // Only the SsaBuilder can currently create floating-point constants. If we
2032 // ever need to create them later in the pipeline, we will have to handle them
2033 // the same way as integral constants.
2034 friend class SsaBuilder;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002035 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2036};
2037
2038class HDoubleConstant : public HConstant {
2039 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002040 double GetValue() const { return value_; }
2041
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002042 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002043 return bit_cast<uint64_t, double>(other->AsDoubleConstant()->value_) ==
2044 bit_cast<uint64_t, double>(value_);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002045 }
2046
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002047 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002048
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002049 bool IsMinusOne() const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002050 return bit_cast<uint64_t, double>(AsDoubleConstant()->GetValue()) ==
2051 bit_cast<uint64_t, double>((-1.0));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002052 }
2053 bool IsZero() const OVERRIDE {
2054 return AsDoubleConstant()->GetValue() == 0.0;
2055 }
2056 bool IsOne() const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002057 return bit_cast<uint64_t, double>(AsDoubleConstant()->GetValue()) ==
2058 bit_cast<uint64_t, double>(1.0);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002059 }
2060
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002061 DECLARE_INSTRUCTION(DoubleConstant);
2062
2063 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002064 explicit HDoubleConstant(double value) : HConstant(Primitive::kPrimDouble), value_(value) {}
2065
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002066 const double value_;
2067
David Brazdil8d5b8b22015-03-24 10:51:52 +00002068 // Only the SsaBuilder can currently create floating-point constants. If we
2069 // ever need to create them later in the pipeline, we will have to handle them
2070 // the same way as integral constants.
2071 friend class SsaBuilder;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002072 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
2073};
2074
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002075class HNullConstant : public HConstant {
2076 public:
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002077 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2078 return true;
2079 }
2080
2081 size_t ComputeHashCode() const OVERRIDE { return 0; }
2082
2083 DECLARE_INSTRUCTION(NullConstant);
2084
2085 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002086 HNullConstant() : HConstant(Primitive::kPrimNot) {}
2087
2088 friend class HGraph;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002089 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2090};
2091
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002092// Constants of the type int. Those can be from Dex instructions, or
2093// synthesized (for example with the if-eqz instruction).
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002094class HIntConstant : public HConstant {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002095 public:
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002096 int32_t GetValue() const { return value_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002097
Calin Juravle61d544b2015-02-23 16:46:57 +00002098 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002099 return other->AsIntConstant()->value_ == value_;
2100 }
2101
Calin Juravle61d544b2015-02-23 16:46:57 +00002102 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2103
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002104 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2105 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2106 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2107
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002108 DECLARE_INSTRUCTION(IntConstant);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002109
2110 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002111 explicit HIntConstant(int32_t value) : HConstant(Primitive::kPrimInt), value_(value) {}
2112
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002113 const int32_t value_;
2114
David Brazdil8d5b8b22015-03-24 10:51:52 +00002115 friend class HGraph;
2116 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
Zheng Xuad4450e2015-04-17 18:48:56 +08002117 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002118 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2119};
2120
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002121class HLongConstant : public HConstant {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002122 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002123 int64_t GetValue() const { return value_; }
2124
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002125 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002126 return other->AsLongConstant()->value_ == value_;
2127 }
2128
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002129 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002130
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002131 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2132 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2133 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2134
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002135 DECLARE_INSTRUCTION(LongConstant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002136
2137 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002138 explicit HLongConstant(int64_t value) : HConstant(Primitive::kPrimLong), value_(value) {}
2139
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002140 const int64_t value_;
2141
David Brazdil8d5b8b22015-03-24 10:51:52 +00002142 friend class HGraph;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002143 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2144};
2145
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002146enum class Intrinsics {
2147#define OPTIMIZING_INTRINSICS(Name, IsStatic) k ## Name,
2148#include "intrinsics_list.h"
2149 kNone,
2150 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
2151#undef INTRINSICS_LIST
2152#undef OPTIMIZING_INTRINSICS
2153};
2154std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
2155
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002156class HInvoke : public HInstruction {
2157 public:
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002158 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002159
2160 // Runtime needs to walk the stack, so Dex -> Dex calls need to
2161 // know their environment.
Calin Juravle77520bc2015-01-12 18:45:46 +00002162 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002163
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01002164 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002165 SetRawInputAt(index, argument);
2166 }
2167
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002168 Primitive::Type GetType() const OVERRIDE { return return_type_; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002169
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002170 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002171
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002172 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
2173
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01002174 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002175 return intrinsic_;
2176 }
2177
2178 void SetIntrinsic(Intrinsics intrinsic) {
2179 intrinsic_ = intrinsic;
2180 }
2181
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002182 DECLARE_INSTRUCTION(Invoke);
2183
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002184 protected:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002185 HInvoke(ArenaAllocator* arena,
2186 uint32_t number_of_arguments,
2187 Primitive::Type return_type,
2188 uint32_t dex_pc,
2189 uint32_t dex_method_index)
2190 : HInstruction(SideEffects::All()),
2191 inputs_(arena, number_of_arguments),
2192 return_type_(return_type),
2193 dex_pc_(dex_pc),
2194 dex_method_index_(dex_method_index),
2195 intrinsic_(Intrinsics::kNone) {
2196 inputs_.SetSize(number_of_arguments);
2197 }
2198
David Brazdil1abb4192015-02-17 18:33:36 +00002199 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
2200 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
2201 inputs_.Put(index, input);
2202 }
2203
2204 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002205 const Primitive::Type return_type_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002206 const uint32_t dex_pc_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002207 const uint32_t dex_method_index_;
2208 Intrinsics intrinsic_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002209
2210 private:
2211 DISALLOW_COPY_AND_ASSIGN(HInvoke);
2212};
2213
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002214class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002215 public:
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002216 HInvokeStaticOrDirect(ArenaAllocator* arena,
2217 uint32_t number_of_arguments,
2218 Primitive::Type return_type,
2219 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002220 uint32_t dex_method_index,
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002221 bool is_recursive,
Nicolas Geoffray79041292015-03-26 10:05:54 +00002222 InvokeType original_invoke_type,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002223 InvokeType invoke_type)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002224 : HInvoke(arena, number_of_arguments, return_type, dex_pc, dex_method_index),
Nicolas Geoffray79041292015-03-26 10:05:54 +00002225 original_invoke_type_(original_invoke_type),
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002226 invoke_type_(invoke_type),
2227 is_recursive_(is_recursive) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002228
Calin Juravle641547a2015-04-21 22:08:51 +01002229 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
2230 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00002231 // We access the method via the dex cache so we can't do an implicit null check.
2232 // TODO: for intrinsics we can generate implicit null checks.
2233 return false;
2234 }
2235
Nicolas Geoffray79041292015-03-26 10:05:54 +00002236 InvokeType GetOriginalInvokeType() const { return original_invoke_type_; }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002237 InvokeType GetInvokeType() const { return invoke_type_; }
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002238 bool IsRecursive() const { return is_recursive_; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002239 bool NeedsDexCache() const OVERRIDE { return !IsRecursive(); }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002240
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002241 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002242
2243 private:
Nicolas Geoffray79041292015-03-26 10:05:54 +00002244 const InvokeType original_invoke_type_;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002245 const InvokeType invoke_type_;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002246 const bool is_recursive_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002247
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002248 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002249};
2250
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002251class HInvokeVirtual : public HInvoke {
2252 public:
2253 HInvokeVirtual(ArenaAllocator* arena,
2254 uint32_t number_of_arguments,
2255 Primitive::Type return_type,
2256 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002257 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002258 uint32_t vtable_index)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002259 : HInvoke(arena, number_of_arguments, return_type, dex_pc, dex_method_index),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002260 vtable_index_(vtable_index) {}
2261
Calin Juravle641547a2015-04-21 22:08:51 +01002262 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00002263 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01002264 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00002265 }
2266
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002267 uint32_t GetVTableIndex() const { return vtable_index_; }
2268
2269 DECLARE_INSTRUCTION(InvokeVirtual);
2270
2271 private:
2272 const uint32_t vtable_index_;
2273
2274 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
2275};
2276
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002277class HInvokeInterface : public HInvoke {
2278 public:
2279 HInvokeInterface(ArenaAllocator* arena,
2280 uint32_t number_of_arguments,
2281 Primitive::Type return_type,
2282 uint32_t dex_pc,
2283 uint32_t dex_method_index,
2284 uint32_t imt_index)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002285 : HInvoke(arena, number_of_arguments, return_type, dex_pc, dex_method_index),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002286 imt_index_(imt_index) {}
2287
Calin Juravle641547a2015-04-21 22:08:51 +01002288 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00002289 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01002290 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00002291 }
2292
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002293 uint32_t GetImtIndex() const { return imt_index_; }
2294 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
2295
2296 DECLARE_INSTRUCTION(InvokeInterface);
2297
2298 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002299 const uint32_t imt_index_;
2300
2301 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
2302};
2303
Dave Allison20dfc792014-06-16 20:44:29 -07002304class HNewInstance : public HExpression<0> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002305 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002306 HNewInstance(uint32_t dex_pc, uint16_t type_index, QuickEntrypointEnum entrypoint)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002307 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2308 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002309 type_index_(type_index),
2310 entrypoint_(entrypoint) {}
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002311
2312 uint32_t GetDexPc() const { return dex_pc_; }
2313 uint16_t GetTypeIndex() const { return type_index_; }
2314
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002315 // Calls runtime so needs an environment.
Calin Juravle92a6ed22014-12-02 18:58:03 +00002316 bool NeedsEnvironment() const OVERRIDE { return true; }
2317 // It may throw when called on:
2318 // - interfaces
2319 // - abstract/innaccessible/unknown classes
2320 // TODO: optimize when possible.
2321 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002322
Calin Juravle10e244f2015-01-26 18:54:32 +00002323 bool CanBeNull() const OVERRIDE { return false; }
2324
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002325 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
2326
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002327 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002328
2329 private:
2330 const uint32_t dex_pc_;
2331 const uint16_t type_index_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002332 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002333
2334 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
2335};
2336
Roland Levillain88cb1752014-10-20 16:36:47 +01002337class HNeg : public HUnaryOperation {
2338 public:
2339 explicit HNeg(Primitive::Type result_type, HInstruction* input)
2340 : HUnaryOperation(result_type, input) {}
2341
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002342 int32_t Evaluate(int32_t x) const OVERRIDE { return -x; }
2343 int64_t Evaluate(int64_t x) const OVERRIDE { return -x; }
Roland Levillain9240d6a2014-10-20 16:47:04 +01002344
Roland Levillain88cb1752014-10-20 16:36:47 +01002345 DECLARE_INSTRUCTION(Neg);
2346
2347 private:
2348 DISALLOW_COPY_AND_ASSIGN(HNeg);
2349};
2350
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002351class HNewArray : public HExpression<1> {
2352 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002353 HNewArray(HInstruction* length,
2354 uint32_t dex_pc,
2355 uint16_t type_index,
2356 QuickEntrypointEnum entrypoint)
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002357 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2358 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002359 type_index_(type_index),
2360 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002361 SetRawInputAt(0, length);
2362 }
2363
2364 uint32_t GetDexPc() const { return dex_pc_; }
2365 uint16_t GetTypeIndex() const { return type_index_; }
2366
2367 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00002368 bool NeedsEnvironment() const OVERRIDE { return true; }
2369
Mingyao Yang0c365e62015-03-31 15:09:29 -07002370 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
2371 bool CanThrow() const OVERRIDE { return true; }
2372
Calin Juravle10e244f2015-01-26 18:54:32 +00002373 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002374
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002375 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
2376
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002377 DECLARE_INSTRUCTION(NewArray);
2378
2379 private:
2380 const uint32_t dex_pc_;
2381 const uint16_t type_index_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002382 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002383
2384 DISALLOW_COPY_AND_ASSIGN(HNewArray);
2385};
2386
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002387class HAdd : public HBinaryOperation {
2388 public:
2389 HAdd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2390 : HBinaryOperation(result_type, left, right) {}
2391
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002392 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002393
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002394 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002395 return x + y;
2396 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002397 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002398 return x + y;
2399 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002400
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002401 DECLARE_INSTRUCTION(Add);
2402
2403 private:
2404 DISALLOW_COPY_AND_ASSIGN(HAdd);
2405};
2406
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002407class HSub : public HBinaryOperation {
2408 public:
2409 HSub(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2410 : HBinaryOperation(result_type, left, right) {}
2411
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002412 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002413 return x - y;
2414 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002415 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002416 return x - y;
2417 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002418
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002419 DECLARE_INSTRUCTION(Sub);
2420
2421 private:
2422 DISALLOW_COPY_AND_ASSIGN(HSub);
2423};
2424
Calin Juravle34bacdf2014-10-07 20:23:36 +01002425class HMul : public HBinaryOperation {
2426 public:
2427 HMul(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2428 : HBinaryOperation(result_type, left, right) {}
2429
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002430 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002431
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002432 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x * y; }
2433 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x * y; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002434
2435 DECLARE_INSTRUCTION(Mul);
2436
2437 private:
2438 DISALLOW_COPY_AND_ASSIGN(HMul);
2439};
2440
Calin Juravle7c4954d2014-10-28 16:57:40 +00002441class HDiv : public HBinaryOperation {
2442 public:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002443 HDiv(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
2444 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00002445
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002446 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00002447 // Our graph structure ensures we never have 0 for `y` during constant folding.
2448 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00002449 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00002450 return (y == -1) ? -x : x / y;
2451 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002452
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002453 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002454 DCHECK_NE(y, 0);
2455 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2456 return (y == -1) ? -x : x / y;
2457 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002458
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002459 uint32_t GetDexPc() const { return dex_pc_; }
2460
Calin Juravle7c4954d2014-10-28 16:57:40 +00002461 DECLARE_INSTRUCTION(Div);
2462
2463 private:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002464 const uint32_t dex_pc_;
2465
Calin Juravle7c4954d2014-10-28 16:57:40 +00002466 DISALLOW_COPY_AND_ASSIGN(HDiv);
2467};
2468
Calin Juravlebacfec32014-11-14 15:54:36 +00002469class HRem : public HBinaryOperation {
2470 public:
2471 HRem(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
2472 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
2473
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002474 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002475 DCHECK_NE(y, 0);
2476 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2477 return (y == -1) ? 0 : x % y;
2478 }
2479
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002480 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002481 DCHECK_NE(y, 0);
2482 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2483 return (y == -1) ? 0 : x % y;
2484 }
2485
2486 uint32_t GetDexPc() const { return dex_pc_; }
2487
2488 DECLARE_INSTRUCTION(Rem);
2489
2490 private:
2491 const uint32_t dex_pc_;
2492
2493 DISALLOW_COPY_AND_ASSIGN(HRem);
2494};
2495
Calin Juravled0d48522014-11-04 16:40:20 +00002496class HDivZeroCheck : public HExpression<1> {
2497 public:
2498 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
2499 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
2500 SetRawInputAt(0, value);
2501 }
2502
2503 bool CanBeMoved() const OVERRIDE { return true; }
2504
2505 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2506 UNUSED(other);
2507 return true;
2508 }
2509
2510 bool NeedsEnvironment() const OVERRIDE { return true; }
2511 bool CanThrow() const OVERRIDE { return true; }
2512
2513 uint32_t GetDexPc() const { return dex_pc_; }
2514
2515 DECLARE_INSTRUCTION(DivZeroCheck);
2516
2517 private:
2518 const uint32_t dex_pc_;
2519
2520 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
2521};
2522
Calin Juravle9aec02f2014-11-18 23:06:35 +00002523class HShl : public HBinaryOperation {
2524 public:
2525 HShl(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2526 : HBinaryOperation(result_type, left, right) {}
2527
2528 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x << (y & kMaxIntShiftValue); }
2529 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x << (y & kMaxLongShiftValue); }
2530
2531 DECLARE_INSTRUCTION(Shl);
2532
2533 private:
2534 DISALLOW_COPY_AND_ASSIGN(HShl);
2535};
2536
2537class HShr : public HBinaryOperation {
2538 public:
2539 HShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2540 : HBinaryOperation(result_type, left, right) {}
2541
2542 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x >> (y & kMaxIntShiftValue); }
2543 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x >> (y & kMaxLongShiftValue); }
2544
2545 DECLARE_INSTRUCTION(Shr);
2546
2547 private:
2548 DISALLOW_COPY_AND_ASSIGN(HShr);
2549};
2550
2551class HUShr : public HBinaryOperation {
2552 public:
2553 HUShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2554 : HBinaryOperation(result_type, left, right) {}
2555
2556 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
2557 uint32_t ux = static_cast<uint32_t>(x);
2558 uint32_t uy = static_cast<uint32_t>(y) & kMaxIntShiftValue;
2559 return static_cast<int32_t>(ux >> uy);
2560 }
2561
2562 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
2563 uint64_t ux = static_cast<uint64_t>(x);
2564 uint64_t uy = static_cast<uint64_t>(y) & kMaxLongShiftValue;
2565 return static_cast<int64_t>(ux >> uy);
2566 }
2567
2568 DECLARE_INSTRUCTION(UShr);
2569
2570 private:
2571 DISALLOW_COPY_AND_ASSIGN(HUShr);
2572};
2573
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002574class HAnd : public HBinaryOperation {
2575 public:
2576 HAnd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2577 : HBinaryOperation(result_type, left, right) {}
2578
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002579 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002580
2581 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x & y; }
2582 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x & y; }
2583
2584 DECLARE_INSTRUCTION(And);
2585
2586 private:
2587 DISALLOW_COPY_AND_ASSIGN(HAnd);
2588};
2589
2590class HOr : public HBinaryOperation {
2591 public:
2592 HOr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2593 : HBinaryOperation(result_type, left, right) {}
2594
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002595 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002596
2597 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x | y; }
2598 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x | y; }
2599
2600 DECLARE_INSTRUCTION(Or);
2601
2602 private:
2603 DISALLOW_COPY_AND_ASSIGN(HOr);
2604};
2605
2606class HXor : public HBinaryOperation {
2607 public:
2608 HXor(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2609 : HBinaryOperation(result_type, left, right) {}
2610
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002611 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002612
2613 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x ^ y; }
2614 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x ^ y; }
2615
2616 DECLARE_INSTRUCTION(Xor);
2617
2618 private:
2619 DISALLOW_COPY_AND_ASSIGN(HXor);
2620};
2621
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002622// The value of a parameter in this method. Its location depends on
2623// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07002624class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002625 public:
Calin Juravle10e244f2015-01-26 18:54:32 +00002626 HParameterValue(uint8_t index, Primitive::Type parameter_type, bool is_this = false)
2627 : HExpression(parameter_type, SideEffects::None()), index_(index), is_this_(is_this) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002628
2629 uint8_t GetIndex() const { return index_; }
2630
Calin Juravle10e244f2015-01-26 18:54:32 +00002631 bool CanBeNull() const OVERRIDE { return !is_this_; }
2632
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002633 DECLARE_INSTRUCTION(ParameterValue);
2634
2635 private:
2636 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00002637 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002638 const uint8_t index_;
2639
Calin Juravle10e244f2015-01-26 18:54:32 +00002640 // Whether or not the parameter value corresponds to 'this' argument.
2641 const bool is_this_;
2642
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002643 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
2644};
2645
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002646class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002647 public:
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002648 explicit HNot(Primitive::Type result_type, HInstruction* input)
2649 : HUnaryOperation(result_type, input) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002650
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002651 bool CanBeMoved() const OVERRIDE { return true; }
2652 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002653 UNUSED(other);
2654 return true;
2655 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002656
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002657 int32_t Evaluate(int32_t x) const OVERRIDE { return ~x; }
2658 int64_t Evaluate(int64_t x) const OVERRIDE { return ~x; }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002659
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002660 DECLARE_INSTRUCTION(Not);
2661
2662 private:
2663 DISALLOW_COPY_AND_ASSIGN(HNot);
2664};
2665
David Brazdil66d126e2015-04-03 16:02:44 +01002666class HBooleanNot : public HUnaryOperation {
2667 public:
2668 explicit HBooleanNot(HInstruction* input)
2669 : HUnaryOperation(Primitive::Type::kPrimBoolean, input) {}
2670
2671 bool CanBeMoved() const OVERRIDE { return true; }
2672 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2673 UNUSED(other);
2674 return true;
2675 }
2676
2677 int32_t Evaluate(int32_t x) const OVERRIDE {
2678 DCHECK(IsUint<1>(x));
2679 return !x;
2680 }
2681
2682 int64_t Evaluate(int64_t x ATTRIBUTE_UNUSED) const OVERRIDE {
2683 LOG(FATAL) << DebugName() << " cannot be used with 64-bit values";
2684 UNREACHABLE();
2685 }
2686
2687 DECLARE_INSTRUCTION(BooleanNot);
2688
2689 private:
2690 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
2691};
2692
Roland Levillaindff1f282014-11-05 14:15:05 +00002693class HTypeConversion : public HExpression<1> {
2694 public:
2695 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00002696 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
2697 : HExpression(result_type, SideEffects::None()), dex_pc_(dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002698 SetRawInputAt(0, input);
2699 DCHECK_NE(input->GetType(), result_type);
2700 }
2701
2702 HInstruction* GetInput() const { return InputAt(0); }
2703 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
2704 Primitive::Type GetResultType() const { return GetType(); }
2705
Roland Levillain624279f2014-12-04 11:54:28 +00002706 // Required by the x86 and ARM code generators when producing calls
2707 // to the runtime.
2708 uint32_t GetDexPc() const { return dex_pc_; }
2709
Roland Levillaindff1f282014-11-05 14:15:05 +00002710 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00002711 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00002712
2713 DECLARE_INSTRUCTION(TypeConversion);
2714
2715 private:
Roland Levillain624279f2014-12-04 11:54:28 +00002716 const uint32_t dex_pc_;
2717
Roland Levillaindff1f282014-11-05 14:15:05 +00002718 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
2719};
2720
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00002721static constexpr uint32_t kNoRegNumber = -1;
2722
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002723class HPhi : public HInstruction {
2724 public:
2725 HPhi(ArenaAllocator* arena, uint32_t reg_number, size_t number_of_inputs, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002726 : HInstruction(SideEffects::None()),
2727 inputs_(arena, number_of_inputs),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002728 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002729 type_(type),
Calin Juravle10e244f2015-01-26 18:54:32 +00002730 is_live_(false),
2731 can_be_null_(true) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002732 inputs_.SetSize(number_of_inputs);
2733 }
2734
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00002735 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
2736 static Primitive::Type ToPhiType(Primitive::Type type) {
2737 switch (type) {
2738 case Primitive::kPrimBoolean:
2739 case Primitive::kPrimByte:
2740 case Primitive::kPrimShort:
2741 case Primitive::kPrimChar:
2742 return Primitive::kPrimInt;
2743 default:
2744 return type;
2745 }
2746 }
2747
Calin Juravle10e244f2015-01-26 18:54:32 +00002748 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002749
2750 void AddInput(HInstruction* input);
2751
Calin Juravle10e244f2015-01-26 18:54:32 +00002752 Primitive::Type GetType() const OVERRIDE { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002753 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002754
Calin Juravle10e244f2015-01-26 18:54:32 +00002755 bool CanBeNull() const OVERRIDE { return can_be_null_; }
2756 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
2757
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002758 uint32_t GetRegNumber() const { return reg_number_; }
2759
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002760 void SetDead() { is_live_ = false; }
2761 void SetLive() { is_live_ = true; }
2762 bool IsDead() const { return !is_live_; }
2763 bool IsLive() const { return is_live_; }
2764
Calin Juravlea4f88312015-04-16 12:57:19 +01002765 // Returns the next equivalent phi (starting from the current one) or null if there is none.
2766 // An equivalent phi is a phi having the same dex register and type.
2767 // It assumes that phis with the same dex register are adjacent.
2768 HPhi* GetNextEquivalentPhiWithSameType() {
2769 HInstruction* next = GetNext();
2770 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
2771 if (next->GetType() == GetType()) {
2772 return next->AsPhi();
2773 }
2774 next = next->GetNext();
2775 }
2776 return nullptr;
2777 }
2778
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002779 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002780
David Brazdil1abb4192015-02-17 18:33:36 +00002781 protected:
2782 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
2783
2784 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
2785 inputs_.Put(index, input);
2786 }
2787
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002788 private:
David Brazdil1abb4192015-02-17 18:33:36 +00002789 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002790 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002791 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002792 bool is_live_;
Calin Juravle10e244f2015-01-26 18:54:32 +00002793 bool can_be_null_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002794
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002795 DISALLOW_COPY_AND_ASSIGN(HPhi);
2796};
2797
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002798class HNullCheck : public HExpression<1> {
2799 public:
2800 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002801 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002802 SetRawInputAt(0, value);
2803 }
2804
Calin Juravle10e244f2015-01-26 18:54:32 +00002805 bool CanBeMoved() const OVERRIDE { return true; }
2806 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002807 UNUSED(other);
2808 return true;
2809 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002810
Calin Juravle10e244f2015-01-26 18:54:32 +00002811 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002812
Calin Juravle10e244f2015-01-26 18:54:32 +00002813 bool CanThrow() const OVERRIDE { return true; }
2814
2815 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01002816
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002817 uint32_t GetDexPc() const { return dex_pc_; }
2818
2819 DECLARE_INSTRUCTION(NullCheck);
2820
2821 private:
2822 const uint32_t dex_pc_;
2823
2824 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
2825};
2826
2827class FieldInfo : public ValueObject {
2828 public:
Calin Juravle52c48962014-12-16 17:02:57 +00002829 FieldInfo(MemberOffset field_offset, Primitive::Type field_type, bool is_volatile)
2830 : field_offset_(field_offset), field_type_(field_type), is_volatile_(is_volatile) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002831
2832 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002833 Primitive::Type GetFieldType() const { return field_type_; }
Calin Juravle52c48962014-12-16 17:02:57 +00002834 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002835
2836 private:
2837 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01002838 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00002839 const bool is_volatile_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002840};
2841
2842class HInstanceFieldGet : public HExpression<1> {
2843 public:
2844 HInstanceFieldGet(HInstruction* value,
2845 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00002846 MemberOffset field_offset,
2847 bool is_volatile)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002848 : HExpression(field_type, SideEffects::DependsOnSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00002849 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002850 SetRawInputAt(0, value);
2851 }
2852
Calin Juravle10c9cbe2014-12-19 10:50:19 +00002853 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002854
2855 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2856 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
2857 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002858 }
2859
Calin Juravle641547a2015-04-21 22:08:51 +01002860 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
2861 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00002862 }
2863
2864 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002865 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
2866 }
2867
Calin Juravle52c48962014-12-16 17:02:57 +00002868 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002869 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002870 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002871 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002872
2873 DECLARE_INSTRUCTION(InstanceFieldGet);
2874
2875 private:
2876 const FieldInfo field_info_;
2877
2878 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
2879};
2880
2881class HInstanceFieldSet : public HTemplateInstruction<2> {
2882 public:
2883 HInstanceFieldSet(HInstruction* object,
2884 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01002885 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00002886 MemberOffset field_offset,
2887 bool is_volatile)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002888 : HTemplateInstruction(SideEffects::ChangesSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00002889 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002890 SetRawInputAt(0, object);
2891 SetRawInputAt(1, value);
2892 }
2893
Calin Juravle641547a2015-04-21 22:08:51 +01002894 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
2895 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00002896 }
2897
Calin Juravle52c48962014-12-16 17:02:57 +00002898 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002899 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002900 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002901 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002902 HInstruction* GetValue() const { return InputAt(1); }
2903
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002904 DECLARE_INSTRUCTION(InstanceFieldSet);
2905
2906 private:
2907 const FieldInfo field_info_;
2908
2909 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
2910};
2911
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002912class HArrayGet : public HExpression<2> {
2913 public:
2914 HArrayGet(HInstruction* array, HInstruction* index, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002915 : HExpression(type, SideEffects::DependsOnSomething()) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002916 SetRawInputAt(0, array);
2917 SetRawInputAt(1, index);
2918 }
2919
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002920 bool CanBeMoved() const OVERRIDE { return true; }
2921 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002922 UNUSED(other);
2923 return true;
2924 }
Calin Juravle641547a2015-04-21 22:08:51 +01002925 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
2926 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00002927 // TODO: We can be smarter here.
2928 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
2929 // which generates the implicit null check. There are cases when these can be removed
2930 // to produce better code. If we ever add optimizations to do so we should allow an
2931 // implicit check here (as long as the address falls in the first page).
2932 return false;
2933 }
2934
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002935 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002936
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002937 HInstruction* GetArray() const { return InputAt(0); }
2938 HInstruction* GetIndex() const { return InputAt(1); }
2939
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002940 DECLARE_INSTRUCTION(ArrayGet);
2941
2942 private:
2943 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
2944};
2945
2946class HArraySet : public HTemplateInstruction<3> {
2947 public:
2948 HArraySet(HInstruction* array,
2949 HInstruction* index,
2950 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002951 Primitive::Type expected_component_type,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002952 uint32_t dex_pc)
2953 : HTemplateInstruction(SideEffects::ChangesSomething()),
2954 dex_pc_(dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002955 expected_component_type_(expected_component_type),
2956 needs_type_check_(value->GetType() == Primitive::kPrimNot) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002957 SetRawInputAt(0, array);
2958 SetRawInputAt(1, index);
2959 SetRawInputAt(2, value);
2960 }
2961
Calin Juravle77520bc2015-01-12 18:45:46 +00002962 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002963 // We currently always call a runtime method to catch array store
2964 // exceptions.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002965 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002966 }
2967
Calin Juravle641547a2015-04-21 22:08:51 +01002968 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
2969 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00002970 // TODO: Same as for ArrayGet.
2971 return false;
2972 }
2973
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002974 void ClearNeedsTypeCheck() {
2975 needs_type_check_ = false;
2976 }
2977
2978 bool NeedsTypeCheck() const { return needs_type_check_; }
2979
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002980 uint32_t GetDexPc() const { return dex_pc_; }
2981
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002982 HInstruction* GetArray() const { return InputAt(0); }
2983 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002984 HInstruction* GetValue() const { return InputAt(2); }
2985
2986 Primitive::Type GetComponentType() const {
2987 // The Dex format does not type floating point index operations. Since the
2988 // `expected_component_type_` is set during building and can therefore not
2989 // be correct, we also check what is the value type. If it is a floating
2990 // point type, we must use that type.
2991 Primitive::Type value_type = GetValue()->GetType();
2992 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
2993 ? value_type
2994 : expected_component_type_;
2995 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002996
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002997 DECLARE_INSTRUCTION(ArraySet);
2998
2999 private:
3000 const uint32_t dex_pc_;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003001 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003002 bool needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003003
3004 DISALLOW_COPY_AND_ASSIGN(HArraySet);
3005};
3006
3007class HArrayLength : public HExpression<1> {
3008 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003009 explicit HArrayLength(HInstruction* array)
3010 : HExpression(Primitive::kPrimInt, SideEffects::None()) {
3011 // Note that arrays do not change length, so the instruction does not
3012 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003013 SetRawInputAt(0, array);
3014 }
3015
Calin Juravle77520bc2015-01-12 18:45:46 +00003016 bool CanBeMoved() const OVERRIDE { return true; }
3017 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003018 UNUSED(other);
3019 return true;
3020 }
Calin Juravle641547a2015-04-21 22:08:51 +01003021 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3022 return obj == InputAt(0);
3023 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003024
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003025 DECLARE_INSTRUCTION(ArrayLength);
3026
3027 private:
3028 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
3029};
3030
3031class HBoundsCheck : public HExpression<2> {
3032 public:
3033 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003034 : HExpression(index->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003035 DCHECK(index->GetType() == Primitive::kPrimInt);
3036 SetRawInputAt(0, index);
3037 SetRawInputAt(1, length);
3038 }
3039
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003040 bool CanBeMoved() const OVERRIDE { return true; }
3041 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003042 UNUSED(other);
3043 return true;
3044 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003045
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003046 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003047
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003048 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01003049
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003050 uint32_t GetDexPc() const { return dex_pc_; }
3051
3052 DECLARE_INSTRUCTION(BoundsCheck);
3053
3054 private:
3055 const uint32_t dex_pc_;
3056
3057 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
3058};
3059
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003060/**
3061 * Some DEX instructions are folded into multiple HInstructions that need
3062 * to stay live until the last HInstruction. This class
3063 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00003064 * HInstruction stays live. `index` represents the stack location index of the
3065 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003066 */
3067class HTemporary : public HTemplateInstruction<0> {
3068 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003069 explicit HTemporary(size_t index) : HTemplateInstruction(SideEffects::None()), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003070
3071 size_t GetIndex() const { return index_; }
3072
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00003073 Primitive::Type GetType() const OVERRIDE {
3074 // The previous instruction is the one that will be stored in the temporary location.
3075 DCHECK(GetPrevious() != nullptr);
3076 return GetPrevious()->GetType();
3077 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00003078
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003079 DECLARE_INSTRUCTION(Temporary);
3080
3081 private:
3082 const size_t index_;
3083
3084 DISALLOW_COPY_AND_ASSIGN(HTemporary);
3085};
3086
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003087class HSuspendCheck : public HTemplateInstruction<0> {
3088 public:
3089 explicit HSuspendCheck(uint32_t dex_pc)
Nicolas Geoffray9ebc72c2014-09-25 16:33:42 +01003090 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003091
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003092 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003093 return true;
3094 }
3095
3096 uint32_t GetDexPc() const { return dex_pc_; }
3097
3098 DECLARE_INSTRUCTION(SuspendCheck);
3099
3100 private:
3101 const uint32_t dex_pc_;
3102
3103 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
3104};
3105
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003106/**
3107 * Instruction to load a Class object.
3108 */
3109class HLoadClass : public HExpression<0> {
3110 public:
3111 HLoadClass(uint16_t type_index,
3112 bool is_referrers_class,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003113 uint32_t dex_pc)
3114 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3115 type_index_(type_index),
3116 is_referrers_class_(is_referrers_class),
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003117 dex_pc_(dex_pc),
Calin Juravleb1498f62015-02-16 13:13:29 +00003118 generate_clinit_check_(false),
3119 loaded_class_rti_(ReferenceTypeInfo::CreateTop(/* is_exact */ false)) {}
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003120
3121 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003122
3123 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3124 return other->AsLoadClass()->type_index_ == type_index_;
3125 }
3126
3127 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
3128
3129 uint32_t GetDexPc() const { return dex_pc_; }
3130 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003131 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003132
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003133 bool NeedsEnvironment() const OVERRIDE {
3134 // Will call runtime and load the class if the class is not loaded yet.
3135 // TODO: finer grain decision.
3136 return !is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003137 }
3138
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003139 bool MustGenerateClinitCheck() const {
3140 return generate_clinit_check_;
3141 }
3142
3143 void SetMustGenerateClinitCheck() {
3144 generate_clinit_check_ = true;
3145 }
3146
3147 bool CanCallRuntime() const {
3148 return MustGenerateClinitCheck() || !is_referrers_class_;
3149 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003150
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003151 bool CanThrow() const OVERRIDE {
3152 // May call runtime and and therefore can throw.
3153 // TODO: finer grain decision.
3154 return !is_referrers_class_;
3155 }
3156
Calin Juravleacf735c2015-02-12 15:25:22 +00003157 ReferenceTypeInfo GetLoadedClassRTI() {
3158 return loaded_class_rti_;
3159 }
3160
3161 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
3162 // Make sure we only set exact types (the loaded class should never be merged).
3163 DCHECK(rti.IsExact());
3164 loaded_class_rti_ = rti;
3165 }
3166
3167 bool IsResolved() {
3168 return loaded_class_rti_.IsExact();
3169 }
3170
Nicolas Geoffray9437b782015-03-25 10:08:51 +00003171 bool NeedsDexCache() const OVERRIDE { return !is_referrers_class_; }
3172
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003173 DECLARE_INSTRUCTION(LoadClass);
3174
3175 private:
3176 const uint16_t type_index_;
3177 const bool is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003178 const uint32_t dex_pc_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003179 // Whether this instruction must generate the initialization check.
3180 // Used for code generation.
3181 bool generate_clinit_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003182
Calin Juravleacf735c2015-02-12 15:25:22 +00003183 ReferenceTypeInfo loaded_class_rti_;
3184
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003185 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
3186};
3187
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003188class HLoadString : public HExpression<0> {
3189 public:
3190 HLoadString(uint32_t string_index, uint32_t dex_pc)
3191 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3192 string_index_(string_index),
3193 dex_pc_(dex_pc) {}
3194
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003195 bool CanBeMoved() const OVERRIDE { return true; }
3196
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003197 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3198 return other->AsLoadString()->string_index_ == string_index_;
3199 }
3200
3201 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
3202
3203 uint32_t GetDexPc() const { return dex_pc_; }
3204 uint32_t GetStringIndex() const { return string_index_; }
3205
3206 // TODO: Can we deopt or debug when we resolve a string?
3207 bool NeedsEnvironment() const OVERRIDE { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00003208 bool NeedsDexCache() const OVERRIDE { return true; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003209
3210 DECLARE_INSTRUCTION(LoadString);
3211
3212 private:
3213 const uint32_t string_index_;
3214 const uint32_t dex_pc_;
3215
3216 DISALLOW_COPY_AND_ASSIGN(HLoadString);
3217};
3218
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003219// TODO: Pass this check to HInvokeStaticOrDirect nodes.
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003220/**
3221 * Performs an initialization check on its Class object input.
3222 */
3223class HClinitCheck : public HExpression<1> {
3224 public:
3225 explicit HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Nicolas Geoffraya0466e12015-03-27 15:00:40 +00003226 : HExpression(Primitive::kPrimNot, SideEffects::ChangesSomething()),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003227 dex_pc_(dex_pc) {
3228 SetRawInputAt(0, constant);
3229 }
3230
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003231 bool CanBeMoved() const OVERRIDE { return true; }
3232 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3233 UNUSED(other);
3234 return true;
3235 }
3236
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003237 bool NeedsEnvironment() const OVERRIDE {
3238 // May call runtime to initialize the class.
3239 return true;
3240 }
3241
3242 uint32_t GetDexPc() const { return dex_pc_; }
3243
3244 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
3245
3246 DECLARE_INSTRUCTION(ClinitCheck);
3247
3248 private:
3249 const uint32_t dex_pc_;
3250
3251 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
3252};
3253
3254class HStaticFieldGet : public HExpression<1> {
3255 public:
3256 HStaticFieldGet(HInstruction* cls,
3257 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003258 MemberOffset field_offset,
3259 bool is_volatile)
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003260 : HExpression(field_type, SideEffects::DependsOnSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00003261 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003262 SetRawInputAt(0, cls);
3263 }
3264
Calin Juravle52c48962014-12-16 17:02:57 +00003265
Calin Juravle10c9cbe2014-12-19 10:50:19 +00003266 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003267
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003268 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00003269 HStaticFieldGet* other_get = other->AsStaticFieldGet();
3270 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003271 }
3272
3273 size_t ComputeHashCode() const OVERRIDE {
3274 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
3275 }
3276
Calin Juravle52c48962014-12-16 17:02:57 +00003277 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003278 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
3279 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003280 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003281
3282 DECLARE_INSTRUCTION(StaticFieldGet);
3283
3284 private:
3285 const FieldInfo field_info_;
3286
3287 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
3288};
3289
3290class HStaticFieldSet : public HTemplateInstruction<2> {
3291 public:
3292 HStaticFieldSet(HInstruction* cls,
3293 HInstruction* value,
3294 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003295 MemberOffset field_offset,
3296 bool is_volatile)
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003297 : HTemplateInstruction(SideEffects::ChangesSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00003298 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003299 SetRawInputAt(0, cls);
3300 SetRawInputAt(1, value);
3301 }
3302
Calin Juravle52c48962014-12-16 17:02:57 +00003303 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003304 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
3305 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003306 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003307
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003308 HInstruction* GetValue() const { return InputAt(1); }
3309
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003310 DECLARE_INSTRUCTION(StaticFieldSet);
3311
3312 private:
3313 const FieldInfo field_info_;
3314
3315 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
3316};
3317
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003318// Implement the move-exception DEX instruction.
3319class HLoadException : public HExpression<0> {
3320 public:
3321 HLoadException() : HExpression(Primitive::kPrimNot, SideEffects::None()) {}
3322
3323 DECLARE_INSTRUCTION(LoadException);
3324
3325 private:
3326 DISALLOW_COPY_AND_ASSIGN(HLoadException);
3327};
3328
3329class HThrow : public HTemplateInstruction<1> {
3330 public:
3331 HThrow(HInstruction* exception, uint32_t dex_pc)
3332 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {
3333 SetRawInputAt(0, exception);
3334 }
3335
3336 bool IsControlFlow() const OVERRIDE { return true; }
3337
3338 bool NeedsEnvironment() const OVERRIDE { return true; }
3339
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003340 bool CanThrow() const OVERRIDE { return true; }
3341
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003342 uint32_t GetDexPc() const { return dex_pc_; }
3343
3344 DECLARE_INSTRUCTION(Throw);
3345
3346 private:
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003347 const uint32_t dex_pc_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003348
3349 DISALLOW_COPY_AND_ASSIGN(HThrow);
3350};
3351
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003352class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003353 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003354 HInstanceOf(HInstruction* object,
3355 HLoadClass* constant,
3356 bool class_is_final,
3357 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003358 : HExpression(Primitive::kPrimBoolean, SideEffects::None()),
3359 class_is_final_(class_is_final),
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003360 must_do_null_check_(true),
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003361 dex_pc_(dex_pc) {
3362 SetRawInputAt(0, object);
3363 SetRawInputAt(1, constant);
3364 }
3365
3366 bool CanBeMoved() const OVERRIDE { return true; }
3367
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003368 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003369 return true;
3370 }
3371
3372 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003373 return false;
3374 }
3375
3376 uint32_t GetDexPc() const { return dex_pc_; }
3377
3378 bool IsClassFinal() const { return class_is_final_; }
3379
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003380 // Used only in code generation.
3381 bool MustDoNullCheck() const { return must_do_null_check_; }
3382 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
3383
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003384 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003385
3386 private:
3387 const bool class_is_final_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003388 bool must_do_null_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003389 const uint32_t dex_pc_;
3390
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003391 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
3392};
3393
Calin Juravleb1498f62015-02-16 13:13:29 +00003394class HBoundType : public HExpression<1> {
3395 public:
3396 HBoundType(HInstruction* input, ReferenceTypeInfo bound_type)
3397 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3398 bound_type_(bound_type) {
Calin Juravle61d544b2015-02-23 16:46:57 +00003399 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00003400 SetRawInputAt(0, input);
3401 }
3402
3403 const ReferenceTypeInfo& GetBoundType() const { return bound_type_; }
3404
3405 bool CanBeNull() const OVERRIDE {
3406 // `null instanceof ClassX` always return false so we can't be null.
3407 return false;
3408 }
3409
3410 DECLARE_INSTRUCTION(BoundType);
3411
3412 private:
3413 // Encodes the most upper class that this instruction can have. In other words
3414 // it is always the case that GetBoundType().IsSupertypeOf(GetReferenceType()).
3415 // It is used to bound the type in cases like `if (x instanceof ClassX) {}`
3416 const ReferenceTypeInfo bound_type_;
3417
3418 DISALLOW_COPY_AND_ASSIGN(HBoundType);
3419};
3420
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003421class HCheckCast : public HTemplateInstruction<2> {
3422 public:
3423 HCheckCast(HInstruction* object,
3424 HLoadClass* constant,
3425 bool class_is_final,
3426 uint32_t dex_pc)
3427 : HTemplateInstruction(SideEffects::None()),
3428 class_is_final_(class_is_final),
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003429 must_do_null_check_(true),
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003430 dex_pc_(dex_pc) {
3431 SetRawInputAt(0, object);
3432 SetRawInputAt(1, constant);
3433 }
3434
3435 bool CanBeMoved() const OVERRIDE { return true; }
3436
3437 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
3438 return true;
3439 }
3440
3441 bool NeedsEnvironment() const OVERRIDE {
3442 // Instruction may throw a CheckCastError.
3443 return true;
3444 }
3445
3446 bool CanThrow() const OVERRIDE { return true; }
3447
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003448 bool MustDoNullCheck() const { return must_do_null_check_; }
3449 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
3450
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003451 uint32_t GetDexPc() const { return dex_pc_; }
3452
3453 bool IsClassFinal() const { return class_is_final_; }
3454
3455 DECLARE_INSTRUCTION(CheckCast);
3456
3457 private:
3458 const bool class_is_final_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003459 bool must_do_null_check_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003460 const uint32_t dex_pc_;
3461
3462 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003463};
3464
Calin Juravle27df7582015-04-17 19:12:31 +01003465class HMemoryBarrier : public HTemplateInstruction<0> {
3466 public:
3467 explicit HMemoryBarrier(MemBarrierKind barrier_kind)
3468 : HTemplateInstruction(SideEffects::None()),
3469 barrier_kind_(barrier_kind) {}
3470
3471 MemBarrierKind GetBarrierKind() { return barrier_kind_; }
3472
3473 DECLARE_INSTRUCTION(MemoryBarrier);
3474
3475 private:
3476 const MemBarrierKind barrier_kind_;
3477
3478 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
3479};
3480
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003481class HMonitorOperation : public HTemplateInstruction<1> {
3482 public:
3483 enum OperationKind {
3484 kEnter,
3485 kExit,
3486 };
3487
3488 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
3489 : HTemplateInstruction(SideEffects::None()), kind_(kind), dex_pc_(dex_pc) {
3490 SetRawInputAt(0, object);
3491 }
3492
3493 // Instruction may throw a Java exception, so we need an environment.
3494 bool NeedsEnvironment() const OVERRIDE { return true; }
3495 bool CanThrow() const OVERRIDE { return true; }
3496
3497 uint32_t GetDexPc() const { return dex_pc_; }
3498
3499 bool IsEnter() const { return kind_ == kEnter; }
3500
3501 DECLARE_INSTRUCTION(MonitorOperation);
3502
Calin Juravle52c48962014-12-16 17:02:57 +00003503 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003504 const OperationKind kind_;
3505 const uint32_t dex_pc_;
3506
3507 private:
3508 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
3509};
3510
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003511class MoveOperands : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003512 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01003513 MoveOperands(Location source,
3514 Location destination,
3515 Primitive::Type type,
3516 HInstruction* instruction)
3517 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003518
3519 Location GetSource() const { return source_; }
3520 Location GetDestination() const { return destination_; }
3521
3522 void SetSource(Location value) { source_ = value; }
3523 void SetDestination(Location value) { destination_ = value; }
3524
3525 // The parallel move resolver marks moves as "in-progress" by clearing the
3526 // destination (but not the source).
3527 Location MarkPending() {
3528 DCHECK(!IsPending());
3529 Location dest = destination_;
3530 destination_ = Location::NoLocation();
3531 return dest;
3532 }
3533
3534 void ClearPending(Location dest) {
3535 DCHECK(IsPending());
3536 destination_ = dest;
3537 }
3538
3539 bool IsPending() const {
3540 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
3541 return destination_.IsInvalid() && !source_.IsInvalid();
3542 }
3543
3544 // True if this blocks a move from the given location.
3545 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08003546 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003547 }
3548
3549 // A move is redundant if it's been eliminated, if its source and
3550 // destination are the same, or if its destination is unneeded.
3551 bool IsRedundant() const {
3552 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
3553 }
3554
3555 // We clear both operands to indicate move that's been eliminated.
3556 void Eliminate() {
3557 source_ = destination_ = Location::NoLocation();
3558 }
3559
3560 bool IsEliminated() const {
3561 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
3562 return source_.IsInvalid();
3563 }
3564
Nicolas Geoffray90218252015-04-15 11:56:51 +01003565 bool Is64BitMove() const {
3566 return Primitive::Is64BitType(type_);
3567 }
3568
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003569 HInstruction* GetInstruction() const { return instruction_; }
3570
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003571 private:
3572 Location source_;
3573 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01003574 // The type this move is for.
3575 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003576 // The instruction this move is assocatied with. Null when this move is
3577 // for moving an input in the expected locations of user (including a phi user).
3578 // This is only used in debug mode, to ensure we do not connect interval siblings
3579 // in the same parallel move.
3580 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003581};
3582
3583static constexpr size_t kDefaultNumberOfMoves = 4;
3584
3585class HParallelMove : public HTemplateInstruction<0> {
3586 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003587 explicit HParallelMove(ArenaAllocator* arena)
3588 : HTemplateInstruction(SideEffects::None()), moves_(arena, kDefaultNumberOfMoves) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003589
Nicolas Geoffray90218252015-04-15 11:56:51 +01003590 void AddMove(Location source,
3591 Location destination,
3592 Primitive::Type type,
3593 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003594 DCHECK(source.IsValid());
3595 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003596 if (kIsDebugBuild) {
3597 if (instruction != nullptr) {
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00003598 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003599 if (moves_.Get(i).GetInstruction() == instruction) {
3600 // Special case the situation where the move is for the spill slot
3601 // of the instruction.
3602 if ((GetPrevious() == instruction)
3603 || ((GetPrevious() == nullptr)
3604 && instruction->IsPhi()
3605 && instruction->GetBlock() == GetBlock())) {
3606 DCHECK_NE(destination.GetKind(), moves_.Get(i).GetDestination().GetKind())
3607 << "Doing parallel moves for the same instruction.";
3608 } else {
3609 DCHECK(false) << "Doing parallel moves for the same instruction.";
3610 }
3611 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00003612 }
3613 }
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003614 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Zheng Xuad4450e2015-04-17 18:48:56 +08003615 DCHECK(!destination.OverlapsWith(moves_.Get(i).GetDestination()))
3616 << "Overlapped destination for two moves in a parallel move.";
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003617 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003618 }
Nicolas Geoffray90218252015-04-15 11:56:51 +01003619 moves_.Add(MoveOperands(source, destination, type, instruction));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003620 }
3621
3622 MoveOperands* MoveOperandsAt(size_t index) const {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003623 return moves_.GetRawStorage() + index;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003624 }
3625
3626 size_t NumMoves() const { return moves_.Size(); }
3627
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003628 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003629
3630 private:
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003631 GrowableArray<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003632
3633 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
3634};
3635
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003636class HGraphVisitor : public ValueObject {
3637 public:
Dave Allison20dfc792014-06-16 20:44:29 -07003638 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
3639 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003640
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003641 virtual void VisitInstruction(HInstruction* instruction) { UNUSED(instruction); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003642 virtual void VisitBasicBlock(HBasicBlock* block);
3643
Roland Levillain633021e2014-10-01 14:12:25 +01003644 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003645 void VisitInsertionOrder();
3646
Roland Levillain633021e2014-10-01 14:12:25 +01003647 // Visit the graph following dominator tree reverse post-order.
3648 void VisitReversePostOrder();
3649
Nicolas Geoffray787c3072014-03-17 10:20:19 +00003650 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00003651
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003652 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003653#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003654 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
3655
3656 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
3657
3658#undef DECLARE_VISIT_INSTRUCTION
3659
3660 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07003661 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003662
3663 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
3664};
3665
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003666class HGraphDelegateVisitor : public HGraphVisitor {
3667 public:
3668 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
3669 virtual ~HGraphDelegateVisitor() {}
3670
3671 // Visit functions that delegate to to super class.
3672#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003673 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003674
3675 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
3676
3677#undef DECLARE_VISIT_INSTRUCTION
3678
3679 private:
3680 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
3681};
3682
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003683class HInsertionOrderIterator : public ValueObject {
3684 public:
3685 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
3686
3687 bool Done() const { return index_ == graph_.GetBlocks().Size(); }
3688 HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); }
3689 void Advance() { ++index_; }
3690
3691 private:
3692 const HGraph& graph_;
3693 size_t index_;
3694
3695 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
3696};
3697
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003698class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003699 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00003700 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
3701 // Check that reverse post order of the graph has been built.
3702 DCHECK(!graph.GetReversePostOrder().IsEmpty());
3703 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003704
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003705 bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); }
3706 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003707 void Advance() { ++index_; }
3708
3709 private:
3710 const HGraph& graph_;
3711 size_t index_;
3712
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003713 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003714};
3715
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003716class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003717 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003718 explicit HPostOrderIterator(const HGraph& graph)
David Brazdil10f56cb2015-03-24 18:49:14 +00003719 : graph_(graph), index_(graph_.GetReversePostOrder().Size()) {
3720 // Check that reverse post order of the graph has been built.
3721 DCHECK(!graph.GetReversePostOrder().IsEmpty());
3722 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003723
3724 bool Done() const { return index_ == 0; }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003725 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003726 void Advance() { --index_; }
3727
3728 private:
3729 const HGraph& graph_;
3730 size_t index_;
3731
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003732 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003733};
3734
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01003735class HLinearPostOrderIterator : public ValueObject {
3736 public:
3737 explicit HLinearPostOrderIterator(const HGraph& graph)
3738 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().Size()) {}
3739
3740 bool Done() const { return index_ == 0; }
3741
3742 HBasicBlock* Current() const { return order_.Get(index_ -1); }
3743
3744 void Advance() {
3745 --index_;
3746 DCHECK_GE(index_, 0U);
3747 }
3748
3749 private:
3750 const GrowableArray<HBasicBlock*>& order_;
3751 size_t index_;
3752
3753 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
3754};
3755
3756class HLinearOrderIterator : public ValueObject {
3757 public:
3758 explicit HLinearOrderIterator(const HGraph& graph)
3759 : order_(graph.GetLinearOrder()), index_(0) {}
3760
3761 bool Done() const { return index_ == order_.Size(); }
3762 HBasicBlock* Current() const { return order_.Get(index_); }
3763 void Advance() { ++index_; }
3764
3765 private:
3766 const GrowableArray<HBasicBlock*>& order_;
3767 size_t index_;
3768
3769 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
3770};
3771
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003772// Iterator over the blocks that art part of the loop. Includes blocks part
3773// of an inner loop. The order in which the blocks are iterated is on their
3774// block id.
3775class HBlocksInLoopIterator : public ValueObject {
3776 public:
3777 explicit HBlocksInLoopIterator(const HLoopInformation& info)
3778 : blocks_in_loop_(info.GetBlocks()),
3779 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
3780 index_(0) {
3781 if (!blocks_in_loop_.IsBitSet(index_)) {
3782 Advance();
3783 }
3784 }
3785
3786 bool Done() const { return index_ == blocks_.Size(); }
3787 HBasicBlock* Current() const { return blocks_.Get(index_); }
3788 void Advance() {
3789 ++index_;
3790 for (size_t e = blocks_.Size(); index_ < e; ++index_) {
3791 if (blocks_in_loop_.IsBitSet(index_)) {
3792 break;
3793 }
3794 }
3795 }
3796
3797 private:
3798 const BitVector& blocks_in_loop_;
3799 const GrowableArray<HBasicBlock*>& blocks_;
3800 size_t index_;
3801
3802 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
3803};
3804
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003805inline int64_t Int64FromConstant(HConstant* constant) {
3806 DCHECK(constant->IsIntConstant() || constant->IsLongConstant());
3807 return constant->IsIntConstant() ? constant->AsIntConstant()->GetValue()
3808 : constant->AsLongConstant()->GetValue();
3809}
3810
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003811} // namespace art
3812
3813#endif // ART_COMPILER_OPTIMIZING_NODES_H_