blob: 0533bff0b33e13ea8534d3ce5d7463eb08695879 [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
David Brazdil769c9e52015-04-27 13:54:09 +0100564 void SwapSuccessors() {
565 DCHECK_EQ(successors_.Size(), 2u);
566 HBasicBlock* temp = successors_.Get(0);
567 successors_.Put(0, successors_.Get(1));
568 successors_.Put(1, temp);
569 }
570
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100571 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) {
572 for (size_t i = 0, e = predecessors_.Size(); i < e; ++i) {
573 if (predecessors_.Get(i) == predecessor) {
574 return i;
575 }
576 }
577 return -1;
578 }
579
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100580 size_t GetSuccessorIndexOf(HBasicBlock* successor) {
581 for (size_t i = 0, e = successors_.Size(); i < e; ++i) {
582 if (successors_.Get(i) == successor) {
583 return i;
584 }
585 }
586 return -1;
587 }
588
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000589 // Split the block into two blocks just after `cursor`. Returns the newly
590 // created block. Note that this method just updates raw block information,
591 // like predecessors, successors, dominators, and instruction list. It does not
592 // update the graph, reverse post order, loop information, nor make sure the
593 // blocks are consistent (for example ending with a control flow instruction).
594 HBasicBlock* SplitAfter(HInstruction* cursor);
595
596 // Merge `other` at the end of `this`. Successors and dominated blocks of
597 // `other` are changed to be successors and dominated blocks of `this`. Note
598 // that this method does not update the graph, reverse post order, loop
599 // information, nor make sure the blocks are consistent (for example ending
600 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +0100601 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000602
603 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
604 // of `this` are moved to `other`.
605 // Note that this method does not update the graph, reverse post order, loop
606 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +0000607 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000608 void ReplaceWith(HBasicBlock* other);
609
David Brazdil2d7352b2015-04-20 14:52:42 +0100610 // Merge `other` at the end of `this`. This method updates loops, reverse post
611 // order, links to predecessors, successors, dominators and deletes the block
612 // from the graph. The two blocks must be successive, i.e. `this` the only
613 // predecessor of `other` and vice versa.
614 void MergeWith(HBasicBlock* other);
615
616 // Disconnects `this` from all its predecessors, successors and dominator,
617 // removes it from all loops it is included in and eventually from the graph.
618 // The block must not dominate any other block. Predecessors and successors
619 // are safely updated.
620 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +0000621
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000622 void AddInstruction(HInstruction* instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100623 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +0100624 // Replace instruction `initial` with `replacement` within this block.
625 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
626 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100627 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100628 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +0000629 // RemoveInstruction and RemovePhi delete a given instruction from the respective
630 // instruction list. With 'ensure_safety' set to true, it verifies that the
631 // instruction is not in use and removes it from the use lists of its inputs.
632 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
633 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +0100634 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100635
636 bool IsLoopHeader() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100637 return (loop_information_ != nullptr) && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100638 }
639
Roland Levillain6b879dd2014-09-22 17:13:44 +0100640 bool IsLoopPreHeaderFirstPredecessor() const {
641 DCHECK(IsLoopHeader());
642 DCHECK(!GetPredecessors().IsEmpty());
643 return GetPredecessors().Get(0) == GetLoopInformation()->GetPreHeader();
644 }
645
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100646 HLoopInformation* GetLoopInformation() const {
647 return loop_information_;
648 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000649
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000650 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100651 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000652 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100653 void SetInLoop(HLoopInformation* info) {
654 if (IsLoopHeader()) {
655 // Nothing to do. This just means `info` is an outer loop.
656 } else if (loop_information_ == nullptr) {
657 loop_information_ = info;
658 } else if (loop_information_->Contains(*info->GetHeader())) {
659 // Block is currently part of an outer loop. Make it part of this inner loop.
660 // Note that a non loop header having a loop information means this loop information
661 // has already been populated
662 loop_information_ = info;
663 } else {
664 // Block is part of an inner loop. Do not update the loop information.
665 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
666 // at this point, because this method is being called while populating `info`.
667 }
668 }
669
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000670 // Raw update of the loop information.
671 void SetLoopInformation(HLoopInformation* info) {
672 loop_information_ = info;
673 }
674
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100675 bool IsInLoop() const { return loop_information_ != nullptr; }
676
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100677 // Returns wheter this block dominates the blocked passed as parameter.
678 bool Dominates(HBasicBlock* block) const;
679
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100680 size_t GetLifetimeStart() const { return lifetime_start_; }
681 size_t GetLifetimeEnd() const { return lifetime_end_; }
682
683 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
684 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
685
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100686 uint32_t GetDexPc() const { return dex_pc_; }
687
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000688 bool IsCatchBlock() const { return is_catch_block_; }
689 void SetIsCatchBlock() { is_catch_block_ = true; }
690
David Brazdil8d5b8b22015-03-24 10:51:52 +0000691 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +0000692 bool EndsWithIf() const;
693 bool HasSinglePhi() const;
694
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000695 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000696 HGraph* graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000697 GrowableArray<HBasicBlock*> predecessors_;
698 GrowableArray<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100699 HInstructionList instructions_;
700 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000701 HLoopInformation* loop_information_;
702 HBasicBlock* dominator_;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100703 GrowableArray<HBasicBlock*> dominated_blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000704 int block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100705 // The dex program counter of the first instruction of this block.
706 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100707 size_t lifetime_start_;
708 size_t lifetime_end_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000709 bool is_catch_block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000710
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000711 friend class HGraph;
712 friend class HInstruction;
713
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000714 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
715};
716
David Brazdilb2bd1c52015-03-25 11:17:37 +0000717// Iterates over the LoopInformation of all loops which contain 'block'
718// from the innermost to the outermost.
719class HLoopInformationOutwardIterator : public ValueObject {
720 public:
721 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
722 : current_(block.GetLoopInformation()) {}
723
724 bool Done() const { return current_ == nullptr; }
725
726 void Advance() {
727 DCHECK(!Done());
728 current_ = current_->GetHeader()->GetDominator()->GetLoopInformation();
729 }
730
731 HLoopInformation* Current() const {
732 DCHECK(!Done());
733 return current_;
734 }
735
736 private:
737 HLoopInformation* current_;
738
739 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
740};
741
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100742#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
743 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000744 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000745 M(ArrayGet, Instruction) \
746 M(ArrayLength, Instruction) \
747 M(ArraySet, Instruction) \
David Brazdil66d126e2015-04-03 16:02:44 +0100748 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000749 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +0000750 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000751 M(CheckCast, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100752 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000753 M(Compare, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100754 M(Condition, BinaryOperation) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700755 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000756 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +0000757 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000758 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100759 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000760 M(Exit, Instruction) \
761 M(FloatConstant, Constant) \
762 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100763 M(GreaterThan, Condition) \
764 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100765 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000766 M(InstanceFieldGet, Instruction) \
767 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000768 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100769 M(IntConstant, Constant) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +0000770 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000771 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100772 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000773 M(LessThan, Condition) \
774 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000775 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000776 M(LoadException, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100777 M(LoadLocal, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000778 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100779 M(Local, Instruction) \
780 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +0100781 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +0000782 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000783 M(Mul, BinaryOperation) \
784 M(Neg, UnaryOperation) \
785 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100786 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +0100787 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000788 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000789 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000790 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000791 M(Or, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100792 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000793 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100794 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000795 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100796 M(Return, Instruction) \
797 M(ReturnVoid, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000798 M(Shl, BinaryOperation) \
799 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100800 M(StaticFieldGet, Instruction) \
801 M(StaticFieldSet, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100802 M(StoreLocal, Instruction) \
803 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100804 M(SuspendCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000805 M(Temporary, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000806 M(Throw, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +0000807 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000808 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000809 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000810
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100811#define FOR_EACH_INSTRUCTION(M) \
812 FOR_EACH_CONCRETE_INSTRUCTION(M) \
813 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +0100814 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +0100815 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100816 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -0700817
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100818#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000819FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
820#undef FORWARD_DECLARATION
821
Roland Levillainccc07a92014-09-16 14:48:16 +0100822#define DECLARE_INSTRUCTION(type) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000823 InstructionKind GetKind() const OVERRIDE { return k##type; } \
824 const char* DebugName() const OVERRIDE { return #type; } \
825 const H##type* As##type() const OVERRIDE { return this; } \
826 H##type* As##type() OVERRIDE { return this; } \
827 bool InstructionTypeEquals(HInstruction* other) const OVERRIDE { \
Roland Levillainccc07a92014-09-16 14:48:16 +0100828 return other->Is##type(); \
829 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000830 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000831
David Brazdiled596192015-01-23 10:39:45 +0000832template <typename T> class HUseList;
833
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100834template <typename T>
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700835class HUseListNode : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000836 public:
David Brazdiled596192015-01-23 10:39:45 +0000837 HUseListNode* GetPrevious() const { return prev_; }
838 HUseListNode* GetNext() const { return next_; }
839 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100840 size_t GetIndex() const { return index_; }
841
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000842 private:
David Brazdiled596192015-01-23 10:39:45 +0000843 HUseListNode(T user, size_t index)
844 : user_(user), index_(index), prev_(nullptr), next_(nullptr) {}
845
846 T const user_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100847 const size_t index_;
David Brazdiled596192015-01-23 10:39:45 +0000848 HUseListNode<T>* prev_;
849 HUseListNode<T>* next_;
850
851 friend class HUseList<T>;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000852
853 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
854};
855
David Brazdiled596192015-01-23 10:39:45 +0000856template <typename T>
857class HUseList : public ValueObject {
858 public:
859 HUseList() : first_(nullptr) {}
860
861 void Clear() {
862 first_ = nullptr;
863 }
864
865 // Adds a new entry at the beginning of the use list and returns
866 // the newly created node.
867 HUseListNode<T>* AddUse(T user, size_t index, ArenaAllocator* arena) {
David Brazdilea55b932015-01-27 17:12:29 +0000868 HUseListNode<T>* new_node = new (arena) HUseListNode<T>(user, index);
David Brazdiled596192015-01-23 10:39:45 +0000869 if (IsEmpty()) {
870 first_ = new_node;
871 } else {
872 first_->prev_ = new_node;
873 new_node->next_ = first_;
874 first_ = new_node;
875 }
876 return new_node;
877 }
878
879 HUseListNode<T>* GetFirst() const {
880 return first_;
881 }
882
883 void Remove(HUseListNode<T>* node) {
David Brazdil1abb4192015-02-17 18:33:36 +0000884 DCHECK(node != nullptr);
885 DCHECK(Contains(node));
886
David Brazdiled596192015-01-23 10:39:45 +0000887 if (node->prev_ != nullptr) {
888 node->prev_->next_ = node->next_;
889 }
890 if (node->next_ != nullptr) {
891 node->next_->prev_ = node->prev_;
892 }
893 if (node == first_) {
894 first_ = node->next_;
895 }
896 }
897
David Brazdil1abb4192015-02-17 18:33:36 +0000898 bool Contains(const HUseListNode<T>* node) const {
899 if (node == nullptr) {
900 return false;
901 }
902 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
903 if (current == node) {
904 return true;
905 }
906 }
907 return false;
908 }
909
David Brazdiled596192015-01-23 10:39:45 +0000910 bool IsEmpty() const {
911 return first_ == nullptr;
912 }
913
914 bool HasOnlyOneUse() const {
915 return first_ != nullptr && first_->next_ == nullptr;
916 }
917
918 private:
919 HUseListNode<T>* first_;
920};
921
922template<typename T>
923class HUseIterator : public ValueObject {
924 public:
925 explicit HUseIterator(const HUseList<T>& uses) : current_(uses.GetFirst()) {}
926
927 bool Done() const { return current_ == nullptr; }
928
929 void Advance() {
930 DCHECK(!Done());
931 current_ = current_->GetNext();
932 }
933
934 HUseListNode<T>* Current() const {
935 DCHECK(!Done());
936 return current_;
937 }
938
939 private:
940 HUseListNode<T>* current_;
941
942 friend class HValue;
943};
944
David Brazdil1abb4192015-02-17 18:33:36 +0000945// This class is used by HEnvironment and HInstruction classes to record the
946// instructions they use and pointers to the corresponding HUseListNodes kept
947// by the used instructions.
948template <typename T>
949class HUserRecord : public ValueObject {
950 public:
951 HUserRecord() : instruction_(nullptr), use_node_(nullptr) {}
952 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), use_node_(nullptr) {}
953
954 HUserRecord(const HUserRecord<T>& old_record, HUseListNode<T>* use_node)
955 : instruction_(old_record.instruction_), use_node_(use_node) {
956 DCHECK(instruction_ != nullptr);
957 DCHECK(use_node_ != nullptr);
958 DCHECK(old_record.use_node_ == nullptr);
959 }
960
961 HInstruction* GetInstruction() const { return instruction_; }
962 HUseListNode<T>* GetUseNode() const { return use_node_; }
963
964 private:
965 // Instruction used by the user.
966 HInstruction* instruction_;
967
968 // Corresponding entry in the use list kept by 'instruction_'.
969 HUseListNode<T>* use_node_;
970};
971
Calin Juravle27df7582015-04-17 19:12:31 +0100972// TODO: Add better documentation to this class and maybe refactor with more suggestive names.
973// - Has(All)SideEffects suggests that all the side effects are present but only ChangesSomething
974// flag is consider.
975// - DependsOn suggests that there is a real dependency between side effects but it only
976// checks DependendsOnSomething flag.
977//
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100978// Represents the side effects an instruction may have.
979class SideEffects : public ValueObject {
980 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100981 SideEffects() : flags_(0) {}
982
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100983 static SideEffects None() {
984 return SideEffects(0);
985 }
986
987 static SideEffects All() {
988 return SideEffects(ChangesSomething().flags_ | DependsOnSomething().flags_);
989 }
990
991 static SideEffects ChangesSomething() {
992 return SideEffects((1 << kFlagChangesCount) - 1);
993 }
994
995 static SideEffects DependsOnSomething() {
996 int count = kFlagDependsOnCount - kFlagChangesCount;
997 return SideEffects(((1 << count) - 1) << kFlagChangesCount);
998 }
999
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001000 SideEffects Union(SideEffects other) const {
1001 return SideEffects(flags_ | other.flags_);
1002 }
1003
Roland Levillain72bceff2014-09-15 18:29:00 +01001004 bool HasSideEffects() const {
1005 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
1006 return (flags_ & all_bits_set) != 0;
1007 }
1008
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001009 bool HasAllSideEffects() const {
1010 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
1011 return all_bits_set == (flags_ & all_bits_set);
1012 }
1013
1014 bool DependsOn(SideEffects other) const {
1015 size_t depends_flags = other.ComputeDependsFlags();
1016 return (flags_ & depends_flags) != 0;
1017 }
1018
1019 bool HasDependencies() const {
1020 int count = kFlagDependsOnCount - kFlagChangesCount;
1021 size_t all_bits_set = (1 << count) - 1;
1022 return ((flags_ >> kFlagChangesCount) & all_bits_set) != 0;
1023 }
1024
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001025 private:
1026 static constexpr int kFlagChangesSomething = 0;
1027 static constexpr int kFlagChangesCount = kFlagChangesSomething + 1;
1028
1029 static constexpr int kFlagDependsOnSomething = kFlagChangesCount;
1030 static constexpr int kFlagDependsOnCount = kFlagDependsOnSomething + 1;
1031
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001032 explicit SideEffects(size_t flags) : flags_(flags) {}
1033
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001034 size_t ComputeDependsFlags() const {
1035 return flags_ << kFlagChangesCount;
1036 }
1037
1038 size_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001039};
1040
David Brazdiled596192015-01-23 10:39:45 +00001041// A HEnvironment object contains the values of virtual registers at a given location.
1042class HEnvironment : public ArenaObject<kArenaAllocMisc> {
1043 public:
1044 HEnvironment(ArenaAllocator* arena, size_t number_of_vregs)
1045 : vregs_(arena, number_of_vregs) {
1046 vregs_.SetSize(number_of_vregs);
1047 for (size_t i = 0; i < number_of_vregs; i++) {
David Brazdil1abb4192015-02-17 18:33:36 +00001048 vregs_.Put(i, HUserRecord<HEnvironment*>());
David Brazdiled596192015-01-23 10:39:45 +00001049 }
1050 }
1051
1052 void CopyFrom(HEnvironment* env);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001053 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1054 // input to the loop phi instead. This is for inserting instructions that
1055 // require an environment (like HDeoptimization) in the loop pre-header.
1056 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001057
1058 void SetRawEnvAt(size_t index, HInstruction* instruction) {
David Brazdil1abb4192015-02-17 18:33:36 +00001059 vregs_.Put(index, HUserRecord<HEnvironment*>(instruction));
David Brazdiled596192015-01-23 10:39:45 +00001060 }
1061
1062 HInstruction* GetInstructionAt(size_t index) const {
David Brazdil1abb4192015-02-17 18:33:36 +00001063 return vregs_.Get(index).GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001064 }
1065
David Brazdil1abb4192015-02-17 18:33:36 +00001066 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001067
1068 size_t Size() const { return vregs_.Size(); }
1069
1070 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001071 // Record instructions' use entries of this environment for constant-time removal.
1072 // It should only be called by HInstruction when a new environment use is added.
1073 void RecordEnvUse(HUseListNode<HEnvironment*>* env_use) {
1074 DCHECK(env_use->GetUser() == this);
1075 size_t index = env_use->GetIndex();
1076 vregs_.Put(index, HUserRecord<HEnvironment*>(vregs_.Get(index), env_use));
1077 }
David Brazdiled596192015-01-23 10:39:45 +00001078
David Brazdil1abb4192015-02-17 18:33:36 +00001079 GrowableArray<HUserRecord<HEnvironment*> > vregs_;
David Brazdiled596192015-01-23 10:39:45 +00001080
David Brazdil1abb4192015-02-17 18:33:36 +00001081 friend HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001082
1083 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1084};
1085
Calin Juravleacf735c2015-02-12 15:25:22 +00001086class ReferenceTypeInfo : ValueObject {
1087 public:
Calin Juravleb1498f62015-02-16 13:13:29 +00001088 typedef Handle<mirror::Class> TypeHandle;
1089
1090 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact)
1091 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1092 if (type_handle->IsObjectClass()) {
1093 // Override the type handle to be consistent with the case when we get to
1094 // Top but don't have the Object class available. It avoids having to guess
1095 // what value the type_handle has when it's Top.
1096 return ReferenceTypeInfo(TypeHandle(), is_exact, true);
1097 } else {
1098 return ReferenceTypeInfo(type_handle, is_exact, false);
1099 }
1100 }
1101
1102 static ReferenceTypeInfo CreateTop(bool is_exact) {
1103 return ReferenceTypeInfo(TypeHandle(), is_exact, true);
Calin Juravleacf735c2015-02-12 15:25:22 +00001104 }
1105
1106 bool IsExact() const { return is_exact_; }
1107 bool IsTop() const { return is_top_; }
1108
1109 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
1110
Calin Juravleb1498f62015-02-16 13:13:29 +00001111 bool IsSupertypeOf(ReferenceTypeInfo rti) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Calin Juravleacf735c2015-02-12 15:25:22 +00001112 if (IsTop()) {
1113 // Top (equivalent for java.lang.Object) is supertype of anything.
1114 return true;
1115 }
1116 if (rti.IsTop()) {
1117 // If we get here `this` is not Top() so it can't be a supertype.
1118 return false;
1119 }
1120 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
1121 }
1122
1123 // Returns true if the type information provide the same amount of details.
1124 // Note that it does not mean that the instructions have the same actual type
1125 // (e.g. tops are equal but they can be the result of a merge).
1126 bool IsEqual(ReferenceTypeInfo rti) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1127 if (IsExact() != rti.IsExact()) {
1128 return false;
1129 }
1130 if (IsTop() && rti.IsTop()) {
1131 // `Top` means java.lang.Object, so the types are equivalent.
1132 return true;
1133 }
1134 if (IsTop() || rti.IsTop()) {
1135 // If only one is top or object than they are not equivalent.
1136 // NB: We need this extra check because the type_handle of `Top` is invalid
1137 // and we cannot inspect its reference.
1138 return false;
1139 }
1140
1141 // Finally check the types.
1142 return GetTypeHandle().Get() == rti.GetTypeHandle().Get();
1143 }
1144
1145 private:
Calin Juravleb1498f62015-02-16 13:13:29 +00001146 ReferenceTypeInfo() : ReferenceTypeInfo(TypeHandle(), false, true) {}
1147 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact, bool is_top)
1148 : type_handle_(type_handle), is_exact_(is_exact), is_top_(is_top) {}
1149
Calin Juravleacf735c2015-02-12 15:25:22 +00001150 // The class of the object.
Calin Juravleb1498f62015-02-16 13:13:29 +00001151 TypeHandle type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001152 // Whether or not the type is exact or a superclass of the actual type.
Calin Juravleb1498f62015-02-16 13:13:29 +00001153 // Whether or not we have any information about this type.
Calin Juravleacf735c2015-02-12 15:25:22 +00001154 bool is_exact_;
1155 // A true value here means that the object type should be java.lang.Object.
1156 // We don't have access to the corresponding mirror object every time so this
1157 // flag acts as a substitute. When true, the TypeHandle refers to a null
1158 // pointer and should not be used.
1159 bool is_top_;
1160};
1161
1162std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
1163
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001164class HInstruction : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001165 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001166 explicit HInstruction(SideEffects side_effects)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001167 : previous_(nullptr),
1168 next_(nullptr),
1169 block_(nullptr),
1170 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001171 ssa_index_(-1),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001172 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001173 locations_(nullptr),
1174 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001175 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001176 side_effects_(side_effects),
1177 reference_type_info_(ReferenceTypeInfo::CreateTop(/* is_exact */ false)) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001178
Dave Allison20dfc792014-06-16 20:44:29 -07001179 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001180
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001181#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001182 enum InstructionKind {
1183 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1184 };
1185#undef DECLARE_KIND
1186
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001187 HInstruction* GetNext() const { return next_; }
1188 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001189
Calin Juravle77520bc2015-01-12 18:45:46 +00001190 HInstruction* GetNextDisregardingMoves() const;
1191 HInstruction* GetPreviousDisregardingMoves() const;
1192
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001193 HBasicBlock* GetBlock() const { return block_; }
1194 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001195 bool IsInBlock() const { return block_ != nullptr; }
1196 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +01001197 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001198
Roland Levillain6b879dd2014-09-22 17:13:44 +01001199 virtual size_t InputCount() const = 0;
David Brazdil1abb4192015-02-17 18:33:36 +00001200 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001201
1202 virtual void Accept(HGraphVisitor* visitor) = 0;
1203 virtual const char* DebugName() const = 0;
1204
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001205 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
David Brazdil1abb4192015-02-17 18:33:36 +00001206 void SetRawInputAt(size_t index, HInstruction* input) {
1207 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1208 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001209
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001210 virtual bool NeedsEnvironment() const { return false; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001211 virtual bool IsControlFlow() const { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01001212 virtual bool CanThrow() const { return false; }
Roland Levillain72bceff2014-09-15 18:29:00 +01001213 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001214
Calin Juravle10e244f2015-01-26 18:54:32 +00001215 // Does not apply for all instructions, but having this at top level greatly
1216 // simplifies the null check elimination.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001217 virtual bool CanBeNull() const {
1218 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1219 return true;
1220 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001221
Calin Juravle641547a2015-04-21 22:08:51 +01001222 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj) const {
1223 UNUSED(obj);
1224 return false;
1225 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001226
Calin Juravleacf735c2015-02-12 15:25:22 +00001227 void SetReferenceTypeInfo(ReferenceTypeInfo reference_type_info) {
Calin Juravle61d544b2015-02-23 16:46:57 +00001228 DCHECK_EQ(GetType(), Primitive::kPrimNot);
Calin Juravleacf735c2015-02-12 15:25:22 +00001229 reference_type_info_ = reference_type_info;
1230 }
1231
Calin Juravle61d544b2015-02-23 16:46:57 +00001232 ReferenceTypeInfo GetReferenceTypeInfo() const {
1233 DCHECK_EQ(GetType(), Primitive::kPrimNot);
1234 return reference_type_info_;
1235 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001236
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001237 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001238 DCHECK(user != nullptr);
1239 HUseListNode<HInstruction*>* use =
1240 uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1241 user->SetRawInputRecordAt(index, HUserRecord<HInstruction*>(user->InputRecordAt(index), use));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001242 }
1243
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001244 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001245 DCHECK(user != nullptr);
David Brazdiled596192015-01-23 10:39:45 +00001246 HUseListNode<HEnvironment*>* env_use =
1247 env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1248 user->RecordEnvUse(env_use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001249 }
1250
David Brazdil1abb4192015-02-17 18:33:36 +00001251 void RemoveAsUserOfInput(size_t input) {
1252 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
1253 input_use.GetInstruction()->uses_.Remove(input_use.GetUseNode());
1254 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001255
David Brazdil1abb4192015-02-17 18:33:36 +00001256 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1257 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001258
David Brazdiled596192015-01-23 10:39:45 +00001259 bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); }
1260 bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); }
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001261 bool HasNonEnvironmentUses() const { return !uses_.IsEmpty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001262 bool HasOnlyOneNonEnvironmentUse() const {
1263 return !HasEnvironmentUses() && GetUses().HasOnlyOneUse();
1264 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001265
Roland Levillain6c82d402014-10-13 16:10:27 +01001266 // Does this instruction strictly dominate `other_instruction`?
1267 // Returns false if this instruction and `other_instruction` are the same.
1268 // Aborts if this instruction and `other_instruction` are both phis.
1269 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001270
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001271 int GetId() const { return id_; }
1272 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001273
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001274 int GetSsaIndex() const { return ssa_index_; }
1275 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1276 bool HasSsaIndex() const { return ssa_index_ != -1; }
1277
1278 bool HasEnvironment() const { return environment_ != nullptr; }
1279 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001280 // Set the `environment_` field. Raw because this method does not
1281 // update the uses lists.
1282 void SetRawEnvironment(HEnvironment* environment) { environment_ = environment; }
1283
1284 // Set the environment of this instruction, copying it from `environment`. While
1285 // copying, the uses lists are being updated.
1286 void CopyEnvironmentFrom(HEnvironment* environment) {
1287 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
1288 environment_ = new (allocator) HEnvironment(allocator, environment->Size());
1289 environment_->CopyFrom(environment);
1290 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001291
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001292 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
1293 HBasicBlock* block) {
1294 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
1295 environment_ = new (allocator) HEnvironment(allocator, environment->Size());
1296 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
1297 }
1298
Nicolas Geoffray39468442014-09-02 15:17:15 +01001299 // Returns the number of entries in the environment. Typically, that is the
1300 // number of dex registers in a method. It could be more in case of inlining.
1301 size_t EnvironmentSize() const;
1302
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001303 LocationSummary* GetLocations() const { return locations_; }
1304 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001305
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001306 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001307 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001308
Alexandre Rames188d4312015-04-09 18:30:21 +01001309 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
1310 // uses of this instruction by `other` are *not* updated.
1311 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
1312 ReplaceWith(other);
1313 other->ReplaceInput(this, use_index);
1314 }
1315
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001316 // Move `this` instruction before `cursor`.
1317 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001318
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001319#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01001320 bool Is##type() const { return (As##type() != nullptr); } \
1321 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001322 virtual H##type* As##type() { return nullptr; }
1323
1324 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
1325#undef INSTRUCTION_TYPE_CHECK
1326
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001327 // Returns whether the instruction can be moved within the graph.
1328 virtual bool CanBeMoved() const { return false; }
1329
1330 // Returns whether the two instructions are of the same kind.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001331 virtual bool InstructionTypeEquals(HInstruction* other) const {
1332 UNUSED(other);
1333 return false;
1334 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001335
1336 // Returns whether any data encoded in the two instructions is equal.
1337 // This method does not look at the inputs. Both instructions must be
1338 // of the same type, otherwise the method has undefined behavior.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001339 virtual bool InstructionDataEquals(HInstruction* other) const {
1340 UNUSED(other);
1341 return false;
1342 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001343
1344 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00001345 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001346 // 2) Their inputs are identical.
1347 bool Equals(HInstruction* other) const;
1348
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001349 virtual InstructionKind GetKind() const = 0;
1350
1351 virtual size_t ComputeHashCode() const {
1352 size_t result = GetKind();
1353 for (size_t i = 0, e = InputCount(); i < e; ++i) {
1354 result = (result * 31) + InputAt(i)->GetId();
1355 }
1356 return result;
1357 }
1358
1359 SideEffects GetSideEffects() const { return side_effects_; }
1360
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001361 size_t GetLifetimePosition() const { return lifetime_position_; }
1362 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
1363 LiveInterval* GetLiveInterval() const { return live_interval_; }
1364 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
1365 bool HasLiveInterval() const { return live_interval_ != nullptr; }
1366
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001367 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
1368
1369 // Returns whether the code generation of the instruction will require to have access
1370 // to the current method. Such instructions are:
1371 // (1): Instructions that require an environment, as calling the runtime requires
1372 // to walk the stack and have the current method stored at a specific stack address.
1373 // (2): Object literals like classes and strings, that are loaded from the dex cache
1374 // fields of the current method.
1375 bool NeedsCurrentMethod() const {
1376 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
1377 }
1378
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001379 virtual bool NeedsDexCache() const { return false; }
1380
David Brazdil1abb4192015-02-17 18:33:36 +00001381 protected:
1382 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
1383 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
1384
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001385 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001386 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); }
1387
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001388 HInstruction* previous_;
1389 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001390 HBasicBlock* block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001391
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001392 // An instruction gets an id when it is added to the graph.
1393 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01001394 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001395 int id_;
1396
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001397 // When doing liveness analysis, instructions that have uses get an SSA index.
1398 int ssa_index_;
1399
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001400 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00001401 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001402
1403 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00001404 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001405
Nicolas Geoffray39468442014-09-02 15:17:15 +01001406 // The environment associated with this instruction. Not null if the instruction
1407 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001408 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001409
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001410 // Set by the code generator.
1411 LocationSummary* locations_;
1412
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001413 // Set by the liveness analysis.
1414 LiveInterval* live_interval_;
1415
1416 // Set by the liveness analysis, this is the position in a linear
1417 // order of blocks where this instruction's live interval start.
1418 size_t lifetime_position_;
1419
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001420 const SideEffects side_effects_;
1421
Calin Juravleacf735c2015-02-12 15:25:22 +00001422 // TODO: for primitive types this should be marked as invalid.
1423 ReferenceTypeInfo reference_type_info_;
1424
David Brazdil1abb4192015-02-17 18:33:36 +00001425 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001426 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00001427 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001428 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001429 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001430
1431 DISALLOW_COPY_AND_ASSIGN(HInstruction);
1432};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001433std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001434
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001435class HInputIterator : public ValueObject {
1436 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001437 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001438
1439 bool Done() const { return index_ == instruction_->InputCount(); }
1440 HInstruction* Current() const { return instruction_->InputAt(index_); }
1441 void Advance() { index_++; }
1442
1443 private:
1444 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001445 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001446
1447 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
1448};
1449
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001450class HInstructionIterator : public ValueObject {
1451 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001452 explicit HInstructionIterator(const HInstructionList& instructions)
1453 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001454 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001455 }
1456
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001457 bool Done() const { return instruction_ == nullptr; }
1458 HInstruction* Current() const { return instruction_; }
1459 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001460 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001461 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001462 }
1463
1464 private:
1465 HInstruction* instruction_;
1466 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001467
1468 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001469};
1470
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001471class HBackwardInstructionIterator : public ValueObject {
1472 public:
1473 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
1474 : instruction_(instructions.last_instruction_) {
1475 next_ = Done() ? nullptr : instruction_->GetPrevious();
1476 }
1477
1478 bool Done() const { return instruction_ == nullptr; }
1479 HInstruction* Current() const { return instruction_; }
1480 void Advance() {
1481 instruction_ = next_;
1482 next_ = Done() ? nullptr : instruction_->GetPrevious();
1483 }
1484
1485 private:
1486 HInstruction* instruction_;
1487 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001488
1489 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001490};
1491
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001492// An embedded container with N elements of type T. Used (with partial
1493// specialization for N=0) because embedded arrays cannot have size 0.
1494template<typename T, intptr_t N>
1495class EmbeddedArray {
1496 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001497 EmbeddedArray() : elements_() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001498
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001499 intptr_t GetLength() const { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001500
1501 const T& operator[](intptr_t i) const {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001502 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001503 return elements_[i];
1504 }
1505
1506 T& operator[](intptr_t i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001507 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001508 return elements_[i];
1509 }
1510
1511 const T& At(intptr_t i) const {
1512 return (*this)[i];
1513 }
1514
1515 void SetAt(intptr_t i, const T& val) {
1516 (*this)[i] = val;
1517 }
1518
1519 private:
1520 T elements_[N];
1521};
1522
1523template<typename T>
1524class EmbeddedArray<T, 0> {
1525 public:
1526 intptr_t length() const { return 0; }
1527 const T& operator[](intptr_t i) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001528 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001529 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001530 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001531 }
1532 T& operator[](intptr_t i) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001533 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001534 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001535 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001536 }
1537};
1538
1539template<intptr_t N>
1540class HTemplateInstruction: public HInstruction {
1541 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001542 HTemplateInstruction<N>(SideEffects side_effects)
1543 : HInstruction(side_effects), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07001544 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001545
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001546 size_t InputCount() const OVERRIDE { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001547
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001548 protected:
David Brazdil1abb4192015-02-17 18:33:36 +00001549 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_[i]; }
1550
1551 void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE {
1552 inputs_[i] = input;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001553 }
1554
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001555 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001556 EmbeddedArray<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001557
1558 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001559};
1560
Dave Allison20dfc792014-06-16 20:44:29 -07001561template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001562class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07001563 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001564 HExpression<N>(Primitive::Type type, SideEffects side_effects)
1565 : HTemplateInstruction<N>(side_effects), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07001566 virtual ~HExpression() {}
1567
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001568 Primitive::Type GetType() const OVERRIDE { return type_; }
Dave Allison20dfc792014-06-16 20:44:29 -07001569
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001570 protected:
1571 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07001572};
1573
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001574// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
1575// instruction that branches to the exit block.
1576class HReturnVoid : public HTemplateInstruction<0> {
1577 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001578 HReturnVoid() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001579
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001580 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001581
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001582 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001583
1584 private:
1585 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
1586};
1587
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001588// Represents dex's RETURN opcodes. A HReturn is a control flow
1589// instruction that branches to the exit block.
1590class HReturn : public HTemplateInstruction<1> {
1591 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001592 explicit HReturn(HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001593 SetRawInputAt(0, value);
1594 }
1595
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001596 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001597
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001598 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001599
1600 private:
1601 DISALLOW_COPY_AND_ASSIGN(HReturn);
1602};
1603
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001604// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001605// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001606// exit block.
1607class HExit : public HTemplateInstruction<0> {
1608 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001609 HExit() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001610
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001611 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001612
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001613 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001614
1615 private:
1616 DISALLOW_COPY_AND_ASSIGN(HExit);
1617};
1618
1619// Jumps from one block to another.
1620class HGoto : public HTemplateInstruction<0> {
1621 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001622 HGoto() : HTemplateInstruction(SideEffects::None()) {}
1623
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001624 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001625
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001626 HBasicBlock* GetSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001627 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001628 }
1629
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001630 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001631
1632 private:
1633 DISALLOW_COPY_AND_ASSIGN(HGoto);
1634};
1635
Dave Allison20dfc792014-06-16 20:44:29 -07001636
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001637// Conditional branch. A block ending with an HIf instruction must have
1638// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001639class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001640 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001641 explicit HIf(HInstruction* input) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001642 SetRawInputAt(0, input);
1643 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001644
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001645 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001646
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001647 HBasicBlock* IfTrueSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001648 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001649 }
1650
1651 HBasicBlock* IfFalseSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001652 return GetBlock()->GetSuccessors().Get(1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001653 }
1654
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001655 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001656
1657 private:
1658 DISALLOW_COPY_AND_ASSIGN(HIf);
1659};
1660
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001661// Deoptimize to interpreter, upon checking a condition.
1662class HDeoptimize : public HTemplateInstruction<1> {
1663 public:
1664 HDeoptimize(HInstruction* cond, uint32_t dex_pc)
1665 : HTemplateInstruction(SideEffects::None()),
1666 dex_pc_(dex_pc) {
1667 SetRawInputAt(0, cond);
1668 }
1669
1670 bool NeedsEnvironment() const OVERRIDE { return true; }
1671 bool CanThrow() const OVERRIDE { return true; }
1672 uint32_t GetDexPc() const { return dex_pc_; }
1673
1674 DECLARE_INSTRUCTION(Deoptimize);
1675
1676 private:
1677 uint32_t dex_pc_;
1678
1679 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
1680};
1681
Roland Levillain88cb1752014-10-20 16:36:47 +01001682class HUnaryOperation : public HExpression<1> {
1683 public:
1684 HUnaryOperation(Primitive::Type result_type, HInstruction* input)
1685 : HExpression(result_type, SideEffects::None()) {
1686 SetRawInputAt(0, input);
1687 }
1688
1689 HInstruction* GetInput() const { return InputAt(0); }
1690 Primitive::Type GetResultType() const { return GetType(); }
1691
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001692 bool CanBeMoved() const OVERRIDE { return true; }
1693 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001694 UNUSED(other);
1695 return true;
1696 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001697
Roland Levillain9240d6a2014-10-20 16:47:04 +01001698 // Try to statically evaluate `operation` and return a HConstant
1699 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001700 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01001701 HConstant* TryStaticEvaluation() const;
1702
1703 // Apply this operation to `x`.
1704 virtual int32_t Evaluate(int32_t x) const = 0;
1705 virtual int64_t Evaluate(int64_t x) const = 0;
1706
Roland Levillain88cb1752014-10-20 16:36:47 +01001707 DECLARE_INSTRUCTION(UnaryOperation);
1708
1709 private:
1710 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
1711};
1712
Dave Allison20dfc792014-06-16 20:44:29 -07001713class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001714 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001715 HBinaryOperation(Primitive::Type result_type,
1716 HInstruction* left,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001717 HInstruction* right) : HExpression(result_type, SideEffects::None()) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001718 SetRawInputAt(0, left);
1719 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001720 }
1721
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001722 HInstruction* GetLeft() const { return InputAt(0); }
1723 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07001724 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001725
Mingyao Yangdc5ac732015-02-25 11:28:05 -08001726 virtual bool IsCommutative() const { return false; }
1727
1728 // Put constant on the right.
1729 // Returns whether order is changed.
1730 bool OrderInputsWithConstantOnTheRight() {
1731 HInstruction* left = InputAt(0);
1732 HInstruction* right = InputAt(1);
1733 if (left->IsConstant() && !right->IsConstant()) {
1734 ReplaceInput(right, 0);
1735 ReplaceInput(left, 1);
1736 return true;
1737 }
1738 return false;
1739 }
1740
1741 // Order inputs by instruction id, but favor constant on the right side.
1742 // This helps GVN for commutative ops.
1743 void OrderInputs() {
1744 DCHECK(IsCommutative());
1745 HInstruction* left = InputAt(0);
1746 HInstruction* right = InputAt(1);
1747 if (left == right || (!left->IsConstant() && right->IsConstant())) {
1748 return;
1749 }
1750 if (OrderInputsWithConstantOnTheRight()) {
1751 return;
1752 }
1753 // Order according to instruction id.
1754 if (left->GetId() > right->GetId()) {
1755 ReplaceInput(right, 0);
1756 ReplaceInput(left, 1);
1757 }
1758 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001759
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001760 bool CanBeMoved() const OVERRIDE { return true; }
1761 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001762 UNUSED(other);
1763 return true;
1764 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001765
Roland Levillain9240d6a2014-10-20 16:47:04 +01001766 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01001767 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001768 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01001769 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01001770
1771 // Apply this operation to `x` and `y`.
1772 virtual int32_t Evaluate(int32_t x, int32_t y) const = 0;
1773 virtual int64_t Evaluate(int64_t x, int64_t y) const = 0;
1774
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001775 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001776 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001777 HConstant* GetConstantRight() const;
1778
1779 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001780 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001781 HInstruction* GetLeastConstantLeft() const;
1782
Roland Levillainccc07a92014-09-16 14:48:16 +01001783 DECLARE_INSTRUCTION(BinaryOperation);
1784
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001785 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001786 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
1787};
1788
Dave Allison20dfc792014-06-16 20:44:29 -07001789class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001790 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001791 HCondition(HInstruction* first, HInstruction* second)
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001792 : HBinaryOperation(Primitive::kPrimBoolean, first, second),
1793 needs_materialization_(true) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001794
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001795 bool NeedsMaterialization() const { return needs_materialization_; }
1796 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001797
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001798 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001799 // `instruction`, and disregard moves in between.
1800 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001801
Dave Allison20dfc792014-06-16 20:44:29 -07001802 DECLARE_INSTRUCTION(Condition);
1803
1804 virtual IfCondition GetCondition() const = 0;
1805
1806 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001807 // For register allocation purposes, returns whether this instruction needs to be
1808 // materialized (that is, not just be in the processor flags).
1809 bool needs_materialization_;
1810
Dave Allison20dfc792014-06-16 20:44:29 -07001811 DISALLOW_COPY_AND_ASSIGN(HCondition);
1812};
1813
1814// Instruction to check if two inputs are equal to each other.
1815class HEqual : public HCondition {
1816 public:
1817 HEqual(HInstruction* first, HInstruction* second)
1818 : HCondition(first, second) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001819
Mingyao Yangdc5ac732015-02-25 11:28:05 -08001820 bool IsCommutative() const OVERRIDE { return true; }
1821
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001822 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001823 return x == y ? 1 : 0;
1824 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001825 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001826 return x == y ? 1 : 0;
1827 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001828
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001829 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001830
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001831 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001832 return kCondEQ;
1833 }
1834
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001835 private:
1836 DISALLOW_COPY_AND_ASSIGN(HEqual);
1837};
1838
Dave Allison20dfc792014-06-16 20:44:29 -07001839class HNotEqual : public HCondition {
1840 public:
1841 HNotEqual(HInstruction* first, HInstruction* second)
1842 : HCondition(first, second) {}
1843
Mingyao Yangdc5ac732015-02-25 11:28:05 -08001844 bool IsCommutative() const OVERRIDE { return true; }
1845
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001846 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001847 return x != y ? 1 : 0;
1848 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001849 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001850 return x != y ? 1 : 0;
1851 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001852
Dave Allison20dfc792014-06-16 20:44:29 -07001853 DECLARE_INSTRUCTION(NotEqual);
1854
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001855 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001856 return kCondNE;
1857 }
1858
1859 private:
1860 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
1861};
1862
1863class HLessThan : public HCondition {
1864 public:
1865 HLessThan(HInstruction* first, HInstruction* second)
1866 : HCondition(first, second) {}
1867
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001868 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001869 return x < y ? 1 : 0;
1870 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001871 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001872 return x < y ? 1 : 0;
1873 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001874
Dave Allison20dfc792014-06-16 20:44:29 -07001875 DECLARE_INSTRUCTION(LessThan);
1876
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001877 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001878 return kCondLT;
1879 }
1880
1881 private:
1882 DISALLOW_COPY_AND_ASSIGN(HLessThan);
1883};
1884
1885class HLessThanOrEqual : public HCondition {
1886 public:
1887 HLessThanOrEqual(HInstruction* first, HInstruction* second)
1888 : HCondition(first, second) {}
1889
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001890 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001891 return x <= y ? 1 : 0;
1892 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001893 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001894 return x <= y ? 1 : 0;
1895 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001896
Dave Allison20dfc792014-06-16 20:44:29 -07001897 DECLARE_INSTRUCTION(LessThanOrEqual);
1898
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001899 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001900 return kCondLE;
1901 }
1902
1903 private:
1904 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
1905};
1906
1907class HGreaterThan : public HCondition {
1908 public:
1909 HGreaterThan(HInstruction* first, HInstruction* second)
1910 : HCondition(first, second) {}
1911
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001912 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001913 return x > y ? 1 : 0;
1914 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001915 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001916 return x > y ? 1 : 0;
1917 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001918
Dave Allison20dfc792014-06-16 20:44:29 -07001919 DECLARE_INSTRUCTION(GreaterThan);
1920
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001921 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001922 return kCondGT;
1923 }
1924
1925 private:
1926 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
1927};
1928
1929class HGreaterThanOrEqual : public HCondition {
1930 public:
1931 HGreaterThanOrEqual(HInstruction* first, HInstruction* second)
1932 : HCondition(first, second) {}
1933
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001934 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001935 return x >= y ? 1 : 0;
1936 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001937 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001938 return x >= y ? 1 : 0;
1939 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001940
Dave Allison20dfc792014-06-16 20:44:29 -07001941 DECLARE_INSTRUCTION(GreaterThanOrEqual);
1942
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001943 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001944 return kCondGE;
1945 }
1946
1947 private:
1948 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
1949};
1950
1951
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001952// Instruction to check how two inputs compare to each other.
1953// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
1954class HCompare : public HBinaryOperation {
1955 public:
Calin Juravleddb7df22014-11-25 20:56:51 +00001956 // The bias applies for floating point operations and indicates how NaN
1957 // comparisons are treated:
1958 enum Bias {
1959 kNoBias, // bias is not applicable (i.e. for long operation)
1960 kGtBias, // return 1 for NaN comparisons
1961 kLtBias, // return -1 for NaN comparisons
1962 };
1963
1964 HCompare(Primitive::Type type, HInstruction* first, HInstruction* second, Bias bias)
1965 : HBinaryOperation(Primitive::kPrimInt, first, second), bias_(bias) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001966 DCHECK_EQ(type, first->GetType());
1967 DCHECK_EQ(type, second->GetType());
1968 }
1969
Calin Juravleddb7df22014-11-25 20:56:51 +00001970 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01001971 return
1972 x == y ? 0 :
1973 x > y ? 1 :
1974 -1;
1975 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001976
1977 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01001978 return
1979 x == y ? 0 :
1980 x > y ? 1 :
1981 -1;
1982 }
1983
Calin Juravleddb7df22014-11-25 20:56:51 +00001984 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
1985 return bias_ == other->AsCompare()->bias_;
1986 }
1987
1988 bool IsGtBias() { return bias_ == kGtBias; }
1989
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001990 DECLARE_INSTRUCTION(Compare);
1991
1992 private:
Calin Juravleddb7df22014-11-25 20:56:51 +00001993 const Bias bias_;
1994
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001995 DISALLOW_COPY_AND_ASSIGN(HCompare);
1996};
1997
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001998// A local in the graph. Corresponds to a Dex register.
1999class HLocal : public HTemplateInstruction<0> {
2000 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002001 explicit HLocal(uint16_t reg_number)
2002 : HTemplateInstruction(SideEffects::None()), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002003
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002004 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002005
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002006 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002007
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002008 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002009 // The Dex register number.
2010 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002011
2012 DISALLOW_COPY_AND_ASSIGN(HLocal);
2013};
2014
2015// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07002016class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002017 public:
Roland Levillain5799fc02014-09-25 12:15:20 +01002018 HLoadLocal(HLocal* local, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002019 : HExpression(type, SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002020 SetRawInputAt(0, local);
2021 }
2022
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002023 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
2024
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002025 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002026
2027 private:
2028 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
2029};
2030
2031// Store a value in a given local. This instruction has two inputs: the value
2032// and the local.
2033class HStoreLocal : public HTemplateInstruction<2> {
2034 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002035 HStoreLocal(HLocal* local, HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002036 SetRawInputAt(0, local);
2037 SetRawInputAt(1, value);
2038 }
2039
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002040 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
2041
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002042 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002043
2044 private:
2045 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
2046};
2047
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002048class HConstant : public HExpression<0> {
2049 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002050 explicit HConstant(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
2051
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002052 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002053
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002054 virtual bool IsMinusOne() const { return false; }
2055 virtual bool IsZero() const { return false; }
2056 virtual bool IsOne() const { return false; }
2057
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002058 DECLARE_INSTRUCTION(Constant);
2059
2060 private:
2061 DISALLOW_COPY_AND_ASSIGN(HConstant);
2062};
2063
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002064class HFloatConstant : public HConstant {
2065 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002066 float GetValue() const { return value_; }
2067
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002068 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002069 return bit_cast<uint32_t, float>(other->AsFloatConstant()->value_) ==
2070 bit_cast<uint32_t, float>(value_);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002071 }
2072
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002073 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002074
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002075 bool IsMinusOne() 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 bool IsZero() const OVERRIDE {
2080 return AsFloatConstant()->GetValue() == 0.0f;
2081 }
2082 bool IsOne() const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002083 return bit_cast<uint32_t, float>(AsFloatConstant()->GetValue()) ==
2084 bit_cast<uint32_t, float>(1.0f);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002085 }
2086
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002087 DECLARE_INSTRUCTION(FloatConstant);
2088
2089 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002090 explicit HFloatConstant(float value) : HConstant(Primitive::kPrimFloat), value_(value) {}
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002091 explicit HFloatConstant(int32_t value)
2092 : HConstant(Primitive::kPrimFloat), value_(bit_cast<float, int32_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00002093
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002094 const float value_;
2095
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002096 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00002097 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002098 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002099 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2100};
2101
2102class HDoubleConstant : public HConstant {
2103 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002104 double GetValue() const { return value_; }
2105
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002106 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002107 return bit_cast<uint64_t, double>(other->AsDoubleConstant()->value_) ==
2108 bit_cast<uint64_t, double>(value_);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002109 }
2110
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002111 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002112
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002113 bool IsMinusOne() 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 bool IsZero() const OVERRIDE {
2118 return AsDoubleConstant()->GetValue() == 0.0;
2119 }
2120 bool IsOne() const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002121 return bit_cast<uint64_t, double>(AsDoubleConstant()->GetValue()) ==
2122 bit_cast<uint64_t, double>(1.0);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002123 }
2124
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002125 DECLARE_INSTRUCTION(DoubleConstant);
2126
2127 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002128 explicit HDoubleConstant(double value) : HConstant(Primitive::kPrimDouble), value_(value) {}
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002129 explicit HDoubleConstant(int64_t value)
2130 : HConstant(Primitive::kPrimDouble), value_(bit_cast<double, int64_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00002131
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002132 const double value_;
2133
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002134 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00002135 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002136 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002137 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
2138};
2139
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002140class HNullConstant : public HConstant {
2141 public:
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002142 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2143 return true;
2144 }
2145
2146 size_t ComputeHashCode() const OVERRIDE { return 0; }
2147
2148 DECLARE_INSTRUCTION(NullConstant);
2149
2150 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002151 HNullConstant() : HConstant(Primitive::kPrimNot) {}
2152
2153 friend class HGraph;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002154 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2155};
2156
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002157// Constants of the type int. Those can be from Dex instructions, or
2158// synthesized (for example with the if-eqz instruction).
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002159class HIntConstant : public HConstant {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002160 public:
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002161 int32_t GetValue() const { return value_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002162
Calin Juravle61d544b2015-02-23 16:46:57 +00002163 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002164 return other->AsIntConstant()->value_ == value_;
2165 }
2166
Calin Juravle61d544b2015-02-23 16:46:57 +00002167 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2168
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002169 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2170 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2171 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2172
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002173 DECLARE_INSTRUCTION(IntConstant);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002174
2175 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002176 explicit HIntConstant(int32_t value) : HConstant(Primitive::kPrimInt), value_(value) {}
2177
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002178 const int32_t value_;
2179
David Brazdil8d5b8b22015-03-24 10:51:52 +00002180 friend class HGraph;
2181 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
Zheng Xuad4450e2015-04-17 18:48:56 +08002182 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002183 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2184};
2185
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002186class HLongConstant : public HConstant {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002187 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002188 int64_t GetValue() const { return value_; }
2189
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002190 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002191 return other->AsLongConstant()->value_ == value_;
2192 }
2193
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002194 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002195
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002196 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2197 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2198 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2199
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002200 DECLARE_INSTRUCTION(LongConstant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002201
2202 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002203 explicit HLongConstant(int64_t value) : HConstant(Primitive::kPrimLong), value_(value) {}
2204
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002205 const int64_t value_;
2206
David Brazdil8d5b8b22015-03-24 10:51:52 +00002207 friend class HGraph;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002208 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2209};
2210
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002211enum class Intrinsics {
2212#define OPTIMIZING_INTRINSICS(Name, IsStatic) k ## Name,
2213#include "intrinsics_list.h"
2214 kNone,
2215 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
2216#undef INTRINSICS_LIST
2217#undef OPTIMIZING_INTRINSICS
2218};
2219std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
2220
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002221class HInvoke : public HInstruction {
2222 public:
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002223 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002224
2225 // Runtime needs to walk the stack, so Dex -> Dex calls need to
2226 // know their environment.
Calin Juravle77520bc2015-01-12 18:45:46 +00002227 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002228
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01002229 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002230 SetRawInputAt(index, argument);
2231 }
2232
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002233 Primitive::Type GetType() const OVERRIDE { return return_type_; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002234
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002235 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002236
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002237 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
2238
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01002239 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002240 return intrinsic_;
2241 }
2242
2243 void SetIntrinsic(Intrinsics intrinsic) {
2244 intrinsic_ = intrinsic;
2245 }
2246
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002247 DECLARE_INSTRUCTION(Invoke);
2248
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002249 protected:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002250 HInvoke(ArenaAllocator* arena,
2251 uint32_t number_of_arguments,
2252 Primitive::Type return_type,
2253 uint32_t dex_pc,
2254 uint32_t dex_method_index)
2255 : HInstruction(SideEffects::All()),
2256 inputs_(arena, number_of_arguments),
2257 return_type_(return_type),
2258 dex_pc_(dex_pc),
2259 dex_method_index_(dex_method_index),
2260 intrinsic_(Intrinsics::kNone) {
2261 inputs_.SetSize(number_of_arguments);
2262 }
2263
David Brazdil1abb4192015-02-17 18:33:36 +00002264 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
2265 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
2266 inputs_.Put(index, input);
2267 }
2268
2269 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002270 const Primitive::Type return_type_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002271 const uint32_t dex_pc_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002272 const uint32_t dex_method_index_;
2273 Intrinsics intrinsic_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002274
2275 private:
2276 DISALLOW_COPY_AND_ASSIGN(HInvoke);
2277};
2278
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002279class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002280 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01002281 // Requirements of this method call regarding the class
2282 // initialization (clinit) check of its declaring class.
2283 enum class ClinitCheckRequirement {
2284 kNone, // Class already initialized.
2285 kExplicit, // Static call having explicit clinit check as last input.
2286 kImplicit, // Static call implicitly requiring a clinit check.
2287 };
2288
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002289 HInvokeStaticOrDirect(ArenaAllocator* arena,
2290 uint32_t number_of_arguments,
2291 Primitive::Type return_type,
2292 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002293 uint32_t dex_method_index,
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002294 bool is_recursive,
Nicolas Geoffray79041292015-03-26 10:05:54 +00002295 InvokeType original_invoke_type,
Roland Levillain4c0eb422015-04-24 16:43:49 +01002296 InvokeType invoke_type,
2297 ClinitCheckRequirement clinit_check_requirement)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002298 : HInvoke(arena, number_of_arguments, return_type, dex_pc, dex_method_index),
Nicolas Geoffray79041292015-03-26 10:05:54 +00002299 original_invoke_type_(original_invoke_type),
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002300 invoke_type_(invoke_type),
Roland Levillain4c0eb422015-04-24 16:43:49 +01002301 is_recursive_(is_recursive),
2302 clinit_check_requirement_(clinit_check_requirement) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002303
Calin Juravle641547a2015-04-21 22:08:51 +01002304 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
2305 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00002306 // We access the method via the dex cache so we can't do an implicit null check.
2307 // TODO: for intrinsics we can generate implicit null checks.
2308 return false;
2309 }
2310
Nicolas Geoffray79041292015-03-26 10:05:54 +00002311 InvokeType GetOriginalInvokeType() const { return original_invoke_type_; }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002312 InvokeType GetInvokeType() const { return invoke_type_; }
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002313 bool IsRecursive() const { return is_recursive_; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002314 bool NeedsDexCache() const OVERRIDE { return !IsRecursive(); }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002315
Roland Levillain4c0eb422015-04-24 16:43:49 +01002316 // Is this instruction a call to a static method?
2317 bool IsStatic() const {
2318 return GetInvokeType() == kStatic;
2319 }
2320
Roland Levillain0379f822015-04-24 23:01:24 +01002321 // Remove the art::HClinitCheck or art::HLoadClass instruction as
2322 // last input (only relevant for static calls with explicit clinit
2323 // check).
2324 void RemoveClinitCheckOrLoadClassAsLastInput() {
Roland Levillain4c0eb422015-04-24 16:43:49 +01002325 DCHECK(IsStaticWithExplicitClinitCheck());
2326 size_t last_input_index = InputCount() - 1;
2327 HInstruction* last_input = InputAt(last_input_index);
2328 DCHECK(last_input != nullptr);
Roland Levillain0379f822015-04-24 23:01:24 +01002329 DCHECK(last_input->IsClinitCheck() || last_input->IsLoadClass()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01002330 RemoveAsUserOfInput(last_input_index);
2331 inputs_.DeleteAt(last_input_index);
2332 clinit_check_requirement_ = ClinitCheckRequirement::kImplicit;
2333 DCHECK(IsStaticWithImplicitClinitCheck());
2334 }
2335
2336 // Is this a call to a static method whose declaring class has an
2337 // explicit intialization check in the graph?
2338 bool IsStaticWithExplicitClinitCheck() const {
2339 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kExplicit);
2340 }
2341
2342 // Is this a call to a static method whose declaring class has an
2343 // implicit intialization check requirement?
2344 bool IsStaticWithImplicitClinitCheck() const {
2345 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kImplicit);
2346 }
2347
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002348 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002349
Roland Levillain4c0eb422015-04-24 16:43:49 +01002350 protected:
2351 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
2352 const HUserRecord<HInstruction*> input_record = HInvoke::InputRecordAt(i);
2353 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck() && (i == InputCount() - 1)) {
2354 HInstruction* input = input_record.GetInstruction();
2355 // `input` is the last input of a static invoke marked as having
2356 // an explicit clinit check. It must either be:
2357 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
2358 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
2359 DCHECK(input != nullptr);
2360 DCHECK(input->IsClinitCheck() || input->IsLoadClass()) << input->DebugName();
2361 }
2362 return input_record;
2363 }
2364
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002365 private:
Nicolas Geoffray79041292015-03-26 10:05:54 +00002366 const InvokeType original_invoke_type_;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002367 const InvokeType invoke_type_;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002368 const bool is_recursive_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01002369 ClinitCheckRequirement clinit_check_requirement_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002370
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002371 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002372};
2373
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002374class HInvokeVirtual : public HInvoke {
2375 public:
2376 HInvokeVirtual(ArenaAllocator* arena,
2377 uint32_t number_of_arguments,
2378 Primitive::Type return_type,
2379 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002380 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002381 uint32_t vtable_index)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002382 : HInvoke(arena, number_of_arguments, return_type, dex_pc, dex_method_index),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002383 vtable_index_(vtable_index) {}
2384
Calin Juravle641547a2015-04-21 22:08:51 +01002385 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00002386 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01002387 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00002388 }
2389
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002390 uint32_t GetVTableIndex() const { return vtable_index_; }
2391
2392 DECLARE_INSTRUCTION(InvokeVirtual);
2393
2394 private:
2395 const uint32_t vtable_index_;
2396
2397 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
2398};
2399
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002400class HInvokeInterface : public HInvoke {
2401 public:
2402 HInvokeInterface(ArenaAllocator* arena,
2403 uint32_t number_of_arguments,
2404 Primitive::Type return_type,
2405 uint32_t dex_pc,
2406 uint32_t dex_method_index,
2407 uint32_t imt_index)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002408 : HInvoke(arena, number_of_arguments, return_type, dex_pc, dex_method_index),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002409 imt_index_(imt_index) {}
2410
Calin Juravle641547a2015-04-21 22:08:51 +01002411 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00002412 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01002413 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00002414 }
2415
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002416 uint32_t GetImtIndex() const { return imt_index_; }
2417 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
2418
2419 DECLARE_INSTRUCTION(InvokeInterface);
2420
2421 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002422 const uint32_t imt_index_;
2423
2424 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
2425};
2426
Dave Allison20dfc792014-06-16 20:44:29 -07002427class HNewInstance : public HExpression<0> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002428 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002429 HNewInstance(uint32_t dex_pc, uint16_t type_index, QuickEntrypointEnum entrypoint)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002430 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2431 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002432 type_index_(type_index),
2433 entrypoint_(entrypoint) {}
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002434
2435 uint32_t GetDexPc() const { return dex_pc_; }
2436 uint16_t GetTypeIndex() const { return type_index_; }
2437
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002438 // Calls runtime so needs an environment.
Calin Juravle92a6ed22014-12-02 18:58:03 +00002439 bool NeedsEnvironment() const OVERRIDE { return true; }
2440 // It may throw when called on:
2441 // - interfaces
2442 // - abstract/innaccessible/unknown classes
2443 // TODO: optimize when possible.
2444 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002445
Calin Juravle10e244f2015-01-26 18:54:32 +00002446 bool CanBeNull() const OVERRIDE { return false; }
2447
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002448 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
2449
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002450 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002451
2452 private:
2453 const uint32_t dex_pc_;
2454 const uint16_t type_index_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002455 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002456
2457 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
2458};
2459
Roland Levillain88cb1752014-10-20 16:36:47 +01002460class HNeg : public HUnaryOperation {
2461 public:
2462 explicit HNeg(Primitive::Type result_type, HInstruction* input)
2463 : HUnaryOperation(result_type, input) {}
2464
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002465 int32_t Evaluate(int32_t x) const OVERRIDE { return -x; }
2466 int64_t Evaluate(int64_t x) const OVERRIDE { return -x; }
Roland Levillain9240d6a2014-10-20 16:47:04 +01002467
Roland Levillain88cb1752014-10-20 16:36:47 +01002468 DECLARE_INSTRUCTION(Neg);
2469
2470 private:
2471 DISALLOW_COPY_AND_ASSIGN(HNeg);
2472};
2473
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002474class HNewArray : public HExpression<1> {
2475 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002476 HNewArray(HInstruction* length,
2477 uint32_t dex_pc,
2478 uint16_t type_index,
2479 QuickEntrypointEnum entrypoint)
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002480 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2481 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002482 type_index_(type_index),
2483 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002484 SetRawInputAt(0, length);
2485 }
2486
2487 uint32_t GetDexPc() const { return dex_pc_; }
2488 uint16_t GetTypeIndex() const { return type_index_; }
2489
2490 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00002491 bool NeedsEnvironment() const OVERRIDE { return true; }
2492
Mingyao Yang0c365e62015-03-31 15:09:29 -07002493 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
2494 bool CanThrow() const OVERRIDE { return true; }
2495
Calin Juravle10e244f2015-01-26 18:54:32 +00002496 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002497
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002498 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
2499
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002500 DECLARE_INSTRUCTION(NewArray);
2501
2502 private:
2503 const uint32_t dex_pc_;
2504 const uint16_t type_index_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002505 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002506
2507 DISALLOW_COPY_AND_ASSIGN(HNewArray);
2508};
2509
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002510class HAdd : public HBinaryOperation {
2511 public:
2512 HAdd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2513 : HBinaryOperation(result_type, left, right) {}
2514
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002515 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002516
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002517 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002518 return x + y;
2519 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002520 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002521 return x + y;
2522 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002523
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002524 DECLARE_INSTRUCTION(Add);
2525
2526 private:
2527 DISALLOW_COPY_AND_ASSIGN(HAdd);
2528};
2529
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002530class HSub : public HBinaryOperation {
2531 public:
2532 HSub(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2533 : HBinaryOperation(result_type, left, right) {}
2534
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002535 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002536 return x - y;
2537 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002538 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002539 return x - y;
2540 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002541
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002542 DECLARE_INSTRUCTION(Sub);
2543
2544 private:
2545 DISALLOW_COPY_AND_ASSIGN(HSub);
2546};
2547
Calin Juravle34bacdf2014-10-07 20:23:36 +01002548class HMul : public HBinaryOperation {
2549 public:
2550 HMul(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2551 : HBinaryOperation(result_type, left, right) {}
2552
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002553 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002554
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002555 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x * y; }
2556 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x * y; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002557
2558 DECLARE_INSTRUCTION(Mul);
2559
2560 private:
2561 DISALLOW_COPY_AND_ASSIGN(HMul);
2562};
2563
Calin Juravle7c4954d2014-10-28 16:57:40 +00002564class HDiv : public HBinaryOperation {
2565 public:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002566 HDiv(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
2567 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00002568
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002569 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00002570 // Our graph structure ensures we never have 0 for `y` during constant folding.
2571 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00002572 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00002573 return (y == -1) ? -x : x / y;
2574 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002575
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002576 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002577 DCHECK_NE(y, 0);
2578 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2579 return (y == -1) ? -x : x / y;
2580 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002581
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002582 uint32_t GetDexPc() const { return dex_pc_; }
2583
Calin Juravle7c4954d2014-10-28 16:57:40 +00002584 DECLARE_INSTRUCTION(Div);
2585
2586 private:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002587 const uint32_t dex_pc_;
2588
Calin Juravle7c4954d2014-10-28 16:57:40 +00002589 DISALLOW_COPY_AND_ASSIGN(HDiv);
2590};
2591
Calin Juravlebacfec32014-11-14 15:54:36 +00002592class HRem : public HBinaryOperation {
2593 public:
2594 HRem(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
2595 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
2596
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002597 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002598 DCHECK_NE(y, 0);
2599 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2600 return (y == -1) ? 0 : x % y;
2601 }
2602
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002603 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002604 DCHECK_NE(y, 0);
2605 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2606 return (y == -1) ? 0 : x % y;
2607 }
2608
2609 uint32_t GetDexPc() const { return dex_pc_; }
2610
2611 DECLARE_INSTRUCTION(Rem);
2612
2613 private:
2614 const uint32_t dex_pc_;
2615
2616 DISALLOW_COPY_AND_ASSIGN(HRem);
2617};
2618
Calin Juravled0d48522014-11-04 16:40:20 +00002619class HDivZeroCheck : public HExpression<1> {
2620 public:
2621 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
2622 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
2623 SetRawInputAt(0, value);
2624 }
2625
2626 bool CanBeMoved() const OVERRIDE { return true; }
2627
2628 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2629 UNUSED(other);
2630 return true;
2631 }
2632
2633 bool NeedsEnvironment() const OVERRIDE { return true; }
2634 bool CanThrow() const OVERRIDE { return true; }
2635
2636 uint32_t GetDexPc() const { return dex_pc_; }
2637
2638 DECLARE_INSTRUCTION(DivZeroCheck);
2639
2640 private:
2641 const uint32_t dex_pc_;
2642
2643 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
2644};
2645
Calin Juravle9aec02f2014-11-18 23:06:35 +00002646class HShl : public HBinaryOperation {
2647 public:
2648 HShl(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2649 : HBinaryOperation(result_type, left, right) {}
2650
2651 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x << (y & kMaxIntShiftValue); }
2652 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x << (y & kMaxLongShiftValue); }
2653
2654 DECLARE_INSTRUCTION(Shl);
2655
2656 private:
2657 DISALLOW_COPY_AND_ASSIGN(HShl);
2658};
2659
2660class HShr : public HBinaryOperation {
2661 public:
2662 HShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2663 : HBinaryOperation(result_type, left, right) {}
2664
2665 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x >> (y & kMaxIntShiftValue); }
2666 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x >> (y & kMaxLongShiftValue); }
2667
2668 DECLARE_INSTRUCTION(Shr);
2669
2670 private:
2671 DISALLOW_COPY_AND_ASSIGN(HShr);
2672};
2673
2674class HUShr : public HBinaryOperation {
2675 public:
2676 HUShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2677 : HBinaryOperation(result_type, left, right) {}
2678
2679 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
2680 uint32_t ux = static_cast<uint32_t>(x);
2681 uint32_t uy = static_cast<uint32_t>(y) & kMaxIntShiftValue;
2682 return static_cast<int32_t>(ux >> uy);
2683 }
2684
2685 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
2686 uint64_t ux = static_cast<uint64_t>(x);
2687 uint64_t uy = static_cast<uint64_t>(y) & kMaxLongShiftValue;
2688 return static_cast<int64_t>(ux >> uy);
2689 }
2690
2691 DECLARE_INSTRUCTION(UShr);
2692
2693 private:
2694 DISALLOW_COPY_AND_ASSIGN(HUShr);
2695};
2696
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002697class HAnd : public HBinaryOperation {
2698 public:
2699 HAnd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2700 : HBinaryOperation(result_type, left, right) {}
2701
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002702 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002703
2704 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x & y; }
2705 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x & y; }
2706
2707 DECLARE_INSTRUCTION(And);
2708
2709 private:
2710 DISALLOW_COPY_AND_ASSIGN(HAnd);
2711};
2712
2713class HOr : public HBinaryOperation {
2714 public:
2715 HOr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2716 : HBinaryOperation(result_type, left, right) {}
2717
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002718 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002719
2720 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x | y; }
2721 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x | y; }
2722
2723 DECLARE_INSTRUCTION(Or);
2724
2725 private:
2726 DISALLOW_COPY_AND_ASSIGN(HOr);
2727};
2728
2729class HXor : public HBinaryOperation {
2730 public:
2731 HXor(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2732 : HBinaryOperation(result_type, left, right) {}
2733
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002734 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002735
2736 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x ^ y; }
2737 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x ^ y; }
2738
2739 DECLARE_INSTRUCTION(Xor);
2740
2741 private:
2742 DISALLOW_COPY_AND_ASSIGN(HXor);
2743};
2744
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002745// The value of a parameter in this method. Its location depends on
2746// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07002747class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002748 public:
Calin Juravle10e244f2015-01-26 18:54:32 +00002749 HParameterValue(uint8_t index, Primitive::Type parameter_type, bool is_this = false)
2750 : HExpression(parameter_type, SideEffects::None()), index_(index), is_this_(is_this) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002751
2752 uint8_t GetIndex() const { return index_; }
2753
Calin Juravle10e244f2015-01-26 18:54:32 +00002754 bool CanBeNull() const OVERRIDE { return !is_this_; }
2755
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002756 DECLARE_INSTRUCTION(ParameterValue);
2757
2758 private:
2759 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00002760 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002761 const uint8_t index_;
2762
Calin Juravle10e244f2015-01-26 18:54:32 +00002763 // Whether or not the parameter value corresponds to 'this' argument.
2764 const bool is_this_;
2765
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002766 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
2767};
2768
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002769class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002770 public:
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002771 explicit HNot(Primitive::Type result_type, HInstruction* input)
2772 : HUnaryOperation(result_type, input) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002773
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002774 bool CanBeMoved() const OVERRIDE { return true; }
2775 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002776 UNUSED(other);
2777 return true;
2778 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002779
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002780 int32_t Evaluate(int32_t x) const OVERRIDE { return ~x; }
2781 int64_t Evaluate(int64_t x) const OVERRIDE { return ~x; }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002782
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002783 DECLARE_INSTRUCTION(Not);
2784
2785 private:
2786 DISALLOW_COPY_AND_ASSIGN(HNot);
2787};
2788
David Brazdil66d126e2015-04-03 16:02:44 +01002789class HBooleanNot : public HUnaryOperation {
2790 public:
2791 explicit HBooleanNot(HInstruction* input)
2792 : HUnaryOperation(Primitive::Type::kPrimBoolean, input) {}
2793
2794 bool CanBeMoved() const OVERRIDE { return true; }
2795 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2796 UNUSED(other);
2797 return true;
2798 }
2799
2800 int32_t Evaluate(int32_t x) const OVERRIDE {
2801 DCHECK(IsUint<1>(x));
2802 return !x;
2803 }
2804
2805 int64_t Evaluate(int64_t x ATTRIBUTE_UNUSED) const OVERRIDE {
2806 LOG(FATAL) << DebugName() << " cannot be used with 64-bit values";
2807 UNREACHABLE();
2808 }
2809
2810 DECLARE_INSTRUCTION(BooleanNot);
2811
2812 private:
2813 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
2814};
2815
Roland Levillaindff1f282014-11-05 14:15:05 +00002816class HTypeConversion : public HExpression<1> {
2817 public:
2818 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00002819 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
2820 : HExpression(result_type, SideEffects::None()), dex_pc_(dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002821 SetRawInputAt(0, input);
2822 DCHECK_NE(input->GetType(), result_type);
2823 }
2824
2825 HInstruction* GetInput() const { return InputAt(0); }
2826 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
2827 Primitive::Type GetResultType() const { return GetType(); }
2828
Roland Levillain624279f2014-12-04 11:54:28 +00002829 // Required by the x86 and ARM code generators when producing calls
2830 // to the runtime.
2831 uint32_t GetDexPc() const { return dex_pc_; }
2832
Roland Levillaindff1f282014-11-05 14:15:05 +00002833 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00002834 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00002835
2836 DECLARE_INSTRUCTION(TypeConversion);
2837
2838 private:
Roland Levillain624279f2014-12-04 11:54:28 +00002839 const uint32_t dex_pc_;
2840
Roland Levillaindff1f282014-11-05 14:15:05 +00002841 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
2842};
2843
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00002844static constexpr uint32_t kNoRegNumber = -1;
2845
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002846class HPhi : public HInstruction {
2847 public:
2848 HPhi(ArenaAllocator* arena, uint32_t reg_number, size_t number_of_inputs, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002849 : HInstruction(SideEffects::None()),
2850 inputs_(arena, number_of_inputs),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002851 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002852 type_(type),
Calin Juravle10e244f2015-01-26 18:54:32 +00002853 is_live_(false),
2854 can_be_null_(true) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002855 inputs_.SetSize(number_of_inputs);
2856 }
2857
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00002858 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
2859 static Primitive::Type ToPhiType(Primitive::Type type) {
2860 switch (type) {
2861 case Primitive::kPrimBoolean:
2862 case Primitive::kPrimByte:
2863 case Primitive::kPrimShort:
2864 case Primitive::kPrimChar:
2865 return Primitive::kPrimInt;
2866 default:
2867 return type;
2868 }
2869 }
2870
Calin Juravle10e244f2015-01-26 18:54:32 +00002871 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002872
2873 void AddInput(HInstruction* input);
David Brazdil2d7352b2015-04-20 14:52:42 +01002874 void RemoveInputAt(size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002875
Calin Juravle10e244f2015-01-26 18:54:32 +00002876 Primitive::Type GetType() const OVERRIDE { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002877 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002878
Calin Juravle10e244f2015-01-26 18:54:32 +00002879 bool CanBeNull() const OVERRIDE { return can_be_null_; }
2880 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
2881
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002882 uint32_t GetRegNumber() const { return reg_number_; }
2883
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002884 void SetDead() { is_live_ = false; }
2885 void SetLive() { is_live_ = true; }
2886 bool IsDead() const { return !is_live_; }
2887 bool IsLive() const { return is_live_; }
2888
Calin Juravlea4f88312015-04-16 12:57:19 +01002889 // Returns the next equivalent phi (starting from the current one) or null if there is none.
2890 // An equivalent phi is a phi having the same dex register and type.
2891 // It assumes that phis with the same dex register are adjacent.
2892 HPhi* GetNextEquivalentPhiWithSameType() {
2893 HInstruction* next = GetNext();
2894 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
2895 if (next->GetType() == GetType()) {
2896 return next->AsPhi();
2897 }
2898 next = next->GetNext();
2899 }
2900 return nullptr;
2901 }
2902
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002903 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002904
David Brazdil1abb4192015-02-17 18:33:36 +00002905 protected:
2906 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
2907
2908 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
2909 inputs_.Put(index, input);
2910 }
2911
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002912 private:
David Brazdil1abb4192015-02-17 18:33:36 +00002913 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002914 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002915 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002916 bool is_live_;
Calin Juravle10e244f2015-01-26 18:54:32 +00002917 bool can_be_null_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002918
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002919 DISALLOW_COPY_AND_ASSIGN(HPhi);
2920};
2921
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002922class HNullCheck : public HExpression<1> {
2923 public:
2924 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002925 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002926 SetRawInputAt(0, value);
2927 }
2928
Calin Juravle10e244f2015-01-26 18:54:32 +00002929 bool CanBeMoved() const OVERRIDE { return true; }
2930 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002931 UNUSED(other);
2932 return true;
2933 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002934
Calin Juravle10e244f2015-01-26 18:54:32 +00002935 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002936
Calin Juravle10e244f2015-01-26 18:54:32 +00002937 bool CanThrow() const OVERRIDE { return true; }
2938
2939 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01002940
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002941 uint32_t GetDexPc() const { return dex_pc_; }
2942
2943 DECLARE_INSTRUCTION(NullCheck);
2944
2945 private:
2946 const uint32_t dex_pc_;
2947
2948 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
2949};
2950
2951class FieldInfo : public ValueObject {
2952 public:
Calin Juravle52c48962014-12-16 17:02:57 +00002953 FieldInfo(MemberOffset field_offset, Primitive::Type field_type, bool is_volatile)
2954 : field_offset_(field_offset), field_type_(field_type), is_volatile_(is_volatile) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002955
2956 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002957 Primitive::Type GetFieldType() const { return field_type_; }
Calin Juravle52c48962014-12-16 17:02:57 +00002958 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002959
2960 private:
2961 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01002962 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00002963 const bool is_volatile_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002964};
2965
2966class HInstanceFieldGet : public HExpression<1> {
2967 public:
2968 HInstanceFieldGet(HInstruction* value,
2969 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00002970 MemberOffset field_offset,
2971 bool is_volatile)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002972 : HExpression(field_type, SideEffects::DependsOnSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00002973 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002974 SetRawInputAt(0, value);
2975 }
2976
Calin Juravle10c9cbe2014-12-19 10:50:19 +00002977 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002978
2979 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2980 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
2981 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002982 }
2983
Calin Juravle641547a2015-04-21 22:08:51 +01002984 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
2985 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00002986 }
2987
2988 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002989 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
2990 }
2991
Calin Juravle52c48962014-12-16 17:02:57 +00002992 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002993 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002994 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002995 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002996
2997 DECLARE_INSTRUCTION(InstanceFieldGet);
2998
2999 private:
3000 const FieldInfo field_info_;
3001
3002 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
3003};
3004
3005class HInstanceFieldSet : public HTemplateInstruction<2> {
3006 public:
3007 HInstanceFieldSet(HInstruction* object,
3008 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01003009 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003010 MemberOffset field_offset,
3011 bool is_volatile)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003012 : HTemplateInstruction(SideEffects::ChangesSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00003013 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003014 SetRawInputAt(0, object);
3015 SetRawInputAt(1, value);
3016 }
3017
Calin Juravle641547a2015-04-21 22:08:51 +01003018 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3019 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00003020 }
3021
Calin Juravle52c48962014-12-16 17:02:57 +00003022 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003023 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003024 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003025 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003026 HInstruction* GetValue() const { return InputAt(1); }
3027
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003028 DECLARE_INSTRUCTION(InstanceFieldSet);
3029
3030 private:
3031 const FieldInfo field_info_;
3032
3033 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
3034};
3035
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003036class HArrayGet : public HExpression<2> {
3037 public:
3038 HArrayGet(HInstruction* array, HInstruction* index, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003039 : HExpression(type, SideEffects::DependsOnSomething()) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003040 SetRawInputAt(0, array);
3041 SetRawInputAt(1, index);
3042 }
3043
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003044 bool CanBeMoved() const OVERRIDE { return true; }
3045 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003046 UNUSED(other);
3047 return true;
3048 }
Calin Juravle641547a2015-04-21 22:08:51 +01003049 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3050 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00003051 // TODO: We can be smarter here.
3052 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
3053 // which generates the implicit null check. There are cases when these can be removed
3054 // to produce better code. If we ever add optimizations to do so we should allow an
3055 // implicit check here (as long as the address falls in the first page).
3056 return false;
3057 }
3058
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003059 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003060
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003061 HInstruction* GetArray() const { return InputAt(0); }
3062 HInstruction* GetIndex() const { return InputAt(1); }
3063
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003064 DECLARE_INSTRUCTION(ArrayGet);
3065
3066 private:
3067 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
3068};
3069
3070class HArraySet : public HTemplateInstruction<3> {
3071 public:
3072 HArraySet(HInstruction* array,
3073 HInstruction* index,
3074 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003075 Primitive::Type expected_component_type,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003076 uint32_t dex_pc)
3077 : HTemplateInstruction(SideEffects::ChangesSomething()),
3078 dex_pc_(dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003079 expected_component_type_(expected_component_type),
3080 needs_type_check_(value->GetType() == Primitive::kPrimNot) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003081 SetRawInputAt(0, array);
3082 SetRawInputAt(1, index);
3083 SetRawInputAt(2, value);
3084 }
3085
Calin Juravle77520bc2015-01-12 18:45:46 +00003086 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003087 // We currently always call a runtime method to catch array store
3088 // exceptions.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003089 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003090 }
3091
Calin Juravle641547a2015-04-21 22:08:51 +01003092 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3093 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00003094 // TODO: Same as for ArrayGet.
3095 return false;
3096 }
3097
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003098 void ClearNeedsTypeCheck() {
3099 needs_type_check_ = false;
3100 }
3101
3102 bool NeedsTypeCheck() const { return needs_type_check_; }
3103
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003104 uint32_t GetDexPc() const { return dex_pc_; }
3105
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003106 HInstruction* GetArray() const { return InputAt(0); }
3107 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003108 HInstruction* GetValue() const { return InputAt(2); }
3109
3110 Primitive::Type GetComponentType() const {
3111 // The Dex format does not type floating point index operations. Since the
3112 // `expected_component_type_` is set during building and can therefore not
3113 // be correct, we also check what is the value type. If it is a floating
3114 // point type, we must use that type.
3115 Primitive::Type value_type = GetValue()->GetType();
3116 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
3117 ? value_type
3118 : expected_component_type_;
3119 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003120
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003121 DECLARE_INSTRUCTION(ArraySet);
3122
3123 private:
3124 const uint32_t dex_pc_;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003125 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003126 bool needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003127
3128 DISALLOW_COPY_AND_ASSIGN(HArraySet);
3129};
3130
3131class HArrayLength : public HExpression<1> {
3132 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003133 explicit HArrayLength(HInstruction* array)
3134 : HExpression(Primitive::kPrimInt, SideEffects::None()) {
3135 // Note that arrays do not change length, so the instruction does not
3136 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003137 SetRawInputAt(0, array);
3138 }
3139
Calin Juravle77520bc2015-01-12 18:45:46 +00003140 bool CanBeMoved() const OVERRIDE { return true; }
3141 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003142 UNUSED(other);
3143 return true;
3144 }
Calin Juravle641547a2015-04-21 22:08:51 +01003145 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3146 return obj == InputAt(0);
3147 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003148
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003149 DECLARE_INSTRUCTION(ArrayLength);
3150
3151 private:
3152 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
3153};
3154
3155class HBoundsCheck : public HExpression<2> {
3156 public:
3157 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003158 : HExpression(index->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003159 DCHECK(index->GetType() == Primitive::kPrimInt);
3160 SetRawInputAt(0, index);
3161 SetRawInputAt(1, length);
3162 }
3163
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003164 bool CanBeMoved() const OVERRIDE { return true; }
3165 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003166 UNUSED(other);
3167 return true;
3168 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003169
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003170 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003171
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003172 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01003173
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003174 uint32_t GetDexPc() const { return dex_pc_; }
3175
3176 DECLARE_INSTRUCTION(BoundsCheck);
3177
3178 private:
3179 const uint32_t dex_pc_;
3180
3181 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
3182};
3183
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003184/**
3185 * Some DEX instructions are folded into multiple HInstructions that need
3186 * to stay live until the last HInstruction. This class
3187 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00003188 * HInstruction stays live. `index` represents the stack location index of the
3189 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003190 */
3191class HTemporary : public HTemplateInstruction<0> {
3192 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003193 explicit HTemporary(size_t index) : HTemplateInstruction(SideEffects::None()), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003194
3195 size_t GetIndex() const { return index_; }
3196
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00003197 Primitive::Type GetType() const OVERRIDE {
3198 // The previous instruction is the one that will be stored in the temporary location.
3199 DCHECK(GetPrevious() != nullptr);
3200 return GetPrevious()->GetType();
3201 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00003202
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003203 DECLARE_INSTRUCTION(Temporary);
3204
3205 private:
3206 const size_t index_;
3207
3208 DISALLOW_COPY_AND_ASSIGN(HTemporary);
3209};
3210
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003211class HSuspendCheck : public HTemplateInstruction<0> {
3212 public:
3213 explicit HSuspendCheck(uint32_t dex_pc)
Nicolas Geoffray9ebc72c2014-09-25 16:33:42 +01003214 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003215
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003216 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003217 return true;
3218 }
3219
3220 uint32_t GetDexPc() const { return dex_pc_; }
3221
3222 DECLARE_INSTRUCTION(SuspendCheck);
3223
3224 private:
3225 const uint32_t dex_pc_;
3226
3227 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
3228};
3229
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003230/**
3231 * Instruction to load a Class object.
3232 */
3233class HLoadClass : public HExpression<0> {
3234 public:
3235 HLoadClass(uint16_t type_index,
3236 bool is_referrers_class,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003237 uint32_t dex_pc)
3238 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3239 type_index_(type_index),
3240 is_referrers_class_(is_referrers_class),
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003241 dex_pc_(dex_pc),
Calin Juravleb1498f62015-02-16 13:13:29 +00003242 generate_clinit_check_(false),
3243 loaded_class_rti_(ReferenceTypeInfo::CreateTop(/* is_exact */ false)) {}
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003244
3245 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003246
3247 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3248 return other->AsLoadClass()->type_index_ == type_index_;
3249 }
3250
3251 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
3252
3253 uint32_t GetDexPc() const { return dex_pc_; }
3254 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003255 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003256
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003257 bool NeedsEnvironment() const OVERRIDE {
3258 // Will call runtime and load the class if the class is not loaded yet.
3259 // TODO: finer grain decision.
3260 return !is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003261 }
3262
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003263 bool MustGenerateClinitCheck() const {
3264 return generate_clinit_check_;
3265 }
3266
3267 void SetMustGenerateClinitCheck() {
3268 generate_clinit_check_ = true;
3269 }
3270
3271 bool CanCallRuntime() const {
3272 return MustGenerateClinitCheck() || !is_referrers_class_;
3273 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003274
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003275 bool CanThrow() const OVERRIDE {
3276 // May call runtime and and therefore can throw.
3277 // TODO: finer grain decision.
3278 return !is_referrers_class_;
3279 }
3280
Calin Juravleacf735c2015-02-12 15:25:22 +00003281 ReferenceTypeInfo GetLoadedClassRTI() {
3282 return loaded_class_rti_;
3283 }
3284
3285 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
3286 // Make sure we only set exact types (the loaded class should never be merged).
3287 DCHECK(rti.IsExact());
3288 loaded_class_rti_ = rti;
3289 }
3290
3291 bool IsResolved() {
3292 return loaded_class_rti_.IsExact();
3293 }
3294
Nicolas Geoffray9437b782015-03-25 10:08:51 +00003295 bool NeedsDexCache() const OVERRIDE { return !is_referrers_class_; }
3296
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003297 DECLARE_INSTRUCTION(LoadClass);
3298
3299 private:
3300 const uint16_t type_index_;
3301 const bool is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003302 const uint32_t dex_pc_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003303 // Whether this instruction must generate the initialization check.
3304 // Used for code generation.
3305 bool generate_clinit_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003306
Calin Juravleacf735c2015-02-12 15:25:22 +00003307 ReferenceTypeInfo loaded_class_rti_;
3308
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003309 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
3310};
3311
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003312class HLoadString : public HExpression<0> {
3313 public:
3314 HLoadString(uint32_t string_index, uint32_t dex_pc)
3315 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3316 string_index_(string_index),
3317 dex_pc_(dex_pc) {}
3318
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003319 bool CanBeMoved() const OVERRIDE { return true; }
3320
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003321 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3322 return other->AsLoadString()->string_index_ == string_index_;
3323 }
3324
3325 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
3326
3327 uint32_t GetDexPc() const { return dex_pc_; }
3328 uint32_t GetStringIndex() const { return string_index_; }
3329
3330 // TODO: Can we deopt or debug when we resolve a string?
3331 bool NeedsEnvironment() const OVERRIDE { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00003332 bool NeedsDexCache() const OVERRIDE { return true; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003333
3334 DECLARE_INSTRUCTION(LoadString);
3335
3336 private:
3337 const uint32_t string_index_;
3338 const uint32_t dex_pc_;
3339
3340 DISALLOW_COPY_AND_ASSIGN(HLoadString);
3341};
3342
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003343/**
3344 * Performs an initialization check on its Class object input.
3345 */
3346class HClinitCheck : public HExpression<1> {
3347 public:
3348 explicit HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Nicolas Geoffraya0466e12015-03-27 15:00:40 +00003349 : HExpression(Primitive::kPrimNot, SideEffects::ChangesSomething()),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003350 dex_pc_(dex_pc) {
3351 SetRawInputAt(0, constant);
3352 }
3353
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003354 bool CanBeMoved() const OVERRIDE { return true; }
3355 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3356 UNUSED(other);
3357 return true;
3358 }
3359
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003360 bool NeedsEnvironment() const OVERRIDE {
3361 // May call runtime to initialize the class.
3362 return true;
3363 }
3364
3365 uint32_t GetDexPc() const { return dex_pc_; }
3366
3367 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
3368
3369 DECLARE_INSTRUCTION(ClinitCheck);
3370
3371 private:
3372 const uint32_t dex_pc_;
3373
3374 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
3375};
3376
3377class HStaticFieldGet : public HExpression<1> {
3378 public:
3379 HStaticFieldGet(HInstruction* cls,
3380 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003381 MemberOffset field_offset,
3382 bool is_volatile)
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003383 : HExpression(field_type, SideEffects::DependsOnSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00003384 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003385 SetRawInputAt(0, cls);
3386 }
3387
Calin Juravle52c48962014-12-16 17:02:57 +00003388
Calin Juravle10c9cbe2014-12-19 10:50:19 +00003389 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003390
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003391 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00003392 HStaticFieldGet* other_get = other->AsStaticFieldGet();
3393 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003394 }
3395
3396 size_t ComputeHashCode() const OVERRIDE {
3397 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
3398 }
3399
Calin Juravle52c48962014-12-16 17:02:57 +00003400 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003401 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
3402 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003403 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003404
3405 DECLARE_INSTRUCTION(StaticFieldGet);
3406
3407 private:
3408 const FieldInfo field_info_;
3409
3410 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
3411};
3412
3413class HStaticFieldSet : public HTemplateInstruction<2> {
3414 public:
3415 HStaticFieldSet(HInstruction* cls,
3416 HInstruction* value,
3417 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003418 MemberOffset field_offset,
3419 bool is_volatile)
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003420 : HTemplateInstruction(SideEffects::ChangesSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00003421 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003422 SetRawInputAt(0, cls);
3423 SetRawInputAt(1, value);
3424 }
3425
Calin Juravle52c48962014-12-16 17:02:57 +00003426 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003427 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
3428 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003429 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003430
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003431 HInstruction* GetValue() const { return InputAt(1); }
3432
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003433 DECLARE_INSTRUCTION(StaticFieldSet);
3434
3435 private:
3436 const FieldInfo field_info_;
3437
3438 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
3439};
3440
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003441// Implement the move-exception DEX instruction.
3442class HLoadException : public HExpression<0> {
3443 public:
3444 HLoadException() : HExpression(Primitive::kPrimNot, SideEffects::None()) {}
3445
3446 DECLARE_INSTRUCTION(LoadException);
3447
3448 private:
3449 DISALLOW_COPY_AND_ASSIGN(HLoadException);
3450};
3451
3452class HThrow : public HTemplateInstruction<1> {
3453 public:
3454 HThrow(HInstruction* exception, uint32_t dex_pc)
3455 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {
3456 SetRawInputAt(0, exception);
3457 }
3458
3459 bool IsControlFlow() const OVERRIDE { return true; }
3460
3461 bool NeedsEnvironment() const OVERRIDE { return true; }
3462
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003463 bool CanThrow() const OVERRIDE { return true; }
3464
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003465 uint32_t GetDexPc() const { return dex_pc_; }
3466
3467 DECLARE_INSTRUCTION(Throw);
3468
3469 private:
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003470 const uint32_t dex_pc_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003471
3472 DISALLOW_COPY_AND_ASSIGN(HThrow);
3473};
3474
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003475class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003476 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003477 HInstanceOf(HInstruction* object,
3478 HLoadClass* constant,
3479 bool class_is_final,
3480 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003481 : HExpression(Primitive::kPrimBoolean, SideEffects::None()),
3482 class_is_final_(class_is_final),
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003483 must_do_null_check_(true),
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003484 dex_pc_(dex_pc) {
3485 SetRawInputAt(0, object);
3486 SetRawInputAt(1, constant);
3487 }
3488
3489 bool CanBeMoved() const OVERRIDE { return true; }
3490
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003491 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003492 return true;
3493 }
3494
3495 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003496 return false;
3497 }
3498
3499 uint32_t GetDexPc() const { return dex_pc_; }
3500
3501 bool IsClassFinal() const { return class_is_final_; }
3502
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003503 // Used only in code generation.
3504 bool MustDoNullCheck() const { return must_do_null_check_; }
3505 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
3506
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003507 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003508
3509 private:
3510 const bool class_is_final_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003511 bool must_do_null_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003512 const uint32_t dex_pc_;
3513
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003514 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
3515};
3516
Calin Juravleb1498f62015-02-16 13:13:29 +00003517class HBoundType : public HExpression<1> {
3518 public:
3519 HBoundType(HInstruction* input, ReferenceTypeInfo bound_type)
3520 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3521 bound_type_(bound_type) {
Calin Juravle61d544b2015-02-23 16:46:57 +00003522 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00003523 SetRawInputAt(0, input);
3524 }
3525
3526 const ReferenceTypeInfo& GetBoundType() const { return bound_type_; }
3527
3528 bool CanBeNull() const OVERRIDE {
3529 // `null instanceof ClassX` always return false so we can't be null.
3530 return false;
3531 }
3532
3533 DECLARE_INSTRUCTION(BoundType);
3534
3535 private:
3536 // Encodes the most upper class that this instruction can have. In other words
3537 // it is always the case that GetBoundType().IsSupertypeOf(GetReferenceType()).
3538 // It is used to bound the type in cases like `if (x instanceof ClassX) {}`
3539 const ReferenceTypeInfo bound_type_;
3540
3541 DISALLOW_COPY_AND_ASSIGN(HBoundType);
3542};
3543
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003544class HCheckCast : public HTemplateInstruction<2> {
3545 public:
3546 HCheckCast(HInstruction* object,
3547 HLoadClass* constant,
3548 bool class_is_final,
3549 uint32_t dex_pc)
3550 : HTemplateInstruction(SideEffects::None()),
3551 class_is_final_(class_is_final),
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003552 must_do_null_check_(true),
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003553 dex_pc_(dex_pc) {
3554 SetRawInputAt(0, object);
3555 SetRawInputAt(1, constant);
3556 }
3557
3558 bool CanBeMoved() const OVERRIDE { return true; }
3559
3560 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
3561 return true;
3562 }
3563
3564 bool NeedsEnvironment() const OVERRIDE {
3565 // Instruction may throw a CheckCastError.
3566 return true;
3567 }
3568
3569 bool CanThrow() const OVERRIDE { return true; }
3570
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003571 bool MustDoNullCheck() const { return must_do_null_check_; }
3572 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
3573
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003574 uint32_t GetDexPc() const { return dex_pc_; }
3575
3576 bool IsClassFinal() const { return class_is_final_; }
3577
3578 DECLARE_INSTRUCTION(CheckCast);
3579
3580 private:
3581 const bool class_is_final_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003582 bool must_do_null_check_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003583 const uint32_t dex_pc_;
3584
3585 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003586};
3587
Calin Juravle27df7582015-04-17 19:12:31 +01003588class HMemoryBarrier : public HTemplateInstruction<0> {
3589 public:
3590 explicit HMemoryBarrier(MemBarrierKind barrier_kind)
3591 : HTemplateInstruction(SideEffects::None()),
3592 barrier_kind_(barrier_kind) {}
3593
3594 MemBarrierKind GetBarrierKind() { return barrier_kind_; }
3595
3596 DECLARE_INSTRUCTION(MemoryBarrier);
3597
3598 private:
3599 const MemBarrierKind barrier_kind_;
3600
3601 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
3602};
3603
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003604class HMonitorOperation : public HTemplateInstruction<1> {
3605 public:
3606 enum OperationKind {
3607 kEnter,
3608 kExit,
3609 };
3610
3611 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
3612 : HTemplateInstruction(SideEffects::None()), kind_(kind), dex_pc_(dex_pc) {
3613 SetRawInputAt(0, object);
3614 }
3615
3616 // Instruction may throw a Java exception, so we need an environment.
3617 bool NeedsEnvironment() const OVERRIDE { return true; }
3618 bool CanThrow() const OVERRIDE { return true; }
3619
3620 uint32_t GetDexPc() const { return dex_pc_; }
3621
3622 bool IsEnter() const { return kind_ == kEnter; }
3623
3624 DECLARE_INSTRUCTION(MonitorOperation);
3625
Calin Juravle52c48962014-12-16 17:02:57 +00003626 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003627 const OperationKind kind_;
3628 const uint32_t dex_pc_;
3629
3630 private:
3631 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
3632};
3633
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003634class MoveOperands : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003635 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01003636 MoveOperands(Location source,
3637 Location destination,
3638 Primitive::Type type,
3639 HInstruction* instruction)
3640 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003641
3642 Location GetSource() const { return source_; }
3643 Location GetDestination() const { return destination_; }
3644
3645 void SetSource(Location value) { source_ = value; }
3646 void SetDestination(Location value) { destination_ = value; }
3647
3648 // The parallel move resolver marks moves as "in-progress" by clearing the
3649 // destination (but not the source).
3650 Location MarkPending() {
3651 DCHECK(!IsPending());
3652 Location dest = destination_;
3653 destination_ = Location::NoLocation();
3654 return dest;
3655 }
3656
3657 void ClearPending(Location dest) {
3658 DCHECK(IsPending());
3659 destination_ = dest;
3660 }
3661
3662 bool IsPending() const {
3663 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
3664 return destination_.IsInvalid() && !source_.IsInvalid();
3665 }
3666
3667 // True if this blocks a move from the given location.
3668 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08003669 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003670 }
3671
3672 // A move is redundant if it's been eliminated, if its source and
3673 // destination are the same, or if its destination is unneeded.
3674 bool IsRedundant() const {
3675 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
3676 }
3677
3678 // We clear both operands to indicate move that's been eliminated.
3679 void Eliminate() {
3680 source_ = destination_ = Location::NoLocation();
3681 }
3682
3683 bool IsEliminated() const {
3684 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
3685 return source_.IsInvalid();
3686 }
3687
Nicolas Geoffray90218252015-04-15 11:56:51 +01003688 bool Is64BitMove() const {
3689 return Primitive::Is64BitType(type_);
3690 }
3691
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003692 HInstruction* GetInstruction() const { return instruction_; }
3693
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003694 private:
3695 Location source_;
3696 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01003697 // The type this move is for.
3698 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003699 // The instruction this move is assocatied with. Null when this move is
3700 // for moving an input in the expected locations of user (including a phi user).
3701 // This is only used in debug mode, to ensure we do not connect interval siblings
3702 // in the same parallel move.
3703 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003704};
3705
3706static constexpr size_t kDefaultNumberOfMoves = 4;
3707
3708class HParallelMove : public HTemplateInstruction<0> {
3709 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003710 explicit HParallelMove(ArenaAllocator* arena)
3711 : HTemplateInstruction(SideEffects::None()), moves_(arena, kDefaultNumberOfMoves) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003712
Nicolas Geoffray90218252015-04-15 11:56:51 +01003713 void AddMove(Location source,
3714 Location destination,
3715 Primitive::Type type,
3716 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003717 DCHECK(source.IsValid());
3718 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003719 if (kIsDebugBuild) {
3720 if (instruction != nullptr) {
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00003721 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003722 if (moves_.Get(i).GetInstruction() == instruction) {
3723 // Special case the situation where the move is for the spill slot
3724 // of the instruction.
3725 if ((GetPrevious() == instruction)
3726 || ((GetPrevious() == nullptr)
3727 && instruction->IsPhi()
3728 && instruction->GetBlock() == GetBlock())) {
3729 DCHECK_NE(destination.GetKind(), moves_.Get(i).GetDestination().GetKind())
3730 << "Doing parallel moves for the same instruction.";
3731 } else {
3732 DCHECK(false) << "Doing parallel moves for the same instruction.";
3733 }
3734 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00003735 }
3736 }
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003737 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Zheng Xuad4450e2015-04-17 18:48:56 +08003738 DCHECK(!destination.OverlapsWith(moves_.Get(i).GetDestination()))
3739 << "Overlapped destination for two moves in a parallel move.";
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003740 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003741 }
Nicolas Geoffray90218252015-04-15 11:56:51 +01003742 moves_.Add(MoveOperands(source, destination, type, instruction));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003743 }
3744
3745 MoveOperands* MoveOperandsAt(size_t index) const {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003746 return moves_.GetRawStorage() + index;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003747 }
3748
3749 size_t NumMoves() const { return moves_.Size(); }
3750
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003751 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003752
3753 private:
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003754 GrowableArray<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003755
3756 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
3757};
3758
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003759class HGraphVisitor : public ValueObject {
3760 public:
Dave Allison20dfc792014-06-16 20:44:29 -07003761 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
3762 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003763
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003764 virtual void VisitInstruction(HInstruction* instruction) { UNUSED(instruction); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003765 virtual void VisitBasicBlock(HBasicBlock* block);
3766
Roland Levillain633021e2014-10-01 14:12:25 +01003767 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003768 void VisitInsertionOrder();
3769
Roland Levillain633021e2014-10-01 14:12:25 +01003770 // Visit the graph following dominator tree reverse post-order.
3771 void VisitReversePostOrder();
3772
Nicolas Geoffray787c3072014-03-17 10:20:19 +00003773 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00003774
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003775 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003776#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003777 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
3778
3779 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
3780
3781#undef DECLARE_VISIT_INSTRUCTION
3782
3783 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07003784 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003785
3786 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
3787};
3788
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003789class HGraphDelegateVisitor : public HGraphVisitor {
3790 public:
3791 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
3792 virtual ~HGraphDelegateVisitor() {}
3793
3794 // Visit functions that delegate to to super class.
3795#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003796 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003797
3798 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
3799
3800#undef DECLARE_VISIT_INSTRUCTION
3801
3802 private:
3803 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
3804};
3805
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003806class HInsertionOrderIterator : public ValueObject {
3807 public:
3808 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
3809
3810 bool Done() const { return index_ == graph_.GetBlocks().Size(); }
3811 HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); }
3812 void Advance() { ++index_; }
3813
3814 private:
3815 const HGraph& graph_;
3816 size_t index_;
3817
3818 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
3819};
3820
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003821class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003822 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00003823 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
3824 // Check that reverse post order of the graph has been built.
3825 DCHECK(!graph.GetReversePostOrder().IsEmpty());
3826 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003827
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003828 bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); }
3829 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003830 void Advance() { ++index_; }
3831
3832 private:
3833 const HGraph& graph_;
3834 size_t index_;
3835
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003836 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003837};
3838
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003839class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003840 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003841 explicit HPostOrderIterator(const HGraph& graph)
David Brazdil10f56cb2015-03-24 18:49:14 +00003842 : graph_(graph), index_(graph_.GetReversePostOrder().Size()) {
3843 // Check that reverse post order of the graph has been built.
3844 DCHECK(!graph.GetReversePostOrder().IsEmpty());
3845 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003846
3847 bool Done() const { return index_ == 0; }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003848 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003849 void Advance() { --index_; }
3850
3851 private:
3852 const HGraph& graph_;
3853 size_t index_;
3854
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003855 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003856};
3857
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01003858class HLinearPostOrderIterator : public ValueObject {
3859 public:
3860 explicit HLinearPostOrderIterator(const HGraph& graph)
3861 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().Size()) {}
3862
3863 bool Done() const { return index_ == 0; }
3864
3865 HBasicBlock* Current() const { return order_.Get(index_ -1); }
3866
3867 void Advance() {
3868 --index_;
3869 DCHECK_GE(index_, 0U);
3870 }
3871
3872 private:
3873 const GrowableArray<HBasicBlock*>& order_;
3874 size_t index_;
3875
3876 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
3877};
3878
3879class HLinearOrderIterator : public ValueObject {
3880 public:
3881 explicit HLinearOrderIterator(const HGraph& graph)
3882 : order_(graph.GetLinearOrder()), index_(0) {}
3883
3884 bool Done() const { return index_ == order_.Size(); }
3885 HBasicBlock* Current() const { return order_.Get(index_); }
3886 void Advance() { ++index_; }
3887
3888 private:
3889 const GrowableArray<HBasicBlock*>& order_;
3890 size_t index_;
3891
3892 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
3893};
3894
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003895// Iterator over the blocks that art part of the loop. Includes blocks part
3896// of an inner loop. The order in which the blocks are iterated is on their
3897// block id.
3898class HBlocksInLoopIterator : public ValueObject {
3899 public:
3900 explicit HBlocksInLoopIterator(const HLoopInformation& info)
3901 : blocks_in_loop_(info.GetBlocks()),
3902 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
3903 index_(0) {
3904 if (!blocks_in_loop_.IsBitSet(index_)) {
3905 Advance();
3906 }
3907 }
3908
3909 bool Done() const { return index_ == blocks_.Size(); }
3910 HBasicBlock* Current() const { return blocks_.Get(index_); }
3911 void Advance() {
3912 ++index_;
3913 for (size_t e = blocks_.Size(); index_ < e; ++index_) {
3914 if (blocks_in_loop_.IsBitSet(index_)) {
3915 break;
3916 }
3917 }
3918 }
3919
3920 private:
3921 const BitVector& blocks_in_loop_;
3922 const GrowableArray<HBasicBlock*>& blocks_;
3923 size_t index_;
3924
3925 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
3926};
3927
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003928inline int64_t Int64FromConstant(HConstant* constant) {
3929 DCHECK(constant->IsIntConstant() || constant->IsLongConstant());
3930 return constant->IsIntConstant() ? constant->AsIntConstant()->GetValue()
3931 : constant->AsLongConstant()->GetValue();
3932}
3933
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003934} // namespace art
3935
3936#endif // ART_COMPILER_OPTIMIZING_NODES_H_