blob: cc76d9ca85d5dc48c2b678b9788da9d0f5a8f6a2 [file] [log] [blame]
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ART_COMPILER_OPTIMIZING_NODES_H_
18#define ART_COMPILER_OPTIMIZING_NODES_H_
19
David Brazdil8d5b8b22015-03-24 10:51:52 +000020#include "base/arena_containers.h"
Mathieu Chartierb666f482015-02-18 14:33:14 -080021#include "base/arena_object.h"
Calin Juravle27df7582015-04-17 19:12:31 +010022#include "dex/compiler_enums.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000023#include "entrypoints/quick/quick_entrypoints_enum.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000024#include "handle.h"
25#include "handle_scope.h"
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000026#include "invoke_type.h"
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +010027#include "locations.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000028#include "mirror/class.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010029#include "offsets.h"
Ian Rogerse63db272014-07-15 15:36:11 -070030#include "primitive.h"
Nicolas Geoffray0e336432014-02-26 18:24:38 +000031#include "utils/arena_bit_vector.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000032#include "utils/growable_array.h"
33
34namespace art {
35
David Brazdil1abb4192015-02-17 18:33:36 +000036class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000037class HBasicBlock;
David Brazdil8d5b8b22015-03-24 10:51:52 +000038class HDoubleConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010039class HEnvironment;
David Brazdil8d5b8b22015-03-24 10:51:52 +000040class HFloatConstant;
41class HGraphVisitor;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000042class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000043class HIntConstant;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000044class HInvoke;
David Brazdil8d5b8b22015-03-24 10:51:52 +000045class HLongConstant;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +000046class HNullConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010047class HPhi;
Nicolas Geoffray3c049742014-09-24 18:10:46 +010048class HSuspendCheck;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010049class LiveInterval;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000050class LocationSummary;
David Brazdil8d5b8b22015-03-24 10:51:52 +000051class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000052
53static const int kDefaultNumberOfBlocks = 8;
54static const int kDefaultNumberOfSuccessors = 2;
55static const int kDefaultNumberOfPredecessors = 2;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010056static const int kDefaultNumberOfDominatedBlocks = 1;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000057static const int kDefaultNumberOfBackEdges = 1;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000058
Calin Juravle9aec02f2014-11-18 23:06:35 +000059static constexpr uint32_t kMaxIntShiftValue = 0x1f;
60static constexpr uint64_t kMaxLongShiftValue = 0x3f;
61
Dave Allison20dfc792014-06-16 20:44:29 -070062enum IfCondition {
63 kCondEQ,
64 kCondNE,
65 kCondLT,
66 kCondLE,
67 kCondGT,
68 kCondGE,
69};
70
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010071class HInstructionList {
72 public:
73 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
74
75 void AddInstruction(HInstruction* instruction);
76 void RemoveInstruction(HInstruction* instruction);
77
David Brazdilc3d743f2015-04-22 13:40:50 +010078 // Insert `instruction` before/after an existing instruction `cursor`.
79 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
80 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
81
Roland Levillain6b469232014-09-25 10:10:38 +010082 // Return true if this list contains `instruction`.
83 bool Contains(HInstruction* instruction) const;
84
Roland Levillainccc07a92014-09-16 14:48:16 +010085 // Return true if `instruction1` is found before `instruction2` in
86 // this instruction list and false otherwise. Abort if none
87 // of these instructions is found.
88 bool FoundBefore(const HInstruction* instruction1,
89 const HInstruction* instruction2) const;
90
Nicolas Geoffray276d9da2015-02-02 18:24:11 +000091 bool IsEmpty() const { return first_instruction_ == nullptr; }
92 void Clear() { first_instruction_ = last_instruction_ = nullptr; }
93
94 // Update the block of all instructions to be `block`.
95 void SetBlockOfInstructions(HBasicBlock* block) const;
96
97 void AddAfter(HInstruction* cursor, const HInstructionList& instruction_list);
98 void Add(const HInstructionList& instruction_list);
99
David Brazdil2d7352b2015-04-20 14:52:42 +0100100 // Return the number of instructions in the list. This is an expensive operation.
101 size_t CountSize() const;
102
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100103 private:
104 HInstruction* first_instruction_;
105 HInstruction* last_instruction_;
106
107 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000108 friend class HGraph;
109 friend class HInstruction;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100110 friend class HInstructionIterator;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100111 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100112
113 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
114};
115
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000116// Control-flow graph of a method. Contains a list of basic blocks.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700117class HGraph : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000118 public:
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000119 HGraph(ArenaAllocator* arena, bool debuggable = false, int start_instruction_id = 0)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000120 : arena_(arena),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000121 blocks_(arena, kDefaultNumberOfBlocks),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100122 reverse_post_order_(arena, kDefaultNumberOfBlocks),
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100123 linear_order_(arena, kDefaultNumberOfBlocks),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700124 entry_block_(nullptr),
125 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100126 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100127 number_of_vregs_(0),
128 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000129 temporaries_vreg_slots_(0),
Mark Mendell1152c922015-04-24 17:06:35 -0400130 has_bounds_checks_(false),
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000131 debuggable_(debuggable),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000132 current_instruction_id_(start_instruction_id),
133 cached_null_constant_(nullptr),
134 cached_int_constants_(std::less<int32_t>(), arena->Adapter()),
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000135 cached_float_constants_(std::less<int32_t>(), arena->Adapter()),
136 cached_long_constants_(std::less<int64_t>(), arena->Adapter()),
137 cached_double_constants_(std::less<int64_t>(), arena->Adapter()) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000138
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000139 ArenaAllocator* GetArena() const { return arena_; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100140 const GrowableArray<HBasicBlock*>& GetBlocks() const { return blocks_; }
Roland Levillain93445682014-10-06 19:24:02 +0100141 HBasicBlock* GetBlock(size_t id) const { return blocks_.Get(id); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000142
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000143 HBasicBlock* GetEntryBlock() const { return entry_block_; }
144 HBasicBlock* GetExitBlock() const { return exit_block_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000145
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000146 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
147 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000148
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000149 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100150
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000151 // Try building the SSA form of this graph, with dominance computation and loop
152 // recognition. Returns whether it was successful in doing all these steps.
153 bool TryBuildingSsa() {
154 BuildDominatorTree();
Nicolas Geoffrayd3350832015-03-12 11:16:23 +0000155 // The SSA builder requires loops to all be natural. Specifically, the dead phi
156 // elimination phase checks the consistency of the graph when doing a post-order
157 // visit for eliminating dead phis: a dead phi can only have loop header phi
158 // users remaining when being visited.
159 if (!AnalyzeNaturalLoops()) return false;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000160 TransformToSsa();
Nicolas Geoffrayd3350832015-03-12 11:16:23 +0000161 return true;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000162 }
163
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000164 void BuildDominatorTree();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000165 void TransformToSsa();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100166 void SimplifyCFG();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000167
Nicolas Geoffrayf5370122014-12-02 11:51:19 +0000168 // Analyze all natural loops in this graph. Returns false if one
169 // loop is not natural, that is the header does not dominate the
170 // back edge.
171 bool AnalyzeNaturalLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100172
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000173 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
174 void InlineInto(HGraph* outer_graph, HInvoke* invoke);
175
David Brazdil2d7352b2015-04-20 14:52:42 +0100176 // Removes `block` from the graph.
177 void DeleteDeadBlock(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000178
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100179 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
180 void SimplifyLoop(HBasicBlock* header);
181
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000182 int32_t GetNextInstructionId() {
183 DCHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000184 return current_instruction_id_++;
185 }
186
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000187 int32_t GetCurrentInstructionId() const {
188 return current_instruction_id_;
189 }
190
191 void SetCurrentInstructionId(int32_t id) {
192 current_instruction_id_ = id;
193 }
194
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100195 uint16_t GetMaximumNumberOfOutVRegs() const {
196 return maximum_number_of_out_vregs_;
197 }
198
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000199 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
200 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100201 }
202
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000203 void UpdateTemporariesVRegSlots(size_t slots) {
204 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100205 }
206
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000207 size_t GetTemporariesVRegSlots() const {
208 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100209 }
210
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100211 void SetNumberOfVRegs(uint16_t number_of_vregs) {
212 number_of_vregs_ = number_of_vregs;
213 }
214
215 uint16_t GetNumberOfVRegs() const {
216 return number_of_vregs_;
217 }
218
219 void SetNumberOfInVRegs(uint16_t value) {
220 number_of_in_vregs_ = value;
221 }
222
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100223 uint16_t GetNumberOfLocalVRegs() const {
224 return number_of_vregs_ - number_of_in_vregs_;
225 }
226
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100227 const GrowableArray<HBasicBlock*>& GetReversePostOrder() const {
228 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100229 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100230
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100231 const GrowableArray<HBasicBlock*>& GetLinearOrder() const {
232 return linear_order_;
233 }
234
Mark Mendell1152c922015-04-24 17:06:35 -0400235 bool HasBoundsChecks() const {
236 return has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800237 }
238
Mark Mendell1152c922015-04-24 17:06:35 -0400239 void SetHasBoundsChecks(bool value) {
240 has_bounds_checks_ = value;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800241 }
242
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000243 bool IsDebuggable() const { return debuggable_; }
244
David Brazdil8d5b8b22015-03-24 10:51:52 +0000245 // Returns a constant of the given type and value. If it does not exist
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000246 // already, it is created and inserted into the graph. This method is only for
247 // integral types.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000248 HConstant* GetConstant(Primitive::Type type, int64_t value);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000249 HNullConstant* GetNullConstant();
David Brazdil8d5b8b22015-03-24 10:51:52 +0000250 HIntConstant* GetIntConstant(int32_t value) {
251 return CreateConstant(value, &cached_int_constants_);
252 }
253 HLongConstant* GetLongConstant(int64_t value) {
254 return CreateConstant(value, &cached_long_constants_);
255 }
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000256 HFloatConstant* GetFloatConstant(float value) {
257 return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_);
258 }
259 HDoubleConstant* GetDoubleConstant(double value) {
260 return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_);
261 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000262
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000263 HBasicBlock* FindCommonDominator(HBasicBlock* first, HBasicBlock* second) const;
David Brazdil2d7352b2015-04-20 14:52:42 +0100264
265 private:
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000266 void VisitBlockForDominatorTree(HBasicBlock* block,
267 HBasicBlock* predecessor,
268 GrowableArray<size_t>* visits);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100269 void FindBackEdges(ArenaBitVector* visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000270 void VisitBlockForBackEdges(HBasicBlock* block,
271 ArenaBitVector* visited,
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100272 ArenaBitVector* visiting);
Roland Levillainfc600dc2014-12-02 17:16:31 +0000273 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100274 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000275
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000276 template <class InstructionType, typename ValueType>
277 InstructionType* CreateConstant(ValueType value,
278 ArenaSafeMap<ValueType, InstructionType*>* cache) {
279 // Try to find an existing constant of the given value.
280 InstructionType* constant = nullptr;
281 auto cached_constant = cache->find(value);
282 if (cached_constant != cache->end()) {
283 constant = cached_constant->second;
284 }
285
286 // If not found or previously deleted, create and cache a new instruction.
287 if (constant == nullptr || constant->GetBlock() == nullptr) {
288 constant = new (arena_) InstructionType(value);
289 cache->Overwrite(value, constant);
290 InsertConstant(constant);
291 }
292 return constant;
293 }
294
David Brazdil8d5b8b22015-03-24 10:51:52 +0000295 void InsertConstant(HConstant* instruction);
296
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000297 // Cache a float constant into the graph. This method should only be
298 // called by the SsaBuilder when creating "equivalent" instructions.
299 void CacheFloatConstant(HFloatConstant* constant);
300
301 // See CacheFloatConstant comment.
302 void CacheDoubleConstant(HDoubleConstant* constant);
303
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000304 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000305
306 // List of blocks in insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000307 GrowableArray<HBasicBlock*> blocks_;
308
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100309 // List of blocks to perform a reverse post order tree traversal.
310 GrowableArray<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000311
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100312 // List of blocks to perform a linear order tree traversal.
313 GrowableArray<HBasicBlock*> linear_order_;
314
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000315 HBasicBlock* entry_block_;
316 HBasicBlock* exit_block_;
317
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100318 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100319 uint16_t maximum_number_of_out_vregs_;
320
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100321 // The number of virtual registers in this method. Contains the parameters.
322 uint16_t number_of_vregs_;
323
324 // The number of virtual registers used by parameters of this method.
325 uint16_t number_of_in_vregs_;
326
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000327 // Number of vreg size slots that the temporaries use (used in baseline compiler).
328 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100329
Mark Mendell1152c922015-04-24 17:06:35 -0400330 // Has bounds checks. We can totally skip BCE if it's false.
331 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800332
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000333 // Indicates whether the graph should be compiled in a way that
334 // ensures full debuggability. If false, we can apply more
335 // aggressive optimizations that may limit the level of debugging.
336 const bool debuggable_;
337
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000338 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000339 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000340
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000341 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000342 HNullConstant* cached_null_constant_;
343 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000344 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000345 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000346 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000347
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000348 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100349 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000350 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000351 DISALLOW_COPY_AND_ASSIGN(HGraph);
352};
353
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700354class HLoopInformation : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000355 public:
356 HLoopInformation(HBasicBlock* header, HGraph* graph)
357 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100358 suspend_check_(nullptr),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100359 back_edges_(graph->GetArena(), kDefaultNumberOfBackEdges),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100360 // Make bit vector growable, as the number of blocks may change.
361 blocks_(graph->GetArena(), graph->GetBlocks().Size(), true) {}
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100362
363 HBasicBlock* GetHeader() const {
364 return header_;
365 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000366
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000367 void SetHeader(HBasicBlock* block) {
368 header_ = block;
369 }
370
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100371 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
372 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
373 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
374
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000375 void AddBackEdge(HBasicBlock* back_edge) {
376 back_edges_.Add(back_edge);
377 }
378
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100379 void RemoveBackEdge(HBasicBlock* back_edge) {
380 back_edges_.Delete(back_edge);
381 }
382
David Brazdil46e2a392015-03-16 17:31:52 +0000383 bool IsBackEdge(const HBasicBlock& block) const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100384 for (size_t i = 0, e = back_edges_.Size(); i < e; ++i) {
David Brazdil46e2a392015-03-16 17:31:52 +0000385 if (back_edges_.Get(i) == &block) return true;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100386 }
387 return false;
388 }
389
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000390 size_t NumberOfBackEdges() const {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000391 return back_edges_.Size();
392 }
393
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100394 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100395
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100396 const GrowableArray<HBasicBlock*>& GetBackEdges() const {
397 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100398 }
399
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100400 void ClearBackEdges() {
401 back_edges_.Reset();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100402 }
403
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100404 // Find blocks that are part of this loop. Returns whether the loop is a natural loop,
405 // that is the header dominates the back edge.
406 bool Populate();
407
408 // Returns whether this loop information contains `block`.
409 // Note that this loop information *must* be populated before entering this function.
410 bool Contains(const HBasicBlock& block) const;
411
412 // Returns whether this loop information is an inner loop of `other`.
413 // Note that `other` *must* be populated before entering this function.
414 bool IsIn(const HLoopInformation& other) const;
415
416 const ArenaBitVector& GetBlocks() const { return blocks_; }
417
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000418 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000419 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000420
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000421 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100422 // Internal recursive implementation of `Populate`.
423 void PopulateRecursive(HBasicBlock* block);
424
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000425 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100426 HSuspendCheck* suspend_check_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000427 GrowableArray<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100428 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000429
430 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
431};
432
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100433static constexpr size_t kNoLifetime = -1;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100434static constexpr uint32_t kNoDexPc = -1;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100435
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000436// A block in a method. Contains the list of instructions represented
437// as a double linked list. Each block knows its predecessors and
438// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100439
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700440class HBasicBlock : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000441 public:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100442 explicit HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000443 : graph_(graph),
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000444 predecessors_(graph->GetArena(), kDefaultNumberOfPredecessors),
445 successors_(graph->GetArena(), kDefaultNumberOfSuccessors),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000446 loop_information_(nullptr),
447 dominator_(nullptr),
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100448 dominated_blocks_(graph->GetArena(), kDefaultNumberOfDominatedBlocks),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100449 block_id_(-1),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100450 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100451 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000452 lifetime_end_(kNoLifetime),
453 is_catch_block_(false) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000454
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100455 const GrowableArray<HBasicBlock*>& GetPredecessors() const {
456 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000457 }
458
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100459 const GrowableArray<HBasicBlock*>& GetSuccessors() const {
460 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000461 }
462
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100463 const GrowableArray<HBasicBlock*>& GetDominatedBlocks() const {
464 return dominated_blocks_;
465 }
466
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100467 bool IsEntryBlock() const {
468 return graph_->GetEntryBlock() == this;
469 }
470
471 bool IsExitBlock() const {
472 return graph_->GetExitBlock() == this;
473 }
474
David Brazdil46e2a392015-03-16 17:31:52 +0000475 bool IsSingleGoto() const;
476
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000477 void AddBackEdge(HBasicBlock* back_edge) {
478 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000479 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000480 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100481 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000482 loop_information_->AddBackEdge(back_edge);
483 }
484
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000485 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000486 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000487
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000488 int GetBlockId() const { return block_id_; }
489 void SetBlockId(int id) { block_id_ = id; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000490
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000491 HBasicBlock* GetDominator() const { return dominator_; }
492 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100493 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.Add(block); }
David Brazdil2d7352b2015-04-20 14:52:42 +0100494 void RemoveDominatedBlock(HBasicBlock* block) { dominated_blocks_.Delete(block); }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000495 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
496 for (size_t i = 0, e = dominated_blocks_.Size(); i < e; ++i) {
497 if (dominated_blocks_.Get(i) == existing) {
498 dominated_blocks_.Put(i, new_block);
499 return;
500 }
501 }
502 LOG(FATAL) << "Unreachable";
503 UNREACHABLE();
504 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000505
506 int NumberOfBackEdges() const {
507 return loop_information_ == nullptr
508 ? 0
509 : loop_information_->NumberOfBackEdges();
510 }
511
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100512 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
513 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100514 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100515 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +0100516 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
517 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000518
519 void AddSuccessor(HBasicBlock* block) {
520 successors_.Add(block);
521 block->predecessors_.Add(this);
522 }
523
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100524 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
525 size_t successor_index = GetSuccessorIndexOf(existing);
526 DCHECK_NE(successor_index, static_cast<size_t>(-1));
527 existing->RemovePredecessor(this);
528 new_block->predecessors_.Add(this);
529 successors_.Put(successor_index, new_block);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000530 }
531
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000532 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
533 size_t predecessor_index = GetPredecessorIndexOf(existing);
534 DCHECK_NE(predecessor_index, static_cast<size_t>(-1));
535 existing->RemoveSuccessor(this);
536 new_block->successors_.Add(this);
537 predecessors_.Put(predecessor_index, new_block);
538 }
539
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100540 void RemovePredecessor(HBasicBlock* block) {
541 predecessors_.Delete(block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100542 }
543
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000544 void RemoveSuccessor(HBasicBlock* block) {
545 successors_.Delete(block);
546 }
547
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100548 void ClearAllPredecessors() {
549 predecessors_.Reset();
550 }
551
552 void AddPredecessor(HBasicBlock* block) {
553 predecessors_.Add(block);
554 block->successors_.Add(this);
555 }
556
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100557 void SwapPredecessors() {
Nicolas Geoffrayc83d4412014-09-18 16:46:20 +0100558 DCHECK_EQ(predecessors_.Size(), 2u);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100559 HBasicBlock* temp = predecessors_.Get(0);
560 predecessors_.Put(0, predecessors_.Get(1));
561 predecessors_.Put(1, temp);
562 }
563
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100564 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) {
565 for (size_t i = 0, e = predecessors_.Size(); i < e; ++i) {
566 if (predecessors_.Get(i) == predecessor) {
567 return i;
568 }
569 }
570 return -1;
571 }
572
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100573 size_t GetSuccessorIndexOf(HBasicBlock* successor) {
574 for (size_t i = 0, e = successors_.Size(); i < e; ++i) {
575 if (successors_.Get(i) == successor) {
576 return i;
577 }
578 }
579 return -1;
580 }
581
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000582 // Split the block into two blocks just after `cursor`. Returns the newly
583 // created block. Note that this method just updates raw block information,
584 // like predecessors, successors, dominators, and instruction list. It does not
585 // update the graph, reverse post order, loop information, nor make sure the
586 // blocks are consistent (for example ending with a control flow instruction).
587 HBasicBlock* SplitAfter(HInstruction* cursor);
588
589 // Merge `other` at the end of `this`. Successors and dominated blocks of
590 // `other` are changed to be successors and dominated blocks of `this`. Note
591 // that this method does not update the graph, reverse post order, loop
592 // information, nor make sure the blocks are consistent (for example ending
593 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +0100594 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000595
596 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
597 // of `this` are moved to `other`.
598 // Note that this method does not update the graph, reverse post order, loop
599 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +0000600 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000601 void ReplaceWith(HBasicBlock* other);
602
David Brazdil2d7352b2015-04-20 14:52:42 +0100603 // Merge `other` at the end of `this`. This method updates loops, reverse post
604 // order, links to predecessors, successors, dominators and deletes the block
605 // from the graph. The two blocks must be successive, i.e. `this` the only
606 // predecessor of `other` and vice versa.
607 void MergeWith(HBasicBlock* other);
608
609 // Disconnects `this` from all its predecessors, successors and dominator,
610 // removes it from all loops it is included in and eventually from the graph.
611 // The block must not dominate any other block. Predecessors and successors
612 // are safely updated.
613 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +0000614
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000615 void AddInstruction(HInstruction* instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100616 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +0100617 // Replace instruction `initial` with `replacement` within this block.
618 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
619 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100620 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100621 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +0000622 // RemoveInstruction and RemovePhi delete a given instruction from the respective
623 // instruction list. With 'ensure_safety' set to true, it verifies that the
624 // instruction is not in use and removes it from the use lists of its inputs.
625 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
626 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +0100627 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100628
629 bool IsLoopHeader() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100630 return (loop_information_ != nullptr) && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100631 }
632
Roland Levillain6b879dd2014-09-22 17:13:44 +0100633 bool IsLoopPreHeaderFirstPredecessor() const {
634 DCHECK(IsLoopHeader());
635 DCHECK(!GetPredecessors().IsEmpty());
636 return GetPredecessors().Get(0) == GetLoopInformation()->GetPreHeader();
637 }
638
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100639 HLoopInformation* GetLoopInformation() const {
640 return loop_information_;
641 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000642
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000643 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100644 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000645 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100646 void SetInLoop(HLoopInformation* info) {
647 if (IsLoopHeader()) {
648 // Nothing to do. This just means `info` is an outer loop.
649 } else if (loop_information_ == nullptr) {
650 loop_information_ = info;
651 } else if (loop_information_->Contains(*info->GetHeader())) {
652 // Block is currently part of an outer loop. Make it part of this inner loop.
653 // Note that a non loop header having a loop information means this loop information
654 // has already been populated
655 loop_information_ = info;
656 } else {
657 // Block is part of an inner loop. Do not update the loop information.
658 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
659 // at this point, because this method is being called while populating `info`.
660 }
661 }
662
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000663 // Raw update of the loop information.
664 void SetLoopInformation(HLoopInformation* info) {
665 loop_information_ = info;
666 }
667
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100668 bool IsInLoop() const { return loop_information_ != nullptr; }
669
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100670 // Returns wheter this block dominates the blocked passed as parameter.
671 bool Dominates(HBasicBlock* block) const;
672
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100673 size_t GetLifetimeStart() const { return lifetime_start_; }
674 size_t GetLifetimeEnd() const { return lifetime_end_; }
675
676 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
677 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
678
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100679 uint32_t GetDexPc() const { return dex_pc_; }
680
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000681 bool IsCatchBlock() const { return is_catch_block_; }
682 void SetIsCatchBlock() { is_catch_block_ = true; }
683
David Brazdil8d5b8b22015-03-24 10:51:52 +0000684 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +0000685 bool EndsWithIf() const;
686 bool HasSinglePhi() const;
687
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000688 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000689 HGraph* graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000690 GrowableArray<HBasicBlock*> predecessors_;
691 GrowableArray<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100692 HInstructionList instructions_;
693 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000694 HLoopInformation* loop_information_;
695 HBasicBlock* dominator_;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100696 GrowableArray<HBasicBlock*> dominated_blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000697 int block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100698 // The dex program counter of the first instruction of this block.
699 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100700 size_t lifetime_start_;
701 size_t lifetime_end_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000702 bool is_catch_block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000703
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000704 friend class HGraph;
705 friend class HInstruction;
706
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000707 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
708};
709
David Brazdilb2bd1c52015-03-25 11:17:37 +0000710// Iterates over the LoopInformation of all loops which contain 'block'
711// from the innermost to the outermost.
712class HLoopInformationOutwardIterator : public ValueObject {
713 public:
714 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
715 : current_(block.GetLoopInformation()) {}
716
717 bool Done() const { return current_ == nullptr; }
718
719 void Advance() {
720 DCHECK(!Done());
721 current_ = current_->GetHeader()->GetDominator()->GetLoopInformation();
722 }
723
724 HLoopInformation* Current() const {
725 DCHECK(!Done());
726 return current_;
727 }
728
729 private:
730 HLoopInformation* current_;
731
732 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
733};
734
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100735#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
736 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000737 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000738 M(ArrayGet, Instruction) \
739 M(ArrayLength, Instruction) \
740 M(ArraySet, Instruction) \
David Brazdil66d126e2015-04-03 16:02:44 +0100741 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000742 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +0000743 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000744 M(CheckCast, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100745 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000746 M(Compare, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100747 M(Condition, BinaryOperation) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700748 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000749 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +0000750 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000751 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100752 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000753 M(Exit, Instruction) \
754 M(FloatConstant, Constant) \
755 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100756 M(GreaterThan, Condition) \
757 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100758 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000759 M(InstanceFieldGet, Instruction) \
760 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000761 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100762 M(IntConstant, Constant) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +0000763 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000764 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100765 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000766 M(LessThan, Condition) \
767 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000768 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000769 M(LoadException, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100770 M(LoadLocal, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000771 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100772 M(Local, Instruction) \
773 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +0100774 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +0000775 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000776 M(Mul, BinaryOperation) \
777 M(Neg, UnaryOperation) \
778 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100779 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +0100780 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000781 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000782 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000783 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000784 M(Or, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100785 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000786 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100787 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000788 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100789 M(Return, Instruction) \
790 M(ReturnVoid, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000791 M(Shl, BinaryOperation) \
792 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100793 M(StaticFieldGet, Instruction) \
794 M(StaticFieldSet, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100795 M(StoreLocal, Instruction) \
796 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100797 M(SuspendCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000798 M(Temporary, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000799 M(Throw, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +0000800 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000801 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000802 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000803
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100804#define FOR_EACH_INSTRUCTION(M) \
805 FOR_EACH_CONCRETE_INSTRUCTION(M) \
806 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +0100807 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +0100808 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100809 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -0700810
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100811#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000812FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
813#undef FORWARD_DECLARATION
814
Roland Levillainccc07a92014-09-16 14:48:16 +0100815#define DECLARE_INSTRUCTION(type) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000816 InstructionKind GetKind() const OVERRIDE { return k##type; } \
817 const char* DebugName() const OVERRIDE { return #type; } \
818 const H##type* As##type() const OVERRIDE { return this; } \
819 H##type* As##type() OVERRIDE { return this; } \
820 bool InstructionTypeEquals(HInstruction* other) const OVERRIDE { \
Roland Levillainccc07a92014-09-16 14:48:16 +0100821 return other->Is##type(); \
822 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000823 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000824
David Brazdiled596192015-01-23 10:39:45 +0000825template <typename T> class HUseList;
826
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100827template <typename T>
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700828class HUseListNode : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000829 public:
David Brazdiled596192015-01-23 10:39:45 +0000830 HUseListNode* GetPrevious() const { return prev_; }
831 HUseListNode* GetNext() const { return next_; }
832 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100833 size_t GetIndex() const { return index_; }
834
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000835 private:
David Brazdiled596192015-01-23 10:39:45 +0000836 HUseListNode(T user, size_t index)
837 : user_(user), index_(index), prev_(nullptr), next_(nullptr) {}
838
839 T const user_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100840 const size_t index_;
David Brazdiled596192015-01-23 10:39:45 +0000841 HUseListNode<T>* prev_;
842 HUseListNode<T>* next_;
843
844 friend class HUseList<T>;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000845
846 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
847};
848
David Brazdiled596192015-01-23 10:39:45 +0000849template <typename T>
850class HUseList : public ValueObject {
851 public:
852 HUseList() : first_(nullptr) {}
853
854 void Clear() {
855 first_ = nullptr;
856 }
857
858 // Adds a new entry at the beginning of the use list and returns
859 // the newly created node.
860 HUseListNode<T>* AddUse(T user, size_t index, ArenaAllocator* arena) {
David Brazdilea55b932015-01-27 17:12:29 +0000861 HUseListNode<T>* new_node = new (arena) HUseListNode<T>(user, index);
David Brazdiled596192015-01-23 10:39:45 +0000862 if (IsEmpty()) {
863 first_ = new_node;
864 } else {
865 first_->prev_ = new_node;
866 new_node->next_ = first_;
867 first_ = new_node;
868 }
869 return new_node;
870 }
871
872 HUseListNode<T>* GetFirst() const {
873 return first_;
874 }
875
876 void Remove(HUseListNode<T>* node) {
David Brazdil1abb4192015-02-17 18:33:36 +0000877 DCHECK(node != nullptr);
878 DCHECK(Contains(node));
879
David Brazdiled596192015-01-23 10:39:45 +0000880 if (node->prev_ != nullptr) {
881 node->prev_->next_ = node->next_;
882 }
883 if (node->next_ != nullptr) {
884 node->next_->prev_ = node->prev_;
885 }
886 if (node == first_) {
887 first_ = node->next_;
888 }
889 }
890
David Brazdil1abb4192015-02-17 18:33:36 +0000891 bool Contains(const HUseListNode<T>* node) const {
892 if (node == nullptr) {
893 return false;
894 }
895 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
896 if (current == node) {
897 return true;
898 }
899 }
900 return false;
901 }
902
David Brazdiled596192015-01-23 10:39:45 +0000903 bool IsEmpty() const {
904 return first_ == nullptr;
905 }
906
907 bool HasOnlyOneUse() const {
908 return first_ != nullptr && first_->next_ == nullptr;
909 }
910
911 private:
912 HUseListNode<T>* first_;
913};
914
915template<typename T>
916class HUseIterator : public ValueObject {
917 public:
918 explicit HUseIterator(const HUseList<T>& uses) : current_(uses.GetFirst()) {}
919
920 bool Done() const { return current_ == nullptr; }
921
922 void Advance() {
923 DCHECK(!Done());
924 current_ = current_->GetNext();
925 }
926
927 HUseListNode<T>* Current() const {
928 DCHECK(!Done());
929 return current_;
930 }
931
932 private:
933 HUseListNode<T>* current_;
934
935 friend class HValue;
936};
937
David Brazdil1abb4192015-02-17 18:33:36 +0000938// This class is used by HEnvironment and HInstruction classes to record the
939// instructions they use and pointers to the corresponding HUseListNodes kept
940// by the used instructions.
941template <typename T>
942class HUserRecord : public ValueObject {
943 public:
944 HUserRecord() : instruction_(nullptr), use_node_(nullptr) {}
945 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), use_node_(nullptr) {}
946
947 HUserRecord(const HUserRecord<T>& old_record, HUseListNode<T>* use_node)
948 : instruction_(old_record.instruction_), use_node_(use_node) {
949 DCHECK(instruction_ != nullptr);
950 DCHECK(use_node_ != nullptr);
951 DCHECK(old_record.use_node_ == nullptr);
952 }
953
954 HInstruction* GetInstruction() const { return instruction_; }
955 HUseListNode<T>* GetUseNode() const { return use_node_; }
956
957 private:
958 // Instruction used by the user.
959 HInstruction* instruction_;
960
961 // Corresponding entry in the use list kept by 'instruction_'.
962 HUseListNode<T>* use_node_;
963};
964
Calin Juravle27df7582015-04-17 19:12:31 +0100965// TODO: Add better documentation to this class and maybe refactor with more suggestive names.
966// - Has(All)SideEffects suggests that all the side effects are present but only ChangesSomething
967// flag is consider.
968// - DependsOn suggests that there is a real dependency between side effects but it only
969// checks DependendsOnSomething flag.
970//
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100971// Represents the side effects an instruction may have.
972class SideEffects : public ValueObject {
973 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100974 SideEffects() : flags_(0) {}
975
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100976 static SideEffects None() {
977 return SideEffects(0);
978 }
979
980 static SideEffects All() {
981 return SideEffects(ChangesSomething().flags_ | DependsOnSomething().flags_);
982 }
983
984 static SideEffects ChangesSomething() {
985 return SideEffects((1 << kFlagChangesCount) - 1);
986 }
987
988 static SideEffects DependsOnSomething() {
989 int count = kFlagDependsOnCount - kFlagChangesCount;
990 return SideEffects(((1 << count) - 1) << kFlagChangesCount);
991 }
992
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100993 SideEffects Union(SideEffects other) const {
994 return SideEffects(flags_ | other.flags_);
995 }
996
Roland Levillain72bceff2014-09-15 18:29:00 +0100997 bool HasSideEffects() const {
998 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
999 return (flags_ & all_bits_set) != 0;
1000 }
1001
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001002 bool HasAllSideEffects() const {
1003 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
1004 return all_bits_set == (flags_ & all_bits_set);
1005 }
1006
1007 bool DependsOn(SideEffects other) const {
1008 size_t depends_flags = other.ComputeDependsFlags();
1009 return (flags_ & depends_flags) != 0;
1010 }
1011
1012 bool HasDependencies() const {
1013 int count = kFlagDependsOnCount - kFlagChangesCount;
1014 size_t all_bits_set = (1 << count) - 1;
1015 return ((flags_ >> kFlagChangesCount) & all_bits_set) != 0;
1016 }
1017
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001018 private:
1019 static constexpr int kFlagChangesSomething = 0;
1020 static constexpr int kFlagChangesCount = kFlagChangesSomething + 1;
1021
1022 static constexpr int kFlagDependsOnSomething = kFlagChangesCount;
1023 static constexpr int kFlagDependsOnCount = kFlagDependsOnSomething + 1;
1024
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001025 explicit SideEffects(size_t flags) : flags_(flags) {}
1026
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001027 size_t ComputeDependsFlags() const {
1028 return flags_ << kFlagChangesCount;
1029 }
1030
1031 size_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001032};
1033
David Brazdiled596192015-01-23 10:39:45 +00001034// A HEnvironment object contains the values of virtual registers at a given location.
1035class HEnvironment : public ArenaObject<kArenaAllocMisc> {
1036 public:
1037 HEnvironment(ArenaAllocator* arena, size_t number_of_vregs)
1038 : vregs_(arena, number_of_vregs) {
1039 vregs_.SetSize(number_of_vregs);
1040 for (size_t i = 0; i < number_of_vregs; i++) {
David Brazdil1abb4192015-02-17 18:33:36 +00001041 vregs_.Put(i, HUserRecord<HEnvironment*>());
David Brazdiled596192015-01-23 10:39:45 +00001042 }
1043 }
1044
1045 void CopyFrom(HEnvironment* env);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001046 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1047 // input to the loop phi instead. This is for inserting instructions that
1048 // require an environment (like HDeoptimization) in the loop pre-header.
1049 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001050
1051 void SetRawEnvAt(size_t index, HInstruction* instruction) {
David Brazdil1abb4192015-02-17 18:33:36 +00001052 vregs_.Put(index, HUserRecord<HEnvironment*>(instruction));
David Brazdiled596192015-01-23 10:39:45 +00001053 }
1054
1055 HInstruction* GetInstructionAt(size_t index) const {
David Brazdil1abb4192015-02-17 18:33:36 +00001056 return vregs_.Get(index).GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001057 }
1058
David Brazdil1abb4192015-02-17 18:33:36 +00001059 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001060
1061 size_t Size() const { return vregs_.Size(); }
1062
1063 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001064 // Record instructions' use entries of this environment for constant-time removal.
1065 // It should only be called by HInstruction when a new environment use is added.
1066 void RecordEnvUse(HUseListNode<HEnvironment*>* env_use) {
1067 DCHECK(env_use->GetUser() == this);
1068 size_t index = env_use->GetIndex();
1069 vregs_.Put(index, HUserRecord<HEnvironment*>(vregs_.Get(index), env_use));
1070 }
David Brazdiled596192015-01-23 10:39:45 +00001071
David Brazdil1abb4192015-02-17 18:33:36 +00001072 GrowableArray<HUserRecord<HEnvironment*> > vregs_;
David Brazdiled596192015-01-23 10:39:45 +00001073
David Brazdil1abb4192015-02-17 18:33:36 +00001074 friend HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001075
1076 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1077};
1078
Calin Juravleacf735c2015-02-12 15:25:22 +00001079class ReferenceTypeInfo : ValueObject {
1080 public:
Calin Juravleb1498f62015-02-16 13:13:29 +00001081 typedef Handle<mirror::Class> TypeHandle;
1082
1083 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact)
1084 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1085 if (type_handle->IsObjectClass()) {
1086 // Override the type handle to be consistent with the case when we get to
1087 // Top but don't have the Object class available. It avoids having to guess
1088 // what value the type_handle has when it's Top.
1089 return ReferenceTypeInfo(TypeHandle(), is_exact, true);
1090 } else {
1091 return ReferenceTypeInfo(type_handle, is_exact, false);
1092 }
1093 }
1094
1095 static ReferenceTypeInfo CreateTop(bool is_exact) {
1096 return ReferenceTypeInfo(TypeHandle(), is_exact, true);
Calin Juravleacf735c2015-02-12 15:25:22 +00001097 }
1098
1099 bool IsExact() const { return is_exact_; }
1100 bool IsTop() const { return is_top_; }
1101
1102 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
1103
Calin Juravleb1498f62015-02-16 13:13:29 +00001104 bool IsSupertypeOf(ReferenceTypeInfo rti) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Calin Juravleacf735c2015-02-12 15:25:22 +00001105 if (IsTop()) {
1106 // Top (equivalent for java.lang.Object) is supertype of anything.
1107 return true;
1108 }
1109 if (rti.IsTop()) {
1110 // If we get here `this` is not Top() so it can't be a supertype.
1111 return false;
1112 }
1113 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
1114 }
1115
1116 // Returns true if the type information provide the same amount of details.
1117 // Note that it does not mean that the instructions have the same actual type
1118 // (e.g. tops are equal but they can be the result of a merge).
1119 bool IsEqual(ReferenceTypeInfo rti) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1120 if (IsExact() != rti.IsExact()) {
1121 return false;
1122 }
1123 if (IsTop() && rti.IsTop()) {
1124 // `Top` means java.lang.Object, so the types are equivalent.
1125 return true;
1126 }
1127 if (IsTop() || rti.IsTop()) {
1128 // If only one is top or object than they are not equivalent.
1129 // NB: We need this extra check because the type_handle of `Top` is invalid
1130 // and we cannot inspect its reference.
1131 return false;
1132 }
1133
1134 // Finally check the types.
1135 return GetTypeHandle().Get() == rti.GetTypeHandle().Get();
1136 }
1137
1138 private:
Calin Juravleb1498f62015-02-16 13:13:29 +00001139 ReferenceTypeInfo() : ReferenceTypeInfo(TypeHandle(), false, true) {}
1140 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact, bool is_top)
1141 : type_handle_(type_handle), is_exact_(is_exact), is_top_(is_top) {}
1142
Calin Juravleacf735c2015-02-12 15:25:22 +00001143 // The class of the object.
Calin Juravleb1498f62015-02-16 13:13:29 +00001144 TypeHandle type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001145 // Whether or not the type is exact or a superclass of the actual type.
Calin Juravleb1498f62015-02-16 13:13:29 +00001146 // Whether or not we have any information about this type.
Calin Juravleacf735c2015-02-12 15:25:22 +00001147 bool is_exact_;
1148 // A true value here means that the object type should be java.lang.Object.
1149 // We don't have access to the corresponding mirror object every time so this
1150 // flag acts as a substitute. When true, the TypeHandle refers to a null
1151 // pointer and should not be used.
1152 bool is_top_;
1153};
1154
1155std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
1156
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001157class HInstruction : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001158 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001159 explicit HInstruction(SideEffects side_effects)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001160 : previous_(nullptr),
1161 next_(nullptr),
1162 block_(nullptr),
1163 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001164 ssa_index_(-1),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001165 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001166 locations_(nullptr),
1167 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001168 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001169 side_effects_(side_effects),
1170 reference_type_info_(ReferenceTypeInfo::CreateTop(/* is_exact */ false)) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001171
Dave Allison20dfc792014-06-16 20:44:29 -07001172 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001173
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001174#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001175 enum InstructionKind {
1176 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1177 };
1178#undef DECLARE_KIND
1179
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001180 HInstruction* GetNext() const { return next_; }
1181 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001182
Calin Juravle77520bc2015-01-12 18:45:46 +00001183 HInstruction* GetNextDisregardingMoves() const;
1184 HInstruction* GetPreviousDisregardingMoves() const;
1185
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001186 HBasicBlock* GetBlock() const { return block_; }
1187 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001188 bool IsInBlock() const { return block_ != nullptr; }
1189 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +01001190 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001191
Roland Levillain6b879dd2014-09-22 17:13:44 +01001192 virtual size_t InputCount() const = 0;
David Brazdil1abb4192015-02-17 18:33:36 +00001193 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001194
1195 virtual void Accept(HGraphVisitor* visitor) = 0;
1196 virtual const char* DebugName() const = 0;
1197
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001198 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
David Brazdil1abb4192015-02-17 18:33:36 +00001199 void SetRawInputAt(size_t index, HInstruction* input) {
1200 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1201 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001202
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001203 virtual bool NeedsEnvironment() const { return false; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001204 virtual bool IsControlFlow() const { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01001205 virtual bool CanThrow() const { return false; }
Roland Levillain72bceff2014-09-15 18:29:00 +01001206 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001207
Calin Juravle10e244f2015-01-26 18:54:32 +00001208 // Does not apply for all instructions, but having this at top level greatly
1209 // simplifies the null check elimination.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001210 virtual bool CanBeNull() const {
1211 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1212 return true;
1213 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001214
Calin Juravle641547a2015-04-21 22:08:51 +01001215 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj) const {
1216 UNUSED(obj);
1217 return false;
1218 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001219
Calin Juravleacf735c2015-02-12 15:25:22 +00001220 void SetReferenceTypeInfo(ReferenceTypeInfo reference_type_info) {
Calin Juravle61d544b2015-02-23 16:46:57 +00001221 DCHECK_EQ(GetType(), Primitive::kPrimNot);
Calin Juravleacf735c2015-02-12 15:25:22 +00001222 reference_type_info_ = reference_type_info;
1223 }
1224
Calin Juravle61d544b2015-02-23 16:46:57 +00001225 ReferenceTypeInfo GetReferenceTypeInfo() const {
1226 DCHECK_EQ(GetType(), Primitive::kPrimNot);
1227 return reference_type_info_;
1228 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001229
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001230 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001231 DCHECK(user != nullptr);
1232 HUseListNode<HInstruction*>* use =
1233 uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1234 user->SetRawInputRecordAt(index, HUserRecord<HInstruction*>(user->InputRecordAt(index), use));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001235 }
1236
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001237 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001238 DCHECK(user != nullptr);
David Brazdiled596192015-01-23 10:39:45 +00001239 HUseListNode<HEnvironment*>* env_use =
1240 env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1241 user->RecordEnvUse(env_use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001242 }
1243
David Brazdil1abb4192015-02-17 18:33:36 +00001244 void RemoveAsUserOfInput(size_t input) {
1245 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
1246 input_use.GetInstruction()->uses_.Remove(input_use.GetUseNode());
1247 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001248
David Brazdil1abb4192015-02-17 18:33:36 +00001249 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1250 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001251
David Brazdiled596192015-01-23 10:39:45 +00001252 bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); }
1253 bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); }
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001254 bool HasNonEnvironmentUses() const { return !uses_.IsEmpty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001255 bool HasOnlyOneNonEnvironmentUse() const {
1256 return !HasEnvironmentUses() && GetUses().HasOnlyOneUse();
1257 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001258
Roland Levillain6c82d402014-10-13 16:10:27 +01001259 // Does this instruction strictly dominate `other_instruction`?
1260 // Returns false if this instruction and `other_instruction` are the same.
1261 // Aborts if this instruction and `other_instruction` are both phis.
1262 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001263
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001264 int GetId() const { return id_; }
1265 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001266
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001267 int GetSsaIndex() const { return ssa_index_; }
1268 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1269 bool HasSsaIndex() const { return ssa_index_ != -1; }
1270
1271 bool HasEnvironment() const { return environment_ != nullptr; }
1272 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001273 // Set the `environment_` field. Raw because this method does not
1274 // update the uses lists.
1275 void SetRawEnvironment(HEnvironment* environment) { environment_ = environment; }
1276
1277 // Set the environment of this instruction, copying it from `environment`. While
1278 // copying, the uses lists are being updated.
1279 void CopyEnvironmentFrom(HEnvironment* environment) {
1280 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
1281 environment_ = new (allocator) HEnvironment(allocator, environment->Size());
1282 environment_->CopyFrom(environment);
1283 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001284
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001285 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
1286 HBasicBlock* block) {
1287 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
1288 environment_ = new (allocator) HEnvironment(allocator, environment->Size());
1289 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
1290 }
1291
Nicolas Geoffray39468442014-09-02 15:17:15 +01001292 // Returns the number of entries in the environment. Typically, that is the
1293 // number of dex registers in a method. It could be more in case of inlining.
1294 size_t EnvironmentSize() const;
1295
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001296 LocationSummary* GetLocations() const { return locations_; }
1297 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001298
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001299 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001300 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001301
Alexandre Rames188d4312015-04-09 18:30:21 +01001302 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
1303 // uses of this instruction by `other` are *not* updated.
1304 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
1305 ReplaceWith(other);
1306 other->ReplaceInput(this, use_index);
1307 }
1308
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001309 // Move `this` instruction before `cursor`.
1310 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001311
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001312#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01001313 bool Is##type() const { return (As##type() != nullptr); } \
1314 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001315 virtual H##type* As##type() { return nullptr; }
1316
1317 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
1318#undef INSTRUCTION_TYPE_CHECK
1319
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001320 // Returns whether the instruction can be moved within the graph.
1321 virtual bool CanBeMoved() const { return false; }
1322
1323 // Returns whether the two instructions are of the same kind.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001324 virtual bool InstructionTypeEquals(HInstruction* other) const {
1325 UNUSED(other);
1326 return false;
1327 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001328
1329 // Returns whether any data encoded in the two instructions is equal.
1330 // This method does not look at the inputs. Both instructions must be
1331 // of the same type, otherwise the method has undefined behavior.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001332 virtual bool InstructionDataEquals(HInstruction* other) const {
1333 UNUSED(other);
1334 return false;
1335 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001336
1337 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00001338 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001339 // 2) Their inputs are identical.
1340 bool Equals(HInstruction* other) const;
1341
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001342 virtual InstructionKind GetKind() const = 0;
1343
1344 virtual size_t ComputeHashCode() const {
1345 size_t result = GetKind();
1346 for (size_t i = 0, e = InputCount(); i < e; ++i) {
1347 result = (result * 31) + InputAt(i)->GetId();
1348 }
1349 return result;
1350 }
1351
1352 SideEffects GetSideEffects() const { return side_effects_; }
1353
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001354 size_t GetLifetimePosition() const { return lifetime_position_; }
1355 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
1356 LiveInterval* GetLiveInterval() const { return live_interval_; }
1357 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
1358 bool HasLiveInterval() const { return live_interval_ != nullptr; }
1359
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001360 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
1361
1362 // Returns whether the code generation of the instruction will require to have access
1363 // to the current method. Such instructions are:
1364 // (1): Instructions that require an environment, as calling the runtime requires
1365 // to walk the stack and have the current method stored at a specific stack address.
1366 // (2): Object literals like classes and strings, that are loaded from the dex cache
1367 // fields of the current method.
1368 bool NeedsCurrentMethod() const {
1369 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
1370 }
1371
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001372 virtual bool NeedsDexCache() const { return false; }
1373
David Brazdil1abb4192015-02-17 18:33:36 +00001374 protected:
1375 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
1376 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
1377
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001378 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001379 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); }
1380
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001381 HInstruction* previous_;
1382 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001383 HBasicBlock* block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001384
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001385 // An instruction gets an id when it is added to the graph.
1386 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01001387 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001388 int id_;
1389
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001390 // When doing liveness analysis, instructions that have uses get an SSA index.
1391 int ssa_index_;
1392
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001393 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00001394 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001395
1396 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00001397 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001398
Nicolas Geoffray39468442014-09-02 15:17:15 +01001399 // The environment associated with this instruction. Not null if the instruction
1400 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001401 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001402
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001403 // Set by the code generator.
1404 LocationSummary* locations_;
1405
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001406 // Set by the liveness analysis.
1407 LiveInterval* live_interval_;
1408
1409 // Set by the liveness analysis, this is the position in a linear
1410 // order of blocks where this instruction's live interval start.
1411 size_t lifetime_position_;
1412
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001413 const SideEffects side_effects_;
1414
Calin Juravleacf735c2015-02-12 15:25:22 +00001415 // TODO: for primitive types this should be marked as invalid.
1416 ReferenceTypeInfo reference_type_info_;
1417
David Brazdil1abb4192015-02-17 18:33:36 +00001418 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001419 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00001420 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001421 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001422 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001423
1424 DISALLOW_COPY_AND_ASSIGN(HInstruction);
1425};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001426std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001427
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001428class HInputIterator : public ValueObject {
1429 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001430 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001431
1432 bool Done() const { return index_ == instruction_->InputCount(); }
1433 HInstruction* Current() const { return instruction_->InputAt(index_); }
1434 void Advance() { index_++; }
1435
1436 private:
1437 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001438 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001439
1440 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
1441};
1442
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001443class HInstructionIterator : public ValueObject {
1444 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001445 explicit HInstructionIterator(const HInstructionList& instructions)
1446 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001447 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001448 }
1449
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001450 bool Done() const { return instruction_ == nullptr; }
1451 HInstruction* Current() const { return instruction_; }
1452 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001453 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001454 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001455 }
1456
1457 private:
1458 HInstruction* instruction_;
1459 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001460
1461 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001462};
1463
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001464class HBackwardInstructionIterator : public ValueObject {
1465 public:
1466 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
1467 : instruction_(instructions.last_instruction_) {
1468 next_ = Done() ? nullptr : instruction_->GetPrevious();
1469 }
1470
1471 bool Done() const { return instruction_ == nullptr; }
1472 HInstruction* Current() const { return instruction_; }
1473 void Advance() {
1474 instruction_ = next_;
1475 next_ = Done() ? nullptr : instruction_->GetPrevious();
1476 }
1477
1478 private:
1479 HInstruction* instruction_;
1480 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001481
1482 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001483};
1484
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001485// An embedded container with N elements of type T. Used (with partial
1486// specialization for N=0) because embedded arrays cannot have size 0.
1487template<typename T, intptr_t N>
1488class EmbeddedArray {
1489 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001490 EmbeddedArray() : elements_() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001491
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001492 intptr_t GetLength() const { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001493
1494 const T& operator[](intptr_t i) const {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001495 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001496 return elements_[i];
1497 }
1498
1499 T& operator[](intptr_t i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001500 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001501 return elements_[i];
1502 }
1503
1504 const T& At(intptr_t i) const {
1505 return (*this)[i];
1506 }
1507
1508 void SetAt(intptr_t i, const T& val) {
1509 (*this)[i] = val;
1510 }
1511
1512 private:
1513 T elements_[N];
1514};
1515
1516template<typename T>
1517class EmbeddedArray<T, 0> {
1518 public:
1519 intptr_t length() const { return 0; }
1520 const T& operator[](intptr_t i) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001521 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001522 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001523 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001524 }
1525 T& operator[](intptr_t i) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001526 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001527 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001528 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001529 }
1530};
1531
1532template<intptr_t N>
1533class HTemplateInstruction: public HInstruction {
1534 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001535 HTemplateInstruction<N>(SideEffects side_effects)
1536 : HInstruction(side_effects), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07001537 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001538
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001539 size_t InputCount() const OVERRIDE { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001540
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001541 protected:
David Brazdil1abb4192015-02-17 18:33:36 +00001542 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_[i]; }
1543
1544 void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE {
1545 inputs_[i] = input;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001546 }
1547
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001548 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001549 EmbeddedArray<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001550
1551 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001552};
1553
Dave Allison20dfc792014-06-16 20:44:29 -07001554template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001555class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07001556 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001557 HExpression<N>(Primitive::Type type, SideEffects side_effects)
1558 : HTemplateInstruction<N>(side_effects), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07001559 virtual ~HExpression() {}
1560
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001561 Primitive::Type GetType() const OVERRIDE { return type_; }
Dave Allison20dfc792014-06-16 20:44:29 -07001562
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001563 protected:
1564 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07001565};
1566
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001567// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
1568// instruction that branches to the exit block.
1569class HReturnVoid : public HTemplateInstruction<0> {
1570 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001571 HReturnVoid() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001572
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001573 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001574
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001575 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001576
1577 private:
1578 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
1579};
1580
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001581// Represents dex's RETURN opcodes. A HReturn is a control flow
1582// instruction that branches to the exit block.
1583class HReturn : public HTemplateInstruction<1> {
1584 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001585 explicit HReturn(HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001586 SetRawInputAt(0, value);
1587 }
1588
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001589 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001590
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001591 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001592
1593 private:
1594 DISALLOW_COPY_AND_ASSIGN(HReturn);
1595};
1596
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001597// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001598// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001599// exit block.
1600class HExit : public HTemplateInstruction<0> {
1601 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001602 HExit() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001603
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001604 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001605
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001606 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001607
1608 private:
1609 DISALLOW_COPY_AND_ASSIGN(HExit);
1610};
1611
1612// Jumps from one block to another.
1613class HGoto : public HTemplateInstruction<0> {
1614 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001615 HGoto() : HTemplateInstruction(SideEffects::None()) {}
1616
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001617 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001618
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001619 HBasicBlock* GetSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001620 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001621 }
1622
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001623 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001624
1625 private:
1626 DISALLOW_COPY_AND_ASSIGN(HGoto);
1627};
1628
Dave Allison20dfc792014-06-16 20:44:29 -07001629
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001630// Conditional branch. A block ending with an HIf instruction must have
1631// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001632class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001633 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001634 explicit HIf(HInstruction* input) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001635 SetRawInputAt(0, input);
1636 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001637
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001638 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001639
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001640 HBasicBlock* IfTrueSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001641 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001642 }
1643
1644 HBasicBlock* IfFalseSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001645 return GetBlock()->GetSuccessors().Get(1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001646 }
1647
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001648 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001649
1650 private:
1651 DISALLOW_COPY_AND_ASSIGN(HIf);
1652};
1653
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001654// Deoptimize to interpreter, upon checking a condition.
1655class HDeoptimize : public HTemplateInstruction<1> {
1656 public:
1657 HDeoptimize(HInstruction* cond, uint32_t dex_pc)
1658 : HTemplateInstruction(SideEffects::None()),
1659 dex_pc_(dex_pc) {
1660 SetRawInputAt(0, cond);
1661 }
1662
1663 bool NeedsEnvironment() const OVERRIDE { return true; }
1664 bool CanThrow() const OVERRIDE { return true; }
1665 uint32_t GetDexPc() const { return dex_pc_; }
1666
1667 DECLARE_INSTRUCTION(Deoptimize);
1668
1669 private:
1670 uint32_t dex_pc_;
1671
1672 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
1673};
1674
Roland Levillain88cb1752014-10-20 16:36:47 +01001675class HUnaryOperation : public HExpression<1> {
1676 public:
1677 HUnaryOperation(Primitive::Type result_type, HInstruction* input)
1678 : HExpression(result_type, SideEffects::None()) {
1679 SetRawInputAt(0, input);
1680 }
1681
1682 HInstruction* GetInput() const { return InputAt(0); }
1683 Primitive::Type GetResultType() const { return GetType(); }
1684
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001685 bool CanBeMoved() const OVERRIDE { return true; }
1686 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001687 UNUSED(other);
1688 return true;
1689 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001690
Roland Levillain9240d6a2014-10-20 16:47:04 +01001691 // Try to statically evaluate `operation` and return a HConstant
1692 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001693 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01001694 HConstant* TryStaticEvaluation() const;
1695
1696 // Apply this operation to `x`.
1697 virtual int32_t Evaluate(int32_t x) const = 0;
1698 virtual int64_t Evaluate(int64_t x) const = 0;
1699
Roland Levillain88cb1752014-10-20 16:36:47 +01001700 DECLARE_INSTRUCTION(UnaryOperation);
1701
1702 private:
1703 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
1704};
1705
Dave Allison20dfc792014-06-16 20:44:29 -07001706class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001707 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001708 HBinaryOperation(Primitive::Type result_type,
1709 HInstruction* left,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001710 HInstruction* right) : HExpression(result_type, SideEffects::None()) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001711 SetRawInputAt(0, left);
1712 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001713 }
1714
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001715 HInstruction* GetLeft() const { return InputAt(0); }
1716 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07001717 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001718
Mingyao Yangdc5ac732015-02-25 11:28:05 -08001719 virtual bool IsCommutative() const { return false; }
1720
1721 // Put constant on the right.
1722 // Returns whether order is changed.
1723 bool OrderInputsWithConstantOnTheRight() {
1724 HInstruction* left = InputAt(0);
1725 HInstruction* right = InputAt(1);
1726 if (left->IsConstant() && !right->IsConstant()) {
1727 ReplaceInput(right, 0);
1728 ReplaceInput(left, 1);
1729 return true;
1730 }
1731 return false;
1732 }
1733
1734 // Order inputs by instruction id, but favor constant on the right side.
1735 // This helps GVN for commutative ops.
1736 void OrderInputs() {
1737 DCHECK(IsCommutative());
1738 HInstruction* left = InputAt(0);
1739 HInstruction* right = InputAt(1);
1740 if (left == right || (!left->IsConstant() && right->IsConstant())) {
1741 return;
1742 }
1743 if (OrderInputsWithConstantOnTheRight()) {
1744 return;
1745 }
1746 // Order according to instruction id.
1747 if (left->GetId() > right->GetId()) {
1748 ReplaceInput(right, 0);
1749 ReplaceInput(left, 1);
1750 }
1751 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001752
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001753 bool CanBeMoved() const OVERRIDE { return true; }
1754 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001755 UNUSED(other);
1756 return true;
1757 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001758
Roland Levillain9240d6a2014-10-20 16:47:04 +01001759 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01001760 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001761 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01001762 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01001763
1764 // Apply this operation to `x` and `y`.
1765 virtual int32_t Evaluate(int32_t x, int32_t y) const = 0;
1766 virtual int64_t Evaluate(int64_t x, int64_t y) const = 0;
1767
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001768 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001769 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001770 HConstant* GetConstantRight() const;
1771
1772 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001773 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001774 HInstruction* GetLeastConstantLeft() const;
1775
Roland Levillainccc07a92014-09-16 14:48:16 +01001776 DECLARE_INSTRUCTION(BinaryOperation);
1777
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001778 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001779 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
1780};
1781
Dave Allison20dfc792014-06-16 20:44:29 -07001782class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001783 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001784 HCondition(HInstruction* first, HInstruction* second)
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001785 : HBinaryOperation(Primitive::kPrimBoolean, first, second),
1786 needs_materialization_(true) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001787
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001788 bool NeedsMaterialization() const { return needs_materialization_; }
1789 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001790
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001791 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001792 // `instruction`, and disregard moves in between.
1793 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001794
Dave Allison20dfc792014-06-16 20:44:29 -07001795 DECLARE_INSTRUCTION(Condition);
1796
1797 virtual IfCondition GetCondition() const = 0;
1798
1799 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001800 // For register allocation purposes, returns whether this instruction needs to be
1801 // materialized (that is, not just be in the processor flags).
1802 bool needs_materialization_;
1803
Dave Allison20dfc792014-06-16 20:44:29 -07001804 DISALLOW_COPY_AND_ASSIGN(HCondition);
1805};
1806
1807// Instruction to check if two inputs are equal to each other.
1808class HEqual : public HCondition {
1809 public:
1810 HEqual(HInstruction* first, HInstruction* second)
1811 : HCondition(first, second) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001812
Mingyao Yangdc5ac732015-02-25 11:28:05 -08001813 bool IsCommutative() const OVERRIDE { return true; }
1814
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001815 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001816 return x == y ? 1 : 0;
1817 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001818 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001819 return x == y ? 1 : 0;
1820 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001821
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001822 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001823
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001824 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001825 return kCondEQ;
1826 }
1827
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001828 private:
1829 DISALLOW_COPY_AND_ASSIGN(HEqual);
1830};
1831
Dave Allison20dfc792014-06-16 20:44:29 -07001832class HNotEqual : public HCondition {
1833 public:
1834 HNotEqual(HInstruction* first, HInstruction* second)
1835 : HCondition(first, second) {}
1836
Mingyao Yangdc5ac732015-02-25 11:28:05 -08001837 bool IsCommutative() const OVERRIDE { return true; }
1838
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001839 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001840 return x != y ? 1 : 0;
1841 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001842 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001843 return x != y ? 1 : 0;
1844 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001845
Dave Allison20dfc792014-06-16 20:44:29 -07001846 DECLARE_INSTRUCTION(NotEqual);
1847
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001848 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001849 return kCondNE;
1850 }
1851
1852 private:
1853 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
1854};
1855
1856class HLessThan : public HCondition {
1857 public:
1858 HLessThan(HInstruction* first, HInstruction* second)
1859 : HCondition(first, second) {}
1860
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001861 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001862 return x < y ? 1 : 0;
1863 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001864 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001865 return x < y ? 1 : 0;
1866 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001867
Dave Allison20dfc792014-06-16 20:44:29 -07001868 DECLARE_INSTRUCTION(LessThan);
1869
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001870 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001871 return kCondLT;
1872 }
1873
1874 private:
1875 DISALLOW_COPY_AND_ASSIGN(HLessThan);
1876};
1877
1878class HLessThanOrEqual : public HCondition {
1879 public:
1880 HLessThanOrEqual(HInstruction* first, HInstruction* second)
1881 : HCondition(first, second) {}
1882
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001883 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001884 return x <= y ? 1 : 0;
1885 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001886 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001887 return x <= y ? 1 : 0;
1888 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001889
Dave Allison20dfc792014-06-16 20:44:29 -07001890 DECLARE_INSTRUCTION(LessThanOrEqual);
1891
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001892 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001893 return kCondLE;
1894 }
1895
1896 private:
1897 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
1898};
1899
1900class HGreaterThan : public HCondition {
1901 public:
1902 HGreaterThan(HInstruction* first, HInstruction* second)
1903 : HCondition(first, second) {}
1904
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001905 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001906 return x > y ? 1 : 0;
1907 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001908 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001909 return x > y ? 1 : 0;
1910 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001911
Dave Allison20dfc792014-06-16 20:44:29 -07001912 DECLARE_INSTRUCTION(GreaterThan);
1913
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001914 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001915 return kCondGT;
1916 }
1917
1918 private:
1919 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
1920};
1921
1922class HGreaterThanOrEqual : public HCondition {
1923 public:
1924 HGreaterThanOrEqual(HInstruction* first, HInstruction* second)
1925 : HCondition(first, second) {}
1926
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001927 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001928 return x >= y ? 1 : 0;
1929 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001930 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001931 return x >= y ? 1 : 0;
1932 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001933
Dave Allison20dfc792014-06-16 20:44:29 -07001934 DECLARE_INSTRUCTION(GreaterThanOrEqual);
1935
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001936 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001937 return kCondGE;
1938 }
1939
1940 private:
1941 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
1942};
1943
1944
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001945// Instruction to check how two inputs compare to each other.
1946// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
1947class HCompare : public HBinaryOperation {
1948 public:
Calin Juravleddb7df22014-11-25 20:56:51 +00001949 // The bias applies for floating point operations and indicates how NaN
1950 // comparisons are treated:
1951 enum Bias {
1952 kNoBias, // bias is not applicable (i.e. for long operation)
1953 kGtBias, // return 1 for NaN comparisons
1954 kLtBias, // return -1 for NaN comparisons
1955 };
1956
1957 HCompare(Primitive::Type type, HInstruction* first, HInstruction* second, Bias bias)
1958 : HBinaryOperation(Primitive::kPrimInt, first, second), bias_(bias) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001959 DCHECK_EQ(type, first->GetType());
1960 DCHECK_EQ(type, second->GetType());
1961 }
1962
Calin Juravleddb7df22014-11-25 20:56:51 +00001963 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01001964 return
1965 x == y ? 0 :
1966 x > y ? 1 :
1967 -1;
1968 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001969
1970 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01001971 return
1972 x == y ? 0 :
1973 x > y ? 1 :
1974 -1;
1975 }
1976
Calin Juravleddb7df22014-11-25 20:56:51 +00001977 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
1978 return bias_ == other->AsCompare()->bias_;
1979 }
1980
1981 bool IsGtBias() { return bias_ == kGtBias; }
1982
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001983 DECLARE_INSTRUCTION(Compare);
1984
1985 private:
Calin Juravleddb7df22014-11-25 20:56:51 +00001986 const Bias bias_;
1987
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001988 DISALLOW_COPY_AND_ASSIGN(HCompare);
1989};
1990
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001991// A local in the graph. Corresponds to a Dex register.
1992class HLocal : public HTemplateInstruction<0> {
1993 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001994 explicit HLocal(uint16_t reg_number)
1995 : HTemplateInstruction(SideEffects::None()), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001996
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001997 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001998
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001999 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002000
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002001 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002002 // The Dex register number.
2003 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002004
2005 DISALLOW_COPY_AND_ASSIGN(HLocal);
2006};
2007
2008// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07002009class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002010 public:
Roland Levillain5799fc02014-09-25 12:15:20 +01002011 HLoadLocal(HLocal* local, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002012 : HExpression(type, SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002013 SetRawInputAt(0, local);
2014 }
2015
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002016 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
2017
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002018 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002019
2020 private:
2021 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
2022};
2023
2024// Store a value in a given local. This instruction has two inputs: the value
2025// and the local.
2026class HStoreLocal : public HTemplateInstruction<2> {
2027 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002028 HStoreLocal(HLocal* local, HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002029 SetRawInputAt(0, local);
2030 SetRawInputAt(1, value);
2031 }
2032
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002033 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
2034
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002035 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002036
2037 private:
2038 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
2039};
2040
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002041class HConstant : public HExpression<0> {
2042 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002043 explicit HConstant(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
2044
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002045 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002046
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002047 virtual bool IsMinusOne() const { return false; }
2048 virtual bool IsZero() const { return false; }
2049 virtual bool IsOne() const { return false; }
2050
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002051 DECLARE_INSTRUCTION(Constant);
2052
2053 private:
2054 DISALLOW_COPY_AND_ASSIGN(HConstant);
2055};
2056
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002057class HFloatConstant : public HConstant {
2058 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002059 float GetValue() const { return value_; }
2060
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002061 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002062 return bit_cast<uint32_t, float>(other->AsFloatConstant()->value_) ==
2063 bit_cast<uint32_t, float>(value_);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002064 }
2065
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002066 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002067
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002068 bool IsMinusOne() const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002069 return bit_cast<uint32_t, float>(AsFloatConstant()->GetValue()) ==
2070 bit_cast<uint32_t, float>((-1.0f));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002071 }
2072 bool IsZero() const OVERRIDE {
2073 return AsFloatConstant()->GetValue() == 0.0f;
2074 }
2075 bool IsOne() const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002076 return bit_cast<uint32_t, float>(AsFloatConstant()->GetValue()) ==
2077 bit_cast<uint32_t, float>(1.0f);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002078 }
2079
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002080 DECLARE_INSTRUCTION(FloatConstant);
2081
2082 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002083 explicit HFloatConstant(float value) : HConstant(Primitive::kPrimFloat), value_(value) {}
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002084 explicit HFloatConstant(int32_t value)
2085 : HConstant(Primitive::kPrimFloat), value_(bit_cast<float, int32_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00002086
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002087 const float value_;
2088
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002089 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00002090 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002091 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002092 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2093};
2094
2095class HDoubleConstant : public HConstant {
2096 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002097 double GetValue() const { return value_; }
2098
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002099 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002100 return bit_cast<uint64_t, double>(other->AsDoubleConstant()->value_) ==
2101 bit_cast<uint64_t, double>(value_);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002102 }
2103
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002104 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002105
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002106 bool IsMinusOne() const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002107 return bit_cast<uint64_t, double>(AsDoubleConstant()->GetValue()) ==
2108 bit_cast<uint64_t, double>((-1.0));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002109 }
2110 bool IsZero() const OVERRIDE {
2111 return AsDoubleConstant()->GetValue() == 0.0;
2112 }
2113 bool IsOne() const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002114 return bit_cast<uint64_t, double>(AsDoubleConstant()->GetValue()) ==
2115 bit_cast<uint64_t, double>(1.0);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002116 }
2117
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002118 DECLARE_INSTRUCTION(DoubleConstant);
2119
2120 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002121 explicit HDoubleConstant(double value) : HConstant(Primitive::kPrimDouble), value_(value) {}
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002122 explicit HDoubleConstant(int64_t value)
2123 : HConstant(Primitive::kPrimDouble), value_(bit_cast<double, int64_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00002124
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002125 const double value_;
2126
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002127 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00002128 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002129 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002130 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
2131};
2132
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002133class HNullConstant : public HConstant {
2134 public:
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002135 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2136 return true;
2137 }
2138
2139 size_t ComputeHashCode() const OVERRIDE { return 0; }
2140
2141 DECLARE_INSTRUCTION(NullConstant);
2142
2143 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002144 HNullConstant() : HConstant(Primitive::kPrimNot) {}
2145
2146 friend class HGraph;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002147 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2148};
2149
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002150// Constants of the type int. Those can be from Dex instructions, or
2151// synthesized (for example with the if-eqz instruction).
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002152class HIntConstant : public HConstant {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002153 public:
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002154 int32_t GetValue() const { return value_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002155
Calin Juravle61d544b2015-02-23 16:46:57 +00002156 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002157 return other->AsIntConstant()->value_ == value_;
2158 }
2159
Calin Juravle61d544b2015-02-23 16:46:57 +00002160 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2161
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002162 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2163 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2164 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2165
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002166 DECLARE_INSTRUCTION(IntConstant);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002167
2168 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002169 explicit HIntConstant(int32_t value) : HConstant(Primitive::kPrimInt), value_(value) {}
2170
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002171 const int32_t value_;
2172
David Brazdil8d5b8b22015-03-24 10:51:52 +00002173 friend class HGraph;
2174 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
Zheng Xuad4450e2015-04-17 18:48:56 +08002175 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002176 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2177};
2178
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002179class HLongConstant : public HConstant {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002180 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002181 int64_t GetValue() const { return value_; }
2182
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002183 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002184 return other->AsLongConstant()->value_ == value_;
2185 }
2186
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002187 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002188
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002189 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2190 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2191 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2192
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002193 DECLARE_INSTRUCTION(LongConstant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002194
2195 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002196 explicit HLongConstant(int64_t value) : HConstant(Primitive::kPrimLong), value_(value) {}
2197
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002198 const int64_t value_;
2199
David Brazdil8d5b8b22015-03-24 10:51:52 +00002200 friend class HGraph;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002201 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2202};
2203
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002204enum class Intrinsics {
2205#define OPTIMIZING_INTRINSICS(Name, IsStatic) k ## Name,
2206#include "intrinsics_list.h"
2207 kNone,
2208 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
2209#undef INTRINSICS_LIST
2210#undef OPTIMIZING_INTRINSICS
2211};
2212std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
2213
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002214class HInvoke : public HInstruction {
2215 public:
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002216 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002217
2218 // Runtime needs to walk the stack, so Dex -> Dex calls need to
2219 // know their environment.
Calin Juravle77520bc2015-01-12 18:45:46 +00002220 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002221
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01002222 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002223 SetRawInputAt(index, argument);
2224 }
2225
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002226 Primitive::Type GetType() const OVERRIDE { return return_type_; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002227
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002228 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002229
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002230 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
2231
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01002232 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002233 return intrinsic_;
2234 }
2235
2236 void SetIntrinsic(Intrinsics intrinsic) {
2237 intrinsic_ = intrinsic;
2238 }
2239
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002240 DECLARE_INSTRUCTION(Invoke);
2241
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002242 protected:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002243 HInvoke(ArenaAllocator* arena,
2244 uint32_t number_of_arguments,
2245 Primitive::Type return_type,
2246 uint32_t dex_pc,
2247 uint32_t dex_method_index)
2248 : HInstruction(SideEffects::All()),
2249 inputs_(arena, number_of_arguments),
2250 return_type_(return_type),
2251 dex_pc_(dex_pc),
2252 dex_method_index_(dex_method_index),
2253 intrinsic_(Intrinsics::kNone) {
2254 inputs_.SetSize(number_of_arguments);
2255 }
2256
David Brazdil1abb4192015-02-17 18:33:36 +00002257 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
2258 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
2259 inputs_.Put(index, input);
2260 }
2261
2262 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002263 const Primitive::Type return_type_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002264 const uint32_t dex_pc_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002265 const uint32_t dex_method_index_;
2266 Intrinsics intrinsic_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002267
2268 private:
2269 DISALLOW_COPY_AND_ASSIGN(HInvoke);
2270};
2271
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002272class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002273 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01002274 // Requirements of this method call regarding the class
2275 // initialization (clinit) check of its declaring class.
2276 enum class ClinitCheckRequirement {
2277 kNone, // Class already initialized.
2278 kExplicit, // Static call having explicit clinit check as last input.
2279 kImplicit, // Static call implicitly requiring a clinit check.
2280 };
2281
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002282 HInvokeStaticOrDirect(ArenaAllocator* arena,
2283 uint32_t number_of_arguments,
2284 Primitive::Type return_type,
2285 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002286 uint32_t dex_method_index,
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002287 bool is_recursive,
Nicolas Geoffray79041292015-03-26 10:05:54 +00002288 InvokeType original_invoke_type,
Roland Levillain4c0eb422015-04-24 16:43:49 +01002289 InvokeType invoke_type,
2290 ClinitCheckRequirement clinit_check_requirement)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002291 : HInvoke(arena, number_of_arguments, return_type, dex_pc, dex_method_index),
Nicolas Geoffray79041292015-03-26 10:05:54 +00002292 original_invoke_type_(original_invoke_type),
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002293 invoke_type_(invoke_type),
Roland Levillain4c0eb422015-04-24 16:43:49 +01002294 is_recursive_(is_recursive),
2295 clinit_check_requirement_(clinit_check_requirement) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002296
Calin Juravle641547a2015-04-21 22:08:51 +01002297 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
2298 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00002299 // We access the method via the dex cache so we can't do an implicit null check.
2300 // TODO: for intrinsics we can generate implicit null checks.
2301 return false;
2302 }
2303
Nicolas Geoffray79041292015-03-26 10:05:54 +00002304 InvokeType GetOriginalInvokeType() const { return original_invoke_type_; }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002305 InvokeType GetInvokeType() const { return invoke_type_; }
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002306 bool IsRecursive() const { return is_recursive_; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002307 bool NeedsDexCache() const OVERRIDE { return !IsRecursive(); }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002308
Roland Levillain4c0eb422015-04-24 16:43:49 +01002309 // Is this instruction a call to a static method?
2310 bool IsStatic() const {
2311 return GetInvokeType() == kStatic;
2312 }
2313
Roland Levillain0379f822015-04-24 23:01:24 +01002314 // Remove the art::HClinitCheck or art::HLoadClass instruction as
2315 // last input (only relevant for static calls with explicit clinit
2316 // check).
2317 void RemoveClinitCheckOrLoadClassAsLastInput() {
Roland Levillain4c0eb422015-04-24 16:43:49 +01002318 DCHECK(IsStaticWithExplicitClinitCheck());
2319 size_t last_input_index = InputCount() - 1;
2320 HInstruction* last_input = InputAt(last_input_index);
2321 DCHECK(last_input != nullptr);
Roland Levillain0379f822015-04-24 23:01:24 +01002322 DCHECK(last_input->IsClinitCheck() || last_input->IsLoadClass()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01002323 RemoveAsUserOfInput(last_input_index);
2324 inputs_.DeleteAt(last_input_index);
2325 clinit_check_requirement_ = ClinitCheckRequirement::kImplicit;
2326 DCHECK(IsStaticWithImplicitClinitCheck());
2327 }
2328
2329 // Is this a call to a static method whose declaring class has an
2330 // explicit intialization check in the graph?
2331 bool IsStaticWithExplicitClinitCheck() const {
2332 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kExplicit);
2333 }
2334
2335 // Is this a call to a static method whose declaring class has an
2336 // implicit intialization check requirement?
2337 bool IsStaticWithImplicitClinitCheck() const {
2338 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kImplicit);
2339 }
2340
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002341 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002342
Roland Levillain4c0eb422015-04-24 16:43:49 +01002343 protected:
2344 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
2345 const HUserRecord<HInstruction*> input_record = HInvoke::InputRecordAt(i);
2346 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck() && (i == InputCount() - 1)) {
2347 HInstruction* input = input_record.GetInstruction();
2348 // `input` is the last input of a static invoke marked as having
2349 // an explicit clinit check. It must either be:
2350 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
2351 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
2352 DCHECK(input != nullptr);
2353 DCHECK(input->IsClinitCheck() || input->IsLoadClass()) << input->DebugName();
2354 }
2355 return input_record;
2356 }
2357
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002358 private:
Nicolas Geoffray79041292015-03-26 10:05:54 +00002359 const InvokeType original_invoke_type_;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002360 const InvokeType invoke_type_;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002361 const bool is_recursive_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01002362 ClinitCheckRequirement clinit_check_requirement_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002363
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002364 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002365};
2366
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002367class HInvokeVirtual : public HInvoke {
2368 public:
2369 HInvokeVirtual(ArenaAllocator* arena,
2370 uint32_t number_of_arguments,
2371 Primitive::Type return_type,
2372 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002373 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002374 uint32_t vtable_index)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002375 : HInvoke(arena, number_of_arguments, return_type, dex_pc, dex_method_index),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002376 vtable_index_(vtable_index) {}
2377
Calin Juravle641547a2015-04-21 22:08:51 +01002378 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00002379 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01002380 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00002381 }
2382
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002383 uint32_t GetVTableIndex() const { return vtable_index_; }
2384
2385 DECLARE_INSTRUCTION(InvokeVirtual);
2386
2387 private:
2388 const uint32_t vtable_index_;
2389
2390 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
2391};
2392
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002393class HInvokeInterface : public HInvoke {
2394 public:
2395 HInvokeInterface(ArenaAllocator* arena,
2396 uint32_t number_of_arguments,
2397 Primitive::Type return_type,
2398 uint32_t dex_pc,
2399 uint32_t dex_method_index,
2400 uint32_t imt_index)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002401 : HInvoke(arena, number_of_arguments, return_type, dex_pc, dex_method_index),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002402 imt_index_(imt_index) {}
2403
Calin Juravle641547a2015-04-21 22:08:51 +01002404 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00002405 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01002406 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00002407 }
2408
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002409 uint32_t GetImtIndex() const { return imt_index_; }
2410 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
2411
2412 DECLARE_INSTRUCTION(InvokeInterface);
2413
2414 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002415 const uint32_t imt_index_;
2416
2417 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
2418};
2419
Dave Allison20dfc792014-06-16 20:44:29 -07002420class HNewInstance : public HExpression<0> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002421 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002422 HNewInstance(uint32_t dex_pc, uint16_t type_index, QuickEntrypointEnum entrypoint)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002423 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2424 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002425 type_index_(type_index),
2426 entrypoint_(entrypoint) {}
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002427
2428 uint32_t GetDexPc() const { return dex_pc_; }
2429 uint16_t GetTypeIndex() const { return type_index_; }
2430
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002431 // Calls runtime so needs an environment.
Calin Juravle92a6ed22014-12-02 18:58:03 +00002432 bool NeedsEnvironment() const OVERRIDE { return true; }
2433 // It may throw when called on:
2434 // - interfaces
2435 // - abstract/innaccessible/unknown classes
2436 // TODO: optimize when possible.
2437 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002438
Calin Juravle10e244f2015-01-26 18:54:32 +00002439 bool CanBeNull() const OVERRIDE { return false; }
2440
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002441 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
2442
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002443 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002444
2445 private:
2446 const uint32_t dex_pc_;
2447 const uint16_t type_index_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002448 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002449
2450 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
2451};
2452
Roland Levillain88cb1752014-10-20 16:36:47 +01002453class HNeg : public HUnaryOperation {
2454 public:
2455 explicit HNeg(Primitive::Type result_type, HInstruction* input)
2456 : HUnaryOperation(result_type, input) {}
2457
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002458 int32_t Evaluate(int32_t x) const OVERRIDE { return -x; }
2459 int64_t Evaluate(int64_t x) const OVERRIDE { return -x; }
Roland Levillain9240d6a2014-10-20 16:47:04 +01002460
Roland Levillain88cb1752014-10-20 16:36:47 +01002461 DECLARE_INSTRUCTION(Neg);
2462
2463 private:
2464 DISALLOW_COPY_AND_ASSIGN(HNeg);
2465};
2466
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002467class HNewArray : public HExpression<1> {
2468 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002469 HNewArray(HInstruction* length,
2470 uint32_t dex_pc,
2471 uint16_t type_index,
2472 QuickEntrypointEnum entrypoint)
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002473 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2474 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002475 type_index_(type_index),
2476 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002477 SetRawInputAt(0, length);
2478 }
2479
2480 uint32_t GetDexPc() const { return dex_pc_; }
2481 uint16_t GetTypeIndex() const { return type_index_; }
2482
2483 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00002484 bool NeedsEnvironment() const OVERRIDE { return true; }
2485
Mingyao Yang0c365e62015-03-31 15:09:29 -07002486 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
2487 bool CanThrow() const OVERRIDE { return true; }
2488
Calin Juravle10e244f2015-01-26 18:54:32 +00002489 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002490
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002491 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
2492
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002493 DECLARE_INSTRUCTION(NewArray);
2494
2495 private:
2496 const uint32_t dex_pc_;
2497 const uint16_t type_index_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002498 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002499
2500 DISALLOW_COPY_AND_ASSIGN(HNewArray);
2501};
2502
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002503class HAdd : public HBinaryOperation {
2504 public:
2505 HAdd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2506 : HBinaryOperation(result_type, left, right) {}
2507
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002508 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002509
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002510 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002511 return x + y;
2512 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002513 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002514 return x + y;
2515 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002516
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002517 DECLARE_INSTRUCTION(Add);
2518
2519 private:
2520 DISALLOW_COPY_AND_ASSIGN(HAdd);
2521};
2522
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002523class HSub : public HBinaryOperation {
2524 public:
2525 HSub(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2526 : HBinaryOperation(result_type, left, right) {}
2527
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002528 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002529 return x - y;
2530 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002531 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002532 return x - y;
2533 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002534
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002535 DECLARE_INSTRUCTION(Sub);
2536
2537 private:
2538 DISALLOW_COPY_AND_ASSIGN(HSub);
2539};
2540
Calin Juravle34bacdf2014-10-07 20:23:36 +01002541class HMul : public HBinaryOperation {
2542 public:
2543 HMul(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2544 : HBinaryOperation(result_type, left, right) {}
2545
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002546 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002547
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002548 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x * y; }
2549 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x * y; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002550
2551 DECLARE_INSTRUCTION(Mul);
2552
2553 private:
2554 DISALLOW_COPY_AND_ASSIGN(HMul);
2555};
2556
Calin Juravle7c4954d2014-10-28 16:57:40 +00002557class HDiv : public HBinaryOperation {
2558 public:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002559 HDiv(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
2560 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00002561
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002562 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00002563 // Our graph structure ensures we never have 0 for `y` during constant folding.
2564 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00002565 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00002566 return (y == -1) ? -x : x / y;
2567 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002568
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002569 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002570 DCHECK_NE(y, 0);
2571 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2572 return (y == -1) ? -x : x / y;
2573 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002574
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002575 uint32_t GetDexPc() const { return dex_pc_; }
2576
Calin Juravle7c4954d2014-10-28 16:57:40 +00002577 DECLARE_INSTRUCTION(Div);
2578
2579 private:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002580 const uint32_t dex_pc_;
2581
Calin Juravle7c4954d2014-10-28 16:57:40 +00002582 DISALLOW_COPY_AND_ASSIGN(HDiv);
2583};
2584
Calin Juravlebacfec32014-11-14 15:54:36 +00002585class HRem : public HBinaryOperation {
2586 public:
2587 HRem(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
2588 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
2589
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002590 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002591 DCHECK_NE(y, 0);
2592 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2593 return (y == -1) ? 0 : x % y;
2594 }
2595
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002596 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002597 DCHECK_NE(y, 0);
2598 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2599 return (y == -1) ? 0 : x % y;
2600 }
2601
2602 uint32_t GetDexPc() const { return dex_pc_; }
2603
2604 DECLARE_INSTRUCTION(Rem);
2605
2606 private:
2607 const uint32_t dex_pc_;
2608
2609 DISALLOW_COPY_AND_ASSIGN(HRem);
2610};
2611
Calin Juravled0d48522014-11-04 16:40:20 +00002612class HDivZeroCheck : public HExpression<1> {
2613 public:
2614 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
2615 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
2616 SetRawInputAt(0, value);
2617 }
2618
2619 bool CanBeMoved() const OVERRIDE { return true; }
2620
2621 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2622 UNUSED(other);
2623 return true;
2624 }
2625
2626 bool NeedsEnvironment() const OVERRIDE { return true; }
2627 bool CanThrow() const OVERRIDE { return true; }
2628
2629 uint32_t GetDexPc() const { return dex_pc_; }
2630
2631 DECLARE_INSTRUCTION(DivZeroCheck);
2632
2633 private:
2634 const uint32_t dex_pc_;
2635
2636 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
2637};
2638
Calin Juravle9aec02f2014-11-18 23:06:35 +00002639class HShl : public HBinaryOperation {
2640 public:
2641 HShl(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2642 : HBinaryOperation(result_type, left, right) {}
2643
2644 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x << (y & kMaxIntShiftValue); }
2645 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x << (y & kMaxLongShiftValue); }
2646
2647 DECLARE_INSTRUCTION(Shl);
2648
2649 private:
2650 DISALLOW_COPY_AND_ASSIGN(HShl);
2651};
2652
2653class HShr : public HBinaryOperation {
2654 public:
2655 HShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2656 : HBinaryOperation(result_type, left, right) {}
2657
2658 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x >> (y & kMaxIntShiftValue); }
2659 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x >> (y & kMaxLongShiftValue); }
2660
2661 DECLARE_INSTRUCTION(Shr);
2662
2663 private:
2664 DISALLOW_COPY_AND_ASSIGN(HShr);
2665};
2666
2667class HUShr : public HBinaryOperation {
2668 public:
2669 HUShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2670 : HBinaryOperation(result_type, left, right) {}
2671
2672 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
2673 uint32_t ux = static_cast<uint32_t>(x);
2674 uint32_t uy = static_cast<uint32_t>(y) & kMaxIntShiftValue;
2675 return static_cast<int32_t>(ux >> uy);
2676 }
2677
2678 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
2679 uint64_t ux = static_cast<uint64_t>(x);
2680 uint64_t uy = static_cast<uint64_t>(y) & kMaxLongShiftValue;
2681 return static_cast<int64_t>(ux >> uy);
2682 }
2683
2684 DECLARE_INSTRUCTION(UShr);
2685
2686 private:
2687 DISALLOW_COPY_AND_ASSIGN(HUShr);
2688};
2689
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002690class HAnd : public HBinaryOperation {
2691 public:
2692 HAnd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2693 : HBinaryOperation(result_type, left, right) {}
2694
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002695 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002696
2697 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x & y; }
2698 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x & y; }
2699
2700 DECLARE_INSTRUCTION(And);
2701
2702 private:
2703 DISALLOW_COPY_AND_ASSIGN(HAnd);
2704};
2705
2706class HOr : public HBinaryOperation {
2707 public:
2708 HOr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2709 : HBinaryOperation(result_type, left, right) {}
2710
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002711 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002712
2713 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x | y; }
2714 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x | y; }
2715
2716 DECLARE_INSTRUCTION(Or);
2717
2718 private:
2719 DISALLOW_COPY_AND_ASSIGN(HOr);
2720};
2721
2722class HXor : public HBinaryOperation {
2723 public:
2724 HXor(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2725 : HBinaryOperation(result_type, left, right) {}
2726
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002727 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002728
2729 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x ^ y; }
2730 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x ^ y; }
2731
2732 DECLARE_INSTRUCTION(Xor);
2733
2734 private:
2735 DISALLOW_COPY_AND_ASSIGN(HXor);
2736};
2737
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002738// The value of a parameter in this method. Its location depends on
2739// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07002740class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002741 public:
Calin Juravle10e244f2015-01-26 18:54:32 +00002742 HParameterValue(uint8_t index, Primitive::Type parameter_type, bool is_this = false)
2743 : HExpression(parameter_type, SideEffects::None()), index_(index), is_this_(is_this) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002744
2745 uint8_t GetIndex() const { return index_; }
2746
Calin Juravle10e244f2015-01-26 18:54:32 +00002747 bool CanBeNull() const OVERRIDE { return !is_this_; }
2748
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002749 DECLARE_INSTRUCTION(ParameterValue);
2750
2751 private:
2752 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00002753 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002754 const uint8_t index_;
2755
Calin Juravle10e244f2015-01-26 18:54:32 +00002756 // Whether or not the parameter value corresponds to 'this' argument.
2757 const bool is_this_;
2758
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002759 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
2760};
2761
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002762class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002763 public:
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002764 explicit HNot(Primitive::Type result_type, HInstruction* input)
2765 : HUnaryOperation(result_type, input) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002766
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002767 bool CanBeMoved() const OVERRIDE { return true; }
2768 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002769 UNUSED(other);
2770 return true;
2771 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002772
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002773 int32_t Evaluate(int32_t x) const OVERRIDE { return ~x; }
2774 int64_t Evaluate(int64_t x) const OVERRIDE { return ~x; }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002775
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002776 DECLARE_INSTRUCTION(Not);
2777
2778 private:
2779 DISALLOW_COPY_AND_ASSIGN(HNot);
2780};
2781
David Brazdil66d126e2015-04-03 16:02:44 +01002782class HBooleanNot : public HUnaryOperation {
2783 public:
2784 explicit HBooleanNot(HInstruction* input)
2785 : HUnaryOperation(Primitive::Type::kPrimBoolean, input) {}
2786
2787 bool CanBeMoved() const OVERRIDE { return true; }
2788 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2789 UNUSED(other);
2790 return true;
2791 }
2792
2793 int32_t Evaluate(int32_t x) const OVERRIDE {
2794 DCHECK(IsUint<1>(x));
2795 return !x;
2796 }
2797
2798 int64_t Evaluate(int64_t x ATTRIBUTE_UNUSED) const OVERRIDE {
2799 LOG(FATAL) << DebugName() << " cannot be used with 64-bit values";
2800 UNREACHABLE();
2801 }
2802
2803 DECLARE_INSTRUCTION(BooleanNot);
2804
2805 private:
2806 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
2807};
2808
Roland Levillaindff1f282014-11-05 14:15:05 +00002809class HTypeConversion : public HExpression<1> {
2810 public:
2811 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00002812 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
2813 : HExpression(result_type, SideEffects::None()), dex_pc_(dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002814 SetRawInputAt(0, input);
2815 DCHECK_NE(input->GetType(), result_type);
2816 }
2817
2818 HInstruction* GetInput() const { return InputAt(0); }
2819 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
2820 Primitive::Type GetResultType() const { return GetType(); }
2821
Roland Levillain624279f2014-12-04 11:54:28 +00002822 // Required by the x86 and ARM code generators when producing calls
2823 // to the runtime.
2824 uint32_t GetDexPc() const { return dex_pc_; }
2825
Roland Levillaindff1f282014-11-05 14:15:05 +00002826 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00002827 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00002828
2829 DECLARE_INSTRUCTION(TypeConversion);
2830
2831 private:
Roland Levillain624279f2014-12-04 11:54:28 +00002832 const uint32_t dex_pc_;
2833
Roland Levillaindff1f282014-11-05 14:15:05 +00002834 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
2835};
2836
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00002837static constexpr uint32_t kNoRegNumber = -1;
2838
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002839class HPhi : public HInstruction {
2840 public:
2841 HPhi(ArenaAllocator* arena, uint32_t reg_number, size_t number_of_inputs, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002842 : HInstruction(SideEffects::None()),
2843 inputs_(arena, number_of_inputs),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002844 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002845 type_(type),
Calin Juravle10e244f2015-01-26 18:54:32 +00002846 is_live_(false),
2847 can_be_null_(true) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002848 inputs_.SetSize(number_of_inputs);
2849 }
2850
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00002851 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
2852 static Primitive::Type ToPhiType(Primitive::Type type) {
2853 switch (type) {
2854 case Primitive::kPrimBoolean:
2855 case Primitive::kPrimByte:
2856 case Primitive::kPrimShort:
2857 case Primitive::kPrimChar:
2858 return Primitive::kPrimInt;
2859 default:
2860 return type;
2861 }
2862 }
2863
Calin Juravle10e244f2015-01-26 18:54:32 +00002864 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002865
2866 void AddInput(HInstruction* input);
David Brazdil2d7352b2015-04-20 14:52:42 +01002867 void RemoveInputAt(size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002868
Calin Juravle10e244f2015-01-26 18:54:32 +00002869 Primitive::Type GetType() const OVERRIDE { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002870 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002871
Calin Juravle10e244f2015-01-26 18:54:32 +00002872 bool CanBeNull() const OVERRIDE { return can_be_null_; }
2873 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
2874
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002875 uint32_t GetRegNumber() const { return reg_number_; }
2876
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002877 void SetDead() { is_live_ = false; }
2878 void SetLive() { is_live_ = true; }
2879 bool IsDead() const { return !is_live_; }
2880 bool IsLive() const { return is_live_; }
2881
Calin Juravlea4f88312015-04-16 12:57:19 +01002882 // Returns the next equivalent phi (starting from the current one) or null if there is none.
2883 // An equivalent phi is a phi having the same dex register and type.
2884 // It assumes that phis with the same dex register are adjacent.
2885 HPhi* GetNextEquivalentPhiWithSameType() {
2886 HInstruction* next = GetNext();
2887 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
2888 if (next->GetType() == GetType()) {
2889 return next->AsPhi();
2890 }
2891 next = next->GetNext();
2892 }
2893 return nullptr;
2894 }
2895
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002896 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002897
David Brazdil1abb4192015-02-17 18:33:36 +00002898 protected:
2899 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
2900
2901 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
2902 inputs_.Put(index, input);
2903 }
2904
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002905 private:
David Brazdil1abb4192015-02-17 18:33:36 +00002906 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002907 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002908 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002909 bool is_live_;
Calin Juravle10e244f2015-01-26 18:54:32 +00002910 bool can_be_null_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002911
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002912 DISALLOW_COPY_AND_ASSIGN(HPhi);
2913};
2914
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002915class HNullCheck : public HExpression<1> {
2916 public:
2917 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002918 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002919 SetRawInputAt(0, value);
2920 }
2921
Calin Juravle10e244f2015-01-26 18:54:32 +00002922 bool CanBeMoved() const OVERRIDE { return true; }
2923 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002924 UNUSED(other);
2925 return true;
2926 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002927
Calin Juravle10e244f2015-01-26 18:54:32 +00002928 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002929
Calin Juravle10e244f2015-01-26 18:54:32 +00002930 bool CanThrow() const OVERRIDE { return true; }
2931
2932 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01002933
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002934 uint32_t GetDexPc() const { return dex_pc_; }
2935
2936 DECLARE_INSTRUCTION(NullCheck);
2937
2938 private:
2939 const uint32_t dex_pc_;
2940
2941 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
2942};
2943
2944class FieldInfo : public ValueObject {
2945 public:
Calin Juravle52c48962014-12-16 17:02:57 +00002946 FieldInfo(MemberOffset field_offset, Primitive::Type field_type, bool is_volatile)
2947 : field_offset_(field_offset), field_type_(field_type), is_volatile_(is_volatile) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002948
2949 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002950 Primitive::Type GetFieldType() const { return field_type_; }
Calin Juravle52c48962014-12-16 17:02:57 +00002951 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002952
2953 private:
2954 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01002955 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00002956 const bool is_volatile_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002957};
2958
2959class HInstanceFieldGet : public HExpression<1> {
2960 public:
2961 HInstanceFieldGet(HInstruction* value,
2962 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00002963 MemberOffset field_offset,
2964 bool is_volatile)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002965 : HExpression(field_type, SideEffects::DependsOnSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00002966 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002967 SetRawInputAt(0, value);
2968 }
2969
Calin Juravle10c9cbe2014-12-19 10:50:19 +00002970 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002971
2972 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2973 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
2974 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002975 }
2976
Calin Juravle641547a2015-04-21 22:08:51 +01002977 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
2978 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00002979 }
2980
2981 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002982 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
2983 }
2984
Calin Juravle52c48962014-12-16 17:02:57 +00002985 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002986 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002987 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002988 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002989
2990 DECLARE_INSTRUCTION(InstanceFieldGet);
2991
2992 private:
2993 const FieldInfo field_info_;
2994
2995 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
2996};
2997
2998class HInstanceFieldSet : public HTemplateInstruction<2> {
2999 public:
3000 HInstanceFieldSet(HInstruction* object,
3001 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01003002 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003003 MemberOffset field_offset,
3004 bool is_volatile)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003005 : HTemplateInstruction(SideEffects::ChangesSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00003006 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003007 SetRawInputAt(0, object);
3008 SetRawInputAt(1, value);
3009 }
3010
Calin Juravle641547a2015-04-21 22:08:51 +01003011 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3012 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00003013 }
3014
Calin Juravle52c48962014-12-16 17:02:57 +00003015 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003016 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003017 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003018 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003019 HInstruction* GetValue() const { return InputAt(1); }
3020
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003021 DECLARE_INSTRUCTION(InstanceFieldSet);
3022
3023 private:
3024 const FieldInfo field_info_;
3025
3026 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
3027};
3028
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003029class HArrayGet : public HExpression<2> {
3030 public:
3031 HArrayGet(HInstruction* array, HInstruction* index, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003032 : HExpression(type, SideEffects::DependsOnSomething()) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003033 SetRawInputAt(0, array);
3034 SetRawInputAt(1, index);
3035 }
3036
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003037 bool CanBeMoved() const OVERRIDE { return true; }
3038 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003039 UNUSED(other);
3040 return true;
3041 }
Calin Juravle641547a2015-04-21 22:08:51 +01003042 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3043 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00003044 // TODO: We can be smarter here.
3045 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
3046 // which generates the implicit null check. There are cases when these can be removed
3047 // to produce better code. If we ever add optimizations to do so we should allow an
3048 // implicit check here (as long as the address falls in the first page).
3049 return false;
3050 }
3051
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003052 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003053
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003054 HInstruction* GetArray() const { return InputAt(0); }
3055 HInstruction* GetIndex() const { return InputAt(1); }
3056
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003057 DECLARE_INSTRUCTION(ArrayGet);
3058
3059 private:
3060 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
3061};
3062
3063class HArraySet : public HTemplateInstruction<3> {
3064 public:
3065 HArraySet(HInstruction* array,
3066 HInstruction* index,
3067 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003068 Primitive::Type expected_component_type,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003069 uint32_t dex_pc)
3070 : HTemplateInstruction(SideEffects::ChangesSomething()),
3071 dex_pc_(dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003072 expected_component_type_(expected_component_type),
3073 needs_type_check_(value->GetType() == Primitive::kPrimNot) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003074 SetRawInputAt(0, array);
3075 SetRawInputAt(1, index);
3076 SetRawInputAt(2, value);
3077 }
3078
Calin Juravle77520bc2015-01-12 18:45:46 +00003079 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003080 // We currently always call a runtime method to catch array store
3081 // exceptions.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003082 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003083 }
3084
Calin Juravle641547a2015-04-21 22:08:51 +01003085 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3086 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00003087 // TODO: Same as for ArrayGet.
3088 return false;
3089 }
3090
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003091 void ClearNeedsTypeCheck() {
3092 needs_type_check_ = false;
3093 }
3094
3095 bool NeedsTypeCheck() const { return needs_type_check_; }
3096
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003097 uint32_t GetDexPc() const { return dex_pc_; }
3098
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003099 HInstruction* GetArray() const { return InputAt(0); }
3100 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003101 HInstruction* GetValue() const { return InputAt(2); }
3102
3103 Primitive::Type GetComponentType() const {
3104 // The Dex format does not type floating point index operations. Since the
3105 // `expected_component_type_` is set during building and can therefore not
3106 // be correct, we also check what is the value type. If it is a floating
3107 // point type, we must use that type.
3108 Primitive::Type value_type = GetValue()->GetType();
3109 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
3110 ? value_type
3111 : expected_component_type_;
3112 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003113
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003114 DECLARE_INSTRUCTION(ArraySet);
3115
3116 private:
3117 const uint32_t dex_pc_;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003118 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003119 bool needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003120
3121 DISALLOW_COPY_AND_ASSIGN(HArraySet);
3122};
3123
3124class HArrayLength : public HExpression<1> {
3125 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003126 explicit HArrayLength(HInstruction* array)
3127 : HExpression(Primitive::kPrimInt, SideEffects::None()) {
3128 // Note that arrays do not change length, so the instruction does not
3129 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003130 SetRawInputAt(0, array);
3131 }
3132
Calin Juravle77520bc2015-01-12 18:45:46 +00003133 bool CanBeMoved() const OVERRIDE { return true; }
3134 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003135 UNUSED(other);
3136 return true;
3137 }
Calin Juravle641547a2015-04-21 22:08:51 +01003138 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3139 return obj == InputAt(0);
3140 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003141
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003142 DECLARE_INSTRUCTION(ArrayLength);
3143
3144 private:
3145 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
3146};
3147
3148class HBoundsCheck : public HExpression<2> {
3149 public:
3150 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003151 : HExpression(index->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003152 DCHECK(index->GetType() == Primitive::kPrimInt);
3153 SetRawInputAt(0, index);
3154 SetRawInputAt(1, length);
3155 }
3156
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003157 bool CanBeMoved() const OVERRIDE { return true; }
3158 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003159 UNUSED(other);
3160 return true;
3161 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003162
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003163 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003164
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003165 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01003166
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003167 uint32_t GetDexPc() const { return dex_pc_; }
3168
3169 DECLARE_INSTRUCTION(BoundsCheck);
3170
3171 private:
3172 const uint32_t dex_pc_;
3173
3174 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
3175};
3176
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003177/**
3178 * Some DEX instructions are folded into multiple HInstructions that need
3179 * to stay live until the last HInstruction. This class
3180 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00003181 * HInstruction stays live. `index` represents the stack location index of the
3182 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003183 */
3184class HTemporary : public HTemplateInstruction<0> {
3185 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003186 explicit HTemporary(size_t index) : HTemplateInstruction(SideEffects::None()), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003187
3188 size_t GetIndex() const { return index_; }
3189
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00003190 Primitive::Type GetType() const OVERRIDE {
3191 // The previous instruction is the one that will be stored in the temporary location.
3192 DCHECK(GetPrevious() != nullptr);
3193 return GetPrevious()->GetType();
3194 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00003195
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003196 DECLARE_INSTRUCTION(Temporary);
3197
3198 private:
3199 const size_t index_;
3200
3201 DISALLOW_COPY_AND_ASSIGN(HTemporary);
3202};
3203
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003204class HSuspendCheck : public HTemplateInstruction<0> {
3205 public:
3206 explicit HSuspendCheck(uint32_t dex_pc)
Nicolas Geoffray9ebc72c2014-09-25 16:33:42 +01003207 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003208
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003209 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003210 return true;
3211 }
3212
3213 uint32_t GetDexPc() const { return dex_pc_; }
3214
3215 DECLARE_INSTRUCTION(SuspendCheck);
3216
3217 private:
3218 const uint32_t dex_pc_;
3219
3220 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
3221};
3222
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003223/**
3224 * Instruction to load a Class object.
3225 */
3226class HLoadClass : public HExpression<0> {
3227 public:
3228 HLoadClass(uint16_t type_index,
3229 bool is_referrers_class,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003230 uint32_t dex_pc)
3231 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3232 type_index_(type_index),
3233 is_referrers_class_(is_referrers_class),
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003234 dex_pc_(dex_pc),
Calin Juravleb1498f62015-02-16 13:13:29 +00003235 generate_clinit_check_(false),
3236 loaded_class_rti_(ReferenceTypeInfo::CreateTop(/* is_exact */ false)) {}
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003237
3238 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003239
3240 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3241 return other->AsLoadClass()->type_index_ == type_index_;
3242 }
3243
3244 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
3245
3246 uint32_t GetDexPc() const { return dex_pc_; }
3247 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003248 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003249
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003250 bool NeedsEnvironment() const OVERRIDE {
3251 // Will call runtime and load the class if the class is not loaded yet.
3252 // TODO: finer grain decision.
3253 return !is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003254 }
3255
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003256 bool MustGenerateClinitCheck() const {
3257 return generate_clinit_check_;
3258 }
3259
3260 void SetMustGenerateClinitCheck() {
3261 generate_clinit_check_ = true;
3262 }
3263
3264 bool CanCallRuntime() const {
3265 return MustGenerateClinitCheck() || !is_referrers_class_;
3266 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003267
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003268 bool CanThrow() const OVERRIDE {
3269 // May call runtime and and therefore can throw.
3270 // TODO: finer grain decision.
3271 return !is_referrers_class_;
3272 }
3273
Calin Juravleacf735c2015-02-12 15:25:22 +00003274 ReferenceTypeInfo GetLoadedClassRTI() {
3275 return loaded_class_rti_;
3276 }
3277
3278 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
3279 // Make sure we only set exact types (the loaded class should never be merged).
3280 DCHECK(rti.IsExact());
3281 loaded_class_rti_ = rti;
3282 }
3283
3284 bool IsResolved() {
3285 return loaded_class_rti_.IsExact();
3286 }
3287
Nicolas Geoffray9437b782015-03-25 10:08:51 +00003288 bool NeedsDexCache() const OVERRIDE { return !is_referrers_class_; }
3289
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003290 DECLARE_INSTRUCTION(LoadClass);
3291
3292 private:
3293 const uint16_t type_index_;
3294 const bool is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003295 const uint32_t dex_pc_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003296 // Whether this instruction must generate the initialization check.
3297 // Used for code generation.
3298 bool generate_clinit_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003299
Calin Juravleacf735c2015-02-12 15:25:22 +00003300 ReferenceTypeInfo loaded_class_rti_;
3301
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003302 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
3303};
3304
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003305class HLoadString : public HExpression<0> {
3306 public:
3307 HLoadString(uint32_t string_index, uint32_t dex_pc)
3308 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3309 string_index_(string_index),
3310 dex_pc_(dex_pc) {}
3311
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003312 bool CanBeMoved() const OVERRIDE { return true; }
3313
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003314 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3315 return other->AsLoadString()->string_index_ == string_index_;
3316 }
3317
3318 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
3319
3320 uint32_t GetDexPc() const { return dex_pc_; }
3321 uint32_t GetStringIndex() const { return string_index_; }
3322
3323 // TODO: Can we deopt or debug when we resolve a string?
3324 bool NeedsEnvironment() const OVERRIDE { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00003325 bool NeedsDexCache() const OVERRIDE { return true; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003326
3327 DECLARE_INSTRUCTION(LoadString);
3328
3329 private:
3330 const uint32_t string_index_;
3331 const uint32_t dex_pc_;
3332
3333 DISALLOW_COPY_AND_ASSIGN(HLoadString);
3334};
3335
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003336/**
3337 * Performs an initialization check on its Class object input.
3338 */
3339class HClinitCheck : public HExpression<1> {
3340 public:
3341 explicit HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Nicolas Geoffraya0466e12015-03-27 15:00:40 +00003342 : HExpression(Primitive::kPrimNot, SideEffects::ChangesSomething()),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003343 dex_pc_(dex_pc) {
3344 SetRawInputAt(0, constant);
3345 }
3346
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003347 bool CanBeMoved() const OVERRIDE { return true; }
3348 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3349 UNUSED(other);
3350 return true;
3351 }
3352
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003353 bool NeedsEnvironment() const OVERRIDE {
3354 // May call runtime to initialize the class.
3355 return true;
3356 }
3357
3358 uint32_t GetDexPc() const { return dex_pc_; }
3359
3360 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
3361
3362 DECLARE_INSTRUCTION(ClinitCheck);
3363
3364 private:
3365 const uint32_t dex_pc_;
3366
3367 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
3368};
3369
3370class HStaticFieldGet : public HExpression<1> {
3371 public:
3372 HStaticFieldGet(HInstruction* cls,
3373 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003374 MemberOffset field_offset,
3375 bool is_volatile)
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003376 : HExpression(field_type, SideEffects::DependsOnSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00003377 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003378 SetRawInputAt(0, cls);
3379 }
3380
Calin Juravle52c48962014-12-16 17:02:57 +00003381
Calin Juravle10c9cbe2014-12-19 10:50:19 +00003382 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003383
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003384 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00003385 HStaticFieldGet* other_get = other->AsStaticFieldGet();
3386 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003387 }
3388
3389 size_t ComputeHashCode() const OVERRIDE {
3390 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
3391 }
3392
Calin Juravle52c48962014-12-16 17:02:57 +00003393 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003394 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
3395 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003396 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003397
3398 DECLARE_INSTRUCTION(StaticFieldGet);
3399
3400 private:
3401 const FieldInfo field_info_;
3402
3403 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
3404};
3405
3406class HStaticFieldSet : public HTemplateInstruction<2> {
3407 public:
3408 HStaticFieldSet(HInstruction* cls,
3409 HInstruction* value,
3410 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003411 MemberOffset field_offset,
3412 bool is_volatile)
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003413 : HTemplateInstruction(SideEffects::ChangesSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00003414 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003415 SetRawInputAt(0, cls);
3416 SetRawInputAt(1, value);
3417 }
3418
Calin Juravle52c48962014-12-16 17:02:57 +00003419 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003420 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
3421 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003422 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003423
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003424 HInstruction* GetValue() const { return InputAt(1); }
3425
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003426 DECLARE_INSTRUCTION(StaticFieldSet);
3427
3428 private:
3429 const FieldInfo field_info_;
3430
3431 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
3432};
3433
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003434// Implement the move-exception DEX instruction.
3435class HLoadException : public HExpression<0> {
3436 public:
3437 HLoadException() : HExpression(Primitive::kPrimNot, SideEffects::None()) {}
3438
3439 DECLARE_INSTRUCTION(LoadException);
3440
3441 private:
3442 DISALLOW_COPY_AND_ASSIGN(HLoadException);
3443};
3444
3445class HThrow : public HTemplateInstruction<1> {
3446 public:
3447 HThrow(HInstruction* exception, uint32_t dex_pc)
3448 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {
3449 SetRawInputAt(0, exception);
3450 }
3451
3452 bool IsControlFlow() const OVERRIDE { return true; }
3453
3454 bool NeedsEnvironment() const OVERRIDE { return true; }
3455
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003456 bool CanThrow() const OVERRIDE { return true; }
3457
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003458 uint32_t GetDexPc() const { return dex_pc_; }
3459
3460 DECLARE_INSTRUCTION(Throw);
3461
3462 private:
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003463 const uint32_t dex_pc_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003464
3465 DISALLOW_COPY_AND_ASSIGN(HThrow);
3466};
3467
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003468class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003469 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003470 HInstanceOf(HInstruction* object,
3471 HLoadClass* constant,
3472 bool class_is_final,
3473 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003474 : HExpression(Primitive::kPrimBoolean, SideEffects::None()),
3475 class_is_final_(class_is_final),
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003476 must_do_null_check_(true),
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003477 dex_pc_(dex_pc) {
3478 SetRawInputAt(0, object);
3479 SetRawInputAt(1, constant);
3480 }
3481
3482 bool CanBeMoved() const OVERRIDE { return true; }
3483
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003484 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003485 return true;
3486 }
3487
3488 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003489 return false;
3490 }
3491
3492 uint32_t GetDexPc() const { return dex_pc_; }
3493
3494 bool IsClassFinal() const { return class_is_final_; }
3495
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003496 // Used only in code generation.
3497 bool MustDoNullCheck() const { return must_do_null_check_; }
3498 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
3499
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003500 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003501
3502 private:
3503 const bool class_is_final_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003504 bool must_do_null_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003505 const uint32_t dex_pc_;
3506
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003507 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
3508};
3509
Calin Juravleb1498f62015-02-16 13:13:29 +00003510class HBoundType : public HExpression<1> {
3511 public:
3512 HBoundType(HInstruction* input, ReferenceTypeInfo bound_type)
3513 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3514 bound_type_(bound_type) {
Calin Juravle61d544b2015-02-23 16:46:57 +00003515 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00003516 SetRawInputAt(0, input);
3517 }
3518
3519 const ReferenceTypeInfo& GetBoundType() const { return bound_type_; }
3520
3521 bool CanBeNull() const OVERRIDE {
3522 // `null instanceof ClassX` always return false so we can't be null.
3523 return false;
3524 }
3525
3526 DECLARE_INSTRUCTION(BoundType);
3527
3528 private:
3529 // Encodes the most upper class that this instruction can have. In other words
3530 // it is always the case that GetBoundType().IsSupertypeOf(GetReferenceType()).
3531 // It is used to bound the type in cases like `if (x instanceof ClassX) {}`
3532 const ReferenceTypeInfo bound_type_;
3533
3534 DISALLOW_COPY_AND_ASSIGN(HBoundType);
3535};
3536
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003537class HCheckCast : public HTemplateInstruction<2> {
3538 public:
3539 HCheckCast(HInstruction* object,
3540 HLoadClass* constant,
3541 bool class_is_final,
3542 uint32_t dex_pc)
3543 : HTemplateInstruction(SideEffects::None()),
3544 class_is_final_(class_is_final),
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003545 must_do_null_check_(true),
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003546 dex_pc_(dex_pc) {
3547 SetRawInputAt(0, object);
3548 SetRawInputAt(1, constant);
3549 }
3550
3551 bool CanBeMoved() const OVERRIDE { return true; }
3552
3553 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
3554 return true;
3555 }
3556
3557 bool NeedsEnvironment() const OVERRIDE {
3558 // Instruction may throw a CheckCastError.
3559 return true;
3560 }
3561
3562 bool CanThrow() const OVERRIDE { return true; }
3563
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003564 bool MustDoNullCheck() const { return must_do_null_check_; }
3565 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
3566
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003567 uint32_t GetDexPc() const { return dex_pc_; }
3568
3569 bool IsClassFinal() const { return class_is_final_; }
3570
3571 DECLARE_INSTRUCTION(CheckCast);
3572
3573 private:
3574 const bool class_is_final_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003575 bool must_do_null_check_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003576 const uint32_t dex_pc_;
3577
3578 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003579};
3580
Calin Juravle27df7582015-04-17 19:12:31 +01003581class HMemoryBarrier : public HTemplateInstruction<0> {
3582 public:
3583 explicit HMemoryBarrier(MemBarrierKind barrier_kind)
3584 : HTemplateInstruction(SideEffects::None()),
3585 barrier_kind_(barrier_kind) {}
3586
3587 MemBarrierKind GetBarrierKind() { return barrier_kind_; }
3588
3589 DECLARE_INSTRUCTION(MemoryBarrier);
3590
3591 private:
3592 const MemBarrierKind barrier_kind_;
3593
3594 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
3595};
3596
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003597class HMonitorOperation : public HTemplateInstruction<1> {
3598 public:
3599 enum OperationKind {
3600 kEnter,
3601 kExit,
3602 };
3603
3604 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
3605 : HTemplateInstruction(SideEffects::None()), kind_(kind), dex_pc_(dex_pc) {
3606 SetRawInputAt(0, object);
3607 }
3608
3609 // Instruction may throw a Java exception, so we need an environment.
3610 bool NeedsEnvironment() const OVERRIDE { return true; }
3611 bool CanThrow() const OVERRIDE { return true; }
3612
3613 uint32_t GetDexPc() const { return dex_pc_; }
3614
3615 bool IsEnter() const { return kind_ == kEnter; }
3616
3617 DECLARE_INSTRUCTION(MonitorOperation);
3618
Calin Juravle52c48962014-12-16 17:02:57 +00003619 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003620 const OperationKind kind_;
3621 const uint32_t dex_pc_;
3622
3623 private:
3624 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
3625};
3626
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003627class MoveOperands : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003628 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01003629 MoveOperands(Location source,
3630 Location destination,
3631 Primitive::Type type,
3632 HInstruction* instruction)
3633 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003634
3635 Location GetSource() const { return source_; }
3636 Location GetDestination() const { return destination_; }
3637
3638 void SetSource(Location value) { source_ = value; }
3639 void SetDestination(Location value) { destination_ = value; }
3640
3641 // The parallel move resolver marks moves as "in-progress" by clearing the
3642 // destination (but not the source).
3643 Location MarkPending() {
3644 DCHECK(!IsPending());
3645 Location dest = destination_;
3646 destination_ = Location::NoLocation();
3647 return dest;
3648 }
3649
3650 void ClearPending(Location dest) {
3651 DCHECK(IsPending());
3652 destination_ = dest;
3653 }
3654
3655 bool IsPending() const {
3656 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
3657 return destination_.IsInvalid() && !source_.IsInvalid();
3658 }
3659
3660 // True if this blocks a move from the given location.
3661 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08003662 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003663 }
3664
3665 // A move is redundant if it's been eliminated, if its source and
3666 // destination are the same, or if its destination is unneeded.
3667 bool IsRedundant() const {
3668 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
3669 }
3670
3671 // We clear both operands to indicate move that's been eliminated.
3672 void Eliminate() {
3673 source_ = destination_ = Location::NoLocation();
3674 }
3675
3676 bool IsEliminated() const {
3677 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
3678 return source_.IsInvalid();
3679 }
3680
Nicolas Geoffray90218252015-04-15 11:56:51 +01003681 bool Is64BitMove() const {
3682 return Primitive::Is64BitType(type_);
3683 }
3684
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003685 HInstruction* GetInstruction() const { return instruction_; }
3686
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003687 private:
3688 Location source_;
3689 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01003690 // The type this move is for.
3691 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003692 // The instruction this move is assocatied with. Null when this move is
3693 // for moving an input in the expected locations of user (including a phi user).
3694 // This is only used in debug mode, to ensure we do not connect interval siblings
3695 // in the same parallel move.
3696 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003697};
3698
3699static constexpr size_t kDefaultNumberOfMoves = 4;
3700
3701class HParallelMove : public HTemplateInstruction<0> {
3702 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003703 explicit HParallelMove(ArenaAllocator* arena)
3704 : HTemplateInstruction(SideEffects::None()), moves_(arena, kDefaultNumberOfMoves) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003705
Nicolas Geoffray90218252015-04-15 11:56:51 +01003706 void AddMove(Location source,
3707 Location destination,
3708 Primitive::Type type,
3709 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003710 DCHECK(source.IsValid());
3711 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003712 if (kIsDebugBuild) {
3713 if (instruction != nullptr) {
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00003714 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003715 if (moves_.Get(i).GetInstruction() == instruction) {
3716 // Special case the situation where the move is for the spill slot
3717 // of the instruction.
3718 if ((GetPrevious() == instruction)
3719 || ((GetPrevious() == nullptr)
3720 && instruction->IsPhi()
3721 && instruction->GetBlock() == GetBlock())) {
3722 DCHECK_NE(destination.GetKind(), moves_.Get(i).GetDestination().GetKind())
3723 << "Doing parallel moves for the same instruction.";
3724 } else {
3725 DCHECK(false) << "Doing parallel moves for the same instruction.";
3726 }
3727 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00003728 }
3729 }
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003730 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Zheng Xuad4450e2015-04-17 18:48:56 +08003731 DCHECK(!destination.OverlapsWith(moves_.Get(i).GetDestination()))
3732 << "Overlapped destination for two moves in a parallel move.";
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003733 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003734 }
Nicolas Geoffray90218252015-04-15 11:56:51 +01003735 moves_.Add(MoveOperands(source, destination, type, instruction));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003736 }
3737
3738 MoveOperands* MoveOperandsAt(size_t index) const {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003739 return moves_.GetRawStorage() + index;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003740 }
3741
3742 size_t NumMoves() const { return moves_.Size(); }
3743
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003744 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003745
3746 private:
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003747 GrowableArray<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003748
3749 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
3750};
3751
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003752class HGraphVisitor : public ValueObject {
3753 public:
Dave Allison20dfc792014-06-16 20:44:29 -07003754 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
3755 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003756
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003757 virtual void VisitInstruction(HInstruction* instruction) { UNUSED(instruction); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003758 virtual void VisitBasicBlock(HBasicBlock* block);
3759
Roland Levillain633021e2014-10-01 14:12:25 +01003760 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003761 void VisitInsertionOrder();
3762
Roland Levillain633021e2014-10-01 14:12:25 +01003763 // Visit the graph following dominator tree reverse post-order.
3764 void VisitReversePostOrder();
3765
Nicolas Geoffray787c3072014-03-17 10:20:19 +00003766 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00003767
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003768 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003769#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003770 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
3771
3772 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
3773
3774#undef DECLARE_VISIT_INSTRUCTION
3775
3776 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07003777 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003778
3779 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
3780};
3781
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003782class HGraphDelegateVisitor : public HGraphVisitor {
3783 public:
3784 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
3785 virtual ~HGraphDelegateVisitor() {}
3786
3787 // Visit functions that delegate to to super class.
3788#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003789 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003790
3791 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
3792
3793#undef DECLARE_VISIT_INSTRUCTION
3794
3795 private:
3796 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
3797};
3798
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003799class HInsertionOrderIterator : public ValueObject {
3800 public:
3801 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
3802
3803 bool Done() const { return index_ == graph_.GetBlocks().Size(); }
3804 HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); }
3805 void Advance() { ++index_; }
3806
3807 private:
3808 const HGraph& graph_;
3809 size_t index_;
3810
3811 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
3812};
3813
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003814class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003815 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00003816 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
3817 // Check that reverse post order of the graph has been built.
3818 DCHECK(!graph.GetReversePostOrder().IsEmpty());
3819 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003820
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003821 bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); }
3822 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003823 void Advance() { ++index_; }
3824
3825 private:
3826 const HGraph& graph_;
3827 size_t index_;
3828
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003829 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003830};
3831
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003832class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003833 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003834 explicit HPostOrderIterator(const HGraph& graph)
David Brazdil10f56cb2015-03-24 18:49:14 +00003835 : graph_(graph), index_(graph_.GetReversePostOrder().Size()) {
3836 // Check that reverse post order of the graph has been built.
3837 DCHECK(!graph.GetReversePostOrder().IsEmpty());
3838 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003839
3840 bool Done() const { return index_ == 0; }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003841 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003842 void Advance() { --index_; }
3843
3844 private:
3845 const HGraph& graph_;
3846 size_t index_;
3847
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003848 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003849};
3850
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01003851class HLinearPostOrderIterator : public ValueObject {
3852 public:
3853 explicit HLinearPostOrderIterator(const HGraph& graph)
3854 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().Size()) {}
3855
3856 bool Done() const { return index_ == 0; }
3857
3858 HBasicBlock* Current() const { return order_.Get(index_ -1); }
3859
3860 void Advance() {
3861 --index_;
3862 DCHECK_GE(index_, 0U);
3863 }
3864
3865 private:
3866 const GrowableArray<HBasicBlock*>& order_;
3867 size_t index_;
3868
3869 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
3870};
3871
3872class HLinearOrderIterator : public ValueObject {
3873 public:
3874 explicit HLinearOrderIterator(const HGraph& graph)
3875 : order_(graph.GetLinearOrder()), index_(0) {}
3876
3877 bool Done() const { return index_ == order_.Size(); }
3878 HBasicBlock* Current() const { return order_.Get(index_); }
3879 void Advance() { ++index_; }
3880
3881 private:
3882 const GrowableArray<HBasicBlock*>& order_;
3883 size_t index_;
3884
3885 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
3886};
3887
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003888// Iterator over the blocks that art part of the loop. Includes blocks part
3889// of an inner loop. The order in which the blocks are iterated is on their
3890// block id.
3891class HBlocksInLoopIterator : public ValueObject {
3892 public:
3893 explicit HBlocksInLoopIterator(const HLoopInformation& info)
3894 : blocks_in_loop_(info.GetBlocks()),
3895 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
3896 index_(0) {
3897 if (!blocks_in_loop_.IsBitSet(index_)) {
3898 Advance();
3899 }
3900 }
3901
3902 bool Done() const { return index_ == blocks_.Size(); }
3903 HBasicBlock* Current() const { return blocks_.Get(index_); }
3904 void Advance() {
3905 ++index_;
3906 for (size_t e = blocks_.Size(); index_ < e; ++index_) {
3907 if (blocks_in_loop_.IsBitSet(index_)) {
3908 break;
3909 }
3910 }
3911 }
3912
3913 private:
3914 const BitVector& blocks_in_loop_;
3915 const GrowableArray<HBasicBlock*>& blocks_;
3916 size_t index_;
3917
3918 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
3919};
3920
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003921inline int64_t Int64FromConstant(HConstant* constant) {
3922 DCHECK(constant->IsIntConstant() || constant->IsLongConstant());
3923 return constant->IsIntConstant() ? constant->AsIntConstant()->GetValue()
3924 : constant->AsLongConstant()->GetValue();
3925}
3926
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003927} // namespace art
3928
3929#endif // ART_COMPILER_OPTIMIZING_NODES_H_