blob: 1a24cb516b271dac82f151adeb37bd3a383429ce [file] [log] [blame]
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ART_COMPILER_OPTIMIZING_NODES_H_
18#define ART_COMPILER_OPTIMIZING_NODES_H_
19
David Brazdil8d5b8b22015-03-24 10:51:52 +000020#include "base/arena_containers.h"
Mathieu Chartierb666f482015-02-18 14:33:14 -080021#include "base/arena_object.h"
Calin Juravle27df7582015-04-17 19:12:31 +010022#include "dex/compiler_enums.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000023#include "entrypoints/quick/quick_entrypoints_enum.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000024#include "handle.h"
25#include "handle_scope.h"
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000026#include "invoke_type.h"
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +010027#include "locations.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000028#include "mirror/class.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010029#include "offsets.h"
Ian Rogerse63db272014-07-15 15:36:11 -070030#include "primitive.h"
Nicolas Geoffray0e336432014-02-26 18:24:38 +000031#include "utils/arena_bit_vector.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000032#include "utils/growable_array.h"
33
34namespace art {
35
David Brazdil1abb4192015-02-17 18:33:36 +000036class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000037class HBasicBlock;
David Brazdil8d5b8b22015-03-24 10:51:52 +000038class HDoubleConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010039class HEnvironment;
David Brazdil8d5b8b22015-03-24 10:51:52 +000040class HFloatConstant;
41class HGraphVisitor;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000042class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000043class HIntConstant;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000044class HInvoke;
David Brazdil8d5b8b22015-03-24 10:51:52 +000045class HLongConstant;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +000046class HNullConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010047class HPhi;
Nicolas Geoffray3c049742014-09-24 18:10:46 +010048class HSuspendCheck;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010049class LiveInterval;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000050class LocationSummary;
David Brazdil8d5b8b22015-03-24 10:51:52 +000051class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000052
53static const int kDefaultNumberOfBlocks = 8;
54static const int kDefaultNumberOfSuccessors = 2;
55static const int kDefaultNumberOfPredecessors = 2;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010056static const int kDefaultNumberOfDominatedBlocks = 1;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000057static const int kDefaultNumberOfBackEdges = 1;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000058
Calin Juravle9aec02f2014-11-18 23:06:35 +000059static constexpr uint32_t kMaxIntShiftValue = 0x1f;
60static constexpr uint64_t kMaxLongShiftValue = 0x3f;
61
Dave Allison20dfc792014-06-16 20:44:29 -070062enum IfCondition {
63 kCondEQ,
64 kCondNE,
65 kCondLT,
66 kCondLE,
67 kCondGT,
68 kCondGE,
69};
70
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010071class HInstructionList {
72 public:
73 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
74
75 void AddInstruction(HInstruction* instruction);
76 void RemoveInstruction(HInstruction* instruction);
77
David Brazdilc3d743f2015-04-22 13:40:50 +010078 // Insert `instruction` before/after an existing instruction `cursor`.
79 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
80 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
81
Roland Levillain6b469232014-09-25 10:10:38 +010082 // Return true if this list contains `instruction`.
83 bool Contains(HInstruction* instruction) const;
84
Roland Levillainccc07a92014-09-16 14:48:16 +010085 // Return true if `instruction1` is found before `instruction2` in
86 // this instruction list and false otherwise. Abort if none
87 // of these instructions is found.
88 bool FoundBefore(const HInstruction* instruction1,
89 const HInstruction* instruction2) const;
90
Nicolas Geoffray276d9da2015-02-02 18:24:11 +000091 bool IsEmpty() const { return first_instruction_ == nullptr; }
92 void Clear() { first_instruction_ = last_instruction_ = nullptr; }
93
94 // Update the block of all instructions to be `block`.
95 void SetBlockOfInstructions(HBasicBlock* block) const;
96
97 void AddAfter(HInstruction* cursor, const HInstructionList& instruction_list);
98 void Add(const HInstructionList& instruction_list);
99
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100100 private:
101 HInstruction* first_instruction_;
102 HInstruction* last_instruction_;
103
104 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000105 friend class HGraph;
106 friend class HInstruction;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100107 friend class HInstructionIterator;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100108 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100109
110 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
111};
112
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000113// Control-flow graph of a method. Contains a list of basic blocks.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700114class HGraph : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000115 public:
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000116 HGraph(ArenaAllocator* arena, bool debuggable = false, int start_instruction_id = 0)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000117 : arena_(arena),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000118 blocks_(arena, kDefaultNumberOfBlocks),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100119 reverse_post_order_(arena, kDefaultNumberOfBlocks),
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100120 linear_order_(arena, kDefaultNumberOfBlocks),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700121 entry_block_(nullptr),
122 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100123 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100124 number_of_vregs_(0),
125 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000126 temporaries_vreg_slots_(0),
Mingyao Yange4335eb2015-03-02 15:14:13 -0800127 has_array_accesses_(false),
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000128 debuggable_(debuggable),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000129 current_instruction_id_(start_instruction_id),
130 cached_null_constant_(nullptr),
131 cached_int_constants_(std::less<int32_t>(), arena->Adapter()),
132 cached_long_constants_(std::less<int64_t>(), arena->Adapter()) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000133
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000134 ArenaAllocator* GetArena() const { return arena_; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100135 const GrowableArray<HBasicBlock*>& GetBlocks() const { return blocks_; }
Roland Levillain93445682014-10-06 19:24:02 +0100136 HBasicBlock* GetBlock(size_t id) const { return blocks_.Get(id); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000137
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000138 HBasicBlock* GetEntryBlock() const { return entry_block_; }
139 HBasicBlock* GetExitBlock() const { return exit_block_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000140
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000141 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
142 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000143
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000144 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100145
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000146 // Try building the SSA form of this graph, with dominance computation and loop
147 // recognition. Returns whether it was successful in doing all these steps.
148 bool TryBuildingSsa() {
149 BuildDominatorTree();
Nicolas Geoffrayd3350832015-03-12 11:16:23 +0000150 // The SSA builder requires loops to all be natural. Specifically, the dead phi
151 // elimination phase checks the consistency of the graph when doing a post-order
152 // visit for eliminating dead phis: a dead phi can only have loop header phi
153 // users remaining when being visited.
154 if (!AnalyzeNaturalLoops()) return false;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000155 TransformToSsa();
Nicolas Geoffrayd3350832015-03-12 11:16:23 +0000156 return true;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000157 }
158
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000159 void BuildDominatorTree();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000160 void TransformToSsa();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100161 void SimplifyCFG();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000162
Nicolas Geoffrayf5370122014-12-02 11:51:19 +0000163 // Analyze all natural loops in this graph. Returns false if one
164 // loop is not natural, that is the header does not dominate the
165 // back edge.
166 bool AnalyzeNaturalLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100167
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000168 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
169 void InlineInto(HGraph* outer_graph, HInvoke* invoke);
170
David Brazdil46e2a392015-03-16 17:31:52 +0000171 void MergeEmptyBranches(HBasicBlock* start_block, HBasicBlock* end_block);
172
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100173 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
174 void SimplifyLoop(HBasicBlock* header);
175
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000176 int32_t GetNextInstructionId() {
177 DCHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000178 return current_instruction_id_++;
179 }
180
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000181 int32_t GetCurrentInstructionId() const {
182 return current_instruction_id_;
183 }
184
185 void SetCurrentInstructionId(int32_t id) {
186 current_instruction_id_ = id;
187 }
188
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100189 uint16_t GetMaximumNumberOfOutVRegs() const {
190 return maximum_number_of_out_vregs_;
191 }
192
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000193 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
194 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100195 }
196
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000197 void UpdateTemporariesVRegSlots(size_t slots) {
198 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100199 }
200
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000201 size_t GetTemporariesVRegSlots() const {
202 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100203 }
204
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100205 void SetNumberOfVRegs(uint16_t number_of_vregs) {
206 number_of_vregs_ = number_of_vregs;
207 }
208
209 uint16_t GetNumberOfVRegs() const {
210 return number_of_vregs_;
211 }
212
213 void SetNumberOfInVRegs(uint16_t value) {
214 number_of_in_vregs_ = value;
215 }
216
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100217 uint16_t GetNumberOfLocalVRegs() const {
218 return number_of_vregs_ - number_of_in_vregs_;
219 }
220
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100221 const GrowableArray<HBasicBlock*>& GetReversePostOrder() const {
222 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100223 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100224
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100225 const GrowableArray<HBasicBlock*>& GetLinearOrder() const {
226 return linear_order_;
227 }
228
Mingyao Yange4335eb2015-03-02 15:14:13 -0800229 bool HasArrayAccesses() const {
230 return has_array_accesses_;
231 }
232
233 void SetHasArrayAccesses(bool value) {
234 has_array_accesses_ = value;
235 }
236
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000237 bool IsDebuggable() const { return debuggable_; }
238
David Brazdil8d5b8b22015-03-24 10:51:52 +0000239 // Returns a constant of the given type and value. If it does not exist
240 // already, it is created and inserted into the graph. Only integral types
241 // are currently supported.
242 HConstant* GetConstant(Primitive::Type type, int64_t value);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000243 HNullConstant* GetNullConstant();
David Brazdil8d5b8b22015-03-24 10:51:52 +0000244 HIntConstant* GetIntConstant(int32_t value) {
245 return CreateConstant(value, &cached_int_constants_);
246 }
247 HLongConstant* GetLongConstant(int64_t value) {
248 return CreateConstant(value, &cached_long_constants_);
249 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000250
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000251 private:
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000252 HBasicBlock* FindCommonDominator(HBasicBlock* first, HBasicBlock* second) const;
253 void VisitBlockForDominatorTree(HBasicBlock* block,
254 HBasicBlock* predecessor,
255 GrowableArray<size_t>* visits);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100256 void FindBackEdges(ArenaBitVector* visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000257 void VisitBlockForBackEdges(HBasicBlock* block,
258 ArenaBitVector* visited,
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100259 ArenaBitVector* visiting);
Roland Levillainfc600dc2014-12-02 17:16:31 +0000260 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100261 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000262
David Brazdil8d5b8b22015-03-24 10:51:52 +0000263 template <class InstType, typename ValueType>
264 InstType* CreateConstant(ValueType value, ArenaSafeMap<ValueType, InstType*>* cache);
265 void InsertConstant(HConstant* instruction);
266
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000267 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000268
269 // List of blocks in insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000270 GrowableArray<HBasicBlock*> blocks_;
271
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100272 // List of blocks to perform a reverse post order tree traversal.
273 GrowableArray<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000274
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100275 // List of blocks to perform a linear order tree traversal.
276 GrowableArray<HBasicBlock*> linear_order_;
277
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000278 HBasicBlock* entry_block_;
279 HBasicBlock* exit_block_;
280
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100281 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100282 uint16_t maximum_number_of_out_vregs_;
283
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100284 // The number of virtual registers in this method. Contains the parameters.
285 uint16_t number_of_vregs_;
286
287 // The number of virtual registers used by parameters of this method.
288 uint16_t number_of_in_vregs_;
289
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000290 // Number of vreg size slots that the temporaries use (used in baseline compiler).
291 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100292
Mingyao Yange4335eb2015-03-02 15:14:13 -0800293 // Has array accesses. We can totally skip BCE if it's false.
294 bool has_array_accesses_;
295
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000296 // Indicates whether the graph should be compiled in a way that
297 // ensures full debuggability. If false, we can apply more
298 // aggressive optimizations that may limit the level of debugging.
299 const bool debuggable_;
300
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000301 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000302 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000303
David Brazdil46e2a392015-03-16 17:31:52 +0000304 // Cached common constants often needed by optimization passes.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000305 HNullConstant* cached_null_constant_;
306 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
307 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000308
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100309 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000310 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000311 DISALLOW_COPY_AND_ASSIGN(HGraph);
312};
313
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700314class HLoopInformation : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000315 public:
316 HLoopInformation(HBasicBlock* header, HGraph* graph)
317 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100318 suspend_check_(nullptr),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100319 back_edges_(graph->GetArena(), kDefaultNumberOfBackEdges),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100320 // Make bit vector growable, as the number of blocks may change.
321 blocks_(graph->GetArena(), graph->GetBlocks().Size(), true) {}
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100322
323 HBasicBlock* GetHeader() const {
324 return header_;
325 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000326
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000327 void SetHeader(HBasicBlock* block) {
328 header_ = block;
329 }
330
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100331 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
332 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
333 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
334
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000335 void AddBackEdge(HBasicBlock* back_edge) {
336 back_edges_.Add(back_edge);
337 }
338
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100339 void RemoveBackEdge(HBasicBlock* back_edge) {
340 back_edges_.Delete(back_edge);
341 }
342
David Brazdil46e2a392015-03-16 17:31:52 +0000343 bool IsBackEdge(const HBasicBlock& block) const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100344 for (size_t i = 0, e = back_edges_.Size(); i < e; ++i) {
David Brazdil46e2a392015-03-16 17:31:52 +0000345 if (back_edges_.Get(i) == &block) return true;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100346 }
347 return false;
348 }
349
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000350 size_t NumberOfBackEdges() const {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000351 return back_edges_.Size();
352 }
353
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100354 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100355
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100356 const GrowableArray<HBasicBlock*>& GetBackEdges() const {
357 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100358 }
359
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100360 void ClearBackEdges() {
361 back_edges_.Reset();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100362 }
363
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100364 // Find blocks that are part of this loop. Returns whether the loop is a natural loop,
365 // that is the header dominates the back edge.
366 bool Populate();
367
368 // Returns whether this loop information contains `block`.
369 // Note that this loop information *must* be populated before entering this function.
370 bool Contains(const HBasicBlock& block) const;
371
372 // Returns whether this loop information is an inner loop of `other`.
373 // Note that `other` *must* be populated before entering this function.
374 bool IsIn(const HLoopInformation& other) const;
375
376 const ArenaBitVector& GetBlocks() const { return blocks_; }
377
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000378 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000379 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000380
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000381 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100382 // Internal recursive implementation of `Populate`.
383 void PopulateRecursive(HBasicBlock* block);
384
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000385 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100386 HSuspendCheck* suspend_check_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000387 GrowableArray<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100388 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000389
390 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
391};
392
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100393static constexpr size_t kNoLifetime = -1;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100394static constexpr uint32_t kNoDexPc = -1;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100395
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000396// A block in a method. Contains the list of instructions represented
397// as a double linked list. Each block knows its predecessors and
398// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100399
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700400class HBasicBlock : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000401 public:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100402 explicit HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000403 : graph_(graph),
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000404 predecessors_(graph->GetArena(), kDefaultNumberOfPredecessors),
405 successors_(graph->GetArena(), kDefaultNumberOfSuccessors),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000406 loop_information_(nullptr),
407 dominator_(nullptr),
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100408 dominated_blocks_(graph->GetArena(), kDefaultNumberOfDominatedBlocks),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100409 block_id_(-1),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100410 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100411 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000412 lifetime_end_(kNoLifetime),
413 is_catch_block_(false) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000414
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100415 const GrowableArray<HBasicBlock*>& GetPredecessors() const {
416 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000417 }
418
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100419 const GrowableArray<HBasicBlock*>& GetSuccessors() const {
420 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000421 }
422
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100423 const GrowableArray<HBasicBlock*>& GetDominatedBlocks() const {
424 return dominated_blocks_;
425 }
426
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100427 bool IsEntryBlock() const {
428 return graph_->GetEntryBlock() == this;
429 }
430
431 bool IsExitBlock() const {
432 return graph_->GetExitBlock() == this;
433 }
434
David Brazdil46e2a392015-03-16 17:31:52 +0000435 bool IsSingleGoto() const;
436
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000437 void AddBackEdge(HBasicBlock* back_edge) {
438 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000439 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000440 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100441 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000442 loop_information_->AddBackEdge(back_edge);
443 }
444
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000445 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000446 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000447
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000448 int GetBlockId() const { return block_id_; }
449 void SetBlockId(int id) { block_id_ = id; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000450
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000451 HBasicBlock* GetDominator() const { return dominator_; }
452 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100453 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.Add(block); }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000454 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
455 for (size_t i = 0, e = dominated_blocks_.Size(); i < e; ++i) {
456 if (dominated_blocks_.Get(i) == existing) {
457 dominated_blocks_.Put(i, new_block);
458 return;
459 }
460 }
461 LOG(FATAL) << "Unreachable";
462 UNREACHABLE();
463 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000464
465 int NumberOfBackEdges() const {
466 return loop_information_ == nullptr
467 ? 0
468 : loop_information_->NumberOfBackEdges();
469 }
470
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100471 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
472 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100473 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100474 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +0100475 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
476 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000477
478 void AddSuccessor(HBasicBlock* block) {
479 successors_.Add(block);
480 block->predecessors_.Add(this);
481 }
482
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100483 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
484 size_t successor_index = GetSuccessorIndexOf(existing);
485 DCHECK_NE(successor_index, static_cast<size_t>(-1));
486 existing->RemovePredecessor(this);
487 new_block->predecessors_.Add(this);
488 successors_.Put(successor_index, new_block);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000489 }
490
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000491 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
492 size_t predecessor_index = GetPredecessorIndexOf(existing);
493 DCHECK_NE(predecessor_index, static_cast<size_t>(-1));
494 existing->RemoveSuccessor(this);
495 new_block->successors_.Add(this);
496 predecessors_.Put(predecessor_index, new_block);
497 }
498
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100499 void RemovePredecessor(HBasicBlock* block) {
500 predecessors_.Delete(block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100501 }
502
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000503 void RemoveSuccessor(HBasicBlock* block) {
504 successors_.Delete(block);
505 }
506
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100507 void ClearAllPredecessors() {
508 predecessors_.Reset();
509 }
510
511 void AddPredecessor(HBasicBlock* block) {
512 predecessors_.Add(block);
513 block->successors_.Add(this);
514 }
515
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100516 void SwapPredecessors() {
Nicolas Geoffrayc83d4412014-09-18 16:46:20 +0100517 DCHECK_EQ(predecessors_.Size(), 2u);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100518 HBasicBlock* temp = predecessors_.Get(0);
519 predecessors_.Put(0, predecessors_.Get(1));
520 predecessors_.Put(1, temp);
521 }
522
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100523 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) {
524 for (size_t i = 0, e = predecessors_.Size(); i < e; ++i) {
525 if (predecessors_.Get(i) == predecessor) {
526 return i;
527 }
528 }
529 return -1;
530 }
531
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100532 size_t GetSuccessorIndexOf(HBasicBlock* successor) {
533 for (size_t i = 0, e = successors_.Size(); i < e; ++i) {
534 if (successors_.Get(i) == successor) {
535 return i;
536 }
537 }
538 return -1;
539 }
540
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000541 // Split the block into two blocks just after `cursor`. Returns the newly
542 // created block. Note that this method just updates raw block information,
543 // like predecessors, successors, dominators, and instruction list. It does not
544 // update the graph, reverse post order, loop information, nor make sure the
545 // blocks are consistent (for example ending with a control flow instruction).
546 HBasicBlock* SplitAfter(HInstruction* cursor);
547
548 // Merge `other` at the end of `this`. Successors and dominated blocks of
549 // `other` are changed to be successors and dominated blocks of `this`. Note
550 // that this method does not update the graph, reverse post order, loop
551 // information, nor make sure the blocks are consistent (for example ending
552 // with a control flow instruction).
553 void MergeWith(HBasicBlock* other);
554
555 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
556 // of `this` are moved to `other`.
557 // Note that this method does not update the graph, reverse post order, loop
558 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +0000559 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000560 void ReplaceWith(HBasicBlock* other);
561
David Brazdil46e2a392015-03-16 17:31:52 +0000562 // Disconnects `this` from all its predecessors, successors and the dominator.
563 // It assumes that `this` does not dominate any blocks.
564 // Note that this method does not update the graph, reverse post order, loop
565 // information, nor make sure the blocks are consistent (for example ending
566 // with a control flow instruction).
567 void DisconnectFromAll();
568
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000569 void AddInstruction(HInstruction* instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100570 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +0100571 // Replace instruction `initial` with `replacement` within this block.
572 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
573 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100574 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100575 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +0000576 // RemoveInstruction and RemovePhi delete a given instruction from the respective
577 // instruction list. With 'ensure_safety' set to true, it verifies that the
578 // instruction is not in use and removes it from the use lists of its inputs.
579 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
580 void RemovePhi(HPhi* phi, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100581
582 bool IsLoopHeader() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100583 return (loop_information_ != nullptr) && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100584 }
585
Roland Levillain6b879dd2014-09-22 17:13:44 +0100586 bool IsLoopPreHeaderFirstPredecessor() const {
587 DCHECK(IsLoopHeader());
588 DCHECK(!GetPredecessors().IsEmpty());
589 return GetPredecessors().Get(0) == GetLoopInformation()->GetPreHeader();
590 }
591
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100592 HLoopInformation* GetLoopInformation() const {
593 return loop_information_;
594 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000595
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000596 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100597 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000598 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100599 void SetInLoop(HLoopInformation* info) {
600 if (IsLoopHeader()) {
601 // Nothing to do. This just means `info` is an outer loop.
602 } else if (loop_information_ == nullptr) {
603 loop_information_ = info;
604 } else if (loop_information_->Contains(*info->GetHeader())) {
605 // Block is currently part of an outer loop. Make it part of this inner loop.
606 // Note that a non loop header having a loop information means this loop information
607 // has already been populated
608 loop_information_ = info;
609 } else {
610 // Block is part of an inner loop. Do not update the loop information.
611 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
612 // at this point, because this method is being called while populating `info`.
613 }
614 }
615
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000616 // Raw update of the loop information.
617 void SetLoopInformation(HLoopInformation* info) {
618 loop_information_ = info;
619 }
620
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100621 bool IsInLoop() const { return loop_information_ != nullptr; }
622
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100623 // Returns wheter this block dominates the blocked passed as parameter.
624 bool Dominates(HBasicBlock* block) const;
625
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100626 size_t GetLifetimeStart() const { return lifetime_start_; }
627 size_t GetLifetimeEnd() const { return lifetime_end_; }
628
629 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
630 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
631
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100632 uint32_t GetDexPc() const { return dex_pc_; }
633
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000634 bool IsCatchBlock() const { return is_catch_block_; }
635 void SetIsCatchBlock() { is_catch_block_ = true; }
636
David Brazdil8d5b8b22015-03-24 10:51:52 +0000637 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +0000638 bool EndsWithIf() const;
639 bool HasSinglePhi() const;
640
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000641 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000642 HGraph* graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000643 GrowableArray<HBasicBlock*> predecessors_;
644 GrowableArray<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100645 HInstructionList instructions_;
646 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000647 HLoopInformation* loop_information_;
648 HBasicBlock* dominator_;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100649 GrowableArray<HBasicBlock*> dominated_blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000650 int block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100651 // The dex program counter of the first instruction of this block.
652 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100653 size_t lifetime_start_;
654 size_t lifetime_end_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000655 bool is_catch_block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000656
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000657 friend class HGraph;
658 friend class HInstruction;
659
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000660 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
661};
662
David Brazdilb2bd1c52015-03-25 11:17:37 +0000663// Iterates over the LoopInformation of all loops which contain 'block'
664// from the innermost to the outermost.
665class HLoopInformationOutwardIterator : public ValueObject {
666 public:
667 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
668 : current_(block.GetLoopInformation()) {}
669
670 bool Done() const { return current_ == nullptr; }
671
672 void Advance() {
673 DCHECK(!Done());
674 current_ = current_->GetHeader()->GetDominator()->GetLoopInformation();
675 }
676
677 HLoopInformation* Current() const {
678 DCHECK(!Done());
679 return current_;
680 }
681
682 private:
683 HLoopInformation* current_;
684
685 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
686};
687
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100688#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
689 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000690 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000691 M(ArrayGet, Instruction) \
692 M(ArrayLength, Instruction) \
693 M(ArraySet, Instruction) \
David Brazdil66d126e2015-04-03 16:02:44 +0100694 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000695 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +0000696 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000697 M(CheckCast, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100698 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000699 M(Compare, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100700 M(Condition, BinaryOperation) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700701 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000702 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +0000703 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000704 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100705 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000706 M(Exit, Instruction) \
707 M(FloatConstant, Constant) \
708 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100709 M(GreaterThan, Condition) \
710 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100711 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000712 M(InstanceFieldGet, Instruction) \
713 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000714 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100715 M(IntConstant, Constant) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +0000716 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000717 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100718 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000719 M(LessThan, Condition) \
720 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000721 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000722 M(LoadException, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100723 M(LoadLocal, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000724 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100725 M(Local, Instruction) \
726 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +0100727 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +0000728 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000729 M(Mul, BinaryOperation) \
730 M(Neg, UnaryOperation) \
731 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100732 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +0100733 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000734 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000735 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000736 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000737 M(Or, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100738 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000739 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100740 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000741 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100742 M(Return, Instruction) \
743 M(ReturnVoid, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000744 M(Shl, BinaryOperation) \
745 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100746 M(StaticFieldGet, Instruction) \
747 M(StaticFieldSet, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100748 M(StoreLocal, Instruction) \
749 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100750 M(SuspendCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000751 M(Temporary, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000752 M(Throw, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +0000753 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000754 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000755 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000756
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100757#define FOR_EACH_INSTRUCTION(M) \
758 FOR_EACH_CONCRETE_INSTRUCTION(M) \
759 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +0100760 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +0100761 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100762 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -0700763
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100764#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000765FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
766#undef FORWARD_DECLARATION
767
Roland Levillainccc07a92014-09-16 14:48:16 +0100768#define DECLARE_INSTRUCTION(type) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000769 InstructionKind GetKind() const OVERRIDE { return k##type; } \
770 const char* DebugName() const OVERRIDE { return #type; } \
771 const H##type* As##type() const OVERRIDE { return this; } \
772 H##type* As##type() OVERRIDE { return this; } \
773 bool InstructionTypeEquals(HInstruction* other) const OVERRIDE { \
Roland Levillainccc07a92014-09-16 14:48:16 +0100774 return other->Is##type(); \
775 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000776 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000777
David Brazdiled596192015-01-23 10:39:45 +0000778template <typename T> class HUseList;
779
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100780template <typename T>
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700781class HUseListNode : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000782 public:
David Brazdiled596192015-01-23 10:39:45 +0000783 HUseListNode* GetPrevious() const { return prev_; }
784 HUseListNode* GetNext() const { return next_; }
785 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100786 size_t GetIndex() const { return index_; }
787
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000788 private:
David Brazdiled596192015-01-23 10:39:45 +0000789 HUseListNode(T user, size_t index)
790 : user_(user), index_(index), prev_(nullptr), next_(nullptr) {}
791
792 T const user_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100793 const size_t index_;
David Brazdiled596192015-01-23 10:39:45 +0000794 HUseListNode<T>* prev_;
795 HUseListNode<T>* next_;
796
797 friend class HUseList<T>;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000798
799 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
800};
801
David Brazdiled596192015-01-23 10:39:45 +0000802template <typename T>
803class HUseList : public ValueObject {
804 public:
805 HUseList() : first_(nullptr) {}
806
807 void Clear() {
808 first_ = nullptr;
809 }
810
811 // Adds a new entry at the beginning of the use list and returns
812 // the newly created node.
813 HUseListNode<T>* AddUse(T user, size_t index, ArenaAllocator* arena) {
David Brazdilea55b932015-01-27 17:12:29 +0000814 HUseListNode<T>* new_node = new (arena) HUseListNode<T>(user, index);
David Brazdiled596192015-01-23 10:39:45 +0000815 if (IsEmpty()) {
816 first_ = new_node;
817 } else {
818 first_->prev_ = new_node;
819 new_node->next_ = first_;
820 first_ = new_node;
821 }
822 return new_node;
823 }
824
825 HUseListNode<T>* GetFirst() const {
826 return first_;
827 }
828
829 void Remove(HUseListNode<T>* node) {
David Brazdil1abb4192015-02-17 18:33:36 +0000830 DCHECK(node != nullptr);
831 DCHECK(Contains(node));
832
David Brazdiled596192015-01-23 10:39:45 +0000833 if (node->prev_ != nullptr) {
834 node->prev_->next_ = node->next_;
835 }
836 if (node->next_ != nullptr) {
837 node->next_->prev_ = node->prev_;
838 }
839 if (node == first_) {
840 first_ = node->next_;
841 }
842 }
843
David Brazdil1abb4192015-02-17 18:33:36 +0000844 bool Contains(const HUseListNode<T>* node) const {
845 if (node == nullptr) {
846 return false;
847 }
848 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
849 if (current == node) {
850 return true;
851 }
852 }
853 return false;
854 }
855
David Brazdiled596192015-01-23 10:39:45 +0000856 bool IsEmpty() const {
857 return first_ == nullptr;
858 }
859
860 bool HasOnlyOneUse() const {
861 return first_ != nullptr && first_->next_ == nullptr;
862 }
863
864 private:
865 HUseListNode<T>* first_;
866};
867
868template<typename T>
869class HUseIterator : public ValueObject {
870 public:
871 explicit HUseIterator(const HUseList<T>& uses) : current_(uses.GetFirst()) {}
872
873 bool Done() const { return current_ == nullptr; }
874
875 void Advance() {
876 DCHECK(!Done());
877 current_ = current_->GetNext();
878 }
879
880 HUseListNode<T>* Current() const {
881 DCHECK(!Done());
882 return current_;
883 }
884
885 private:
886 HUseListNode<T>* current_;
887
888 friend class HValue;
889};
890
David Brazdil1abb4192015-02-17 18:33:36 +0000891// This class is used by HEnvironment and HInstruction classes to record the
892// instructions they use and pointers to the corresponding HUseListNodes kept
893// by the used instructions.
894template <typename T>
895class HUserRecord : public ValueObject {
896 public:
897 HUserRecord() : instruction_(nullptr), use_node_(nullptr) {}
898 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), use_node_(nullptr) {}
899
900 HUserRecord(const HUserRecord<T>& old_record, HUseListNode<T>* use_node)
901 : instruction_(old_record.instruction_), use_node_(use_node) {
902 DCHECK(instruction_ != nullptr);
903 DCHECK(use_node_ != nullptr);
904 DCHECK(old_record.use_node_ == nullptr);
905 }
906
907 HInstruction* GetInstruction() const { return instruction_; }
908 HUseListNode<T>* GetUseNode() const { return use_node_; }
909
910 private:
911 // Instruction used by the user.
912 HInstruction* instruction_;
913
914 // Corresponding entry in the use list kept by 'instruction_'.
915 HUseListNode<T>* use_node_;
916};
917
Calin Juravle27df7582015-04-17 19:12:31 +0100918// TODO: Add better documentation to this class and maybe refactor with more suggestive names.
919// - Has(All)SideEffects suggests that all the side effects are present but only ChangesSomething
920// flag is consider.
921// - DependsOn suggests that there is a real dependency between side effects but it only
922// checks DependendsOnSomething flag.
923//
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100924// Represents the side effects an instruction may have.
925class SideEffects : public ValueObject {
926 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100927 SideEffects() : flags_(0) {}
928
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100929 static SideEffects None() {
930 return SideEffects(0);
931 }
932
933 static SideEffects All() {
934 return SideEffects(ChangesSomething().flags_ | DependsOnSomething().flags_);
935 }
936
937 static SideEffects ChangesSomething() {
938 return SideEffects((1 << kFlagChangesCount) - 1);
939 }
940
941 static SideEffects DependsOnSomething() {
942 int count = kFlagDependsOnCount - kFlagChangesCount;
943 return SideEffects(((1 << count) - 1) << kFlagChangesCount);
944 }
945
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100946 SideEffects Union(SideEffects other) const {
947 return SideEffects(flags_ | other.flags_);
948 }
949
Roland Levillain72bceff2014-09-15 18:29:00 +0100950 bool HasSideEffects() const {
951 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
952 return (flags_ & all_bits_set) != 0;
953 }
954
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100955 bool HasAllSideEffects() const {
956 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
957 return all_bits_set == (flags_ & all_bits_set);
958 }
959
960 bool DependsOn(SideEffects other) const {
961 size_t depends_flags = other.ComputeDependsFlags();
962 return (flags_ & depends_flags) != 0;
963 }
964
965 bool HasDependencies() const {
966 int count = kFlagDependsOnCount - kFlagChangesCount;
967 size_t all_bits_set = (1 << count) - 1;
968 return ((flags_ >> kFlagChangesCount) & all_bits_set) != 0;
969 }
970
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100971 private:
972 static constexpr int kFlagChangesSomething = 0;
973 static constexpr int kFlagChangesCount = kFlagChangesSomething + 1;
974
975 static constexpr int kFlagDependsOnSomething = kFlagChangesCount;
976 static constexpr int kFlagDependsOnCount = kFlagDependsOnSomething + 1;
977
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100978 explicit SideEffects(size_t flags) : flags_(flags) {}
979
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100980 size_t ComputeDependsFlags() const {
981 return flags_ << kFlagChangesCount;
982 }
983
984 size_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100985};
986
David Brazdiled596192015-01-23 10:39:45 +0000987// A HEnvironment object contains the values of virtual registers at a given location.
988class HEnvironment : public ArenaObject<kArenaAllocMisc> {
989 public:
990 HEnvironment(ArenaAllocator* arena, size_t number_of_vregs)
991 : vregs_(arena, number_of_vregs) {
992 vregs_.SetSize(number_of_vregs);
993 for (size_t i = 0; i < number_of_vregs; i++) {
David Brazdil1abb4192015-02-17 18:33:36 +0000994 vregs_.Put(i, HUserRecord<HEnvironment*>());
David Brazdiled596192015-01-23 10:39:45 +0000995 }
996 }
997
998 void CopyFrom(HEnvironment* env);
999
1000 void SetRawEnvAt(size_t index, HInstruction* instruction) {
David Brazdil1abb4192015-02-17 18:33:36 +00001001 vregs_.Put(index, HUserRecord<HEnvironment*>(instruction));
David Brazdiled596192015-01-23 10:39:45 +00001002 }
1003
1004 HInstruction* GetInstructionAt(size_t index) const {
David Brazdil1abb4192015-02-17 18:33:36 +00001005 return vregs_.Get(index).GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001006 }
1007
David Brazdil1abb4192015-02-17 18:33:36 +00001008 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001009
1010 size_t Size() const { return vregs_.Size(); }
1011
1012 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001013 // Record instructions' use entries of this environment for constant-time removal.
1014 // It should only be called by HInstruction when a new environment use is added.
1015 void RecordEnvUse(HUseListNode<HEnvironment*>* env_use) {
1016 DCHECK(env_use->GetUser() == this);
1017 size_t index = env_use->GetIndex();
1018 vregs_.Put(index, HUserRecord<HEnvironment*>(vregs_.Get(index), env_use));
1019 }
David Brazdiled596192015-01-23 10:39:45 +00001020
David Brazdil1abb4192015-02-17 18:33:36 +00001021 GrowableArray<HUserRecord<HEnvironment*> > vregs_;
David Brazdiled596192015-01-23 10:39:45 +00001022
David Brazdil1abb4192015-02-17 18:33:36 +00001023 friend HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001024
1025 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1026};
1027
Calin Juravleacf735c2015-02-12 15:25:22 +00001028class ReferenceTypeInfo : ValueObject {
1029 public:
Calin Juravleb1498f62015-02-16 13:13:29 +00001030 typedef Handle<mirror::Class> TypeHandle;
1031
1032 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact)
1033 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1034 if (type_handle->IsObjectClass()) {
1035 // Override the type handle to be consistent with the case when we get to
1036 // Top but don't have the Object class available. It avoids having to guess
1037 // what value the type_handle has when it's Top.
1038 return ReferenceTypeInfo(TypeHandle(), is_exact, true);
1039 } else {
1040 return ReferenceTypeInfo(type_handle, is_exact, false);
1041 }
1042 }
1043
1044 static ReferenceTypeInfo CreateTop(bool is_exact) {
1045 return ReferenceTypeInfo(TypeHandle(), is_exact, true);
Calin Juravleacf735c2015-02-12 15:25:22 +00001046 }
1047
1048 bool IsExact() const { return is_exact_; }
1049 bool IsTop() const { return is_top_; }
1050
1051 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
1052
Calin Juravleb1498f62015-02-16 13:13:29 +00001053 bool IsSupertypeOf(ReferenceTypeInfo rti) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Calin Juravleacf735c2015-02-12 15:25:22 +00001054 if (IsTop()) {
1055 // Top (equivalent for java.lang.Object) is supertype of anything.
1056 return true;
1057 }
1058 if (rti.IsTop()) {
1059 // If we get here `this` is not Top() so it can't be a supertype.
1060 return false;
1061 }
1062 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
1063 }
1064
1065 // Returns true if the type information provide the same amount of details.
1066 // Note that it does not mean that the instructions have the same actual type
1067 // (e.g. tops are equal but they can be the result of a merge).
1068 bool IsEqual(ReferenceTypeInfo rti) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1069 if (IsExact() != rti.IsExact()) {
1070 return false;
1071 }
1072 if (IsTop() && rti.IsTop()) {
1073 // `Top` means java.lang.Object, so the types are equivalent.
1074 return true;
1075 }
1076 if (IsTop() || rti.IsTop()) {
1077 // If only one is top or object than they are not equivalent.
1078 // NB: We need this extra check because the type_handle of `Top` is invalid
1079 // and we cannot inspect its reference.
1080 return false;
1081 }
1082
1083 // Finally check the types.
1084 return GetTypeHandle().Get() == rti.GetTypeHandle().Get();
1085 }
1086
1087 private:
Calin Juravleb1498f62015-02-16 13:13:29 +00001088 ReferenceTypeInfo() : ReferenceTypeInfo(TypeHandle(), false, true) {}
1089 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact, bool is_top)
1090 : type_handle_(type_handle), is_exact_(is_exact), is_top_(is_top) {}
1091
Calin Juravleacf735c2015-02-12 15:25:22 +00001092 // The class of the object.
Calin Juravleb1498f62015-02-16 13:13:29 +00001093 TypeHandle type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001094 // Whether or not the type is exact or a superclass of the actual type.
Calin Juravleb1498f62015-02-16 13:13:29 +00001095 // Whether or not we have any information about this type.
Calin Juravleacf735c2015-02-12 15:25:22 +00001096 bool is_exact_;
1097 // A true value here means that the object type should be java.lang.Object.
1098 // We don't have access to the corresponding mirror object every time so this
1099 // flag acts as a substitute. When true, the TypeHandle refers to a null
1100 // pointer and should not be used.
1101 bool is_top_;
1102};
1103
1104std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
1105
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001106class HInstruction : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001107 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001108 explicit HInstruction(SideEffects side_effects)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001109 : previous_(nullptr),
1110 next_(nullptr),
1111 block_(nullptr),
1112 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001113 ssa_index_(-1),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001114 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001115 locations_(nullptr),
1116 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001117 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001118 side_effects_(side_effects),
1119 reference_type_info_(ReferenceTypeInfo::CreateTop(/* is_exact */ false)) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001120
Dave Allison20dfc792014-06-16 20:44:29 -07001121 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001122
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001123#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001124 enum InstructionKind {
1125 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1126 };
1127#undef DECLARE_KIND
1128
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001129 HInstruction* GetNext() const { return next_; }
1130 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001131
Calin Juravle77520bc2015-01-12 18:45:46 +00001132 HInstruction* GetNextDisregardingMoves() const;
1133 HInstruction* GetPreviousDisregardingMoves() const;
1134
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001135 HBasicBlock* GetBlock() const { return block_; }
1136 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001137 bool IsInBlock() const { return block_ != nullptr; }
1138 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +01001139 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001140
Roland Levillain6b879dd2014-09-22 17:13:44 +01001141 virtual size_t InputCount() const = 0;
David Brazdil1abb4192015-02-17 18:33:36 +00001142 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001143
1144 virtual void Accept(HGraphVisitor* visitor) = 0;
1145 virtual const char* DebugName() const = 0;
1146
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001147 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
David Brazdil1abb4192015-02-17 18:33:36 +00001148 void SetRawInputAt(size_t index, HInstruction* input) {
1149 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1150 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001151
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001152 virtual bool NeedsEnvironment() const { return false; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001153 virtual bool IsControlFlow() const { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01001154 virtual bool CanThrow() const { return false; }
Roland Levillain72bceff2014-09-15 18:29:00 +01001155 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001156
Calin Juravle61d544b2015-02-23 16:46:57 +00001157 virtual bool ActAsNullConstant() const { return false; }
1158
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
1637 // be evaluated as a constant, return nullptr.
1638 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
1705 // be evaluated as a constant, return nullptr.
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
1713 // constant, or nullptr.
1714 HConstant* GetConstantRight() const;
1715
1716 // If `GetConstantRight()` returns one of the input, this returns the other
1717 // one. Otherwise it returns nullptr.
1718 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
Calin Juravle61d544b2015-02-23 16:46:57 +00002083 bool ActAsNullConstant() const OVERRIDE { return true; }
2084
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002085 DECLARE_INSTRUCTION(NullConstant);
2086
2087 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002088 HNullConstant() : HConstant(Primitive::kPrimNot) {}
2089
2090 friend class HGraph;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002091 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2092};
2093
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002094// Constants of the type int. Those can be from Dex instructions, or
2095// synthesized (for example with the if-eqz instruction).
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002096class HIntConstant : public HConstant {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002097 public:
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002098 int32_t GetValue() const { return value_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002099
Calin Juravle61d544b2015-02-23 16:46:57 +00002100 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002101 return other->AsIntConstant()->value_ == value_;
2102 }
2103
Calin Juravle61d544b2015-02-23 16:46:57 +00002104 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2105
2106 // TODO: Null is represented by the `0` constant. In most cases we replace it
2107 // with a HNullConstant but we don't do it when comparing (a != null). This
2108 // method is an workaround until we fix the above.
2109 bool ActAsNullConstant() const OVERRIDE { return value_ == 0; }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002110
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002111 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2112 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2113 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2114
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002115 DECLARE_INSTRUCTION(IntConstant);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002116
2117 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002118 explicit HIntConstant(int32_t value) : HConstant(Primitive::kPrimInt), value_(value) {}
2119
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002120 const int32_t value_;
2121
David Brazdil8d5b8b22015-03-24 10:51:52 +00002122 friend class HGraph;
2123 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
Zheng Xuad4450e2015-04-17 18:48:56 +08002124 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002125 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2126};
2127
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002128class HLongConstant : public HConstant {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002129 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002130 int64_t GetValue() const { return value_; }
2131
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002132 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002133 return other->AsLongConstant()->value_ == value_;
2134 }
2135
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002136 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002137
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002138 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2139 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2140 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2141
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002142 DECLARE_INSTRUCTION(LongConstant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002143
2144 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002145 explicit HLongConstant(int64_t value) : HConstant(Primitive::kPrimLong), value_(value) {}
2146
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002147 const int64_t value_;
2148
David Brazdil8d5b8b22015-03-24 10:51:52 +00002149 friend class HGraph;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002150 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2151};
2152
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002153enum class Intrinsics {
2154#define OPTIMIZING_INTRINSICS(Name, IsStatic) k ## Name,
2155#include "intrinsics_list.h"
2156 kNone,
2157 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
2158#undef INTRINSICS_LIST
2159#undef OPTIMIZING_INTRINSICS
2160};
2161std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
2162
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002163class HInvoke : public HInstruction {
2164 public:
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002165 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002166
2167 // Runtime needs to walk the stack, so Dex -> Dex calls need to
2168 // know their environment.
Calin Juravle77520bc2015-01-12 18:45:46 +00002169 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002170
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01002171 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002172 SetRawInputAt(index, argument);
2173 }
2174
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002175 Primitive::Type GetType() const OVERRIDE { return return_type_; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002176
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002177 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002178
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002179 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
2180
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01002181 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002182 return intrinsic_;
2183 }
2184
2185 void SetIntrinsic(Intrinsics intrinsic) {
2186 intrinsic_ = intrinsic;
2187 }
2188
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002189 DECLARE_INSTRUCTION(Invoke);
2190
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002191 protected:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002192 HInvoke(ArenaAllocator* arena,
2193 uint32_t number_of_arguments,
2194 Primitive::Type return_type,
2195 uint32_t dex_pc,
2196 uint32_t dex_method_index)
2197 : HInstruction(SideEffects::All()),
2198 inputs_(arena, number_of_arguments),
2199 return_type_(return_type),
2200 dex_pc_(dex_pc),
2201 dex_method_index_(dex_method_index),
2202 intrinsic_(Intrinsics::kNone) {
2203 inputs_.SetSize(number_of_arguments);
2204 }
2205
David Brazdil1abb4192015-02-17 18:33:36 +00002206 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
2207 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
2208 inputs_.Put(index, input);
2209 }
2210
2211 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002212 const Primitive::Type return_type_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002213 const uint32_t dex_pc_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002214 const uint32_t dex_method_index_;
2215 Intrinsics intrinsic_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002216
2217 private:
2218 DISALLOW_COPY_AND_ASSIGN(HInvoke);
2219};
2220
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002221class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002222 public:
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002223 HInvokeStaticOrDirect(ArenaAllocator* arena,
2224 uint32_t number_of_arguments,
2225 Primitive::Type return_type,
2226 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002227 uint32_t dex_method_index,
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002228 bool is_recursive,
Nicolas Geoffray79041292015-03-26 10:05:54 +00002229 InvokeType original_invoke_type,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002230 InvokeType invoke_type)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002231 : HInvoke(arena, number_of_arguments, return_type, dex_pc, dex_method_index),
Nicolas Geoffray79041292015-03-26 10:05:54 +00002232 original_invoke_type_(original_invoke_type),
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002233 invoke_type_(invoke_type),
2234 is_recursive_(is_recursive) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002235
Calin Juravle641547a2015-04-21 22:08:51 +01002236 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
2237 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00002238 // We access the method via the dex cache so we can't do an implicit null check.
2239 // TODO: for intrinsics we can generate implicit null checks.
2240 return false;
2241 }
2242
Nicolas Geoffray79041292015-03-26 10:05:54 +00002243 InvokeType GetOriginalInvokeType() const { return original_invoke_type_; }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002244 InvokeType GetInvokeType() const { return invoke_type_; }
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002245 bool IsRecursive() const { return is_recursive_; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002246 bool NeedsDexCache() const OVERRIDE { return !IsRecursive(); }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002247
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002248 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002249
2250 private:
Nicolas Geoffray79041292015-03-26 10:05:54 +00002251 const InvokeType original_invoke_type_;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002252 const InvokeType invoke_type_;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002253 const bool is_recursive_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002254
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002255 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002256};
2257
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002258class HInvokeVirtual : public HInvoke {
2259 public:
2260 HInvokeVirtual(ArenaAllocator* arena,
2261 uint32_t number_of_arguments,
2262 Primitive::Type return_type,
2263 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002264 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002265 uint32_t vtable_index)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002266 : HInvoke(arena, number_of_arguments, return_type, dex_pc, dex_method_index),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002267 vtable_index_(vtable_index) {}
2268
Calin Juravle641547a2015-04-21 22:08:51 +01002269 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00002270 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01002271 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00002272 }
2273
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002274 uint32_t GetVTableIndex() const { return vtable_index_; }
2275
2276 DECLARE_INSTRUCTION(InvokeVirtual);
2277
2278 private:
2279 const uint32_t vtable_index_;
2280
2281 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
2282};
2283
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002284class HInvokeInterface : public HInvoke {
2285 public:
2286 HInvokeInterface(ArenaAllocator* arena,
2287 uint32_t number_of_arguments,
2288 Primitive::Type return_type,
2289 uint32_t dex_pc,
2290 uint32_t dex_method_index,
2291 uint32_t imt_index)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002292 : HInvoke(arena, number_of_arguments, return_type, dex_pc, dex_method_index),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002293 imt_index_(imt_index) {}
2294
Calin Juravle641547a2015-04-21 22:08:51 +01002295 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00002296 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01002297 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00002298 }
2299
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002300 uint32_t GetImtIndex() const { return imt_index_; }
2301 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
2302
2303 DECLARE_INSTRUCTION(InvokeInterface);
2304
2305 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002306 const uint32_t imt_index_;
2307
2308 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
2309};
2310
Dave Allison20dfc792014-06-16 20:44:29 -07002311class HNewInstance : public HExpression<0> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002312 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002313 HNewInstance(uint32_t dex_pc, uint16_t type_index, QuickEntrypointEnum entrypoint)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002314 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2315 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002316 type_index_(type_index),
2317 entrypoint_(entrypoint) {}
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002318
2319 uint32_t GetDexPc() const { return dex_pc_; }
2320 uint16_t GetTypeIndex() const { return type_index_; }
2321
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002322 // Calls runtime so needs an environment.
Calin Juravle92a6ed22014-12-02 18:58:03 +00002323 bool NeedsEnvironment() const OVERRIDE { return true; }
2324 // It may throw when called on:
2325 // - interfaces
2326 // - abstract/innaccessible/unknown classes
2327 // TODO: optimize when possible.
2328 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002329
Calin Juravle10e244f2015-01-26 18:54:32 +00002330 bool CanBeNull() const OVERRIDE { return false; }
2331
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002332 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
2333
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002334 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002335
2336 private:
2337 const uint32_t dex_pc_;
2338 const uint16_t type_index_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002339 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002340
2341 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
2342};
2343
Roland Levillain88cb1752014-10-20 16:36:47 +01002344class HNeg : public HUnaryOperation {
2345 public:
2346 explicit HNeg(Primitive::Type result_type, HInstruction* input)
2347 : HUnaryOperation(result_type, input) {}
2348
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002349 int32_t Evaluate(int32_t x) const OVERRIDE { return -x; }
2350 int64_t Evaluate(int64_t x) const OVERRIDE { return -x; }
Roland Levillain9240d6a2014-10-20 16:47:04 +01002351
Roland Levillain88cb1752014-10-20 16:36:47 +01002352 DECLARE_INSTRUCTION(Neg);
2353
2354 private:
2355 DISALLOW_COPY_AND_ASSIGN(HNeg);
2356};
2357
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002358class HNewArray : public HExpression<1> {
2359 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002360 HNewArray(HInstruction* length,
2361 uint32_t dex_pc,
2362 uint16_t type_index,
2363 QuickEntrypointEnum entrypoint)
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002364 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2365 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002366 type_index_(type_index),
2367 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002368 SetRawInputAt(0, length);
2369 }
2370
2371 uint32_t GetDexPc() const { return dex_pc_; }
2372 uint16_t GetTypeIndex() const { return type_index_; }
2373
2374 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00002375 bool NeedsEnvironment() const OVERRIDE { return true; }
2376
Mingyao Yang0c365e62015-03-31 15:09:29 -07002377 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
2378 bool CanThrow() const OVERRIDE { return true; }
2379
Calin Juravle10e244f2015-01-26 18:54:32 +00002380 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002381
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002382 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
2383
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002384 DECLARE_INSTRUCTION(NewArray);
2385
2386 private:
2387 const uint32_t dex_pc_;
2388 const uint16_t type_index_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002389 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002390
2391 DISALLOW_COPY_AND_ASSIGN(HNewArray);
2392};
2393
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002394class HAdd : public HBinaryOperation {
2395 public:
2396 HAdd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2397 : HBinaryOperation(result_type, left, right) {}
2398
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002399 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002400
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002401 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002402 return x + y;
2403 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002404 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002405 return x + y;
2406 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002407
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002408 DECLARE_INSTRUCTION(Add);
2409
2410 private:
2411 DISALLOW_COPY_AND_ASSIGN(HAdd);
2412};
2413
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002414class HSub : public HBinaryOperation {
2415 public:
2416 HSub(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2417 : HBinaryOperation(result_type, left, right) {}
2418
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002419 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002420 return x - y;
2421 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002422 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002423 return x - y;
2424 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002425
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002426 DECLARE_INSTRUCTION(Sub);
2427
2428 private:
2429 DISALLOW_COPY_AND_ASSIGN(HSub);
2430};
2431
Calin Juravle34bacdf2014-10-07 20:23:36 +01002432class HMul : public HBinaryOperation {
2433 public:
2434 HMul(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2435 : HBinaryOperation(result_type, left, right) {}
2436
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002437 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002438
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002439 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x * y; }
2440 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x * y; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002441
2442 DECLARE_INSTRUCTION(Mul);
2443
2444 private:
2445 DISALLOW_COPY_AND_ASSIGN(HMul);
2446};
2447
Calin Juravle7c4954d2014-10-28 16:57:40 +00002448class HDiv : public HBinaryOperation {
2449 public:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002450 HDiv(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
2451 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00002452
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002453 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00002454 // Our graph structure ensures we never have 0 for `y` during constant folding.
2455 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00002456 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00002457 return (y == -1) ? -x : x / y;
2458 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002459
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002460 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002461 DCHECK_NE(y, 0);
2462 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2463 return (y == -1) ? -x : x / y;
2464 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002465
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002466 uint32_t GetDexPc() const { return dex_pc_; }
2467
Calin Juravle7c4954d2014-10-28 16:57:40 +00002468 DECLARE_INSTRUCTION(Div);
2469
2470 private:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002471 const uint32_t dex_pc_;
2472
Calin Juravle7c4954d2014-10-28 16:57:40 +00002473 DISALLOW_COPY_AND_ASSIGN(HDiv);
2474};
2475
Calin Juravlebacfec32014-11-14 15:54:36 +00002476class HRem : public HBinaryOperation {
2477 public:
2478 HRem(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
2479 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
2480
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002481 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002482 DCHECK_NE(y, 0);
2483 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2484 return (y == -1) ? 0 : x % y;
2485 }
2486
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002487 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002488 DCHECK_NE(y, 0);
2489 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2490 return (y == -1) ? 0 : x % y;
2491 }
2492
2493 uint32_t GetDexPc() const { return dex_pc_; }
2494
2495 DECLARE_INSTRUCTION(Rem);
2496
2497 private:
2498 const uint32_t dex_pc_;
2499
2500 DISALLOW_COPY_AND_ASSIGN(HRem);
2501};
2502
Calin Juravled0d48522014-11-04 16:40:20 +00002503class HDivZeroCheck : public HExpression<1> {
2504 public:
2505 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
2506 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
2507 SetRawInputAt(0, value);
2508 }
2509
2510 bool CanBeMoved() const OVERRIDE { return true; }
2511
2512 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2513 UNUSED(other);
2514 return true;
2515 }
2516
2517 bool NeedsEnvironment() const OVERRIDE { return true; }
2518 bool CanThrow() const OVERRIDE { return true; }
2519
2520 uint32_t GetDexPc() const { return dex_pc_; }
2521
2522 DECLARE_INSTRUCTION(DivZeroCheck);
2523
2524 private:
2525 const uint32_t dex_pc_;
2526
2527 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
2528};
2529
Calin Juravle9aec02f2014-11-18 23:06:35 +00002530class HShl : public HBinaryOperation {
2531 public:
2532 HShl(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2533 : HBinaryOperation(result_type, left, right) {}
2534
2535 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x << (y & kMaxIntShiftValue); }
2536 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x << (y & kMaxLongShiftValue); }
2537
2538 DECLARE_INSTRUCTION(Shl);
2539
2540 private:
2541 DISALLOW_COPY_AND_ASSIGN(HShl);
2542};
2543
2544class HShr : public HBinaryOperation {
2545 public:
2546 HShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2547 : HBinaryOperation(result_type, left, right) {}
2548
2549 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x >> (y & kMaxIntShiftValue); }
2550 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x >> (y & kMaxLongShiftValue); }
2551
2552 DECLARE_INSTRUCTION(Shr);
2553
2554 private:
2555 DISALLOW_COPY_AND_ASSIGN(HShr);
2556};
2557
2558class HUShr : public HBinaryOperation {
2559 public:
2560 HUShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2561 : HBinaryOperation(result_type, left, right) {}
2562
2563 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
2564 uint32_t ux = static_cast<uint32_t>(x);
2565 uint32_t uy = static_cast<uint32_t>(y) & kMaxIntShiftValue;
2566 return static_cast<int32_t>(ux >> uy);
2567 }
2568
2569 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
2570 uint64_t ux = static_cast<uint64_t>(x);
2571 uint64_t uy = static_cast<uint64_t>(y) & kMaxLongShiftValue;
2572 return static_cast<int64_t>(ux >> uy);
2573 }
2574
2575 DECLARE_INSTRUCTION(UShr);
2576
2577 private:
2578 DISALLOW_COPY_AND_ASSIGN(HUShr);
2579};
2580
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002581class HAnd : public HBinaryOperation {
2582 public:
2583 HAnd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2584 : HBinaryOperation(result_type, left, right) {}
2585
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002586 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002587
2588 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x & y; }
2589 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x & y; }
2590
2591 DECLARE_INSTRUCTION(And);
2592
2593 private:
2594 DISALLOW_COPY_AND_ASSIGN(HAnd);
2595};
2596
2597class HOr : public HBinaryOperation {
2598 public:
2599 HOr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2600 : HBinaryOperation(result_type, left, right) {}
2601
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002602 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002603
2604 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x | y; }
2605 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x | y; }
2606
2607 DECLARE_INSTRUCTION(Or);
2608
2609 private:
2610 DISALLOW_COPY_AND_ASSIGN(HOr);
2611};
2612
2613class HXor : public HBinaryOperation {
2614 public:
2615 HXor(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2616 : HBinaryOperation(result_type, left, right) {}
2617
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002618 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002619
2620 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x ^ y; }
2621 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x ^ y; }
2622
2623 DECLARE_INSTRUCTION(Xor);
2624
2625 private:
2626 DISALLOW_COPY_AND_ASSIGN(HXor);
2627};
2628
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002629// The value of a parameter in this method. Its location depends on
2630// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07002631class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002632 public:
Calin Juravle10e244f2015-01-26 18:54:32 +00002633 HParameterValue(uint8_t index, Primitive::Type parameter_type, bool is_this = false)
2634 : HExpression(parameter_type, SideEffects::None()), index_(index), is_this_(is_this) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002635
2636 uint8_t GetIndex() const { return index_; }
2637
Calin Juravle10e244f2015-01-26 18:54:32 +00002638 bool CanBeNull() const OVERRIDE { return !is_this_; }
2639
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002640 DECLARE_INSTRUCTION(ParameterValue);
2641
2642 private:
2643 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00002644 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002645 const uint8_t index_;
2646
Calin Juravle10e244f2015-01-26 18:54:32 +00002647 // Whether or not the parameter value corresponds to 'this' argument.
2648 const bool is_this_;
2649
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002650 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
2651};
2652
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002653class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002654 public:
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002655 explicit HNot(Primitive::Type result_type, HInstruction* input)
2656 : HUnaryOperation(result_type, input) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002657
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002658 bool CanBeMoved() const OVERRIDE { return true; }
2659 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002660 UNUSED(other);
2661 return true;
2662 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002663
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002664 int32_t Evaluate(int32_t x) const OVERRIDE { return ~x; }
2665 int64_t Evaluate(int64_t x) const OVERRIDE { return ~x; }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002666
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002667 DECLARE_INSTRUCTION(Not);
2668
2669 private:
2670 DISALLOW_COPY_AND_ASSIGN(HNot);
2671};
2672
David Brazdil66d126e2015-04-03 16:02:44 +01002673class HBooleanNot : public HUnaryOperation {
2674 public:
2675 explicit HBooleanNot(HInstruction* input)
2676 : HUnaryOperation(Primitive::Type::kPrimBoolean, input) {}
2677
2678 bool CanBeMoved() const OVERRIDE { return true; }
2679 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2680 UNUSED(other);
2681 return true;
2682 }
2683
2684 int32_t Evaluate(int32_t x) const OVERRIDE {
2685 DCHECK(IsUint<1>(x));
2686 return !x;
2687 }
2688
2689 int64_t Evaluate(int64_t x ATTRIBUTE_UNUSED) const OVERRIDE {
2690 LOG(FATAL) << DebugName() << " cannot be used with 64-bit values";
2691 UNREACHABLE();
2692 }
2693
2694 DECLARE_INSTRUCTION(BooleanNot);
2695
2696 private:
2697 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
2698};
2699
Roland Levillaindff1f282014-11-05 14:15:05 +00002700class HTypeConversion : public HExpression<1> {
2701 public:
2702 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00002703 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
2704 : HExpression(result_type, SideEffects::None()), dex_pc_(dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002705 SetRawInputAt(0, input);
2706 DCHECK_NE(input->GetType(), result_type);
2707 }
2708
2709 HInstruction* GetInput() const { return InputAt(0); }
2710 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
2711 Primitive::Type GetResultType() const { return GetType(); }
2712
Roland Levillain624279f2014-12-04 11:54:28 +00002713 // Required by the x86 and ARM code generators when producing calls
2714 // to the runtime.
2715 uint32_t GetDexPc() const { return dex_pc_; }
2716
Roland Levillaindff1f282014-11-05 14:15:05 +00002717 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00002718 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00002719
2720 DECLARE_INSTRUCTION(TypeConversion);
2721
2722 private:
Roland Levillain624279f2014-12-04 11:54:28 +00002723 const uint32_t dex_pc_;
2724
Roland Levillaindff1f282014-11-05 14:15:05 +00002725 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
2726};
2727
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00002728static constexpr uint32_t kNoRegNumber = -1;
2729
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002730class HPhi : public HInstruction {
2731 public:
2732 HPhi(ArenaAllocator* arena, uint32_t reg_number, size_t number_of_inputs, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002733 : HInstruction(SideEffects::None()),
2734 inputs_(arena, number_of_inputs),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002735 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002736 type_(type),
Calin Juravle10e244f2015-01-26 18:54:32 +00002737 is_live_(false),
2738 can_be_null_(true) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002739 inputs_.SetSize(number_of_inputs);
2740 }
2741
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00002742 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
2743 static Primitive::Type ToPhiType(Primitive::Type type) {
2744 switch (type) {
2745 case Primitive::kPrimBoolean:
2746 case Primitive::kPrimByte:
2747 case Primitive::kPrimShort:
2748 case Primitive::kPrimChar:
2749 return Primitive::kPrimInt;
2750 default:
2751 return type;
2752 }
2753 }
2754
Calin Juravle10e244f2015-01-26 18:54:32 +00002755 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002756
2757 void AddInput(HInstruction* input);
2758
Calin Juravle10e244f2015-01-26 18:54:32 +00002759 Primitive::Type GetType() const OVERRIDE { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002760 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002761
Calin Juravle10e244f2015-01-26 18:54:32 +00002762 bool CanBeNull() const OVERRIDE { return can_be_null_; }
2763 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
2764
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002765 uint32_t GetRegNumber() const { return reg_number_; }
2766
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002767 void SetDead() { is_live_ = false; }
2768 void SetLive() { is_live_ = true; }
2769 bool IsDead() const { return !is_live_; }
2770 bool IsLive() const { return is_live_; }
2771
Calin Juravlea4f88312015-04-16 12:57:19 +01002772 // Returns the next equivalent phi (starting from the current one) or null if there is none.
2773 // An equivalent phi is a phi having the same dex register and type.
2774 // It assumes that phis with the same dex register are adjacent.
2775 HPhi* GetNextEquivalentPhiWithSameType() {
2776 HInstruction* next = GetNext();
2777 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
2778 if (next->GetType() == GetType()) {
2779 return next->AsPhi();
2780 }
2781 next = next->GetNext();
2782 }
2783 return nullptr;
2784 }
2785
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002786 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002787
David Brazdil1abb4192015-02-17 18:33:36 +00002788 protected:
2789 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
2790
2791 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
2792 inputs_.Put(index, input);
2793 }
2794
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002795 private:
David Brazdil1abb4192015-02-17 18:33:36 +00002796 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002797 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002798 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002799 bool is_live_;
Calin Juravle10e244f2015-01-26 18:54:32 +00002800 bool can_be_null_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002801
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002802 DISALLOW_COPY_AND_ASSIGN(HPhi);
2803};
2804
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002805class HNullCheck : public HExpression<1> {
2806 public:
2807 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002808 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002809 SetRawInputAt(0, value);
2810 }
2811
Calin Juravle10e244f2015-01-26 18:54:32 +00002812 bool CanBeMoved() const OVERRIDE { return true; }
2813 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002814 UNUSED(other);
2815 return true;
2816 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002817
Calin Juravle10e244f2015-01-26 18:54:32 +00002818 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002819
Calin Juravle10e244f2015-01-26 18:54:32 +00002820 bool CanThrow() const OVERRIDE { return true; }
2821
2822 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01002823
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002824 uint32_t GetDexPc() const { return dex_pc_; }
2825
2826 DECLARE_INSTRUCTION(NullCheck);
2827
2828 private:
2829 const uint32_t dex_pc_;
2830
2831 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
2832};
2833
2834class FieldInfo : public ValueObject {
2835 public:
Calin Juravle52c48962014-12-16 17:02:57 +00002836 FieldInfo(MemberOffset field_offset, Primitive::Type field_type, bool is_volatile)
2837 : field_offset_(field_offset), field_type_(field_type), is_volatile_(is_volatile) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002838
2839 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002840 Primitive::Type GetFieldType() const { return field_type_; }
Calin Juravle52c48962014-12-16 17:02:57 +00002841 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002842
2843 private:
2844 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01002845 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00002846 const bool is_volatile_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002847};
2848
2849class HInstanceFieldGet : public HExpression<1> {
2850 public:
2851 HInstanceFieldGet(HInstruction* value,
2852 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00002853 MemberOffset field_offset,
2854 bool is_volatile)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002855 : HExpression(field_type, SideEffects::DependsOnSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00002856 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002857 SetRawInputAt(0, value);
2858 }
2859
Calin Juravle10c9cbe2014-12-19 10:50:19 +00002860 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002861
2862 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2863 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
2864 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002865 }
2866
Calin Juravle641547a2015-04-21 22:08:51 +01002867 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
2868 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00002869 }
2870
2871 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002872 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
2873 }
2874
Calin Juravle52c48962014-12-16 17:02:57 +00002875 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002876 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002877 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002878 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002879
2880 DECLARE_INSTRUCTION(InstanceFieldGet);
2881
2882 private:
2883 const FieldInfo field_info_;
2884
2885 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
2886};
2887
2888class HInstanceFieldSet : public HTemplateInstruction<2> {
2889 public:
2890 HInstanceFieldSet(HInstruction* object,
2891 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01002892 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00002893 MemberOffset field_offset,
2894 bool is_volatile)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002895 : HTemplateInstruction(SideEffects::ChangesSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00002896 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002897 SetRawInputAt(0, object);
2898 SetRawInputAt(1, value);
2899 }
2900
Calin Juravle641547a2015-04-21 22:08:51 +01002901 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
2902 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00002903 }
2904
Calin Juravle52c48962014-12-16 17:02:57 +00002905 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002906 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002907 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002908 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002909 HInstruction* GetValue() const { return InputAt(1); }
2910
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002911 DECLARE_INSTRUCTION(InstanceFieldSet);
2912
2913 private:
2914 const FieldInfo field_info_;
2915
2916 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
2917};
2918
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002919class HArrayGet : public HExpression<2> {
2920 public:
2921 HArrayGet(HInstruction* array, HInstruction* index, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002922 : HExpression(type, SideEffects::DependsOnSomething()) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002923 SetRawInputAt(0, array);
2924 SetRawInputAt(1, index);
2925 }
2926
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002927 bool CanBeMoved() const OVERRIDE { return true; }
2928 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002929 UNUSED(other);
2930 return true;
2931 }
Calin Juravle641547a2015-04-21 22:08:51 +01002932 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
2933 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00002934 // TODO: We can be smarter here.
2935 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
2936 // which generates the implicit null check. There are cases when these can be removed
2937 // to produce better code. If we ever add optimizations to do so we should allow an
2938 // implicit check here (as long as the address falls in the first page).
2939 return false;
2940 }
2941
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002942 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002943
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002944 HInstruction* GetArray() const { return InputAt(0); }
2945 HInstruction* GetIndex() const { return InputAt(1); }
2946
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002947 DECLARE_INSTRUCTION(ArrayGet);
2948
2949 private:
2950 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
2951};
2952
2953class HArraySet : public HTemplateInstruction<3> {
2954 public:
2955 HArraySet(HInstruction* array,
2956 HInstruction* index,
2957 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002958 Primitive::Type expected_component_type,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002959 uint32_t dex_pc)
2960 : HTemplateInstruction(SideEffects::ChangesSomething()),
2961 dex_pc_(dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002962 expected_component_type_(expected_component_type),
2963 needs_type_check_(value->GetType() == Primitive::kPrimNot) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002964 SetRawInputAt(0, array);
2965 SetRawInputAt(1, index);
2966 SetRawInputAt(2, value);
2967 }
2968
Calin Juravle77520bc2015-01-12 18:45:46 +00002969 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002970 // We currently always call a runtime method to catch array store
2971 // exceptions.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002972 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002973 }
2974
Calin Juravle641547a2015-04-21 22:08:51 +01002975 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
2976 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00002977 // TODO: Same as for ArrayGet.
2978 return false;
2979 }
2980
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002981 void ClearNeedsTypeCheck() {
2982 needs_type_check_ = false;
2983 }
2984
2985 bool NeedsTypeCheck() const { return needs_type_check_; }
2986
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002987 uint32_t GetDexPc() const { return dex_pc_; }
2988
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002989 HInstruction* GetArray() const { return InputAt(0); }
2990 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002991 HInstruction* GetValue() const { return InputAt(2); }
2992
2993 Primitive::Type GetComponentType() const {
2994 // The Dex format does not type floating point index operations. Since the
2995 // `expected_component_type_` is set during building and can therefore not
2996 // be correct, we also check what is the value type. If it is a floating
2997 // point type, we must use that type.
2998 Primitive::Type value_type = GetValue()->GetType();
2999 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
3000 ? value_type
3001 : expected_component_type_;
3002 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003003
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003004 DECLARE_INSTRUCTION(ArraySet);
3005
3006 private:
3007 const uint32_t dex_pc_;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003008 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003009 bool needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003010
3011 DISALLOW_COPY_AND_ASSIGN(HArraySet);
3012};
3013
3014class HArrayLength : public HExpression<1> {
3015 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003016 explicit HArrayLength(HInstruction* array)
3017 : HExpression(Primitive::kPrimInt, SideEffects::None()) {
3018 // Note that arrays do not change length, so the instruction does not
3019 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003020 SetRawInputAt(0, array);
3021 }
3022
Calin Juravle77520bc2015-01-12 18:45:46 +00003023 bool CanBeMoved() const OVERRIDE { return true; }
3024 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003025 UNUSED(other);
3026 return true;
3027 }
Calin Juravle641547a2015-04-21 22:08:51 +01003028 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3029 return obj == InputAt(0);
3030 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003031
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003032 DECLARE_INSTRUCTION(ArrayLength);
3033
3034 private:
3035 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
3036};
3037
3038class HBoundsCheck : public HExpression<2> {
3039 public:
3040 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003041 : HExpression(index->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003042 DCHECK(index->GetType() == Primitive::kPrimInt);
3043 SetRawInputAt(0, index);
3044 SetRawInputAt(1, length);
3045 }
3046
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003047 bool CanBeMoved() const OVERRIDE { return true; }
3048 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003049 UNUSED(other);
3050 return true;
3051 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003052
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003053 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003054
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003055 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01003056
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003057 uint32_t GetDexPc() const { return dex_pc_; }
3058
3059 DECLARE_INSTRUCTION(BoundsCheck);
3060
3061 private:
3062 const uint32_t dex_pc_;
3063
3064 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
3065};
3066
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003067/**
3068 * Some DEX instructions are folded into multiple HInstructions that need
3069 * to stay live until the last HInstruction. This class
3070 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00003071 * HInstruction stays live. `index` represents the stack location index of the
3072 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003073 */
3074class HTemporary : public HTemplateInstruction<0> {
3075 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003076 explicit HTemporary(size_t index) : HTemplateInstruction(SideEffects::None()), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003077
3078 size_t GetIndex() const { return index_; }
3079
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00003080 Primitive::Type GetType() const OVERRIDE {
3081 // The previous instruction is the one that will be stored in the temporary location.
3082 DCHECK(GetPrevious() != nullptr);
3083 return GetPrevious()->GetType();
3084 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00003085
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003086 DECLARE_INSTRUCTION(Temporary);
3087
3088 private:
3089 const size_t index_;
3090
3091 DISALLOW_COPY_AND_ASSIGN(HTemporary);
3092};
3093
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003094class HSuspendCheck : public HTemplateInstruction<0> {
3095 public:
3096 explicit HSuspendCheck(uint32_t dex_pc)
Nicolas Geoffray9ebc72c2014-09-25 16:33:42 +01003097 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003098
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003099 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003100 return true;
3101 }
3102
3103 uint32_t GetDexPc() const { return dex_pc_; }
3104
3105 DECLARE_INSTRUCTION(SuspendCheck);
3106
3107 private:
3108 const uint32_t dex_pc_;
3109
3110 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
3111};
3112
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003113/**
3114 * Instruction to load a Class object.
3115 */
3116class HLoadClass : public HExpression<0> {
3117 public:
3118 HLoadClass(uint16_t type_index,
3119 bool is_referrers_class,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003120 uint32_t dex_pc)
3121 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3122 type_index_(type_index),
3123 is_referrers_class_(is_referrers_class),
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003124 dex_pc_(dex_pc),
Calin Juravleb1498f62015-02-16 13:13:29 +00003125 generate_clinit_check_(false),
3126 loaded_class_rti_(ReferenceTypeInfo::CreateTop(/* is_exact */ false)) {}
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003127
3128 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003129
3130 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3131 return other->AsLoadClass()->type_index_ == type_index_;
3132 }
3133
3134 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
3135
3136 uint32_t GetDexPc() const { return dex_pc_; }
3137 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003138 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003139
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003140 bool NeedsEnvironment() const OVERRIDE {
3141 // Will call runtime and load the class if the class is not loaded yet.
3142 // TODO: finer grain decision.
3143 return !is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003144 }
3145
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003146 bool MustGenerateClinitCheck() const {
3147 return generate_clinit_check_;
3148 }
3149
3150 void SetMustGenerateClinitCheck() {
3151 generate_clinit_check_ = true;
3152 }
3153
3154 bool CanCallRuntime() const {
3155 return MustGenerateClinitCheck() || !is_referrers_class_;
3156 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003157
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003158 bool CanThrow() const OVERRIDE {
3159 // May call runtime and and therefore can throw.
3160 // TODO: finer grain decision.
3161 return !is_referrers_class_;
3162 }
3163
Calin Juravleacf735c2015-02-12 15:25:22 +00003164 ReferenceTypeInfo GetLoadedClassRTI() {
3165 return loaded_class_rti_;
3166 }
3167
3168 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
3169 // Make sure we only set exact types (the loaded class should never be merged).
3170 DCHECK(rti.IsExact());
3171 loaded_class_rti_ = rti;
3172 }
3173
3174 bool IsResolved() {
3175 return loaded_class_rti_.IsExact();
3176 }
3177
Nicolas Geoffray9437b782015-03-25 10:08:51 +00003178 bool NeedsDexCache() const OVERRIDE { return !is_referrers_class_; }
3179
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003180 DECLARE_INSTRUCTION(LoadClass);
3181
3182 private:
3183 const uint16_t type_index_;
3184 const bool is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003185 const uint32_t dex_pc_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003186 // Whether this instruction must generate the initialization check.
3187 // Used for code generation.
3188 bool generate_clinit_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003189
Calin Juravleacf735c2015-02-12 15:25:22 +00003190 ReferenceTypeInfo loaded_class_rti_;
3191
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003192 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
3193};
3194
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003195class HLoadString : public HExpression<0> {
3196 public:
3197 HLoadString(uint32_t string_index, uint32_t dex_pc)
3198 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3199 string_index_(string_index),
3200 dex_pc_(dex_pc) {}
3201
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003202 bool CanBeMoved() const OVERRIDE { return true; }
3203
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003204 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3205 return other->AsLoadString()->string_index_ == string_index_;
3206 }
3207
3208 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
3209
3210 uint32_t GetDexPc() const { return dex_pc_; }
3211 uint32_t GetStringIndex() const { return string_index_; }
3212
3213 // TODO: Can we deopt or debug when we resolve a string?
3214 bool NeedsEnvironment() const OVERRIDE { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00003215 bool NeedsDexCache() const OVERRIDE { return true; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003216
3217 DECLARE_INSTRUCTION(LoadString);
3218
3219 private:
3220 const uint32_t string_index_;
3221 const uint32_t dex_pc_;
3222
3223 DISALLOW_COPY_AND_ASSIGN(HLoadString);
3224};
3225
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003226// TODO: Pass this check to HInvokeStaticOrDirect nodes.
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003227/**
3228 * Performs an initialization check on its Class object input.
3229 */
3230class HClinitCheck : public HExpression<1> {
3231 public:
3232 explicit HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Nicolas Geoffraya0466e12015-03-27 15:00:40 +00003233 : HExpression(Primitive::kPrimNot, SideEffects::ChangesSomething()),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003234 dex_pc_(dex_pc) {
3235 SetRawInputAt(0, constant);
3236 }
3237
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003238 bool CanBeMoved() const OVERRIDE { return true; }
3239 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3240 UNUSED(other);
3241 return true;
3242 }
3243
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003244 bool NeedsEnvironment() const OVERRIDE {
3245 // May call runtime to initialize the class.
3246 return true;
3247 }
3248
3249 uint32_t GetDexPc() const { return dex_pc_; }
3250
3251 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
3252
3253 DECLARE_INSTRUCTION(ClinitCheck);
3254
3255 private:
3256 const uint32_t dex_pc_;
3257
3258 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
3259};
3260
3261class HStaticFieldGet : public HExpression<1> {
3262 public:
3263 HStaticFieldGet(HInstruction* cls,
3264 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003265 MemberOffset field_offset,
3266 bool is_volatile)
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003267 : HExpression(field_type, SideEffects::DependsOnSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00003268 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003269 SetRawInputAt(0, cls);
3270 }
3271
Calin Juravle52c48962014-12-16 17:02:57 +00003272
Calin Juravle10c9cbe2014-12-19 10:50:19 +00003273 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003274
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003275 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00003276 HStaticFieldGet* other_get = other->AsStaticFieldGet();
3277 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003278 }
3279
3280 size_t ComputeHashCode() const OVERRIDE {
3281 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
3282 }
3283
Calin Juravle52c48962014-12-16 17:02:57 +00003284 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003285 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
3286 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003287 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003288
3289 DECLARE_INSTRUCTION(StaticFieldGet);
3290
3291 private:
3292 const FieldInfo field_info_;
3293
3294 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
3295};
3296
3297class HStaticFieldSet : public HTemplateInstruction<2> {
3298 public:
3299 HStaticFieldSet(HInstruction* cls,
3300 HInstruction* value,
3301 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003302 MemberOffset field_offset,
3303 bool is_volatile)
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003304 : HTemplateInstruction(SideEffects::ChangesSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00003305 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003306 SetRawInputAt(0, cls);
3307 SetRawInputAt(1, value);
3308 }
3309
Calin Juravle52c48962014-12-16 17:02:57 +00003310 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003311 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
3312 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003313 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003314
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003315 HInstruction* GetValue() const { return InputAt(1); }
3316
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003317 DECLARE_INSTRUCTION(StaticFieldSet);
3318
3319 private:
3320 const FieldInfo field_info_;
3321
3322 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
3323};
3324
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003325// Implement the move-exception DEX instruction.
3326class HLoadException : public HExpression<0> {
3327 public:
3328 HLoadException() : HExpression(Primitive::kPrimNot, SideEffects::None()) {}
3329
3330 DECLARE_INSTRUCTION(LoadException);
3331
3332 private:
3333 DISALLOW_COPY_AND_ASSIGN(HLoadException);
3334};
3335
3336class HThrow : public HTemplateInstruction<1> {
3337 public:
3338 HThrow(HInstruction* exception, uint32_t dex_pc)
3339 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {
3340 SetRawInputAt(0, exception);
3341 }
3342
3343 bool IsControlFlow() const OVERRIDE { return true; }
3344
3345 bool NeedsEnvironment() const OVERRIDE { return true; }
3346
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003347 bool CanThrow() const OVERRIDE { return true; }
3348
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003349 uint32_t GetDexPc() const { return dex_pc_; }
3350
3351 DECLARE_INSTRUCTION(Throw);
3352
3353 private:
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003354 const uint32_t dex_pc_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003355
3356 DISALLOW_COPY_AND_ASSIGN(HThrow);
3357};
3358
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003359class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003360 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003361 HInstanceOf(HInstruction* object,
3362 HLoadClass* constant,
3363 bool class_is_final,
3364 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003365 : HExpression(Primitive::kPrimBoolean, SideEffects::None()),
3366 class_is_final_(class_is_final),
3367 dex_pc_(dex_pc) {
3368 SetRawInputAt(0, object);
3369 SetRawInputAt(1, constant);
3370 }
3371
3372 bool CanBeMoved() const OVERRIDE { return true; }
3373
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003374 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003375 return true;
3376 }
3377
3378 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003379 return false;
3380 }
3381
3382 uint32_t GetDexPc() const { return dex_pc_; }
3383
3384 bool IsClassFinal() const { return class_is_final_; }
3385
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003386 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003387
3388 private:
3389 const bool class_is_final_;
3390 const uint32_t dex_pc_;
3391
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003392 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
3393};
3394
Calin Juravleb1498f62015-02-16 13:13:29 +00003395class HBoundType : public HExpression<1> {
3396 public:
3397 HBoundType(HInstruction* input, ReferenceTypeInfo bound_type)
3398 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3399 bound_type_(bound_type) {
Calin Juravle61d544b2015-02-23 16:46:57 +00003400 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00003401 SetRawInputAt(0, input);
3402 }
3403
3404 const ReferenceTypeInfo& GetBoundType() const { return bound_type_; }
3405
3406 bool CanBeNull() const OVERRIDE {
3407 // `null instanceof ClassX` always return false so we can't be null.
3408 return false;
3409 }
3410
3411 DECLARE_INSTRUCTION(BoundType);
3412
3413 private:
3414 // Encodes the most upper class that this instruction can have. In other words
3415 // it is always the case that GetBoundType().IsSupertypeOf(GetReferenceType()).
3416 // It is used to bound the type in cases like `if (x instanceof ClassX) {}`
3417 const ReferenceTypeInfo bound_type_;
3418
3419 DISALLOW_COPY_AND_ASSIGN(HBoundType);
3420};
3421
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003422class HCheckCast : public HTemplateInstruction<2> {
3423 public:
3424 HCheckCast(HInstruction* object,
3425 HLoadClass* constant,
3426 bool class_is_final,
3427 uint32_t dex_pc)
3428 : HTemplateInstruction(SideEffects::None()),
3429 class_is_final_(class_is_final),
3430 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
3448 uint32_t GetDexPc() const { return dex_pc_; }
3449
3450 bool IsClassFinal() const { return class_is_final_; }
3451
3452 DECLARE_INSTRUCTION(CheckCast);
3453
3454 private:
3455 const bool class_is_final_;
3456 const uint32_t dex_pc_;
3457
3458 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003459};
3460
Calin Juravle27df7582015-04-17 19:12:31 +01003461class HMemoryBarrier : public HTemplateInstruction<0> {
3462 public:
3463 explicit HMemoryBarrier(MemBarrierKind barrier_kind)
3464 : HTemplateInstruction(SideEffects::None()),
3465 barrier_kind_(barrier_kind) {}
3466
3467 MemBarrierKind GetBarrierKind() { return barrier_kind_; }
3468
3469 DECLARE_INSTRUCTION(MemoryBarrier);
3470
3471 private:
3472 const MemBarrierKind barrier_kind_;
3473
3474 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
3475};
3476
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003477class HMonitorOperation : public HTemplateInstruction<1> {
3478 public:
3479 enum OperationKind {
3480 kEnter,
3481 kExit,
3482 };
3483
3484 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
3485 : HTemplateInstruction(SideEffects::None()), kind_(kind), dex_pc_(dex_pc) {
3486 SetRawInputAt(0, object);
3487 }
3488
3489 // Instruction may throw a Java exception, so we need an environment.
3490 bool NeedsEnvironment() const OVERRIDE { return true; }
3491 bool CanThrow() const OVERRIDE { return true; }
3492
3493 uint32_t GetDexPc() const { return dex_pc_; }
3494
3495 bool IsEnter() const { return kind_ == kEnter; }
3496
3497 DECLARE_INSTRUCTION(MonitorOperation);
3498
Calin Juravle52c48962014-12-16 17:02:57 +00003499 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003500 const OperationKind kind_;
3501 const uint32_t dex_pc_;
3502
3503 private:
3504 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
3505};
3506
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003507class MoveOperands : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003508 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01003509 MoveOperands(Location source,
3510 Location destination,
3511 Primitive::Type type,
3512 HInstruction* instruction)
3513 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003514
3515 Location GetSource() const { return source_; }
3516 Location GetDestination() const { return destination_; }
3517
3518 void SetSource(Location value) { source_ = value; }
3519 void SetDestination(Location value) { destination_ = value; }
3520
3521 // The parallel move resolver marks moves as "in-progress" by clearing the
3522 // destination (but not the source).
3523 Location MarkPending() {
3524 DCHECK(!IsPending());
3525 Location dest = destination_;
3526 destination_ = Location::NoLocation();
3527 return dest;
3528 }
3529
3530 void ClearPending(Location dest) {
3531 DCHECK(IsPending());
3532 destination_ = dest;
3533 }
3534
3535 bool IsPending() const {
3536 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
3537 return destination_.IsInvalid() && !source_.IsInvalid();
3538 }
3539
3540 // True if this blocks a move from the given location.
3541 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08003542 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003543 }
3544
3545 // A move is redundant if it's been eliminated, if its source and
3546 // destination are the same, or if its destination is unneeded.
3547 bool IsRedundant() const {
3548 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
3549 }
3550
3551 // We clear both operands to indicate move that's been eliminated.
3552 void Eliminate() {
3553 source_ = destination_ = Location::NoLocation();
3554 }
3555
3556 bool IsEliminated() const {
3557 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
3558 return source_.IsInvalid();
3559 }
3560
Nicolas Geoffray90218252015-04-15 11:56:51 +01003561 bool Is64BitMove() const {
3562 return Primitive::Is64BitType(type_);
3563 }
3564
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003565 HInstruction* GetInstruction() const { return instruction_; }
3566
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003567 private:
3568 Location source_;
3569 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01003570 // The type this move is for.
3571 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003572 // The instruction this move is assocatied with. Null when this move is
3573 // for moving an input in the expected locations of user (including a phi user).
3574 // This is only used in debug mode, to ensure we do not connect interval siblings
3575 // in the same parallel move.
3576 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003577};
3578
3579static constexpr size_t kDefaultNumberOfMoves = 4;
3580
3581class HParallelMove : public HTemplateInstruction<0> {
3582 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003583 explicit HParallelMove(ArenaAllocator* arena)
3584 : HTemplateInstruction(SideEffects::None()), moves_(arena, kDefaultNumberOfMoves) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003585
Nicolas Geoffray90218252015-04-15 11:56:51 +01003586 void AddMove(Location source,
3587 Location destination,
3588 Primitive::Type type,
3589 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003590 DCHECK(source.IsValid());
3591 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003592 if (kIsDebugBuild) {
3593 if (instruction != nullptr) {
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00003594 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003595 if (moves_.Get(i).GetInstruction() == instruction) {
3596 // Special case the situation where the move is for the spill slot
3597 // of the instruction.
3598 if ((GetPrevious() == instruction)
3599 || ((GetPrevious() == nullptr)
3600 && instruction->IsPhi()
3601 && instruction->GetBlock() == GetBlock())) {
3602 DCHECK_NE(destination.GetKind(), moves_.Get(i).GetDestination().GetKind())
3603 << "Doing parallel moves for the same instruction.";
3604 } else {
3605 DCHECK(false) << "Doing parallel moves for the same instruction.";
3606 }
3607 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00003608 }
3609 }
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003610 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Zheng Xuad4450e2015-04-17 18:48:56 +08003611 DCHECK(!destination.OverlapsWith(moves_.Get(i).GetDestination()))
3612 << "Overlapped destination for two moves in a parallel move.";
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003613 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003614 }
Nicolas Geoffray90218252015-04-15 11:56:51 +01003615 moves_.Add(MoveOperands(source, destination, type, instruction));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003616 }
3617
3618 MoveOperands* MoveOperandsAt(size_t index) const {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003619 return moves_.GetRawStorage() + index;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003620 }
3621
3622 size_t NumMoves() const { return moves_.Size(); }
3623
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003624 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003625
3626 private:
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003627 GrowableArray<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003628
3629 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
3630};
3631
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003632class HGraphVisitor : public ValueObject {
3633 public:
Dave Allison20dfc792014-06-16 20:44:29 -07003634 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
3635 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003636
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003637 virtual void VisitInstruction(HInstruction* instruction) { UNUSED(instruction); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003638 virtual void VisitBasicBlock(HBasicBlock* block);
3639
Roland Levillain633021e2014-10-01 14:12:25 +01003640 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003641 void VisitInsertionOrder();
3642
Roland Levillain633021e2014-10-01 14:12:25 +01003643 // Visit the graph following dominator tree reverse post-order.
3644 void VisitReversePostOrder();
3645
Nicolas Geoffray787c3072014-03-17 10:20:19 +00003646 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00003647
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003648 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003649#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003650 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
3651
3652 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
3653
3654#undef DECLARE_VISIT_INSTRUCTION
3655
3656 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07003657 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003658
3659 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
3660};
3661
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003662class HGraphDelegateVisitor : public HGraphVisitor {
3663 public:
3664 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
3665 virtual ~HGraphDelegateVisitor() {}
3666
3667 // Visit functions that delegate to to super class.
3668#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003669 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003670
3671 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
3672
3673#undef DECLARE_VISIT_INSTRUCTION
3674
3675 private:
3676 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
3677};
3678
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003679class HInsertionOrderIterator : public ValueObject {
3680 public:
3681 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
3682
3683 bool Done() const { return index_ == graph_.GetBlocks().Size(); }
3684 HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); }
3685 void Advance() { ++index_; }
3686
3687 private:
3688 const HGraph& graph_;
3689 size_t index_;
3690
3691 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
3692};
3693
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003694class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003695 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00003696 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
3697 // Check that reverse post order of the graph has been built.
3698 DCHECK(!graph.GetReversePostOrder().IsEmpty());
3699 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003700
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003701 bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); }
3702 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003703 void Advance() { ++index_; }
3704
3705 private:
3706 const HGraph& graph_;
3707 size_t index_;
3708
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003709 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003710};
3711
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003712class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003713 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003714 explicit HPostOrderIterator(const HGraph& graph)
David Brazdil10f56cb2015-03-24 18:49:14 +00003715 : graph_(graph), index_(graph_.GetReversePostOrder().Size()) {
3716 // Check that reverse post order of the graph has been built.
3717 DCHECK(!graph.GetReversePostOrder().IsEmpty());
3718 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003719
3720 bool Done() const { return index_ == 0; }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003721 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003722 void Advance() { --index_; }
3723
3724 private:
3725 const HGraph& graph_;
3726 size_t index_;
3727
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003728 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003729};
3730
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01003731class HLinearPostOrderIterator : public ValueObject {
3732 public:
3733 explicit HLinearPostOrderIterator(const HGraph& graph)
3734 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().Size()) {}
3735
3736 bool Done() const { return index_ == 0; }
3737
3738 HBasicBlock* Current() const { return order_.Get(index_ -1); }
3739
3740 void Advance() {
3741 --index_;
3742 DCHECK_GE(index_, 0U);
3743 }
3744
3745 private:
3746 const GrowableArray<HBasicBlock*>& order_;
3747 size_t index_;
3748
3749 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
3750};
3751
3752class HLinearOrderIterator : public ValueObject {
3753 public:
3754 explicit HLinearOrderIterator(const HGraph& graph)
3755 : order_(graph.GetLinearOrder()), index_(0) {}
3756
3757 bool Done() const { return index_ == order_.Size(); }
3758 HBasicBlock* Current() const { return order_.Get(index_); }
3759 void Advance() { ++index_; }
3760
3761 private:
3762 const GrowableArray<HBasicBlock*>& order_;
3763 size_t index_;
3764
3765 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
3766};
3767
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003768// Iterator over the blocks that art part of the loop. Includes blocks part
3769// of an inner loop. The order in which the blocks are iterated is on their
3770// block id.
3771class HBlocksInLoopIterator : public ValueObject {
3772 public:
3773 explicit HBlocksInLoopIterator(const HLoopInformation& info)
3774 : blocks_in_loop_(info.GetBlocks()),
3775 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
3776 index_(0) {
3777 if (!blocks_in_loop_.IsBitSet(index_)) {
3778 Advance();
3779 }
3780 }
3781
3782 bool Done() const { return index_ == blocks_.Size(); }
3783 HBasicBlock* Current() const { return blocks_.Get(index_); }
3784 void Advance() {
3785 ++index_;
3786 for (size_t e = blocks_.Size(); index_ < e; ++index_) {
3787 if (blocks_in_loop_.IsBitSet(index_)) {
3788 break;
3789 }
3790 }
3791 }
3792
3793 private:
3794 const BitVector& blocks_in_loop_;
3795 const GrowableArray<HBasicBlock*>& blocks_;
3796 size_t index_;
3797
3798 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
3799};
3800
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003801inline int64_t Int64FromConstant(HConstant* constant) {
3802 DCHECK(constant->IsIntConstant() || constant->IsLongConstant());
3803 return constant->IsIntConstant() ? constant->AsIntConstant()->GetValue()
3804 : constant->AsLongConstant()->GetValue();
3805}
3806
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003807} // namespace art
3808
3809#endif // ART_COMPILER_OPTIMIZING_NODES_H_