blob: dca95694069be6f9738e50c8cc436bd0b0a32bca [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
David Brazdil2d7352b2015-04-20 14:52:42 +0100100 // Return the number of instructions in the list. This is an expensive operation.
101 size_t CountSize() const;
102
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100103 private:
104 HInstruction* first_instruction_;
105 HInstruction* last_instruction_;
106
107 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000108 friend class HGraph;
109 friend class HInstruction;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100110 friend class HInstructionIterator;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100111 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100112
113 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
114};
115
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000116// Control-flow graph of a method. Contains a list of basic blocks.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700117class HGraph : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000118 public:
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000119 HGraph(ArenaAllocator* arena, bool debuggable = false, int start_instruction_id = 0)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000120 : arena_(arena),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000121 blocks_(arena, kDefaultNumberOfBlocks),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100122 reverse_post_order_(arena, kDefaultNumberOfBlocks),
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100123 linear_order_(arena, kDefaultNumberOfBlocks),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700124 entry_block_(nullptr),
125 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100126 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100127 number_of_vregs_(0),
128 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000129 temporaries_vreg_slots_(0),
Mark Mendell1152c922015-04-24 17:06:35 -0400130 has_bounds_checks_(false),
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000131 debuggable_(debuggable),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000132 current_instruction_id_(start_instruction_id),
133 cached_null_constant_(nullptr),
134 cached_int_constants_(std::less<int32_t>(), arena->Adapter()),
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000135 cached_float_constants_(std::less<int32_t>(), arena->Adapter()),
136 cached_long_constants_(std::less<int64_t>(), arena->Adapter()),
137 cached_double_constants_(std::less<int64_t>(), arena->Adapter()) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000138
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000139 ArenaAllocator* GetArena() const { return arena_; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100140 const GrowableArray<HBasicBlock*>& GetBlocks() const { return blocks_; }
Roland Levillain93445682014-10-06 19:24:02 +0100141 HBasicBlock* GetBlock(size_t id) const { return blocks_.Get(id); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000142
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000143 HBasicBlock* GetEntryBlock() const { return entry_block_; }
144 HBasicBlock* GetExitBlock() const { return exit_block_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000145
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000146 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
147 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000148
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000149 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100150
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000151 // Try building the SSA form of this graph, with dominance computation and loop
152 // recognition. Returns whether it was successful in doing all these steps.
153 bool TryBuildingSsa() {
154 BuildDominatorTree();
Nicolas Geoffrayd3350832015-03-12 11:16:23 +0000155 // The SSA builder requires loops to all be natural. Specifically, the dead phi
156 // elimination phase checks the consistency of the graph when doing a post-order
157 // visit for eliminating dead phis: a dead phi can only have loop header phi
158 // users remaining when being visited.
159 if (!AnalyzeNaturalLoops()) return false;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000160 TransformToSsa();
Nicolas Geoffrayd3350832015-03-12 11:16:23 +0000161 return true;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000162 }
163
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000164 void BuildDominatorTree();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000165 void TransformToSsa();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100166 void SimplifyCFG();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000167
Nicolas Geoffrayf5370122014-12-02 11:51:19 +0000168 // Analyze all natural loops in this graph. Returns false if one
169 // loop is not natural, that is the header does not dominate the
170 // back edge.
171 bool AnalyzeNaturalLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100172
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000173 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
174 void InlineInto(HGraph* outer_graph, HInvoke* invoke);
175
David Brazdil2d7352b2015-04-20 14:52:42 +0100176 // Removes `block` from the graph.
177 void DeleteDeadBlock(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000178
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100179 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
180 void SimplifyLoop(HBasicBlock* header);
181
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000182 int32_t GetNextInstructionId() {
183 DCHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000184 return current_instruction_id_++;
185 }
186
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000187 int32_t GetCurrentInstructionId() const {
188 return current_instruction_id_;
189 }
190
191 void SetCurrentInstructionId(int32_t id) {
192 current_instruction_id_ = id;
193 }
194
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100195 uint16_t GetMaximumNumberOfOutVRegs() const {
196 return maximum_number_of_out_vregs_;
197 }
198
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000199 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
200 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100201 }
202
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000203 void UpdateTemporariesVRegSlots(size_t slots) {
204 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100205 }
206
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000207 size_t GetTemporariesVRegSlots() const {
208 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100209 }
210
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100211 void SetNumberOfVRegs(uint16_t number_of_vregs) {
212 number_of_vregs_ = number_of_vregs;
213 }
214
215 uint16_t GetNumberOfVRegs() const {
216 return number_of_vregs_;
217 }
218
219 void SetNumberOfInVRegs(uint16_t value) {
220 number_of_in_vregs_ = value;
221 }
222
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100223 uint16_t GetNumberOfLocalVRegs() const {
224 return number_of_vregs_ - number_of_in_vregs_;
225 }
226
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100227 const GrowableArray<HBasicBlock*>& GetReversePostOrder() const {
228 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100229 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100230
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100231 const GrowableArray<HBasicBlock*>& GetLinearOrder() const {
232 return linear_order_;
233 }
234
Mark Mendell1152c922015-04-24 17:06:35 -0400235 bool HasBoundsChecks() const {
236 return has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800237 }
238
Mark Mendell1152c922015-04-24 17:06:35 -0400239 void SetHasBoundsChecks(bool value) {
240 has_bounds_checks_ = value;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800241 }
242
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000243 bool IsDebuggable() const { return debuggable_; }
244
David Brazdil8d5b8b22015-03-24 10:51:52 +0000245 // Returns a constant of the given type and value. If it does not exist
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000246 // already, it is created and inserted into the graph. This method is only for
247 // integral types.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000248 HConstant* GetConstant(Primitive::Type type, int64_t value);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000249 HNullConstant* GetNullConstant();
David Brazdil8d5b8b22015-03-24 10:51:52 +0000250 HIntConstant* GetIntConstant(int32_t value) {
251 return CreateConstant(value, &cached_int_constants_);
252 }
253 HLongConstant* GetLongConstant(int64_t value) {
254 return CreateConstant(value, &cached_long_constants_);
255 }
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000256 HFloatConstant* GetFloatConstant(float value) {
257 return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_);
258 }
259 HDoubleConstant* GetDoubleConstant(double value) {
260 return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_);
261 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000262
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000263 HBasicBlock* FindCommonDominator(HBasicBlock* first, HBasicBlock* second) const;
David Brazdil2d7352b2015-04-20 14:52:42 +0100264
265 private:
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000266 void VisitBlockForDominatorTree(HBasicBlock* block,
267 HBasicBlock* predecessor,
268 GrowableArray<size_t>* visits);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100269 void FindBackEdges(ArenaBitVector* visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000270 void VisitBlockForBackEdges(HBasicBlock* block,
271 ArenaBitVector* visited,
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100272 ArenaBitVector* visiting);
Roland Levillainfc600dc2014-12-02 17:16:31 +0000273 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100274 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000275
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000276 template <class InstructionType, typename ValueType>
277 InstructionType* CreateConstant(ValueType value,
278 ArenaSafeMap<ValueType, InstructionType*>* cache) {
279 // Try to find an existing constant of the given value.
280 InstructionType* constant = nullptr;
281 auto cached_constant = cache->find(value);
282 if (cached_constant != cache->end()) {
283 constant = cached_constant->second;
284 }
285
286 // If not found or previously deleted, create and cache a new instruction.
287 if (constant == nullptr || constant->GetBlock() == nullptr) {
288 constant = new (arena_) InstructionType(value);
289 cache->Overwrite(value, constant);
290 InsertConstant(constant);
291 }
292 return constant;
293 }
294
David Brazdil8d5b8b22015-03-24 10:51:52 +0000295 void InsertConstant(HConstant* instruction);
296
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000297 // Cache a float constant into the graph. This method should only be
298 // called by the SsaBuilder when creating "equivalent" instructions.
299 void CacheFloatConstant(HFloatConstant* constant);
300
301 // See CacheFloatConstant comment.
302 void CacheDoubleConstant(HDoubleConstant* constant);
303
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000304 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000305
306 // List of blocks in insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000307 GrowableArray<HBasicBlock*> blocks_;
308
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100309 // List of blocks to perform a reverse post order tree traversal.
310 GrowableArray<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000311
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100312 // List of blocks to perform a linear order tree traversal.
313 GrowableArray<HBasicBlock*> linear_order_;
314
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000315 HBasicBlock* entry_block_;
316 HBasicBlock* exit_block_;
317
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100318 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100319 uint16_t maximum_number_of_out_vregs_;
320
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100321 // The number of virtual registers in this method. Contains the parameters.
322 uint16_t number_of_vregs_;
323
324 // The number of virtual registers used by parameters of this method.
325 uint16_t number_of_in_vregs_;
326
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000327 // Number of vreg size slots that the temporaries use (used in baseline compiler).
328 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100329
Mark Mendell1152c922015-04-24 17:06:35 -0400330 // Has bounds checks. We can totally skip BCE if it's false.
331 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800332
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000333 // Indicates whether the graph should be compiled in a way that
334 // ensures full debuggability. If false, we can apply more
335 // aggressive optimizations that may limit the level of debugging.
336 const bool debuggable_;
337
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000338 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000339 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000340
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000341 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000342 HNullConstant* cached_null_constant_;
343 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000344 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000345 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000346 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000347
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000348 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100349 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000350 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000351 DISALLOW_COPY_AND_ASSIGN(HGraph);
352};
353
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700354class HLoopInformation : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000355 public:
356 HLoopInformation(HBasicBlock* header, HGraph* graph)
357 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100358 suspend_check_(nullptr),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100359 back_edges_(graph->GetArena(), kDefaultNumberOfBackEdges),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100360 // Make bit vector growable, as the number of blocks may change.
361 blocks_(graph->GetArena(), graph->GetBlocks().Size(), true) {}
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100362
363 HBasicBlock* GetHeader() const {
364 return header_;
365 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000366
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000367 void SetHeader(HBasicBlock* block) {
368 header_ = block;
369 }
370
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100371 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
372 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
373 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
374
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000375 void AddBackEdge(HBasicBlock* back_edge) {
376 back_edges_.Add(back_edge);
377 }
378
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100379 void RemoveBackEdge(HBasicBlock* back_edge) {
380 back_edges_.Delete(back_edge);
381 }
382
David Brazdil46e2a392015-03-16 17:31:52 +0000383 bool IsBackEdge(const HBasicBlock& block) const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100384 for (size_t i = 0, e = back_edges_.Size(); i < e; ++i) {
David Brazdil46e2a392015-03-16 17:31:52 +0000385 if (back_edges_.Get(i) == &block) return true;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100386 }
387 return false;
388 }
389
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000390 size_t NumberOfBackEdges() const {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000391 return back_edges_.Size();
392 }
393
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100394 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100395
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100396 const GrowableArray<HBasicBlock*>& GetBackEdges() const {
397 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100398 }
399
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100400 void ClearBackEdges() {
401 back_edges_.Reset();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100402 }
403
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100404 // Find blocks that are part of this loop. Returns whether the loop is a natural loop,
405 // that is the header dominates the back edge.
406 bool Populate();
407
408 // Returns whether this loop information contains `block`.
409 // Note that this loop information *must* be populated before entering this function.
410 bool Contains(const HBasicBlock& block) const;
411
412 // Returns whether this loop information is an inner loop of `other`.
413 // Note that `other` *must* be populated before entering this function.
414 bool IsIn(const HLoopInformation& other) const;
415
416 const ArenaBitVector& GetBlocks() const { return blocks_; }
417
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000418 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000419 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000420
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000421 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100422 // Internal recursive implementation of `Populate`.
423 void PopulateRecursive(HBasicBlock* block);
424
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000425 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100426 HSuspendCheck* suspend_check_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000427 GrowableArray<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100428 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000429
430 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
431};
432
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100433static constexpr size_t kNoLifetime = -1;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100434static constexpr uint32_t kNoDexPc = -1;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100435
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000436// A block in a method. Contains the list of instructions represented
437// as a double linked list. Each block knows its predecessors and
438// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100439
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700440class HBasicBlock : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000441 public:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100442 explicit HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000443 : graph_(graph),
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000444 predecessors_(graph->GetArena(), kDefaultNumberOfPredecessors),
445 successors_(graph->GetArena(), kDefaultNumberOfSuccessors),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000446 loop_information_(nullptr),
447 dominator_(nullptr),
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100448 dominated_blocks_(graph->GetArena(), kDefaultNumberOfDominatedBlocks),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100449 block_id_(-1),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100450 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100451 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000452 lifetime_end_(kNoLifetime),
453 is_catch_block_(false) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000454
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100455 const GrowableArray<HBasicBlock*>& GetPredecessors() const {
456 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000457 }
458
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100459 const GrowableArray<HBasicBlock*>& GetSuccessors() const {
460 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000461 }
462
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100463 const GrowableArray<HBasicBlock*>& GetDominatedBlocks() const {
464 return dominated_blocks_;
465 }
466
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100467 bool IsEntryBlock() const {
468 return graph_->GetEntryBlock() == this;
469 }
470
471 bool IsExitBlock() const {
472 return graph_->GetExitBlock() == this;
473 }
474
David Brazdil46e2a392015-03-16 17:31:52 +0000475 bool IsSingleGoto() const;
476
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000477 void AddBackEdge(HBasicBlock* back_edge) {
478 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000479 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000480 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100481 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000482 loop_information_->AddBackEdge(back_edge);
483 }
484
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000485 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000486 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000487
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000488 int GetBlockId() const { return block_id_; }
489 void SetBlockId(int id) { block_id_ = id; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000490
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000491 HBasicBlock* GetDominator() const { return dominator_; }
492 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100493 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.Add(block); }
David Brazdil2d7352b2015-04-20 14:52:42 +0100494 void RemoveDominatedBlock(HBasicBlock* block) { dominated_blocks_.Delete(block); }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000495 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
496 for (size_t i = 0, e = dominated_blocks_.Size(); i < e; ++i) {
497 if (dominated_blocks_.Get(i) == existing) {
498 dominated_blocks_.Put(i, new_block);
499 return;
500 }
501 }
502 LOG(FATAL) << "Unreachable";
503 UNREACHABLE();
504 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000505
506 int NumberOfBackEdges() const {
507 return loop_information_ == nullptr
508 ? 0
509 : loop_information_->NumberOfBackEdges();
510 }
511
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100512 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
513 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100514 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100515 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +0100516 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
517 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000518
519 void AddSuccessor(HBasicBlock* block) {
520 successors_.Add(block);
521 block->predecessors_.Add(this);
522 }
523
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100524 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
525 size_t successor_index = GetSuccessorIndexOf(existing);
526 DCHECK_NE(successor_index, static_cast<size_t>(-1));
527 existing->RemovePredecessor(this);
528 new_block->predecessors_.Add(this);
529 successors_.Put(successor_index, new_block);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000530 }
531
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000532 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
533 size_t predecessor_index = GetPredecessorIndexOf(existing);
534 DCHECK_NE(predecessor_index, static_cast<size_t>(-1));
535 existing->RemoveSuccessor(this);
536 new_block->successors_.Add(this);
537 predecessors_.Put(predecessor_index, new_block);
538 }
539
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100540 void RemovePredecessor(HBasicBlock* block) {
541 predecessors_.Delete(block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100542 }
543
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000544 void RemoveSuccessor(HBasicBlock* block) {
545 successors_.Delete(block);
546 }
547
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100548 void ClearAllPredecessors() {
549 predecessors_.Reset();
550 }
551
552 void AddPredecessor(HBasicBlock* block) {
553 predecessors_.Add(block);
554 block->successors_.Add(this);
555 }
556
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100557 void SwapPredecessors() {
Nicolas Geoffrayc83d4412014-09-18 16:46:20 +0100558 DCHECK_EQ(predecessors_.Size(), 2u);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100559 HBasicBlock* temp = predecessors_.Get(0);
560 predecessors_.Put(0, predecessors_.Get(1));
561 predecessors_.Put(1, temp);
562 }
563
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100564 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) {
565 for (size_t i = 0, e = predecessors_.Size(); i < e; ++i) {
566 if (predecessors_.Get(i) == predecessor) {
567 return i;
568 }
569 }
570 return -1;
571 }
572
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100573 size_t GetSuccessorIndexOf(HBasicBlock* successor) {
574 for (size_t i = 0, e = successors_.Size(); i < e; ++i) {
575 if (successors_.Get(i) == successor) {
576 return i;
577 }
578 }
579 return -1;
580 }
581
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000582 // Split the block into two blocks just after `cursor`. Returns the newly
583 // created block. Note that this method just updates raw block information,
584 // like predecessors, successors, dominators, and instruction list. It does not
585 // update the graph, reverse post order, loop information, nor make sure the
586 // blocks are consistent (for example ending with a control flow instruction).
587 HBasicBlock* SplitAfter(HInstruction* cursor);
588
589 // Merge `other` at the end of `this`. Successors and dominated blocks of
590 // `other` are changed to be successors and dominated blocks of `this`. Note
591 // that this method does not update the graph, reverse post order, loop
592 // information, nor make sure the blocks are consistent (for example ending
593 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +0100594 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000595
596 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
597 // of `this` are moved to `other`.
598 // Note that this method does not update the graph, reverse post order, loop
599 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +0000600 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000601 void ReplaceWith(HBasicBlock* other);
602
David Brazdil2d7352b2015-04-20 14:52:42 +0100603 // Merge `other` at the end of `this`. This method updates loops, reverse post
604 // order, links to predecessors, successors, dominators and deletes the block
605 // from the graph. The two blocks must be successive, i.e. `this` the only
606 // predecessor of `other` and vice versa.
607 void MergeWith(HBasicBlock* other);
608
609 // Disconnects `this` from all its predecessors, successors and dominator,
610 // removes it from all loops it is included in and eventually from the graph.
611 // The block must not dominate any other block. Predecessors and successors
612 // are safely updated.
613 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +0000614
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000615 void AddInstruction(HInstruction* instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100616 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +0100617 // Replace instruction `initial` with `replacement` within this block.
618 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
619 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100620 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100621 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +0000622 // RemoveInstruction and RemovePhi delete a given instruction from the respective
623 // instruction list. With 'ensure_safety' set to true, it verifies that the
624 // instruction is not in use and removes it from the use lists of its inputs.
625 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
626 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +0100627 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100628
629 bool IsLoopHeader() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100630 return (loop_information_ != nullptr) && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100631 }
632
Roland Levillain6b879dd2014-09-22 17:13:44 +0100633 bool IsLoopPreHeaderFirstPredecessor() const {
634 DCHECK(IsLoopHeader());
635 DCHECK(!GetPredecessors().IsEmpty());
636 return GetPredecessors().Get(0) == GetLoopInformation()->GetPreHeader();
637 }
638
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100639 HLoopInformation* GetLoopInformation() const {
640 return loop_information_;
641 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000642
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000643 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100644 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000645 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100646 void SetInLoop(HLoopInformation* info) {
647 if (IsLoopHeader()) {
648 // Nothing to do. This just means `info` is an outer loop.
649 } else if (loop_information_ == nullptr) {
650 loop_information_ = info;
651 } else if (loop_information_->Contains(*info->GetHeader())) {
652 // Block is currently part of an outer loop. Make it part of this inner loop.
653 // Note that a non loop header having a loop information means this loop information
654 // has already been populated
655 loop_information_ = info;
656 } else {
657 // Block is part of an inner loop. Do not update the loop information.
658 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
659 // at this point, because this method is being called while populating `info`.
660 }
661 }
662
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000663 // Raw update of the loop information.
664 void SetLoopInformation(HLoopInformation* info) {
665 loop_information_ = info;
666 }
667
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100668 bool IsInLoop() const { return loop_information_ != nullptr; }
669
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100670 // Returns wheter this block dominates the blocked passed as parameter.
671 bool Dominates(HBasicBlock* block) const;
672
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100673 size_t GetLifetimeStart() const { return lifetime_start_; }
674 size_t GetLifetimeEnd() const { return lifetime_end_; }
675
676 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
677 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
678
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100679 uint32_t GetDexPc() const { return dex_pc_; }
680
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000681 bool IsCatchBlock() const { return is_catch_block_; }
682 void SetIsCatchBlock() { is_catch_block_ = true; }
683
David Brazdil8d5b8b22015-03-24 10:51:52 +0000684 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +0000685 bool EndsWithIf() const;
686 bool HasSinglePhi() const;
687
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000688 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000689 HGraph* graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000690 GrowableArray<HBasicBlock*> predecessors_;
691 GrowableArray<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100692 HInstructionList instructions_;
693 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000694 HLoopInformation* loop_information_;
695 HBasicBlock* dominator_;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100696 GrowableArray<HBasicBlock*> dominated_blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000697 int block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100698 // The dex program counter of the first instruction of this block.
699 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100700 size_t lifetime_start_;
701 size_t lifetime_end_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000702 bool is_catch_block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000703
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000704 friend class HGraph;
705 friend class HInstruction;
706
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000707 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
708};
709
David Brazdilb2bd1c52015-03-25 11:17:37 +0000710// Iterates over the LoopInformation of all loops which contain 'block'
711// from the innermost to the outermost.
712class HLoopInformationOutwardIterator : public ValueObject {
713 public:
714 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
715 : current_(block.GetLoopInformation()) {}
716
717 bool Done() const { return current_ == nullptr; }
718
719 void Advance() {
720 DCHECK(!Done());
721 current_ = current_->GetHeader()->GetDominator()->GetLoopInformation();
722 }
723
724 HLoopInformation* Current() const {
725 DCHECK(!Done());
726 return current_;
727 }
728
729 private:
730 HLoopInformation* current_;
731
732 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
733};
734
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100735#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
736 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000737 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000738 M(ArrayGet, Instruction) \
739 M(ArrayLength, Instruction) \
740 M(ArraySet, Instruction) \
David Brazdil66d126e2015-04-03 16:02:44 +0100741 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000742 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +0000743 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000744 M(CheckCast, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100745 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000746 M(Compare, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100747 M(Condition, BinaryOperation) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700748 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000749 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +0000750 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000751 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100752 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000753 M(Exit, Instruction) \
754 M(FloatConstant, Constant) \
755 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100756 M(GreaterThan, Condition) \
757 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100758 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000759 M(InstanceFieldGet, Instruction) \
760 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000761 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100762 M(IntConstant, Constant) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +0000763 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000764 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100765 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000766 M(LessThan, Condition) \
767 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000768 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000769 M(LoadException, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100770 M(LoadLocal, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000771 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100772 M(Local, Instruction) \
773 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +0100774 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +0000775 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000776 M(Mul, BinaryOperation) \
777 M(Neg, UnaryOperation) \
778 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100779 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +0100780 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000781 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000782 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000783 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000784 M(Or, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100785 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000786 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100787 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000788 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100789 M(Return, Instruction) \
790 M(ReturnVoid, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000791 M(Shl, BinaryOperation) \
792 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100793 M(StaticFieldGet, Instruction) \
794 M(StaticFieldSet, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100795 M(StoreLocal, Instruction) \
796 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100797 M(SuspendCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000798 M(Temporary, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000799 M(Throw, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +0000800 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000801 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000802 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000803
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100804#define FOR_EACH_INSTRUCTION(M) \
805 FOR_EACH_CONCRETE_INSTRUCTION(M) \
806 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +0100807 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +0100808 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100809 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -0700810
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100811#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000812FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
813#undef FORWARD_DECLARATION
814
Roland Levillainccc07a92014-09-16 14:48:16 +0100815#define DECLARE_INSTRUCTION(type) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000816 InstructionKind GetKind() const OVERRIDE { return k##type; } \
817 const char* DebugName() const OVERRIDE { return #type; } \
818 const H##type* As##type() const OVERRIDE { return this; } \
819 H##type* As##type() OVERRIDE { return this; } \
820 bool InstructionTypeEquals(HInstruction* other) const OVERRIDE { \
Roland Levillainccc07a92014-09-16 14:48:16 +0100821 return other->Is##type(); \
822 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000823 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000824
David Brazdiled596192015-01-23 10:39:45 +0000825template <typename T> class HUseList;
826
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100827template <typename T>
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700828class HUseListNode : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000829 public:
David Brazdiled596192015-01-23 10:39:45 +0000830 HUseListNode* GetPrevious() const { return prev_; }
831 HUseListNode* GetNext() const { return next_; }
832 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100833 size_t GetIndex() const { return index_; }
834
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000835 private:
David Brazdiled596192015-01-23 10:39:45 +0000836 HUseListNode(T user, size_t index)
837 : user_(user), index_(index), prev_(nullptr), next_(nullptr) {}
838
839 T const user_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100840 const size_t index_;
David Brazdiled596192015-01-23 10:39:45 +0000841 HUseListNode<T>* prev_;
842 HUseListNode<T>* next_;
843
844 friend class HUseList<T>;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000845
846 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
847};
848
David Brazdiled596192015-01-23 10:39:45 +0000849template <typename T>
850class HUseList : public ValueObject {
851 public:
852 HUseList() : first_(nullptr) {}
853
854 void Clear() {
855 first_ = nullptr;
856 }
857
858 // Adds a new entry at the beginning of the use list and returns
859 // the newly created node.
860 HUseListNode<T>* AddUse(T user, size_t index, ArenaAllocator* arena) {
David Brazdilea55b932015-01-27 17:12:29 +0000861 HUseListNode<T>* new_node = new (arena) HUseListNode<T>(user, index);
David Brazdiled596192015-01-23 10:39:45 +0000862 if (IsEmpty()) {
863 first_ = new_node;
864 } else {
865 first_->prev_ = new_node;
866 new_node->next_ = first_;
867 first_ = new_node;
868 }
869 return new_node;
870 }
871
872 HUseListNode<T>* GetFirst() const {
873 return first_;
874 }
875
876 void Remove(HUseListNode<T>* node) {
David Brazdil1abb4192015-02-17 18:33:36 +0000877 DCHECK(node != nullptr);
878 DCHECK(Contains(node));
879
David Brazdiled596192015-01-23 10:39:45 +0000880 if (node->prev_ != nullptr) {
881 node->prev_->next_ = node->next_;
882 }
883 if (node->next_ != nullptr) {
884 node->next_->prev_ = node->prev_;
885 }
886 if (node == first_) {
887 first_ = node->next_;
888 }
889 }
890
David Brazdil1abb4192015-02-17 18:33:36 +0000891 bool Contains(const HUseListNode<T>* node) const {
892 if (node == nullptr) {
893 return false;
894 }
895 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
896 if (current == node) {
897 return true;
898 }
899 }
900 return false;
901 }
902
David Brazdiled596192015-01-23 10:39:45 +0000903 bool IsEmpty() const {
904 return first_ == nullptr;
905 }
906
907 bool HasOnlyOneUse() const {
908 return first_ != nullptr && first_->next_ == nullptr;
909 }
910
911 private:
912 HUseListNode<T>* first_;
913};
914
915template<typename T>
916class HUseIterator : public ValueObject {
917 public:
918 explicit HUseIterator(const HUseList<T>& uses) : current_(uses.GetFirst()) {}
919
920 bool Done() const { return current_ == nullptr; }
921
922 void Advance() {
923 DCHECK(!Done());
924 current_ = current_->GetNext();
925 }
926
927 HUseListNode<T>* Current() const {
928 DCHECK(!Done());
929 return current_;
930 }
931
932 private:
933 HUseListNode<T>* current_;
934
935 friend class HValue;
936};
937
David Brazdil1abb4192015-02-17 18:33:36 +0000938// This class is used by HEnvironment and HInstruction classes to record the
939// instructions they use and pointers to the corresponding HUseListNodes kept
940// by the used instructions.
941template <typename T>
942class HUserRecord : public ValueObject {
943 public:
944 HUserRecord() : instruction_(nullptr), use_node_(nullptr) {}
945 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), use_node_(nullptr) {}
946
947 HUserRecord(const HUserRecord<T>& old_record, HUseListNode<T>* use_node)
948 : instruction_(old_record.instruction_), use_node_(use_node) {
949 DCHECK(instruction_ != nullptr);
950 DCHECK(use_node_ != nullptr);
951 DCHECK(old_record.use_node_ == nullptr);
952 }
953
954 HInstruction* GetInstruction() const { return instruction_; }
955 HUseListNode<T>* GetUseNode() const { return use_node_; }
956
957 private:
958 // Instruction used by the user.
959 HInstruction* instruction_;
960
961 // Corresponding entry in the use list kept by 'instruction_'.
962 HUseListNode<T>* use_node_;
963};
964
Calin Juravle27df7582015-04-17 19:12:31 +0100965// TODO: Add better documentation to this class and maybe refactor with more suggestive names.
966// - Has(All)SideEffects suggests that all the side effects are present but only ChangesSomething
967// flag is consider.
968// - DependsOn suggests that there is a real dependency between side effects but it only
969// checks DependendsOnSomething flag.
970//
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100971// Represents the side effects an instruction may have.
972class SideEffects : public ValueObject {
973 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100974 SideEffects() : flags_(0) {}
975
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100976 static SideEffects None() {
977 return SideEffects(0);
978 }
979
980 static SideEffects All() {
981 return SideEffects(ChangesSomething().flags_ | DependsOnSomething().flags_);
982 }
983
984 static SideEffects ChangesSomething() {
985 return SideEffects((1 << kFlagChangesCount) - 1);
986 }
987
988 static SideEffects DependsOnSomething() {
989 int count = kFlagDependsOnCount - kFlagChangesCount;
990 return SideEffects(((1 << count) - 1) << kFlagChangesCount);
991 }
992
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100993 SideEffects Union(SideEffects other) const {
994 return SideEffects(flags_ | other.flags_);
995 }
996
Roland Levillain72bceff2014-09-15 18:29:00 +0100997 bool HasSideEffects() const {
998 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
999 return (flags_ & all_bits_set) != 0;
1000 }
1001
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001002 bool HasAllSideEffects() const {
1003 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
1004 return all_bits_set == (flags_ & all_bits_set);
1005 }
1006
1007 bool DependsOn(SideEffects other) const {
1008 size_t depends_flags = other.ComputeDependsFlags();
1009 return (flags_ & depends_flags) != 0;
1010 }
1011
1012 bool HasDependencies() const {
1013 int count = kFlagDependsOnCount - kFlagChangesCount;
1014 size_t all_bits_set = (1 << count) - 1;
1015 return ((flags_ >> kFlagChangesCount) & all_bits_set) != 0;
1016 }
1017
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001018 private:
1019 static constexpr int kFlagChangesSomething = 0;
1020 static constexpr int kFlagChangesCount = kFlagChangesSomething + 1;
1021
1022 static constexpr int kFlagDependsOnSomething = kFlagChangesCount;
1023 static constexpr int kFlagDependsOnCount = kFlagDependsOnSomething + 1;
1024
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001025 explicit SideEffects(size_t flags) : flags_(flags) {}
1026
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001027 size_t ComputeDependsFlags() const {
1028 return flags_ << kFlagChangesCount;
1029 }
1030
1031 size_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001032};
1033
David Brazdiled596192015-01-23 10:39:45 +00001034// A HEnvironment object contains the values of virtual registers at a given location.
1035class HEnvironment : public ArenaObject<kArenaAllocMisc> {
1036 public:
1037 HEnvironment(ArenaAllocator* arena, size_t number_of_vregs)
1038 : vregs_(arena, number_of_vregs) {
1039 vregs_.SetSize(number_of_vregs);
1040 for (size_t i = 0; i < number_of_vregs; i++) {
David Brazdil1abb4192015-02-17 18:33:36 +00001041 vregs_.Put(i, HUserRecord<HEnvironment*>());
David Brazdiled596192015-01-23 10:39:45 +00001042 }
1043 }
1044
1045 void CopyFrom(HEnvironment* env);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001046 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1047 // input to the loop phi instead. This is for inserting instructions that
1048 // require an environment (like HDeoptimization) in the loop pre-header.
1049 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001050
1051 void SetRawEnvAt(size_t index, HInstruction* instruction) {
David Brazdil1abb4192015-02-17 18:33:36 +00001052 vregs_.Put(index, HUserRecord<HEnvironment*>(instruction));
David Brazdiled596192015-01-23 10:39:45 +00001053 }
1054
1055 HInstruction* GetInstructionAt(size_t index) const {
David Brazdil1abb4192015-02-17 18:33:36 +00001056 return vregs_.Get(index).GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001057 }
1058
David Brazdil1abb4192015-02-17 18:33:36 +00001059 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001060
1061 size_t Size() const { return vregs_.Size(); }
1062
1063 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001064 // Record instructions' use entries of this environment for constant-time removal.
1065 // It should only be called by HInstruction when a new environment use is added.
1066 void RecordEnvUse(HUseListNode<HEnvironment*>* env_use) {
1067 DCHECK(env_use->GetUser() == this);
1068 size_t index = env_use->GetIndex();
1069 vregs_.Put(index, HUserRecord<HEnvironment*>(vregs_.Get(index), env_use));
1070 }
David Brazdiled596192015-01-23 10:39:45 +00001071
David Brazdil1abb4192015-02-17 18:33:36 +00001072 GrowableArray<HUserRecord<HEnvironment*> > vregs_;
David Brazdiled596192015-01-23 10:39:45 +00001073
David Brazdil1abb4192015-02-17 18:33:36 +00001074 friend HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001075
1076 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1077};
1078
Calin Juravleacf735c2015-02-12 15:25:22 +00001079class ReferenceTypeInfo : ValueObject {
1080 public:
Calin Juravleb1498f62015-02-16 13:13:29 +00001081 typedef Handle<mirror::Class> TypeHandle;
1082
1083 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact)
1084 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1085 if (type_handle->IsObjectClass()) {
1086 // Override the type handle to be consistent with the case when we get to
1087 // Top but don't have the Object class available. It avoids having to guess
1088 // what value the type_handle has when it's Top.
1089 return ReferenceTypeInfo(TypeHandle(), is_exact, true);
1090 } else {
1091 return ReferenceTypeInfo(type_handle, is_exact, false);
1092 }
1093 }
1094
1095 static ReferenceTypeInfo CreateTop(bool is_exact) {
1096 return ReferenceTypeInfo(TypeHandle(), is_exact, true);
Calin Juravleacf735c2015-02-12 15:25:22 +00001097 }
1098
1099 bool IsExact() const { return is_exact_; }
1100 bool IsTop() const { return is_top_; }
1101
1102 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
1103
Calin Juravleb1498f62015-02-16 13:13:29 +00001104 bool IsSupertypeOf(ReferenceTypeInfo rti) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Calin Juravleacf735c2015-02-12 15:25:22 +00001105 if (IsTop()) {
1106 // Top (equivalent for java.lang.Object) is supertype of anything.
1107 return true;
1108 }
1109 if (rti.IsTop()) {
1110 // If we get here `this` is not Top() so it can't be a supertype.
1111 return false;
1112 }
1113 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
1114 }
1115
1116 // Returns true if the type information provide the same amount of details.
1117 // Note that it does not mean that the instructions have the same actual type
1118 // (e.g. tops are equal but they can be the result of a merge).
1119 bool IsEqual(ReferenceTypeInfo rti) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1120 if (IsExact() != rti.IsExact()) {
1121 return false;
1122 }
1123 if (IsTop() && rti.IsTop()) {
1124 // `Top` means java.lang.Object, so the types are equivalent.
1125 return true;
1126 }
1127 if (IsTop() || rti.IsTop()) {
1128 // If only one is top or object than they are not equivalent.
1129 // NB: We need this extra check because the type_handle of `Top` is invalid
1130 // and we cannot inspect its reference.
1131 return false;
1132 }
1133
1134 // Finally check the types.
1135 return GetTypeHandle().Get() == rti.GetTypeHandle().Get();
1136 }
1137
1138 private:
Calin Juravleb1498f62015-02-16 13:13:29 +00001139 ReferenceTypeInfo() : ReferenceTypeInfo(TypeHandle(), false, true) {}
1140 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact, bool is_top)
1141 : type_handle_(type_handle), is_exact_(is_exact), is_top_(is_top) {}
1142
Calin Juravleacf735c2015-02-12 15:25:22 +00001143 // The class of the object.
Calin Juravleb1498f62015-02-16 13:13:29 +00001144 TypeHandle type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001145 // Whether or not the type is exact or a superclass of the actual type.
Calin Juravleb1498f62015-02-16 13:13:29 +00001146 // Whether or not we have any information about this type.
Calin Juravleacf735c2015-02-12 15:25:22 +00001147 bool is_exact_;
1148 // A true value here means that the object type should be java.lang.Object.
1149 // We don't have access to the corresponding mirror object every time so this
1150 // flag acts as a substitute. When true, the TypeHandle refers to a null
1151 // pointer and should not be used.
1152 bool is_top_;
1153};
1154
1155std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
1156
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001157class HInstruction : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001158 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001159 explicit HInstruction(SideEffects side_effects)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001160 : previous_(nullptr),
1161 next_(nullptr),
1162 block_(nullptr),
1163 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001164 ssa_index_(-1),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001165 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001166 locations_(nullptr),
1167 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001168 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001169 side_effects_(side_effects),
1170 reference_type_info_(ReferenceTypeInfo::CreateTop(/* is_exact */ false)) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001171
Dave Allison20dfc792014-06-16 20:44:29 -07001172 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001173
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001174#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001175 enum InstructionKind {
1176 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1177 };
1178#undef DECLARE_KIND
1179
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001180 HInstruction* GetNext() const { return next_; }
1181 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001182
Calin Juravle77520bc2015-01-12 18:45:46 +00001183 HInstruction* GetNextDisregardingMoves() const;
1184 HInstruction* GetPreviousDisregardingMoves() const;
1185
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001186 HBasicBlock* GetBlock() const { return block_; }
1187 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001188 bool IsInBlock() const { return block_ != nullptr; }
1189 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +01001190 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001191
Roland Levillain6b879dd2014-09-22 17:13:44 +01001192 virtual size_t InputCount() const = 0;
David Brazdil1abb4192015-02-17 18:33:36 +00001193 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001194
1195 virtual void Accept(HGraphVisitor* visitor) = 0;
1196 virtual const char* DebugName() const = 0;
1197
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001198 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
David Brazdil1abb4192015-02-17 18:33:36 +00001199 void SetRawInputAt(size_t index, HInstruction* input) {
1200 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1201 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001202
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001203 virtual bool NeedsEnvironment() const { return false; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001204 virtual bool IsControlFlow() const { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01001205 virtual bool CanThrow() const { return false; }
Roland Levillain72bceff2014-09-15 18:29:00 +01001206 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001207
Calin Juravle10e244f2015-01-26 18:54:32 +00001208 // Does not apply for all instructions, but having this at top level greatly
1209 // simplifies the null check elimination.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001210 virtual bool CanBeNull() const {
1211 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1212 return true;
1213 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001214
Calin Juravle641547a2015-04-21 22:08:51 +01001215 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj) const {
1216 UNUSED(obj);
1217 return false;
1218 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001219
Calin Juravleacf735c2015-02-12 15:25:22 +00001220 void SetReferenceTypeInfo(ReferenceTypeInfo reference_type_info) {
Calin Juravle61d544b2015-02-23 16:46:57 +00001221 DCHECK_EQ(GetType(), Primitive::kPrimNot);
Calin Juravleacf735c2015-02-12 15:25:22 +00001222 reference_type_info_ = reference_type_info;
1223 }
1224
Calin Juravle61d544b2015-02-23 16:46:57 +00001225 ReferenceTypeInfo GetReferenceTypeInfo() const {
1226 DCHECK_EQ(GetType(), Primitive::kPrimNot);
1227 return reference_type_info_;
1228 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001229
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001230 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001231 DCHECK(user != nullptr);
1232 HUseListNode<HInstruction*>* use =
1233 uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1234 user->SetRawInputRecordAt(index, HUserRecord<HInstruction*>(user->InputRecordAt(index), use));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001235 }
1236
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001237 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001238 DCHECK(user != nullptr);
David Brazdiled596192015-01-23 10:39:45 +00001239 HUseListNode<HEnvironment*>* env_use =
1240 env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1241 user->RecordEnvUse(env_use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001242 }
1243
David Brazdil1abb4192015-02-17 18:33:36 +00001244 void RemoveAsUserOfInput(size_t input) {
1245 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
1246 input_use.GetInstruction()->uses_.Remove(input_use.GetUseNode());
1247 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001248
David Brazdil1abb4192015-02-17 18:33:36 +00001249 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1250 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001251
David Brazdiled596192015-01-23 10:39:45 +00001252 bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); }
1253 bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); }
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001254 bool HasNonEnvironmentUses() const { return !uses_.IsEmpty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001255 bool HasOnlyOneNonEnvironmentUse() const {
1256 return !HasEnvironmentUses() && GetUses().HasOnlyOneUse();
1257 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001258
Roland Levillain6c82d402014-10-13 16:10:27 +01001259 // Does this instruction strictly dominate `other_instruction`?
1260 // Returns false if this instruction and `other_instruction` are the same.
1261 // Aborts if this instruction and `other_instruction` are both phis.
1262 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001263
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001264 int GetId() const { return id_; }
1265 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001266
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001267 int GetSsaIndex() const { return ssa_index_; }
1268 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1269 bool HasSsaIndex() const { return ssa_index_ != -1; }
1270
1271 bool HasEnvironment() const { return environment_ != nullptr; }
1272 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001273 // Set the `environment_` field. Raw because this method does not
1274 // update the uses lists.
1275 void SetRawEnvironment(HEnvironment* environment) { environment_ = environment; }
1276
1277 // Set the environment of this instruction, copying it from `environment`. While
1278 // copying, the uses lists are being updated.
1279 void CopyEnvironmentFrom(HEnvironment* environment) {
1280 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
1281 environment_ = new (allocator) HEnvironment(allocator, environment->Size());
1282 environment_->CopyFrom(environment);
1283 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001284
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001285 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
1286 HBasicBlock* block) {
1287 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
1288 environment_ = new (allocator) HEnvironment(allocator, environment->Size());
1289 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
1290 }
1291
Nicolas Geoffray39468442014-09-02 15:17:15 +01001292 // Returns the number of entries in the environment. Typically, that is the
1293 // number of dex registers in a method. It could be more in case of inlining.
1294 size_t EnvironmentSize() const;
1295
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001296 LocationSummary* GetLocations() const { return locations_; }
1297 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001298
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001299 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001300 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001301
Alexandre Rames188d4312015-04-09 18:30:21 +01001302 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
1303 // uses of this instruction by `other` are *not* updated.
1304 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
1305 ReplaceWith(other);
1306 other->ReplaceInput(this, use_index);
1307 }
1308
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001309 // Move `this` instruction before `cursor`.
1310 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001311
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001312#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01001313 bool Is##type() const { return (As##type() != nullptr); } \
1314 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001315 virtual H##type* As##type() { return nullptr; }
1316
1317 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
1318#undef INSTRUCTION_TYPE_CHECK
1319
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001320 // Returns whether the instruction can be moved within the graph.
1321 virtual bool CanBeMoved() const { return false; }
1322
1323 // Returns whether the two instructions are of the same kind.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001324 virtual bool InstructionTypeEquals(HInstruction* other) const {
1325 UNUSED(other);
1326 return false;
1327 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001328
1329 // Returns whether any data encoded in the two instructions is equal.
1330 // This method does not look at the inputs. Both instructions must be
1331 // of the same type, otherwise the method has undefined behavior.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001332 virtual bool InstructionDataEquals(HInstruction* other) const {
1333 UNUSED(other);
1334 return false;
1335 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001336
1337 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00001338 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001339 // 2) Their inputs are identical.
1340 bool Equals(HInstruction* other) const;
1341
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001342 virtual InstructionKind GetKind() const = 0;
1343
1344 virtual size_t ComputeHashCode() const {
1345 size_t result = GetKind();
1346 for (size_t i = 0, e = InputCount(); i < e; ++i) {
1347 result = (result * 31) + InputAt(i)->GetId();
1348 }
1349 return result;
1350 }
1351
1352 SideEffects GetSideEffects() const { return side_effects_; }
1353
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001354 size_t GetLifetimePosition() const { return lifetime_position_; }
1355 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
1356 LiveInterval* GetLiveInterval() const { return live_interval_; }
1357 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
1358 bool HasLiveInterval() const { return live_interval_ != nullptr; }
1359
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001360 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
1361
1362 // Returns whether the code generation of the instruction will require to have access
1363 // to the current method. Such instructions are:
1364 // (1): Instructions that require an environment, as calling the runtime requires
1365 // to walk the stack and have the current method stored at a specific stack address.
1366 // (2): Object literals like classes and strings, that are loaded from the dex cache
1367 // fields of the current method.
1368 bool NeedsCurrentMethod() const {
1369 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
1370 }
1371
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001372 virtual bool NeedsDexCache() const { return false; }
1373
David Brazdil1abb4192015-02-17 18:33:36 +00001374 protected:
1375 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
1376 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
1377
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001378 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001379 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); }
1380
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001381 HInstruction* previous_;
1382 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001383 HBasicBlock* block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001384
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001385 // An instruction gets an id when it is added to the graph.
1386 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01001387 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001388 int id_;
1389
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001390 // When doing liveness analysis, instructions that have uses get an SSA index.
1391 int ssa_index_;
1392
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001393 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00001394 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001395
1396 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00001397 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001398
Nicolas Geoffray39468442014-09-02 15:17:15 +01001399 // The environment associated with this instruction. Not null if the instruction
1400 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001401 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001402
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001403 // Set by the code generator.
1404 LocationSummary* locations_;
1405
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001406 // Set by the liveness analysis.
1407 LiveInterval* live_interval_;
1408
1409 // Set by the liveness analysis, this is the position in a linear
1410 // order of blocks where this instruction's live interval start.
1411 size_t lifetime_position_;
1412
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001413 const SideEffects side_effects_;
1414
Calin Juravleacf735c2015-02-12 15:25:22 +00001415 // TODO: for primitive types this should be marked as invalid.
1416 ReferenceTypeInfo reference_type_info_;
1417
David Brazdil1abb4192015-02-17 18:33:36 +00001418 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001419 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00001420 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001421 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001422 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001423
1424 DISALLOW_COPY_AND_ASSIGN(HInstruction);
1425};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001426std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001427
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001428class HInputIterator : public ValueObject {
1429 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001430 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001431
1432 bool Done() const { return index_ == instruction_->InputCount(); }
1433 HInstruction* Current() const { return instruction_->InputAt(index_); }
1434 void Advance() { index_++; }
1435
1436 private:
1437 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001438 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001439
1440 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
1441};
1442
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001443class HInstructionIterator : public ValueObject {
1444 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001445 explicit HInstructionIterator(const HInstructionList& instructions)
1446 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001447 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001448 }
1449
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001450 bool Done() const { return instruction_ == nullptr; }
1451 HInstruction* Current() const { return instruction_; }
1452 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001453 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001454 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001455 }
1456
1457 private:
1458 HInstruction* instruction_;
1459 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001460
1461 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001462};
1463
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001464class HBackwardInstructionIterator : public ValueObject {
1465 public:
1466 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
1467 : instruction_(instructions.last_instruction_) {
1468 next_ = Done() ? nullptr : instruction_->GetPrevious();
1469 }
1470
1471 bool Done() const { return instruction_ == nullptr; }
1472 HInstruction* Current() const { return instruction_; }
1473 void Advance() {
1474 instruction_ = next_;
1475 next_ = Done() ? nullptr : instruction_->GetPrevious();
1476 }
1477
1478 private:
1479 HInstruction* instruction_;
1480 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001481
1482 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001483};
1484
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001485// An embedded container with N elements of type T. Used (with partial
1486// specialization for N=0) because embedded arrays cannot have size 0.
1487template<typename T, intptr_t N>
1488class EmbeddedArray {
1489 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001490 EmbeddedArray() : elements_() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001491
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001492 intptr_t GetLength() const { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001493
1494 const T& operator[](intptr_t i) const {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001495 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001496 return elements_[i];
1497 }
1498
1499 T& operator[](intptr_t i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001500 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001501 return elements_[i];
1502 }
1503
1504 const T& At(intptr_t i) const {
1505 return (*this)[i];
1506 }
1507
1508 void SetAt(intptr_t i, const T& val) {
1509 (*this)[i] = val;
1510 }
1511
1512 private:
1513 T elements_[N];
1514};
1515
1516template<typename T>
1517class EmbeddedArray<T, 0> {
1518 public:
1519 intptr_t length() const { return 0; }
1520 const T& operator[](intptr_t i) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001521 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001522 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001523 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001524 }
1525 T& operator[](intptr_t i) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001526 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001527 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001528 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001529 }
1530};
1531
1532template<intptr_t N>
1533class HTemplateInstruction: public HInstruction {
1534 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001535 HTemplateInstruction<N>(SideEffects side_effects)
1536 : HInstruction(side_effects), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07001537 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001538
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001539 size_t InputCount() const OVERRIDE { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001540
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001541 protected:
David Brazdil1abb4192015-02-17 18:33:36 +00001542 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_[i]; }
1543
1544 void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE {
1545 inputs_[i] = input;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001546 }
1547
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001548 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001549 EmbeddedArray<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001550
1551 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001552};
1553
Dave Allison20dfc792014-06-16 20:44:29 -07001554template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001555class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07001556 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001557 HExpression<N>(Primitive::Type type, SideEffects side_effects)
1558 : HTemplateInstruction<N>(side_effects), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07001559 virtual ~HExpression() {}
1560
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001561 Primitive::Type GetType() const OVERRIDE { return type_; }
Dave Allison20dfc792014-06-16 20:44:29 -07001562
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001563 protected:
1564 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07001565};
1566
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001567// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
1568// instruction that branches to the exit block.
1569class HReturnVoid : public HTemplateInstruction<0> {
1570 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001571 HReturnVoid() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001572
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001573 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001574
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001575 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001576
1577 private:
1578 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
1579};
1580
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001581// Represents dex's RETURN opcodes. A HReturn is a control flow
1582// instruction that branches to the exit block.
1583class HReturn : public HTemplateInstruction<1> {
1584 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001585 explicit HReturn(HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001586 SetRawInputAt(0, value);
1587 }
1588
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001589 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001590
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001591 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001592
1593 private:
1594 DISALLOW_COPY_AND_ASSIGN(HReturn);
1595};
1596
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001597// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001598// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001599// exit block.
1600class HExit : public HTemplateInstruction<0> {
1601 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001602 HExit() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001603
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001604 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001605
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001606 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001607
1608 private:
1609 DISALLOW_COPY_AND_ASSIGN(HExit);
1610};
1611
1612// Jumps from one block to another.
1613class HGoto : public HTemplateInstruction<0> {
1614 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001615 HGoto() : HTemplateInstruction(SideEffects::None()) {}
1616
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001617 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001618
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001619 HBasicBlock* GetSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001620 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001621 }
1622
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001623 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001624
1625 private:
1626 DISALLOW_COPY_AND_ASSIGN(HGoto);
1627};
1628
Dave Allison20dfc792014-06-16 20:44:29 -07001629
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001630// Conditional branch. A block ending with an HIf instruction must have
1631// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001632class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001633 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001634 explicit HIf(HInstruction* input) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001635 SetRawInputAt(0, input);
1636 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001637
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001638 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001639
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001640 HBasicBlock* IfTrueSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001641 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001642 }
1643
1644 HBasicBlock* IfFalseSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001645 return GetBlock()->GetSuccessors().Get(1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001646 }
1647
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001648 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001649
1650 private:
1651 DISALLOW_COPY_AND_ASSIGN(HIf);
1652};
1653
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001654// Deoptimize to interpreter, upon checking a condition.
1655class HDeoptimize : public HTemplateInstruction<1> {
1656 public:
1657 HDeoptimize(HInstruction* cond, uint32_t dex_pc)
1658 : HTemplateInstruction(SideEffects::None()),
1659 dex_pc_(dex_pc) {
1660 SetRawInputAt(0, cond);
1661 }
1662
1663 bool NeedsEnvironment() const OVERRIDE { return true; }
1664 bool CanThrow() const OVERRIDE { return true; }
1665 uint32_t GetDexPc() const { return dex_pc_; }
1666
1667 DECLARE_INSTRUCTION(Deoptimize);
1668
1669 private:
1670 uint32_t dex_pc_;
1671
1672 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
1673};
1674
Roland Levillain88cb1752014-10-20 16:36:47 +01001675class HUnaryOperation : public HExpression<1> {
1676 public:
1677 HUnaryOperation(Primitive::Type result_type, HInstruction* input)
1678 : HExpression(result_type, SideEffects::None()) {
1679 SetRawInputAt(0, input);
1680 }
1681
1682 HInstruction* GetInput() const { return InputAt(0); }
1683 Primitive::Type GetResultType() const { return GetType(); }
1684
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001685 bool CanBeMoved() const OVERRIDE { return true; }
1686 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001687 UNUSED(other);
1688 return true;
1689 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001690
Roland Levillain9240d6a2014-10-20 16:47:04 +01001691 // Try to statically evaluate `operation` and return a HConstant
1692 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001693 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01001694 HConstant* TryStaticEvaluation() const;
1695
1696 // Apply this operation to `x`.
1697 virtual int32_t Evaluate(int32_t x) const = 0;
1698 virtual int64_t Evaluate(int64_t x) const = 0;
1699
Roland Levillain88cb1752014-10-20 16:36:47 +01001700 DECLARE_INSTRUCTION(UnaryOperation);
1701
1702 private:
1703 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
1704};
1705
Dave Allison20dfc792014-06-16 20:44:29 -07001706class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001707 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001708 HBinaryOperation(Primitive::Type result_type,
1709 HInstruction* left,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001710 HInstruction* right) : HExpression(result_type, SideEffects::None()) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001711 SetRawInputAt(0, left);
1712 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001713 }
1714
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001715 HInstruction* GetLeft() const { return InputAt(0); }
1716 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07001717 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001718
Mingyao Yangdc5ac732015-02-25 11:28:05 -08001719 virtual bool IsCommutative() const { return false; }
1720
1721 // Put constant on the right.
1722 // Returns whether order is changed.
1723 bool OrderInputsWithConstantOnTheRight() {
1724 HInstruction* left = InputAt(0);
1725 HInstruction* right = InputAt(1);
1726 if (left->IsConstant() && !right->IsConstant()) {
1727 ReplaceInput(right, 0);
1728 ReplaceInput(left, 1);
1729 return true;
1730 }
1731 return false;
1732 }
1733
1734 // Order inputs by instruction id, but favor constant on the right side.
1735 // This helps GVN for commutative ops.
1736 void OrderInputs() {
1737 DCHECK(IsCommutative());
1738 HInstruction* left = InputAt(0);
1739 HInstruction* right = InputAt(1);
1740 if (left == right || (!left->IsConstant() && right->IsConstant())) {
1741 return;
1742 }
1743 if (OrderInputsWithConstantOnTheRight()) {
1744 return;
1745 }
1746 // Order according to instruction id.
1747 if (left->GetId() > right->GetId()) {
1748 ReplaceInput(right, 0);
1749 ReplaceInput(left, 1);
1750 }
1751 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001752
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001753 bool CanBeMoved() const OVERRIDE { return true; }
1754 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001755 UNUSED(other);
1756 return true;
1757 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001758
Roland Levillain9240d6a2014-10-20 16:47:04 +01001759 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01001760 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001761 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01001762 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01001763
1764 // Apply this operation to `x` and `y`.
1765 virtual int32_t Evaluate(int32_t x, int32_t y) const = 0;
1766 virtual int64_t Evaluate(int64_t x, int64_t y) const = 0;
1767
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001768 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001769 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001770 HConstant* GetConstantRight() const;
1771
1772 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001773 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001774 HInstruction* GetLeastConstantLeft() const;
1775
Roland Levillainccc07a92014-09-16 14:48:16 +01001776 DECLARE_INSTRUCTION(BinaryOperation);
1777
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001778 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001779 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
1780};
1781
Dave Allison20dfc792014-06-16 20:44:29 -07001782class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001783 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001784 HCondition(HInstruction* first, HInstruction* second)
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001785 : HBinaryOperation(Primitive::kPrimBoolean, first, second),
1786 needs_materialization_(true) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001787
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001788 bool NeedsMaterialization() const { return needs_materialization_; }
1789 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001790
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001791 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001792 // `instruction`, and disregard moves in between.
1793 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001794
Dave Allison20dfc792014-06-16 20:44:29 -07001795 DECLARE_INSTRUCTION(Condition);
1796
1797 virtual IfCondition GetCondition() const = 0;
1798
1799 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001800 // For register allocation purposes, returns whether this instruction needs to be
1801 // materialized (that is, not just be in the processor flags).
1802 bool needs_materialization_;
1803
Dave Allison20dfc792014-06-16 20:44:29 -07001804 DISALLOW_COPY_AND_ASSIGN(HCondition);
1805};
1806
1807// Instruction to check if two inputs are equal to each other.
1808class HEqual : public HCondition {
1809 public:
1810 HEqual(HInstruction* first, HInstruction* second)
1811 : HCondition(first, second) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001812
Mingyao Yangdc5ac732015-02-25 11:28:05 -08001813 bool IsCommutative() const OVERRIDE { return true; }
1814
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001815 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001816 return x == y ? 1 : 0;
1817 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001818 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001819 return x == y ? 1 : 0;
1820 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001821
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001822 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001823
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001824 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001825 return kCondEQ;
1826 }
1827
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001828 private:
1829 DISALLOW_COPY_AND_ASSIGN(HEqual);
1830};
1831
Dave Allison20dfc792014-06-16 20:44:29 -07001832class HNotEqual : public HCondition {
1833 public:
1834 HNotEqual(HInstruction* first, HInstruction* second)
1835 : HCondition(first, second) {}
1836
Mingyao Yangdc5ac732015-02-25 11:28:05 -08001837 bool IsCommutative() const OVERRIDE { return true; }
1838
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001839 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001840 return x != y ? 1 : 0;
1841 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001842 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001843 return x != y ? 1 : 0;
1844 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001845
Dave Allison20dfc792014-06-16 20:44:29 -07001846 DECLARE_INSTRUCTION(NotEqual);
1847
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001848 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001849 return kCondNE;
1850 }
1851
1852 private:
1853 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
1854};
1855
1856class HLessThan : public HCondition {
1857 public:
1858 HLessThan(HInstruction* first, HInstruction* second)
1859 : HCondition(first, second) {}
1860
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001861 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001862 return x < y ? 1 : 0;
1863 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001864 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001865 return x < y ? 1 : 0;
1866 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001867
Dave Allison20dfc792014-06-16 20:44:29 -07001868 DECLARE_INSTRUCTION(LessThan);
1869
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001870 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001871 return kCondLT;
1872 }
1873
1874 private:
1875 DISALLOW_COPY_AND_ASSIGN(HLessThan);
1876};
1877
1878class HLessThanOrEqual : public HCondition {
1879 public:
1880 HLessThanOrEqual(HInstruction* first, HInstruction* second)
1881 : HCondition(first, second) {}
1882
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001883 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001884 return x <= y ? 1 : 0;
1885 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001886 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001887 return x <= y ? 1 : 0;
1888 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001889
Dave Allison20dfc792014-06-16 20:44:29 -07001890 DECLARE_INSTRUCTION(LessThanOrEqual);
1891
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001892 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001893 return kCondLE;
1894 }
1895
1896 private:
1897 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
1898};
1899
1900class HGreaterThan : public HCondition {
1901 public:
1902 HGreaterThan(HInstruction* first, HInstruction* second)
1903 : HCondition(first, second) {}
1904
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001905 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001906 return x > y ? 1 : 0;
1907 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001908 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001909 return x > y ? 1 : 0;
1910 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001911
Dave Allison20dfc792014-06-16 20:44:29 -07001912 DECLARE_INSTRUCTION(GreaterThan);
1913
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001914 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001915 return kCondGT;
1916 }
1917
1918 private:
1919 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
1920};
1921
1922class HGreaterThanOrEqual : public HCondition {
1923 public:
1924 HGreaterThanOrEqual(HInstruction* first, HInstruction* second)
1925 : HCondition(first, second) {}
1926
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001927 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001928 return x >= y ? 1 : 0;
1929 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001930 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001931 return x >= y ? 1 : 0;
1932 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001933
Dave Allison20dfc792014-06-16 20:44:29 -07001934 DECLARE_INSTRUCTION(GreaterThanOrEqual);
1935
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001936 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001937 return kCondGE;
1938 }
1939
1940 private:
1941 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
1942};
1943
1944
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001945// Instruction to check how two inputs compare to each other.
1946// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
1947class HCompare : public HBinaryOperation {
1948 public:
Calin Juravleddb7df22014-11-25 20:56:51 +00001949 // The bias applies for floating point operations and indicates how NaN
1950 // comparisons are treated:
1951 enum Bias {
1952 kNoBias, // bias is not applicable (i.e. for long operation)
1953 kGtBias, // return 1 for NaN comparisons
1954 kLtBias, // return -1 for NaN comparisons
1955 };
1956
1957 HCompare(Primitive::Type type, HInstruction* first, HInstruction* second, Bias bias)
1958 : HBinaryOperation(Primitive::kPrimInt, first, second), bias_(bias) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001959 DCHECK_EQ(type, first->GetType());
1960 DCHECK_EQ(type, second->GetType());
1961 }
1962
Calin Juravleddb7df22014-11-25 20:56:51 +00001963 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01001964 return
1965 x == y ? 0 :
1966 x > y ? 1 :
1967 -1;
1968 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001969
1970 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01001971 return
1972 x == y ? 0 :
1973 x > y ? 1 :
1974 -1;
1975 }
1976
Calin Juravleddb7df22014-11-25 20:56:51 +00001977 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
1978 return bias_ == other->AsCompare()->bias_;
1979 }
1980
1981 bool IsGtBias() { return bias_ == kGtBias; }
1982
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001983 DECLARE_INSTRUCTION(Compare);
1984
1985 private:
Calin Juravleddb7df22014-11-25 20:56:51 +00001986 const Bias bias_;
1987
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001988 DISALLOW_COPY_AND_ASSIGN(HCompare);
1989};
1990
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001991// A local in the graph. Corresponds to a Dex register.
1992class HLocal : public HTemplateInstruction<0> {
1993 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001994 explicit HLocal(uint16_t reg_number)
1995 : HTemplateInstruction(SideEffects::None()), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001996
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001997 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001998
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001999 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002000
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002001 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002002 // The Dex register number.
2003 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002004
2005 DISALLOW_COPY_AND_ASSIGN(HLocal);
2006};
2007
2008// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07002009class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002010 public:
Roland Levillain5799fc02014-09-25 12:15:20 +01002011 HLoadLocal(HLocal* local, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002012 : HExpression(type, SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002013 SetRawInputAt(0, local);
2014 }
2015
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002016 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
2017
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002018 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002019
2020 private:
2021 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
2022};
2023
2024// Store a value in a given local. This instruction has two inputs: the value
2025// and the local.
2026class HStoreLocal : public HTemplateInstruction<2> {
2027 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002028 HStoreLocal(HLocal* local, HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002029 SetRawInputAt(0, local);
2030 SetRawInputAt(1, value);
2031 }
2032
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002033 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
2034
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002035 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002036
2037 private:
2038 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
2039};
2040
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002041class HConstant : public HExpression<0> {
2042 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002043 explicit HConstant(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
2044
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002045 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002046
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002047 virtual bool IsMinusOne() const { return false; }
2048 virtual bool IsZero() const { return false; }
2049 virtual bool IsOne() const { return false; }
2050
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002051 DECLARE_INSTRUCTION(Constant);
2052
2053 private:
2054 DISALLOW_COPY_AND_ASSIGN(HConstant);
2055};
2056
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002057class HFloatConstant : public HConstant {
2058 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002059 float GetValue() const { return value_; }
2060
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002061 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002062 return bit_cast<uint32_t, float>(other->AsFloatConstant()->value_) ==
2063 bit_cast<uint32_t, float>(value_);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002064 }
2065
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002066 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002067
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002068 bool IsMinusOne() const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002069 return bit_cast<uint32_t, float>(AsFloatConstant()->GetValue()) ==
2070 bit_cast<uint32_t, float>((-1.0f));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002071 }
2072 bool IsZero() const OVERRIDE {
2073 return AsFloatConstant()->GetValue() == 0.0f;
2074 }
2075 bool IsOne() const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002076 return bit_cast<uint32_t, float>(AsFloatConstant()->GetValue()) ==
2077 bit_cast<uint32_t, float>(1.0f);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002078 }
2079
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002080 DECLARE_INSTRUCTION(FloatConstant);
2081
2082 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002083 explicit HFloatConstant(float value) : HConstant(Primitive::kPrimFloat), value_(value) {}
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002084 explicit HFloatConstant(int32_t value)
2085 : HConstant(Primitive::kPrimFloat), value_(bit_cast<float, int32_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00002086
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002087 const float value_;
2088
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002089 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00002090 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002091 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002092 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2093};
2094
2095class HDoubleConstant : public HConstant {
2096 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002097 double GetValue() const { return value_; }
2098
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002099 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002100 return bit_cast<uint64_t, double>(other->AsDoubleConstant()->value_) ==
2101 bit_cast<uint64_t, double>(value_);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002102 }
2103
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002104 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002105
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002106 bool IsMinusOne() const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002107 return bit_cast<uint64_t, double>(AsDoubleConstant()->GetValue()) ==
2108 bit_cast<uint64_t, double>((-1.0));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002109 }
2110 bool IsZero() const OVERRIDE {
2111 return AsDoubleConstant()->GetValue() == 0.0;
2112 }
2113 bool IsOne() const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002114 return bit_cast<uint64_t, double>(AsDoubleConstant()->GetValue()) ==
2115 bit_cast<uint64_t, double>(1.0);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002116 }
2117
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002118 DECLARE_INSTRUCTION(DoubleConstant);
2119
2120 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002121 explicit HDoubleConstant(double value) : HConstant(Primitive::kPrimDouble), value_(value) {}
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002122 explicit HDoubleConstant(int64_t value)
2123 : HConstant(Primitive::kPrimDouble), value_(bit_cast<double, int64_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00002124
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002125 const double value_;
2126
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002127 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00002128 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002129 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002130 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
2131};
2132
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002133class HNullConstant : public HConstant {
2134 public:
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002135 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2136 return true;
2137 }
2138
2139 size_t ComputeHashCode() const OVERRIDE { return 0; }
2140
2141 DECLARE_INSTRUCTION(NullConstant);
2142
2143 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002144 HNullConstant() : HConstant(Primitive::kPrimNot) {}
2145
2146 friend class HGraph;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002147 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2148};
2149
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002150// Constants of the type int. Those can be from Dex instructions, or
2151// synthesized (for example with the if-eqz instruction).
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002152class HIntConstant : public HConstant {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002153 public:
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002154 int32_t GetValue() const { return value_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002155
Calin Juravle61d544b2015-02-23 16:46:57 +00002156 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002157 return other->AsIntConstant()->value_ == value_;
2158 }
2159
Calin Juravle61d544b2015-02-23 16:46:57 +00002160 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2161
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002162 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2163 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2164 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2165
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002166 DECLARE_INSTRUCTION(IntConstant);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002167
2168 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002169 explicit HIntConstant(int32_t value) : HConstant(Primitive::kPrimInt), value_(value) {}
2170
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002171 const int32_t value_;
2172
David Brazdil8d5b8b22015-03-24 10:51:52 +00002173 friend class HGraph;
2174 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
Zheng Xuad4450e2015-04-17 18:48:56 +08002175 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002176 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2177};
2178
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002179class HLongConstant : public HConstant {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002180 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002181 int64_t GetValue() const { return value_; }
2182
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002183 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002184 return other->AsLongConstant()->value_ == value_;
2185 }
2186
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002187 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002188
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002189 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2190 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2191 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2192
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002193 DECLARE_INSTRUCTION(LongConstant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002194
2195 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002196 explicit HLongConstant(int64_t value) : HConstant(Primitive::kPrimLong), value_(value) {}
2197
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002198 const int64_t value_;
2199
David Brazdil8d5b8b22015-03-24 10:51:52 +00002200 friend class HGraph;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002201 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2202};
2203
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002204enum class Intrinsics {
2205#define OPTIMIZING_INTRINSICS(Name, IsStatic) k ## Name,
2206#include "intrinsics_list.h"
2207 kNone,
2208 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
2209#undef INTRINSICS_LIST
2210#undef OPTIMIZING_INTRINSICS
2211};
2212std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
2213
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002214class HInvoke : public HInstruction {
2215 public:
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002216 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002217
2218 // Runtime needs to walk the stack, so Dex -> Dex calls need to
2219 // know their environment.
Calin Juravle77520bc2015-01-12 18:45:46 +00002220 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002221
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01002222 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002223 SetRawInputAt(index, argument);
2224 }
2225
Roland Levillain3e3d7332015-04-28 11:00:54 +01002226 // Return the number of arguments. This number can be lower than
2227 // the number of inputs returned by InputCount(), as some invoke
2228 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
2229 // inputs at the end of their list of inputs.
2230 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
2231
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002232 Primitive::Type GetType() const OVERRIDE { return return_type_; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002233
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002234 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002235
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002236 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
2237
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01002238 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002239 return intrinsic_;
2240 }
2241
2242 void SetIntrinsic(Intrinsics intrinsic) {
2243 intrinsic_ = intrinsic;
2244 }
2245
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002246 DECLARE_INSTRUCTION(Invoke);
2247
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002248 protected:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002249 HInvoke(ArenaAllocator* arena,
2250 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01002251 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002252 Primitive::Type return_type,
2253 uint32_t dex_pc,
2254 uint32_t dex_method_index)
2255 : HInstruction(SideEffects::All()),
Roland Levillain3e3d7332015-04-28 11:00:54 +01002256 number_of_arguments_(number_of_arguments),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002257 inputs_(arena, number_of_arguments),
2258 return_type_(return_type),
2259 dex_pc_(dex_pc),
2260 dex_method_index_(dex_method_index),
2261 intrinsic_(Intrinsics::kNone) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01002262 uint32_t number_of_inputs = number_of_arguments + number_of_other_inputs;
2263 inputs_.SetSize(number_of_inputs);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002264 }
2265
David Brazdil1abb4192015-02-17 18:33:36 +00002266 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
2267 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
2268 inputs_.Put(index, input);
2269 }
2270
Roland Levillain3e3d7332015-04-28 11:00:54 +01002271 uint32_t number_of_arguments_;
David Brazdil1abb4192015-02-17 18:33:36 +00002272 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002273 const Primitive::Type return_type_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002274 const uint32_t dex_pc_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002275 const uint32_t dex_method_index_;
2276 Intrinsics intrinsic_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002277
2278 private:
2279 DISALLOW_COPY_AND_ASSIGN(HInvoke);
2280};
2281
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002282class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002283 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01002284 // Requirements of this method call regarding the class
2285 // initialization (clinit) check of its declaring class.
2286 enum class ClinitCheckRequirement {
2287 kNone, // Class already initialized.
2288 kExplicit, // Static call having explicit clinit check as last input.
2289 kImplicit, // Static call implicitly requiring a clinit check.
2290 };
2291
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002292 HInvokeStaticOrDirect(ArenaAllocator* arena,
2293 uint32_t number_of_arguments,
2294 Primitive::Type return_type,
2295 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002296 uint32_t dex_method_index,
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002297 bool is_recursive,
Nicolas Geoffray79041292015-03-26 10:05:54 +00002298 InvokeType original_invoke_type,
Roland Levillain4c0eb422015-04-24 16:43:49 +01002299 InvokeType invoke_type,
2300 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01002301 : HInvoke(arena,
2302 number_of_arguments,
2303 clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u,
2304 return_type,
2305 dex_pc,
2306 dex_method_index),
Nicolas Geoffray79041292015-03-26 10:05:54 +00002307 original_invoke_type_(original_invoke_type),
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002308 invoke_type_(invoke_type),
Roland Levillain4c0eb422015-04-24 16:43:49 +01002309 is_recursive_(is_recursive),
2310 clinit_check_requirement_(clinit_check_requirement) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002311
Calin Juravle641547a2015-04-21 22:08:51 +01002312 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
2313 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00002314 // We access the method via the dex cache so we can't do an implicit null check.
2315 // TODO: for intrinsics we can generate implicit null checks.
2316 return false;
2317 }
2318
Nicolas Geoffray79041292015-03-26 10:05:54 +00002319 InvokeType GetOriginalInvokeType() const { return original_invoke_type_; }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002320 InvokeType GetInvokeType() const { return invoke_type_; }
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002321 bool IsRecursive() const { return is_recursive_; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002322 bool NeedsDexCache() const OVERRIDE { return !IsRecursive(); }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002323
Roland Levillain4c0eb422015-04-24 16:43:49 +01002324 // Is this instruction a call to a static method?
2325 bool IsStatic() const {
2326 return GetInvokeType() == kStatic;
2327 }
2328
Roland Levillain3e3d7332015-04-28 11:00:54 +01002329 // Remove the art::HLoadClass instruction set as last input by
2330 // art::PrepareForRegisterAllocation::VisitClinitCheck in lieu of
2331 // the initial art::HClinitCheck instruction (only relevant for
2332 // static calls with explicit clinit check).
2333 void RemoveLoadClassAsLastInput() {
Roland Levillain4c0eb422015-04-24 16:43:49 +01002334 DCHECK(IsStaticWithExplicitClinitCheck());
2335 size_t last_input_index = InputCount() - 1;
2336 HInstruction* last_input = InputAt(last_input_index);
2337 DCHECK(last_input != nullptr);
Roland Levillain3e3d7332015-04-28 11:00:54 +01002338 DCHECK(last_input->IsLoadClass()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01002339 RemoveAsUserOfInput(last_input_index);
2340 inputs_.DeleteAt(last_input_index);
2341 clinit_check_requirement_ = ClinitCheckRequirement::kImplicit;
2342 DCHECK(IsStaticWithImplicitClinitCheck());
2343 }
2344
2345 // Is this a call to a static method whose declaring class has an
2346 // explicit intialization check in the graph?
2347 bool IsStaticWithExplicitClinitCheck() const {
2348 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kExplicit);
2349 }
2350
2351 // Is this a call to a static method whose declaring class has an
2352 // implicit intialization check requirement?
2353 bool IsStaticWithImplicitClinitCheck() const {
2354 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kImplicit);
2355 }
2356
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002357 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002358
Roland Levillain4c0eb422015-04-24 16:43:49 +01002359 protected:
2360 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
2361 const HUserRecord<HInstruction*> input_record = HInvoke::InputRecordAt(i);
2362 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck() && (i == InputCount() - 1)) {
2363 HInstruction* input = input_record.GetInstruction();
2364 // `input` is the last input of a static invoke marked as having
2365 // an explicit clinit check. It must either be:
2366 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
2367 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
2368 DCHECK(input != nullptr);
2369 DCHECK(input->IsClinitCheck() || input->IsLoadClass()) << input->DebugName();
2370 }
2371 return input_record;
2372 }
2373
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002374 private:
Nicolas Geoffray79041292015-03-26 10:05:54 +00002375 const InvokeType original_invoke_type_;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002376 const InvokeType invoke_type_;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002377 const bool is_recursive_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01002378 ClinitCheckRequirement clinit_check_requirement_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002379
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002380 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002381};
2382
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002383class HInvokeVirtual : public HInvoke {
2384 public:
2385 HInvokeVirtual(ArenaAllocator* arena,
2386 uint32_t number_of_arguments,
2387 Primitive::Type return_type,
2388 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002389 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002390 uint32_t vtable_index)
Roland Levillain3e3d7332015-04-28 11:00:54 +01002391 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002392 vtable_index_(vtable_index) {}
2393
Calin Juravle641547a2015-04-21 22:08:51 +01002394 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00002395 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01002396 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00002397 }
2398
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002399 uint32_t GetVTableIndex() const { return vtable_index_; }
2400
2401 DECLARE_INSTRUCTION(InvokeVirtual);
2402
2403 private:
2404 const uint32_t vtable_index_;
2405
2406 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
2407};
2408
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002409class HInvokeInterface : public HInvoke {
2410 public:
2411 HInvokeInterface(ArenaAllocator* arena,
2412 uint32_t number_of_arguments,
2413 Primitive::Type return_type,
2414 uint32_t dex_pc,
2415 uint32_t dex_method_index,
2416 uint32_t imt_index)
Roland Levillain3e3d7332015-04-28 11:00:54 +01002417 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002418 imt_index_(imt_index) {}
2419
Calin Juravle641547a2015-04-21 22:08:51 +01002420 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00002421 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01002422 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00002423 }
2424
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002425 uint32_t GetImtIndex() const { return imt_index_; }
2426 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
2427
2428 DECLARE_INSTRUCTION(InvokeInterface);
2429
2430 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002431 const uint32_t imt_index_;
2432
2433 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
2434};
2435
Dave Allison20dfc792014-06-16 20:44:29 -07002436class HNewInstance : public HExpression<0> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002437 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002438 HNewInstance(uint32_t dex_pc, uint16_t type_index, QuickEntrypointEnum entrypoint)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002439 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2440 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002441 type_index_(type_index),
2442 entrypoint_(entrypoint) {}
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002443
2444 uint32_t GetDexPc() const { return dex_pc_; }
2445 uint16_t GetTypeIndex() const { return type_index_; }
2446
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002447 // Calls runtime so needs an environment.
Calin Juravle92a6ed22014-12-02 18:58:03 +00002448 bool NeedsEnvironment() const OVERRIDE { return true; }
2449 // It may throw when called on:
2450 // - interfaces
2451 // - abstract/innaccessible/unknown classes
2452 // TODO: optimize when possible.
2453 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002454
Calin Juravle10e244f2015-01-26 18:54:32 +00002455 bool CanBeNull() const OVERRIDE { return false; }
2456
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002457 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
2458
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002459 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002460
2461 private:
2462 const uint32_t dex_pc_;
2463 const uint16_t type_index_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002464 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002465
2466 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
2467};
2468
Roland Levillain88cb1752014-10-20 16:36:47 +01002469class HNeg : public HUnaryOperation {
2470 public:
2471 explicit HNeg(Primitive::Type result_type, HInstruction* input)
2472 : HUnaryOperation(result_type, input) {}
2473
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002474 int32_t Evaluate(int32_t x) const OVERRIDE { return -x; }
2475 int64_t Evaluate(int64_t x) const OVERRIDE { return -x; }
Roland Levillain9240d6a2014-10-20 16:47:04 +01002476
Roland Levillain88cb1752014-10-20 16:36:47 +01002477 DECLARE_INSTRUCTION(Neg);
2478
2479 private:
2480 DISALLOW_COPY_AND_ASSIGN(HNeg);
2481};
2482
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002483class HNewArray : public HExpression<1> {
2484 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002485 HNewArray(HInstruction* length,
2486 uint32_t dex_pc,
2487 uint16_t type_index,
2488 QuickEntrypointEnum entrypoint)
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002489 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2490 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002491 type_index_(type_index),
2492 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002493 SetRawInputAt(0, length);
2494 }
2495
2496 uint32_t GetDexPc() const { return dex_pc_; }
2497 uint16_t GetTypeIndex() const { return type_index_; }
2498
2499 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00002500 bool NeedsEnvironment() const OVERRIDE { return true; }
2501
Mingyao Yang0c365e62015-03-31 15:09:29 -07002502 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
2503 bool CanThrow() const OVERRIDE { return true; }
2504
Calin Juravle10e244f2015-01-26 18:54:32 +00002505 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002506
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002507 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
2508
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002509 DECLARE_INSTRUCTION(NewArray);
2510
2511 private:
2512 const uint32_t dex_pc_;
2513 const uint16_t type_index_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002514 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002515
2516 DISALLOW_COPY_AND_ASSIGN(HNewArray);
2517};
2518
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002519class HAdd : public HBinaryOperation {
2520 public:
2521 HAdd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2522 : HBinaryOperation(result_type, left, right) {}
2523
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002524 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002525
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002526 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002527 return x + y;
2528 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002529 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002530 return x + y;
2531 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002532
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002533 DECLARE_INSTRUCTION(Add);
2534
2535 private:
2536 DISALLOW_COPY_AND_ASSIGN(HAdd);
2537};
2538
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002539class HSub : public HBinaryOperation {
2540 public:
2541 HSub(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2542 : HBinaryOperation(result_type, left, right) {}
2543
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002544 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002545 return x - y;
2546 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002547 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002548 return x - y;
2549 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002550
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002551 DECLARE_INSTRUCTION(Sub);
2552
2553 private:
2554 DISALLOW_COPY_AND_ASSIGN(HSub);
2555};
2556
Calin Juravle34bacdf2014-10-07 20:23:36 +01002557class HMul : public HBinaryOperation {
2558 public:
2559 HMul(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2560 : HBinaryOperation(result_type, left, right) {}
2561
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002562 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002563
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002564 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x * y; }
2565 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x * y; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002566
2567 DECLARE_INSTRUCTION(Mul);
2568
2569 private:
2570 DISALLOW_COPY_AND_ASSIGN(HMul);
2571};
2572
Calin Juravle7c4954d2014-10-28 16:57:40 +00002573class HDiv : public HBinaryOperation {
2574 public:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002575 HDiv(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
2576 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00002577
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002578 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00002579 // Our graph structure ensures we never have 0 for `y` during constant folding.
2580 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00002581 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00002582 return (y == -1) ? -x : x / y;
2583 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002584
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002585 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002586 DCHECK_NE(y, 0);
2587 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2588 return (y == -1) ? -x : x / y;
2589 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002590
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002591 uint32_t GetDexPc() const { return dex_pc_; }
2592
Calin Juravle7c4954d2014-10-28 16:57:40 +00002593 DECLARE_INSTRUCTION(Div);
2594
2595 private:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002596 const uint32_t dex_pc_;
2597
Calin Juravle7c4954d2014-10-28 16:57:40 +00002598 DISALLOW_COPY_AND_ASSIGN(HDiv);
2599};
2600
Calin Juravlebacfec32014-11-14 15:54:36 +00002601class HRem : public HBinaryOperation {
2602 public:
2603 HRem(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
2604 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
2605
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002606 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002607 DCHECK_NE(y, 0);
2608 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2609 return (y == -1) ? 0 : x % y;
2610 }
2611
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002612 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002613 DCHECK_NE(y, 0);
2614 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2615 return (y == -1) ? 0 : x % y;
2616 }
2617
2618 uint32_t GetDexPc() const { return dex_pc_; }
2619
2620 DECLARE_INSTRUCTION(Rem);
2621
2622 private:
2623 const uint32_t dex_pc_;
2624
2625 DISALLOW_COPY_AND_ASSIGN(HRem);
2626};
2627
Calin Juravled0d48522014-11-04 16:40:20 +00002628class HDivZeroCheck : public HExpression<1> {
2629 public:
2630 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
2631 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
2632 SetRawInputAt(0, value);
2633 }
2634
2635 bool CanBeMoved() const OVERRIDE { return true; }
2636
2637 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2638 UNUSED(other);
2639 return true;
2640 }
2641
2642 bool NeedsEnvironment() const OVERRIDE { return true; }
2643 bool CanThrow() const OVERRIDE { return true; }
2644
2645 uint32_t GetDexPc() const { return dex_pc_; }
2646
2647 DECLARE_INSTRUCTION(DivZeroCheck);
2648
2649 private:
2650 const uint32_t dex_pc_;
2651
2652 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
2653};
2654
Calin Juravle9aec02f2014-11-18 23:06:35 +00002655class HShl : public HBinaryOperation {
2656 public:
2657 HShl(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2658 : HBinaryOperation(result_type, left, right) {}
2659
2660 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x << (y & kMaxIntShiftValue); }
2661 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x << (y & kMaxLongShiftValue); }
2662
2663 DECLARE_INSTRUCTION(Shl);
2664
2665 private:
2666 DISALLOW_COPY_AND_ASSIGN(HShl);
2667};
2668
2669class HShr : public HBinaryOperation {
2670 public:
2671 HShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2672 : HBinaryOperation(result_type, left, right) {}
2673
2674 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x >> (y & kMaxIntShiftValue); }
2675 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x >> (y & kMaxLongShiftValue); }
2676
2677 DECLARE_INSTRUCTION(Shr);
2678
2679 private:
2680 DISALLOW_COPY_AND_ASSIGN(HShr);
2681};
2682
2683class HUShr : public HBinaryOperation {
2684 public:
2685 HUShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2686 : HBinaryOperation(result_type, left, right) {}
2687
2688 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
2689 uint32_t ux = static_cast<uint32_t>(x);
2690 uint32_t uy = static_cast<uint32_t>(y) & kMaxIntShiftValue;
2691 return static_cast<int32_t>(ux >> uy);
2692 }
2693
2694 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
2695 uint64_t ux = static_cast<uint64_t>(x);
2696 uint64_t uy = static_cast<uint64_t>(y) & kMaxLongShiftValue;
2697 return static_cast<int64_t>(ux >> uy);
2698 }
2699
2700 DECLARE_INSTRUCTION(UShr);
2701
2702 private:
2703 DISALLOW_COPY_AND_ASSIGN(HUShr);
2704};
2705
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002706class HAnd : public HBinaryOperation {
2707 public:
2708 HAnd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2709 : HBinaryOperation(result_type, left, right) {}
2710
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002711 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002712
2713 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x & y; }
2714 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x & y; }
2715
2716 DECLARE_INSTRUCTION(And);
2717
2718 private:
2719 DISALLOW_COPY_AND_ASSIGN(HAnd);
2720};
2721
2722class HOr : public HBinaryOperation {
2723 public:
2724 HOr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2725 : HBinaryOperation(result_type, left, right) {}
2726
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002727 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002728
2729 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x | y; }
2730 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x | y; }
2731
2732 DECLARE_INSTRUCTION(Or);
2733
2734 private:
2735 DISALLOW_COPY_AND_ASSIGN(HOr);
2736};
2737
2738class HXor : public HBinaryOperation {
2739 public:
2740 HXor(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2741 : HBinaryOperation(result_type, left, right) {}
2742
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002743 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002744
2745 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x ^ y; }
2746 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x ^ y; }
2747
2748 DECLARE_INSTRUCTION(Xor);
2749
2750 private:
2751 DISALLOW_COPY_AND_ASSIGN(HXor);
2752};
2753
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002754// The value of a parameter in this method. Its location depends on
2755// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07002756class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002757 public:
Calin Juravle10e244f2015-01-26 18:54:32 +00002758 HParameterValue(uint8_t index, Primitive::Type parameter_type, bool is_this = false)
2759 : HExpression(parameter_type, SideEffects::None()), index_(index), is_this_(is_this) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002760
2761 uint8_t GetIndex() const { return index_; }
2762
Calin Juravle10e244f2015-01-26 18:54:32 +00002763 bool CanBeNull() const OVERRIDE { return !is_this_; }
2764
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002765 DECLARE_INSTRUCTION(ParameterValue);
2766
2767 private:
2768 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00002769 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002770 const uint8_t index_;
2771
Calin Juravle10e244f2015-01-26 18:54:32 +00002772 // Whether or not the parameter value corresponds to 'this' argument.
2773 const bool is_this_;
2774
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002775 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
2776};
2777
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002778class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002779 public:
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002780 explicit HNot(Primitive::Type result_type, HInstruction* input)
2781 : HUnaryOperation(result_type, input) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002782
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002783 bool CanBeMoved() const OVERRIDE { return true; }
2784 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002785 UNUSED(other);
2786 return true;
2787 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002788
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002789 int32_t Evaluate(int32_t x) const OVERRIDE { return ~x; }
2790 int64_t Evaluate(int64_t x) const OVERRIDE { return ~x; }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002791
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002792 DECLARE_INSTRUCTION(Not);
2793
2794 private:
2795 DISALLOW_COPY_AND_ASSIGN(HNot);
2796};
2797
David Brazdil66d126e2015-04-03 16:02:44 +01002798class HBooleanNot : public HUnaryOperation {
2799 public:
2800 explicit HBooleanNot(HInstruction* input)
2801 : HUnaryOperation(Primitive::Type::kPrimBoolean, input) {}
2802
2803 bool CanBeMoved() const OVERRIDE { return true; }
2804 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2805 UNUSED(other);
2806 return true;
2807 }
2808
2809 int32_t Evaluate(int32_t x) const OVERRIDE {
2810 DCHECK(IsUint<1>(x));
2811 return !x;
2812 }
2813
2814 int64_t Evaluate(int64_t x ATTRIBUTE_UNUSED) const OVERRIDE {
2815 LOG(FATAL) << DebugName() << " cannot be used with 64-bit values";
2816 UNREACHABLE();
2817 }
2818
2819 DECLARE_INSTRUCTION(BooleanNot);
2820
2821 private:
2822 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
2823};
2824
Roland Levillaindff1f282014-11-05 14:15:05 +00002825class HTypeConversion : public HExpression<1> {
2826 public:
2827 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00002828 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
2829 : HExpression(result_type, SideEffects::None()), dex_pc_(dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002830 SetRawInputAt(0, input);
2831 DCHECK_NE(input->GetType(), result_type);
2832 }
2833
2834 HInstruction* GetInput() const { return InputAt(0); }
2835 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
2836 Primitive::Type GetResultType() const { return GetType(); }
2837
Roland Levillain624279f2014-12-04 11:54:28 +00002838 // Required by the x86 and ARM code generators when producing calls
2839 // to the runtime.
2840 uint32_t GetDexPc() const { return dex_pc_; }
2841
Roland Levillaindff1f282014-11-05 14:15:05 +00002842 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00002843 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00002844
2845 DECLARE_INSTRUCTION(TypeConversion);
2846
2847 private:
Roland Levillain624279f2014-12-04 11:54:28 +00002848 const uint32_t dex_pc_;
2849
Roland Levillaindff1f282014-11-05 14:15:05 +00002850 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
2851};
2852
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00002853static constexpr uint32_t kNoRegNumber = -1;
2854
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002855class HPhi : public HInstruction {
2856 public:
2857 HPhi(ArenaAllocator* arena, uint32_t reg_number, size_t number_of_inputs, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002858 : HInstruction(SideEffects::None()),
2859 inputs_(arena, number_of_inputs),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002860 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002861 type_(type),
Calin Juravle10e244f2015-01-26 18:54:32 +00002862 is_live_(false),
2863 can_be_null_(true) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002864 inputs_.SetSize(number_of_inputs);
2865 }
2866
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00002867 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
2868 static Primitive::Type ToPhiType(Primitive::Type type) {
2869 switch (type) {
2870 case Primitive::kPrimBoolean:
2871 case Primitive::kPrimByte:
2872 case Primitive::kPrimShort:
2873 case Primitive::kPrimChar:
2874 return Primitive::kPrimInt;
2875 default:
2876 return type;
2877 }
2878 }
2879
Calin Juravle10e244f2015-01-26 18:54:32 +00002880 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002881
2882 void AddInput(HInstruction* input);
David Brazdil2d7352b2015-04-20 14:52:42 +01002883 void RemoveInputAt(size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002884
Calin Juravle10e244f2015-01-26 18:54:32 +00002885 Primitive::Type GetType() const OVERRIDE { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002886 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002887
Calin Juravle10e244f2015-01-26 18:54:32 +00002888 bool CanBeNull() const OVERRIDE { return can_be_null_; }
2889 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
2890
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002891 uint32_t GetRegNumber() const { return reg_number_; }
2892
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002893 void SetDead() { is_live_ = false; }
2894 void SetLive() { is_live_ = true; }
2895 bool IsDead() const { return !is_live_; }
2896 bool IsLive() const { return is_live_; }
2897
Calin Juravlea4f88312015-04-16 12:57:19 +01002898 // Returns the next equivalent phi (starting from the current one) or null if there is none.
2899 // An equivalent phi is a phi having the same dex register and type.
2900 // It assumes that phis with the same dex register are adjacent.
2901 HPhi* GetNextEquivalentPhiWithSameType() {
2902 HInstruction* next = GetNext();
2903 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
2904 if (next->GetType() == GetType()) {
2905 return next->AsPhi();
2906 }
2907 next = next->GetNext();
2908 }
2909 return nullptr;
2910 }
2911
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002912 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002913
David Brazdil1abb4192015-02-17 18:33:36 +00002914 protected:
2915 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
2916
2917 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
2918 inputs_.Put(index, input);
2919 }
2920
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002921 private:
David Brazdil1abb4192015-02-17 18:33:36 +00002922 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002923 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002924 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002925 bool is_live_;
Calin Juravle10e244f2015-01-26 18:54:32 +00002926 bool can_be_null_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002927
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002928 DISALLOW_COPY_AND_ASSIGN(HPhi);
2929};
2930
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002931class HNullCheck : public HExpression<1> {
2932 public:
2933 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002934 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002935 SetRawInputAt(0, value);
2936 }
2937
Calin Juravle10e244f2015-01-26 18:54:32 +00002938 bool CanBeMoved() const OVERRIDE { return true; }
2939 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002940 UNUSED(other);
2941 return true;
2942 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002943
Calin Juravle10e244f2015-01-26 18:54:32 +00002944 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002945
Calin Juravle10e244f2015-01-26 18:54:32 +00002946 bool CanThrow() const OVERRIDE { return true; }
2947
2948 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01002949
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002950 uint32_t GetDexPc() const { return dex_pc_; }
2951
2952 DECLARE_INSTRUCTION(NullCheck);
2953
2954 private:
2955 const uint32_t dex_pc_;
2956
2957 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
2958};
2959
2960class FieldInfo : public ValueObject {
2961 public:
Calin Juravle52c48962014-12-16 17:02:57 +00002962 FieldInfo(MemberOffset field_offset, Primitive::Type field_type, bool is_volatile)
2963 : field_offset_(field_offset), field_type_(field_type), is_volatile_(is_volatile) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002964
2965 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002966 Primitive::Type GetFieldType() const { return field_type_; }
Calin Juravle52c48962014-12-16 17:02:57 +00002967 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002968
2969 private:
2970 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01002971 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00002972 const bool is_volatile_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002973};
2974
2975class HInstanceFieldGet : public HExpression<1> {
2976 public:
2977 HInstanceFieldGet(HInstruction* value,
2978 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00002979 MemberOffset field_offset,
2980 bool is_volatile)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002981 : HExpression(field_type, SideEffects::DependsOnSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00002982 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002983 SetRawInputAt(0, value);
2984 }
2985
Calin Juravle10c9cbe2014-12-19 10:50:19 +00002986 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002987
2988 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2989 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
2990 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002991 }
2992
Calin Juravle641547a2015-04-21 22:08:51 +01002993 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
2994 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00002995 }
2996
2997 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002998 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
2999 }
3000
Calin Juravle52c48962014-12-16 17:02:57 +00003001 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003002 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003003 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003004 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003005
3006 DECLARE_INSTRUCTION(InstanceFieldGet);
3007
3008 private:
3009 const FieldInfo field_info_;
3010
3011 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
3012};
3013
3014class HInstanceFieldSet : public HTemplateInstruction<2> {
3015 public:
3016 HInstanceFieldSet(HInstruction* object,
3017 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01003018 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003019 MemberOffset field_offset,
3020 bool is_volatile)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003021 : HTemplateInstruction(SideEffects::ChangesSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00003022 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003023 SetRawInputAt(0, object);
3024 SetRawInputAt(1, value);
3025 }
3026
Calin Juravle641547a2015-04-21 22:08:51 +01003027 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3028 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00003029 }
3030
Calin Juravle52c48962014-12-16 17:02:57 +00003031 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003032 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003033 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003034 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003035 HInstruction* GetValue() const { return InputAt(1); }
3036
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003037 DECLARE_INSTRUCTION(InstanceFieldSet);
3038
3039 private:
3040 const FieldInfo field_info_;
3041
3042 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
3043};
3044
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003045class HArrayGet : public HExpression<2> {
3046 public:
3047 HArrayGet(HInstruction* array, HInstruction* index, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003048 : HExpression(type, SideEffects::DependsOnSomething()) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003049 SetRawInputAt(0, array);
3050 SetRawInputAt(1, index);
3051 }
3052
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003053 bool CanBeMoved() const OVERRIDE { return true; }
3054 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003055 UNUSED(other);
3056 return true;
3057 }
Calin Juravle641547a2015-04-21 22:08:51 +01003058 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3059 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00003060 // TODO: We can be smarter here.
3061 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
3062 // which generates the implicit null check. There are cases when these can be removed
3063 // to produce better code. If we ever add optimizations to do so we should allow an
3064 // implicit check here (as long as the address falls in the first page).
3065 return false;
3066 }
3067
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003068 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003069
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003070 HInstruction* GetArray() const { return InputAt(0); }
3071 HInstruction* GetIndex() const { return InputAt(1); }
3072
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003073 DECLARE_INSTRUCTION(ArrayGet);
3074
3075 private:
3076 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
3077};
3078
3079class HArraySet : public HTemplateInstruction<3> {
3080 public:
3081 HArraySet(HInstruction* array,
3082 HInstruction* index,
3083 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003084 Primitive::Type expected_component_type,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003085 uint32_t dex_pc)
3086 : HTemplateInstruction(SideEffects::ChangesSomething()),
3087 dex_pc_(dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003088 expected_component_type_(expected_component_type),
3089 needs_type_check_(value->GetType() == Primitive::kPrimNot) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003090 SetRawInputAt(0, array);
3091 SetRawInputAt(1, index);
3092 SetRawInputAt(2, value);
3093 }
3094
Calin Juravle77520bc2015-01-12 18:45:46 +00003095 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003096 // We currently always call a runtime method to catch array store
3097 // exceptions.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003098 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003099 }
3100
Calin Juravle641547a2015-04-21 22:08:51 +01003101 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3102 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00003103 // TODO: Same as for ArrayGet.
3104 return false;
3105 }
3106
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003107 void ClearNeedsTypeCheck() {
3108 needs_type_check_ = false;
3109 }
3110
3111 bool NeedsTypeCheck() const { return needs_type_check_; }
3112
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003113 uint32_t GetDexPc() const { return dex_pc_; }
3114
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003115 HInstruction* GetArray() const { return InputAt(0); }
3116 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003117 HInstruction* GetValue() const { return InputAt(2); }
3118
3119 Primitive::Type GetComponentType() const {
3120 // The Dex format does not type floating point index operations. Since the
3121 // `expected_component_type_` is set during building and can therefore not
3122 // be correct, we also check what is the value type. If it is a floating
3123 // point type, we must use that type.
3124 Primitive::Type value_type = GetValue()->GetType();
3125 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
3126 ? value_type
3127 : expected_component_type_;
3128 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003129
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003130 DECLARE_INSTRUCTION(ArraySet);
3131
3132 private:
3133 const uint32_t dex_pc_;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003134 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003135 bool needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003136
3137 DISALLOW_COPY_AND_ASSIGN(HArraySet);
3138};
3139
3140class HArrayLength : public HExpression<1> {
3141 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003142 explicit HArrayLength(HInstruction* array)
3143 : HExpression(Primitive::kPrimInt, SideEffects::None()) {
3144 // Note that arrays do not change length, so the instruction does not
3145 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003146 SetRawInputAt(0, array);
3147 }
3148
Calin Juravle77520bc2015-01-12 18:45:46 +00003149 bool CanBeMoved() const OVERRIDE { return true; }
3150 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003151 UNUSED(other);
3152 return true;
3153 }
Calin Juravle641547a2015-04-21 22:08:51 +01003154 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3155 return obj == InputAt(0);
3156 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003157
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003158 DECLARE_INSTRUCTION(ArrayLength);
3159
3160 private:
3161 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
3162};
3163
3164class HBoundsCheck : public HExpression<2> {
3165 public:
3166 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003167 : HExpression(index->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003168 DCHECK(index->GetType() == Primitive::kPrimInt);
3169 SetRawInputAt(0, index);
3170 SetRawInputAt(1, length);
3171 }
3172
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003173 bool CanBeMoved() const OVERRIDE { return true; }
3174 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003175 UNUSED(other);
3176 return true;
3177 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003178
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003179 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003180
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003181 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01003182
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003183 uint32_t GetDexPc() const { return dex_pc_; }
3184
3185 DECLARE_INSTRUCTION(BoundsCheck);
3186
3187 private:
3188 const uint32_t dex_pc_;
3189
3190 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
3191};
3192
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003193/**
3194 * Some DEX instructions are folded into multiple HInstructions that need
3195 * to stay live until the last HInstruction. This class
3196 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00003197 * HInstruction stays live. `index` represents the stack location index of the
3198 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003199 */
3200class HTemporary : public HTemplateInstruction<0> {
3201 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003202 explicit HTemporary(size_t index) : HTemplateInstruction(SideEffects::None()), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003203
3204 size_t GetIndex() const { return index_; }
3205
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00003206 Primitive::Type GetType() const OVERRIDE {
3207 // The previous instruction is the one that will be stored in the temporary location.
3208 DCHECK(GetPrevious() != nullptr);
3209 return GetPrevious()->GetType();
3210 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00003211
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003212 DECLARE_INSTRUCTION(Temporary);
3213
3214 private:
3215 const size_t index_;
3216
3217 DISALLOW_COPY_AND_ASSIGN(HTemporary);
3218};
3219
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003220class HSuspendCheck : public HTemplateInstruction<0> {
3221 public:
3222 explicit HSuspendCheck(uint32_t dex_pc)
Nicolas Geoffray9ebc72c2014-09-25 16:33:42 +01003223 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003224
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003225 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003226 return true;
3227 }
3228
3229 uint32_t GetDexPc() const { return dex_pc_; }
3230
3231 DECLARE_INSTRUCTION(SuspendCheck);
3232
3233 private:
3234 const uint32_t dex_pc_;
3235
3236 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
3237};
3238
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003239/**
3240 * Instruction to load a Class object.
3241 */
3242class HLoadClass : public HExpression<0> {
3243 public:
3244 HLoadClass(uint16_t type_index,
3245 bool is_referrers_class,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003246 uint32_t dex_pc)
3247 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3248 type_index_(type_index),
3249 is_referrers_class_(is_referrers_class),
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003250 dex_pc_(dex_pc),
Calin Juravleb1498f62015-02-16 13:13:29 +00003251 generate_clinit_check_(false),
3252 loaded_class_rti_(ReferenceTypeInfo::CreateTop(/* is_exact */ false)) {}
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003253
3254 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003255
3256 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3257 return other->AsLoadClass()->type_index_ == type_index_;
3258 }
3259
3260 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
3261
3262 uint32_t GetDexPc() const { return dex_pc_; }
3263 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003264 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003265
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003266 bool NeedsEnvironment() const OVERRIDE {
3267 // Will call runtime and load the class if the class is not loaded yet.
3268 // TODO: finer grain decision.
3269 return !is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003270 }
3271
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003272 bool MustGenerateClinitCheck() const {
3273 return generate_clinit_check_;
3274 }
3275
3276 void SetMustGenerateClinitCheck() {
3277 generate_clinit_check_ = true;
3278 }
3279
3280 bool CanCallRuntime() const {
3281 return MustGenerateClinitCheck() || !is_referrers_class_;
3282 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003283
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003284 bool CanThrow() const OVERRIDE {
3285 // May call runtime and and therefore can throw.
3286 // TODO: finer grain decision.
3287 return !is_referrers_class_;
3288 }
3289
Calin Juravleacf735c2015-02-12 15:25:22 +00003290 ReferenceTypeInfo GetLoadedClassRTI() {
3291 return loaded_class_rti_;
3292 }
3293
3294 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
3295 // Make sure we only set exact types (the loaded class should never be merged).
3296 DCHECK(rti.IsExact());
3297 loaded_class_rti_ = rti;
3298 }
3299
3300 bool IsResolved() {
3301 return loaded_class_rti_.IsExact();
3302 }
3303
Nicolas Geoffray9437b782015-03-25 10:08:51 +00003304 bool NeedsDexCache() const OVERRIDE { return !is_referrers_class_; }
3305
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003306 DECLARE_INSTRUCTION(LoadClass);
3307
3308 private:
3309 const uint16_t type_index_;
3310 const bool is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003311 const uint32_t dex_pc_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003312 // Whether this instruction must generate the initialization check.
3313 // Used for code generation.
3314 bool generate_clinit_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003315
Calin Juravleacf735c2015-02-12 15:25:22 +00003316 ReferenceTypeInfo loaded_class_rti_;
3317
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003318 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
3319};
3320
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003321class HLoadString : public HExpression<0> {
3322 public:
3323 HLoadString(uint32_t string_index, uint32_t dex_pc)
3324 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3325 string_index_(string_index),
3326 dex_pc_(dex_pc) {}
3327
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003328 bool CanBeMoved() const OVERRIDE { return true; }
3329
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003330 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3331 return other->AsLoadString()->string_index_ == string_index_;
3332 }
3333
3334 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
3335
3336 uint32_t GetDexPc() const { return dex_pc_; }
3337 uint32_t GetStringIndex() const { return string_index_; }
3338
3339 // TODO: Can we deopt or debug when we resolve a string?
3340 bool NeedsEnvironment() const OVERRIDE { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00003341 bool NeedsDexCache() const OVERRIDE { return true; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003342
3343 DECLARE_INSTRUCTION(LoadString);
3344
3345 private:
3346 const uint32_t string_index_;
3347 const uint32_t dex_pc_;
3348
3349 DISALLOW_COPY_AND_ASSIGN(HLoadString);
3350};
3351
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003352/**
3353 * Performs an initialization check on its Class object input.
3354 */
3355class HClinitCheck : public HExpression<1> {
3356 public:
3357 explicit HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Nicolas Geoffraya0466e12015-03-27 15:00:40 +00003358 : HExpression(Primitive::kPrimNot, SideEffects::ChangesSomething()),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003359 dex_pc_(dex_pc) {
3360 SetRawInputAt(0, constant);
3361 }
3362
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003363 bool CanBeMoved() const OVERRIDE { return true; }
3364 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3365 UNUSED(other);
3366 return true;
3367 }
3368
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003369 bool NeedsEnvironment() const OVERRIDE {
3370 // May call runtime to initialize the class.
3371 return true;
3372 }
3373
3374 uint32_t GetDexPc() const { return dex_pc_; }
3375
3376 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
3377
3378 DECLARE_INSTRUCTION(ClinitCheck);
3379
3380 private:
3381 const uint32_t dex_pc_;
3382
3383 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
3384};
3385
3386class HStaticFieldGet : public HExpression<1> {
3387 public:
3388 HStaticFieldGet(HInstruction* cls,
3389 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003390 MemberOffset field_offset,
3391 bool is_volatile)
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003392 : HExpression(field_type, SideEffects::DependsOnSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00003393 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003394 SetRawInputAt(0, cls);
3395 }
3396
Calin Juravle52c48962014-12-16 17:02:57 +00003397
Calin Juravle10c9cbe2014-12-19 10:50:19 +00003398 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003399
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003400 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00003401 HStaticFieldGet* other_get = other->AsStaticFieldGet();
3402 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003403 }
3404
3405 size_t ComputeHashCode() const OVERRIDE {
3406 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
3407 }
3408
Calin Juravle52c48962014-12-16 17:02:57 +00003409 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003410 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
3411 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003412 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003413
3414 DECLARE_INSTRUCTION(StaticFieldGet);
3415
3416 private:
3417 const FieldInfo field_info_;
3418
3419 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
3420};
3421
3422class HStaticFieldSet : public HTemplateInstruction<2> {
3423 public:
3424 HStaticFieldSet(HInstruction* cls,
3425 HInstruction* value,
3426 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003427 MemberOffset field_offset,
3428 bool is_volatile)
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003429 : HTemplateInstruction(SideEffects::ChangesSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00003430 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003431 SetRawInputAt(0, cls);
3432 SetRawInputAt(1, value);
3433 }
3434
Calin Juravle52c48962014-12-16 17:02:57 +00003435 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003436 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
3437 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003438 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003439
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003440 HInstruction* GetValue() const { return InputAt(1); }
3441
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003442 DECLARE_INSTRUCTION(StaticFieldSet);
3443
3444 private:
3445 const FieldInfo field_info_;
3446
3447 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
3448};
3449
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003450// Implement the move-exception DEX instruction.
3451class HLoadException : public HExpression<0> {
3452 public:
3453 HLoadException() : HExpression(Primitive::kPrimNot, SideEffects::None()) {}
3454
3455 DECLARE_INSTRUCTION(LoadException);
3456
3457 private:
3458 DISALLOW_COPY_AND_ASSIGN(HLoadException);
3459};
3460
3461class HThrow : public HTemplateInstruction<1> {
3462 public:
3463 HThrow(HInstruction* exception, uint32_t dex_pc)
3464 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {
3465 SetRawInputAt(0, exception);
3466 }
3467
3468 bool IsControlFlow() const OVERRIDE { return true; }
3469
3470 bool NeedsEnvironment() const OVERRIDE { return true; }
3471
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003472 bool CanThrow() const OVERRIDE { return true; }
3473
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003474 uint32_t GetDexPc() const { return dex_pc_; }
3475
3476 DECLARE_INSTRUCTION(Throw);
3477
3478 private:
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003479 const uint32_t dex_pc_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003480
3481 DISALLOW_COPY_AND_ASSIGN(HThrow);
3482};
3483
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003484class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003485 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003486 HInstanceOf(HInstruction* object,
3487 HLoadClass* constant,
3488 bool class_is_final,
3489 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003490 : HExpression(Primitive::kPrimBoolean, SideEffects::None()),
3491 class_is_final_(class_is_final),
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003492 must_do_null_check_(true),
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003493 dex_pc_(dex_pc) {
3494 SetRawInputAt(0, object);
3495 SetRawInputAt(1, constant);
3496 }
3497
3498 bool CanBeMoved() const OVERRIDE { return true; }
3499
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003500 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003501 return true;
3502 }
3503
3504 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003505 return false;
3506 }
3507
3508 uint32_t GetDexPc() const { return dex_pc_; }
3509
3510 bool IsClassFinal() const { return class_is_final_; }
3511
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003512 // Used only in code generation.
3513 bool MustDoNullCheck() const { return must_do_null_check_; }
3514 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
3515
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003516 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003517
3518 private:
3519 const bool class_is_final_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003520 bool must_do_null_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003521 const uint32_t dex_pc_;
3522
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003523 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
3524};
3525
Calin Juravleb1498f62015-02-16 13:13:29 +00003526class HBoundType : public HExpression<1> {
3527 public:
3528 HBoundType(HInstruction* input, ReferenceTypeInfo bound_type)
3529 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3530 bound_type_(bound_type) {
Calin Juravle61d544b2015-02-23 16:46:57 +00003531 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00003532 SetRawInputAt(0, input);
3533 }
3534
3535 const ReferenceTypeInfo& GetBoundType() const { return bound_type_; }
3536
3537 bool CanBeNull() const OVERRIDE {
3538 // `null instanceof ClassX` always return false so we can't be null.
3539 return false;
3540 }
3541
3542 DECLARE_INSTRUCTION(BoundType);
3543
3544 private:
3545 // Encodes the most upper class that this instruction can have. In other words
3546 // it is always the case that GetBoundType().IsSupertypeOf(GetReferenceType()).
3547 // It is used to bound the type in cases like `if (x instanceof ClassX) {}`
3548 const ReferenceTypeInfo bound_type_;
3549
3550 DISALLOW_COPY_AND_ASSIGN(HBoundType);
3551};
3552
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003553class HCheckCast : public HTemplateInstruction<2> {
3554 public:
3555 HCheckCast(HInstruction* object,
3556 HLoadClass* constant,
3557 bool class_is_final,
3558 uint32_t dex_pc)
3559 : HTemplateInstruction(SideEffects::None()),
3560 class_is_final_(class_is_final),
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003561 must_do_null_check_(true),
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003562 dex_pc_(dex_pc) {
3563 SetRawInputAt(0, object);
3564 SetRawInputAt(1, constant);
3565 }
3566
3567 bool CanBeMoved() const OVERRIDE { return true; }
3568
3569 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
3570 return true;
3571 }
3572
3573 bool NeedsEnvironment() const OVERRIDE {
3574 // Instruction may throw a CheckCastError.
3575 return true;
3576 }
3577
3578 bool CanThrow() const OVERRIDE { return true; }
3579
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003580 bool MustDoNullCheck() const { return must_do_null_check_; }
3581 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
3582
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003583 uint32_t GetDexPc() const { return dex_pc_; }
3584
3585 bool IsClassFinal() const { return class_is_final_; }
3586
3587 DECLARE_INSTRUCTION(CheckCast);
3588
3589 private:
3590 const bool class_is_final_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003591 bool must_do_null_check_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003592 const uint32_t dex_pc_;
3593
3594 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003595};
3596
Calin Juravle27df7582015-04-17 19:12:31 +01003597class HMemoryBarrier : public HTemplateInstruction<0> {
3598 public:
3599 explicit HMemoryBarrier(MemBarrierKind barrier_kind)
3600 : HTemplateInstruction(SideEffects::None()),
3601 barrier_kind_(barrier_kind) {}
3602
3603 MemBarrierKind GetBarrierKind() { return barrier_kind_; }
3604
3605 DECLARE_INSTRUCTION(MemoryBarrier);
3606
3607 private:
3608 const MemBarrierKind barrier_kind_;
3609
3610 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
3611};
3612
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003613class HMonitorOperation : public HTemplateInstruction<1> {
3614 public:
3615 enum OperationKind {
3616 kEnter,
3617 kExit,
3618 };
3619
3620 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
3621 : HTemplateInstruction(SideEffects::None()), kind_(kind), dex_pc_(dex_pc) {
3622 SetRawInputAt(0, object);
3623 }
3624
3625 // Instruction may throw a Java exception, so we need an environment.
3626 bool NeedsEnvironment() const OVERRIDE { return true; }
3627 bool CanThrow() const OVERRIDE { return true; }
3628
3629 uint32_t GetDexPc() const { return dex_pc_; }
3630
3631 bool IsEnter() const { return kind_ == kEnter; }
3632
3633 DECLARE_INSTRUCTION(MonitorOperation);
3634
Calin Juravle52c48962014-12-16 17:02:57 +00003635 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003636 const OperationKind kind_;
3637 const uint32_t dex_pc_;
3638
3639 private:
3640 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
3641};
3642
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003643class MoveOperands : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003644 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01003645 MoveOperands(Location source,
3646 Location destination,
3647 Primitive::Type type,
3648 HInstruction* instruction)
3649 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003650
3651 Location GetSource() const { return source_; }
3652 Location GetDestination() const { return destination_; }
3653
3654 void SetSource(Location value) { source_ = value; }
3655 void SetDestination(Location value) { destination_ = value; }
3656
3657 // The parallel move resolver marks moves as "in-progress" by clearing the
3658 // destination (but not the source).
3659 Location MarkPending() {
3660 DCHECK(!IsPending());
3661 Location dest = destination_;
3662 destination_ = Location::NoLocation();
3663 return dest;
3664 }
3665
3666 void ClearPending(Location dest) {
3667 DCHECK(IsPending());
3668 destination_ = dest;
3669 }
3670
3671 bool IsPending() const {
3672 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
3673 return destination_.IsInvalid() && !source_.IsInvalid();
3674 }
3675
3676 // True if this blocks a move from the given location.
3677 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08003678 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003679 }
3680
3681 // A move is redundant if it's been eliminated, if its source and
3682 // destination are the same, or if its destination is unneeded.
3683 bool IsRedundant() const {
3684 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
3685 }
3686
3687 // We clear both operands to indicate move that's been eliminated.
3688 void Eliminate() {
3689 source_ = destination_ = Location::NoLocation();
3690 }
3691
3692 bool IsEliminated() const {
3693 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
3694 return source_.IsInvalid();
3695 }
3696
Nicolas Geoffray90218252015-04-15 11:56:51 +01003697 bool Is64BitMove() const {
3698 return Primitive::Is64BitType(type_);
3699 }
3700
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003701 HInstruction* GetInstruction() const { return instruction_; }
3702
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003703 private:
3704 Location source_;
3705 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01003706 // The type this move is for.
3707 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003708 // The instruction this move is assocatied with. Null when this move is
3709 // for moving an input in the expected locations of user (including a phi user).
3710 // This is only used in debug mode, to ensure we do not connect interval siblings
3711 // in the same parallel move.
3712 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003713};
3714
3715static constexpr size_t kDefaultNumberOfMoves = 4;
3716
3717class HParallelMove : public HTemplateInstruction<0> {
3718 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003719 explicit HParallelMove(ArenaAllocator* arena)
3720 : HTemplateInstruction(SideEffects::None()), moves_(arena, kDefaultNumberOfMoves) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003721
Nicolas Geoffray90218252015-04-15 11:56:51 +01003722 void AddMove(Location source,
3723 Location destination,
3724 Primitive::Type type,
3725 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003726 DCHECK(source.IsValid());
3727 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003728 if (kIsDebugBuild) {
3729 if (instruction != nullptr) {
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00003730 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003731 if (moves_.Get(i).GetInstruction() == instruction) {
3732 // Special case the situation where the move is for the spill slot
3733 // of the instruction.
3734 if ((GetPrevious() == instruction)
3735 || ((GetPrevious() == nullptr)
3736 && instruction->IsPhi()
3737 && instruction->GetBlock() == GetBlock())) {
3738 DCHECK_NE(destination.GetKind(), moves_.Get(i).GetDestination().GetKind())
3739 << "Doing parallel moves for the same instruction.";
3740 } else {
3741 DCHECK(false) << "Doing parallel moves for the same instruction.";
3742 }
3743 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00003744 }
3745 }
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003746 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Zheng Xuad4450e2015-04-17 18:48:56 +08003747 DCHECK(!destination.OverlapsWith(moves_.Get(i).GetDestination()))
3748 << "Overlapped destination for two moves in a parallel move.";
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003749 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003750 }
Nicolas Geoffray90218252015-04-15 11:56:51 +01003751 moves_.Add(MoveOperands(source, destination, type, instruction));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003752 }
3753
3754 MoveOperands* MoveOperandsAt(size_t index) const {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003755 return moves_.GetRawStorage() + index;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003756 }
3757
3758 size_t NumMoves() const { return moves_.Size(); }
3759
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003760 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003761
3762 private:
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003763 GrowableArray<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003764
3765 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
3766};
3767
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003768class HGraphVisitor : public ValueObject {
3769 public:
Dave Allison20dfc792014-06-16 20:44:29 -07003770 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
3771 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003772
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003773 virtual void VisitInstruction(HInstruction* instruction) { UNUSED(instruction); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003774 virtual void VisitBasicBlock(HBasicBlock* block);
3775
Roland Levillain633021e2014-10-01 14:12:25 +01003776 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003777 void VisitInsertionOrder();
3778
Roland Levillain633021e2014-10-01 14:12:25 +01003779 // Visit the graph following dominator tree reverse post-order.
3780 void VisitReversePostOrder();
3781
Nicolas Geoffray787c3072014-03-17 10:20:19 +00003782 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00003783
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003784 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003785#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003786 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
3787
3788 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
3789
3790#undef DECLARE_VISIT_INSTRUCTION
3791
3792 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07003793 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003794
3795 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
3796};
3797
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003798class HGraphDelegateVisitor : public HGraphVisitor {
3799 public:
3800 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
3801 virtual ~HGraphDelegateVisitor() {}
3802
3803 // Visit functions that delegate to to super class.
3804#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003805 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003806
3807 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
3808
3809#undef DECLARE_VISIT_INSTRUCTION
3810
3811 private:
3812 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
3813};
3814
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003815class HInsertionOrderIterator : public ValueObject {
3816 public:
3817 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
3818
3819 bool Done() const { return index_ == graph_.GetBlocks().Size(); }
3820 HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); }
3821 void Advance() { ++index_; }
3822
3823 private:
3824 const HGraph& graph_;
3825 size_t index_;
3826
3827 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
3828};
3829
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003830class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003831 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00003832 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
3833 // Check that reverse post order of the graph has been built.
3834 DCHECK(!graph.GetReversePostOrder().IsEmpty());
3835 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003836
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003837 bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); }
3838 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003839 void Advance() { ++index_; }
3840
3841 private:
3842 const HGraph& graph_;
3843 size_t index_;
3844
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003845 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003846};
3847
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003848class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003849 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003850 explicit HPostOrderIterator(const HGraph& graph)
David Brazdil10f56cb2015-03-24 18:49:14 +00003851 : graph_(graph), index_(graph_.GetReversePostOrder().Size()) {
3852 // Check that reverse post order of the graph has been built.
3853 DCHECK(!graph.GetReversePostOrder().IsEmpty());
3854 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003855
3856 bool Done() const { return index_ == 0; }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003857 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003858 void Advance() { --index_; }
3859
3860 private:
3861 const HGraph& graph_;
3862 size_t index_;
3863
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003864 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003865};
3866
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01003867class HLinearPostOrderIterator : public ValueObject {
3868 public:
3869 explicit HLinearPostOrderIterator(const HGraph& graph)
3870 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().Size()) {}
3871
3872 bool Done() const { return index_ == 0; }
3873
3874 HBasicBlock* Current() const { return order_.Get(index_ -1); }
3875
3876 void Advance() {
3877 --index_;
3878 DCHECK_GE(index_, 0U);
3879 }
3880
3881 private:
3882 const GrowableArray<HBasicBlock*>& order_;
3883 size_t index_;
3884
3885 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
3886};
3887
3888class HLinearOrderIterator : public ValueObject {
3889 public:
3890 explicit HLinearOrderIterator(const HGraph& graph)
3891 : order_(graph.GetLinearOrder()), index_(0) {}
3892
3893 bool Done() const { return index_ == order_.Size(); }
3894 HBasicBlock* Current() const { return order_.Get(index_); }
3895 void Advance() { ++index_; }
3896
3897 private:
3898 const GrowableArray<HBasicBlock*>& order_;
3899 size_t index_;
3900
3901 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
3902};
3903
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003904// Iterator over the blocks that art part of the loop. Includes blocks part
3905// of an inner loop. The order in which the blocks are iterated is on their
3906// block id.
3907class HBlocksInLoopIterator : public ValueObject {
3908 public:
3909 explicit HBlocksInLoopIterator(const HLoopInformation& info)
3910 : blocks_in_loop_(info.GetBlocks()),
3911 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
3912 index_(0) {
3913 if (!blocks_in_loop_.IsBitSet(index_)) {
3914 Advance();
3915 }
3916 }
3917
3918 bool Done() const { return index_ == blocks_.Size(); }
3919 HBasicBlock* Current() const { return blocks_.Get(index_); }
3920 void Advance() {
3921 ++index_;
3922 for (size_t e = blocks_.Size(); index_ < e; ++index_) {
3923 if (blocks_in_loop_.IsBitSet(index_)) {
3924 break;
3925 }
3926 }
3927 }
3928
3929 private:
3930 const BitVector& blocks_in_loop_;
3931 const GrowableArray<HBasicBlock*>& blocks_;
3932 size_t index_;
3933
3934 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
3935};
3936
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003937inline int64_t Int64FromConstant(HConstant* constant) {
3938 DCHECK(constant->IsIntConstant() || constant->IsLongConstant());
3939 return constant->IsIntConstant() ? constant->AsIntConstant()->GetValue()
3940 : constant->AsLongConstant()->GetValue();
3941}
3942
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003943} // namespace art
3944
3945#endif // ART_COMPILER_OPTIMIZING_NODES_H_