blob: 25ef78064c994040631e236a670c0b60ecdcdfc4 [file] [log] [blame]
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ART_COMPILER_OPTIMIZING_NODES_H_
18#define ART_COMPILER_OPTIMIZING_NODES_H_
19
David Brazdil8d5b8b22015-03-24 10:51:52 +000020#include "base/arena_containers.h"
Mathieu Chartierb666f482015-02-18 14:33:14 -080021#include "base/arena_object.h"
Calin Juravle27df7582015-04-17 19:12:31 +010022#include "dex/compiler_enums.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000023#include "entrypoints/quick/quick_entrypoints_enum.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000024#include "handle.h"
25#include "handle_scope.h"
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000026#include "invoke_type.h"
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +010027#include "locations.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000028#include "mirror/class.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010029#include "offsets.h"
Ian Rogerse63db272014-07-15 15:36:11 -070030#include "primitive.h"
Nicolas Geoffray0e336432014-02-26 18:24:38 +000031#include "utils/arena_bit_vector.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000032#include "utils/growable_array.h"
33
34namespace art {
35
David Brazdil1abb4192015-02-17 18:33:36 +000036class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000037class HBasicBlock;
David Brazdil8d5b8b22015-03-24 10:51:52 +000038class HDoubleConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010039class HEnvironment;
David Brazdil8d5b8b22015-03-24 10:51:52 +000040class HFloatConstant;
41class HGraphVisitor;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000042class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000043class HIntConstant;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000044class HInvoke;
David Brazdil8d5b8b22015-03-24 10:51:52 +000045class HLongConstant;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +000046class HNullConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010047class HPhi;
Nicolas Geoffray3c049742014-09-24 18:10:46 +010048class HSuspendCheck;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010049class LiveInterval;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000050class LocationSummary;
David Brazdil8d5b8b22015-03-24 10:51:52 +000051class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000052
53static const int kDefaultNumberOfBlocks = 8;
54static const int kDefaultNumberOfSuccessors = 2;
55static const int kDefaultNumberOfPredecessors = 2;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010056static const int kDefaultNumberOfDominatedBlocks = 1;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000057static const int kDefaultNumberOfBackEdges = 1;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000058
Calin Juravle9aec02f2014-11-18 23:06:35 +000059static constexpr uint32_t kMaxIntShiftValue = 0x1f;
60static constexpr uint64_t kMaxLongShiftValue = 0x3f;
61
Dave Allison20dfc792014-06-16 20:44:29 -070062enum IfCondition {
63 kCondEQ,
64 kCondNE,
65 kCondLT,
66 kCondLE,
67 kCondGT,
68 kCondGE,
69};
70
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010071class HInstructionList {
72 public:
73 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
74
75 void AddInstruction(HInstruction* instruction);
76 void RemoveInstruction(HInstruction* instruction);
77
David Brazdilc3d743f2015-04-22 13:40:50 +010078 // Insert `instruction` before/after an existing instruction `cursor`.
79 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
80 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
81
Roland Levillain6b469232014-09-25 10:10:38 +010082 // Return true if this list contains `instruction`.
83 bool Contains(HInstruction* instruction) const;
84
Roland Levillainccc07a92014-09-16 14:48:16 +010085 // Return true if `instruction1` is found before `instruction2` in
86 // this instruction list and false otherwise. Abort if none
87 // of these instructions is found.
88 bool FoundBefore(const HInstruction* instruction1,
89 const HInstruction* instruction2) const;
90
Nicolas Geoffray276d9da2015-02-02 18:24:11 +000091 bool IsEmpty() const { return first_instruction_ == nullptr; }
92 void Clear() { first_instruction_ = last_instruction_ = nullptr; }
93
94 // Update the block of all instructions to be `block`.
95 void SetBlockOfInstructions(HBasicBlock* block) const;
96
97 void AddAfter(HInstruction* cursor, const HInstructionList& instruction_list);
98 void Add(const HInstructionList& instruction_list);
99
David Brazdil2d7352b2015-04-20 14:52:42 +0100100 // Return the number of instructions in the list. This is an expensive operation.
101 size_t CountSize() const;
102
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100103 private:
104 HInstruction* first_instruction_;
105 HInstruction* last_instruction_;
106
107 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000108 friend class HGraph;
109 friend class HInstruction;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100110 friend class HInstructionIterator;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100111 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100112
113 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
114};
115
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000116// Control-flow graph of a method. Contains a list of basic blocks.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700117class HGraph : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000118 public:
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000119 HGraph(ArenaAllocator* arena, bool debuggable = false, int start_instruction_id = 0)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000120 : arena_(arena),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000121 blocks_(arena, kDefaultNumberOfBlocks),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100122 reverse_post_order_(arena, kDefaultNumberOfBlocks),
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100123 linear_order_(arena, kDefaultNumberOfBlocks),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700124 entry_block_(nullptr),
125 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100126 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100127 number_of_vregs_(0),
128 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000129 temporaries_vreg_slots_(0),
Mark Mendell1152c922015-04-24 17:06:35 -0400130 has_bounds_checks_(false),
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000131 debuggable_(debuggable),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000132 current_instruction_id_(start_instruction_id),
133 cached_null_constant_(nullptr),
134 cached_int_constants_(std::less<int32_t>(), arena->Adapter()),
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000135 cached_float_constants_(std::less<int32_t>(), arena->Adapter()),
136 cached_long_constants_(std::less<int64_t>(), arena->Adapter()),
137 cached_double_constants_(std::less<int64_t>(), arena->Adapter()) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000138
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000139 ArenaAllocator* GetArena() const { return arena_; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100140 const GrowableArray<HBasicBlock*>& GetBlocks() const { return blocks_; }
Roland Levillain93445682014-10-06 19:24:02 +0100141 HBasicBlock* GetBlock(size_t id) const { return blocks_.Get(id); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000142
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000143 HBasicBlock* GetEntryBlock() const { return entry_block_; }
144 HBasicBlock* GetExitBlock() const { return exit_block_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000145
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000146 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
147 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000148
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000149 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100150
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000151 // Try building the SSA form of this graph, with dominance computation and loop
152 // recognition. Returns whether it was successful in doing all these steps.
153 bool TryBuildingSsa() {
154 BuildDominatorTree();
Nicolas Geoffrayd3350832015-03-12 11:16:23 +0000155 // The SSA builder requires loops to all be natural. Specifically, the dead phi
156 // elimination phase checks the consistency of the graph when doing a post-order
157 // visit for eliminating dead phis: a dead phi can only have loop header phi
158 // users remaining when being visited.
159 if (!AnalyzeNaturalLoops()) return false;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000160 TransformToSsa();
Nicolas Geoffrayd3350832015-03-12 11:16:23 +0000161 return true;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000162 }
163
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000164 void BuildDominatorTree();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000165 void TransformToSsa();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100166 void SimplifyCFG();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000167
Nicolas Geoffrayf5370122014-12-02 11:51:19 +0000168 // Analyze all natural loops in this graph. Returns false if one
169 // loop is not natural, that is the header does not dominate the
170 // back edge.
171 bool AnalyzeNaturalLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100172
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000173 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
174 void InlineInto(HGraph* outer_graph, HInvoke* invoke);
175
David Brazdil2d7352b2015-04-20 14:52:42 +0100176 // Removes `block` from the graph.
177 void DeleteDeadBlock(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000178
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100179 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
180 void SimplifyLoop(HBasicBlock* header);
181
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000182 int32_t GetNextInstructionId() {
183 DCHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000184 return current_instruction_id_++;
185 }
186
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000187 int32_t GetCurrentInstructionId() const {
188 return current_instruction_id_;
189 }
190
191 void SetCurrentInstructionId(int32_t id) {
192 current_instruction_id_ = id;
193 }
194
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100195 uint16_t GetMaximumNumberOfOutVRegs() const {
196 return maximum_number_of_out_vregs_;
197 }
198
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000199 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
200 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100201 }
202
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000203 void UpdateTemporariesVRegSlots(size_t slots) {
204 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100205 }
206
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000207 size_t GetTemporariesVRegSlots() const {
208 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100209 }
210
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100211 void SetNumberOfVRegs(uint16_t number_of_vregs) {
212 number_of_vregs_ = number_of_vregs;
213 }
214
215 uint16_t GetNumberOfVRegs() const {
216 return number_of_vregs_;
217 }
218
219 void SetNumberOfInVRegs(uint16_t value) {
220 number_of_in_vregs_ = value;
221 }
222
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100223 uint16_t GetNumberOfLocalVRegs() const {
224 return number_of_vregs_ - number_of_in_vregs_;
225 }
226
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100227 const GrowableArray<HBasicBlock*>& GetReversePostOrder() const {
228 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100229 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100230
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100231 const GrowableArray<HBasicBlock*>& GetLinearOrder() const {
232 return linear_order_;
233 }
234
Mark Mendell1152c922015-04-24 17:06:35 -0400235 bool HasBoundsChecks() const {
236 return has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800237 }
238
Mark Mendell1152c922015-04-24 17:06:35 -0400239 void SetHasBoundsChecks(bool value) {
240 has_bounds_checks_ = value;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800241 }
242
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000243 bool IsDebuggable() const { return debuggable_; }
244
David Brazdil8d5b8b22015-03-24 10:51:52 +0000245 // Returns a constant of the given type and value. If it does not exist
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000246 // already, it is created and inserted into the graph. This method is only for
247 // integral types.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000248 HConstant* GetConstant(Primitive::Type type, int64_t value);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000249 HNullConstant* GetNullConstant();
David Brazdil8d5b8b22015-03-24 10:51:52 +0000250 HIntConstant* GetIntConstant(int32_t value) {
251 return CreateConstant(value, &cached_int_constants_);
252 }
253 HLongConstant* GetLongConstant(int64_t value) {
254 return CreateConstant(value, &cached_long_constants_);
255 }
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000256 HFloatConstant* GetFloatConstant(float value) {
257 return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_);
258 }
259 HDoubleConstant* GetDoubleConstant(double value) {
260 return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_);
261 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000262
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000263 HBasicBlock* FindCommonDominator(HBasicBlock* first, HBasicBlock* second) const;
David Brazdil2d7352b2015-04-20 14:52:42 +0100264
265 private:
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000266 void VisitBlockForDominatorTree(HBasicBlock* block,
267 HBasicBlock* predecessor,
268 GrowableArray<size_t>* visits);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100269 void FindBackEdges(ArenaBitVector* visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000270 void VisitBlockForBackEdges(HBasicBlock* block,
271 ArenaBitVector* visited,
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100272 ArenaBitVector* visiting);
Roland Levillainfc600dc2014-12-02 17:16:31 +0000273 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100274 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000275
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000276 template <class InstructionType, typename ValueType>
277 InstructionType* CreateConstant(ValueType value,
278 ArenaSafeMap<ValueType, InstructionType*>* cache) {
279 // Try to find an existing constant of the given value.
280 InstructionType* constant = nullptr;
281 auto cached_constant = cache->find(value);
282 if (cached_constant != cache->end()) {
283 constant = cached_constant->second;
284 }
285
286 // If not found or previously deleted, create and cache a new instruction.
287 if (constant == nullptr || constant->GetBlock() == nullptr) {
288 constant = new (arena_) InstructionType(value);
289 cache->Overwrite(value, constant);
290 InsertConstant(constant);
291 }
292 return constant;
293 }
294
David Brazdil8d5b8b22015-03-24 10:51:52 +0000295 void InsertConstant(HConstant* instruction);
296
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000297 // Cache a float constant into the graph. This method should only be
298 // called by the SsaBuilder when creating "equivalent" instructions.
299 void CacheFloatConstant(HFloatConstant* constant);
300
301 // See CacheFloatConstant comment.
302 void CacheDoubleConstant(HDoubleConstant* constant);
303
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000304 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000305
306 // List of blocks in insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000307 GrowableArray<HBasicBlock*> blocks_;
308
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100309 // List of blocks to perform a reverse post order tree traversal.
310 GrowableArray<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000311
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100312 // List of blocks to perform a linear order tree traversal.
313 GrowableArray<HBasicBlock*> linear_order_;
314
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000315 HBasicBlock* entry_block_;
316 HBasicBlock* exit_block_;
317
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100318 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100319 uint16_t maximum_number_of_out_vregs_;
320
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100321 // The number of virtual registers in this method. Contains the parameters.
322 uint16_t number_of_vregs_;
323
324 // The number of virtual registers used by parameters of this method.
325 uint16_t number_of_in_vregs_;
326
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000327 // Number of vreg size slots that the temporaries use (used in baseline compiler).
328 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100329
Mark Mendell1152c922015-04-24 17:06:35 -0400330 // Has bounds checks. We can totally skip BCE if it's false.
331 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800332
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000333 // Indicates whether the graph should be compiled in a way that
334 // ensures full debuggability. If false, we can apply more
335 // aggressive optimizations that may limit the level of debugging.
336 const bool debuggable_;
337
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000338 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000339 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000340
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000341 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000342 HNullConstant* cached_null_constant_;
343 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000344 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000345 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000346 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000347
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000348 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100349 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000350 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000351 DISALLOW_COPY_AND_ASSIGN(HGraph);
352};
353
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700354class HLoopInformation : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000355 public:
356 HLoopInformation(HBasicBlock* header, HGraph* graph)
357 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100358 suspend_check_(nullptr),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100359 back_edges_(graph->GetArena(), kDefaultNumberOfBackEdges),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100360 // Make bit vector growable, as the number of blocks may change.
361 blocks_(graph->GetArena(), graph->GetBlocks().Size(), true) {}
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100362
363 HBasicBlock* GetHeader() const {
364 return header_;
365 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000366
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000367 void SetHeader(HBasicBlock* block) {
368 header_ = block;
369 }
370
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100371 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
372 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
373 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
374
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000375 void AddBackEdge(HBasicBlock* back_edge) {
376 back_edges_.Add(back_edge);
377 }
378
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100379 void RemoveBackEdge(HBasicBlock* back_edge) {
380 back_edges_.Delete(back_edge);
381 }
382
David Brazdil46e2a392015-03-16 17:31:52 +0000383 bool IsBackEdge(const HBasicBlock& block) const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100384 for (size_t i = 0, e = back_edges_.Size(); i < e; ++i) {
David Brazdil46e2a392015-03-16 17:31:52 +0000385 if (back_edges_.Get(i) == &block) return true;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100386 }
387 return false;
388 }
389
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000390 size_t NumberOfBackEdges() const {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000391 return back_edges_.Size();
392 }
393
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100394 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100395
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100396 const GrowableArray<HBasicBlock*>& GetBackEdges() const {
397 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100398 }
399
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100400 void ClearBackEdges() {
401 back_edges_.Reset();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100402 }
403
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100404 // Find blocks that are part of this loop. Returns whether the loop is a natural loop,
405 // that is the header dominates the back edge.
406 bool Populate();
407
408 // Returns whether this loop information contains `block`.
409 // Note that this loop information *must* be populated before entering this function.
410 bool Contains(const HBasicBlock& block) const;
411
412 // Returns whether this loop information is an inner loop of `other`.
413 // Note that `other` *must* be populated before entering this function.
414 bool IsIn(const HLoopInformation& other) const;
415
416 const ArenaBitVector& GetBlocks() const { return blocks_; }
417
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000418 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000419 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000420
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000421 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100422 // Internal recursive implementation of `Populate`.
423 void PopulateRecursive(HBasicBlock* block);
424
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000425 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100426 HSuspendCheck* suspend_check_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000427 GrowableArray<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100428 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000429
430 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
431};
432
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100433static constexpr size_t kNoLifetime = -1;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100434static constexpr uint32_t kNoDexPc = -1;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100435
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000436// A block in a method. Contains the list of instructions represented
437// as a double linked list. Each block knows its predecessors and
438// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100439
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700440class HBasicBlock : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000441 public:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100442 explicit HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000443 : graph_(graph),
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000444 predecessors_(graph->GetArena(), kDefaultNumberOfPredecessors),
445 successors_(graph->GetArena(), kDefaultNumberOfSuccessors),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000446 loop_information_(nullptr),
447 dominator_(nullptr),
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100448 dominated_blocks_(graph->GetArena(), kDefaultNumberOfDominatedBlocks),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100449 block_id_(-1),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100450 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100451 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000452 lifetime_end_(kNoLifetime),
453 is_catch_block_(false) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000454
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100455 const GrowableArray<HBasicBlock*>& GetPredecessors() const {
456 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000457 }
458
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100459 const GrowableArray<HBasicBlock*>& GetSuccessors() const {
460 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000461 }
462
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100463 const GrowableArray<HBasicBlock*>& GetDominatedBlocks() const {
464 return dominated_blocks_;
465 }
466
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100467 bool IsEntryBlock() const {
468 return graph_->GetEntryBlock() == this;
469 }
470
471 bool IsExitBlock() const {
472 return graph_->GetExitBlock() == this;
473 }
474
David Brazdil46e2a392015-03-16 17:31:52 +0000475 bool IsSingleGoto() const;
476
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000477 void AddBackEdge(HBasicBlock* back_edge) {
478 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000479 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000480 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100481 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000482 loop_information_->AddBackEdge(back_edge);
483 }
484
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000485 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000486 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000487
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000488 int GetBlockId() const { return block_id_; }
489 void SetBlockId(int id) { block_id_ = id; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000490
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000491 HBasicBlock* GetDominator() const { return dominator_; }
492 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100493 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.Add(block); }
David Brazdil2d7352b2015-04-20 14:52:42 +0100494 void RemoveDominatedBlock(HBasicBlock* block) { dominated_blocks_.Delete(block); }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000495 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
496 for (size_t i = 0, e = dominated_blocks_.Size(); i < e; ++i) {
497 if (dominated_blocks_.Get(i) == existing) {
498 dominated_blocks_.Put(i, new_block);
499 return;
500 }
501 }
502 LOG(FATAL) << "Unreachable";
503 UNREACHABLE();
504 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000505
506 int NumberOfBackEdges() const {
507 return loop_information_ == nullptr
508 ? 0
509 : loop_information_->NumberOfBackEdges();
510 }
511
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100512 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
513 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100514 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100515 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +0100516 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
517 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000518
519 void AddSuccessor(HBasicBlock* block) {
520 successors_.Add(block);
521 block->predecessors_.Add(this);
522 }
523
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100524 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
525 size_t successor_index = GetSuccessorIndexOf(existing);
526 DCHECK_NE(successor_index, static_cast<size_t>(-1));
527 existing->RemovePredecessor(this);
528 new_block->predecessors_.Add(this);
529 successors_.Put(successor_index, new_block);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000530 }
531
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000532 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
533 size_t predecessor_index = GetPredecessorIndexOf(existing);
534 DCHECK_NE(predecessor_index, static_cast<size_t>(-1));
535 existing->RemoveSuccessor(this);
536 new_block->successors_.Add(this);
537 predecessors_.Put(predecessor_index, new_block);
538 }
539
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100540 void RemovePredecessor(HBasicBlock* block) {
541 predecessors_.Delete(block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100542 }
543
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000544 void RemoveSuccessor(HBasicBlock* block) {
545 successors_.Delete(block);
546 }
547
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100548 void ClearAllPredecessors() {
549 predecessors_.Reset();
550 }
551
552 void AddPredecessor(HBasicBlock* block) {
553 predecessors_.Add(block);
554 block->successors_.Add(this);
555 }
556
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100557 void SwapPredecessors() {
Nicolas Geoffrayc83d4412014-09-18 16:46:20 +0100558 DCHECK_EQ(predecessors_.Size(), 2u);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100559 HBasicBlock* temp = predecessors_.Get(0);
560 predecessors_.Put(0, predecessors_.Get(1));
561 predecessors_.Put(1, temp);
562 }
563
David Brazdil769c9e52015-04-27 13:54:09 +0100564 void SwapSuccessors() {
565 DCHECK_EQ(successors_.Size(), 2u);
566 HBasicBlock* temp = successors_.Get(0);
567 successors_.Put(0, successors_.Get(1));
568 successors_.Put(1, temp);
569 }
570
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100571 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) {
572 for (size_t i = 0, e = predecessors_.Size(); i < e; ++i) {
573 if (predecessors_.Get(i) == predecessor) {
574 return i;
575 }
576 }
577 return -1;
578 }
579
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100580 size_t GetSuccessorIndexOf(HBasicBlock* successor) {
581 for (size_t i = 0, e = successors_.Size(); i < e; ++i) {
582 if (successors_.Get(i) == successor) {
583 return i;
584 }
585 }
586 return -1;
587 }
588
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000589 // Split the block into two blocks just after `cursor`. Returns the newly
590 // created block. Note that this method just updates raw block information,
591 // like predecessors, successors, dominators, and instruction list. It does not
592 // update the graph, reverse post order, loop information, nor make sure the
593 // blocks are consistent (for example ending with a control flow instruction).
594 HBasicBlock* SplitAfter(HInstruction* cursor);
595
596 // Merge `other` at the end of `this`. Successors and dominated blocks of
597 // `other` are changed to be successors and dominated blocks of `this`. Note
598 // that this method does not update the graph, reverse post order, loop
599 // information, nor make sure the blocks are consistent (for example ending
600 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +0100601 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000602
603 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
604 // of `this` are moved to `other`.
605 // Note that this method does not update the graph, reverse post order, loop
606 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +0000607 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000608 void ReplaceWith(HBasicBlock* other);
609
David Brazdil2d7352b2015-04-20 14:52:42 +0100610 // Merge `other` at the end of `this`. This method updates loops, reverse post
611 // order, links to predecessors, successors, dominators and deletes the block
612 // from the graph. The two blocks must be successive, i.e. `this` the only
613 // predecessor of `other` and vice versa.
614 void MergeWith(HBasicBlock* other);
615
616 // Disconnects `this` from all its predecessors, successors and dominator,
617 // removes it from all loops it is included in and eventually from the graph.
618 // The block must not dominate any other block. Predecessors and successors
619 // are safely updated.
620 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +0000621
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000622 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +0100623 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100624 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +0100625 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +0100626 // Replace instruction `initial` with `replacement` within this block.
627 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
628 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100629 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100630 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +0000631 // RemoveInstruction and RemovePhi delete a given instruction from the respective
632 // instruction list. With 'ensure_safety' set to true, it verifies that the
633 // instruction is not in use and removes it from the use lists of its inputs.
634 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
635 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +0100636 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100637
638 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +0100639 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100640 }
641
Roland Levillain6b879dd2014-09-22 17:13:44 +0100642 bool IsLoopPreHeaderFirstPredecessor() const {
643 DCHECK(IsLoopHeader());
644 DCHECK(!GetPredecessors().IsEmpty());
645 return GetPredecessors().Get(0) == GetLoopInformation()->GetPreHeader();
646 }
647
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100648 HLoopInformation* GetLoopInformation() const {
649 return loop_information_;
650 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000651
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000652 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100653 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000654 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100655 void SetInLoop(HLoopInformation* info) {
656 if (IsLoopHeader()) {
657 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +0100658 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100659 loop_information_ = info;
660 } else if (loop_information_->Contains(*info->GetHeader())) {
661 // Block is currently part of an outer loop. Make it part of this inner loop.
662 // Note that a non loop header having a loop information means this loop information
663 // has already been populated
664 loop_information_ = info;
665 } else {
666 // Block is part of an inner loop. Do not update the loop information.
667 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
668 // at this point, because this method is being called while populating `info`.
669 }
670 }
671
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000672 // Raw update of the loop information.
673 void SetLoopInformation(HLoopInformation* info) {
674 loop_information_ = info;
675 }
676
David Brazdil69a28042015-04-29 17:16:07 +0100677 // Checks if the loop information points to a valid loop. If the loop has been
678 // dismantled (does not have a back edge any more), loop information is
679 // removed or replaced with the information of the first valid outer loop.
680 void UpdateLoopInformation();
681
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100682 bool IsInLoop() const { return loop_information_ != nullptr; }
683
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100684 // Returns wheter this block dominates the blocked passed as parameter.
685 bool Dominates(HBasicBlock* block) const;
686
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100687 size_t GetLifetimeStart() const { return lifetime_start_; }
688 size_t GetLifetimeEnd() const { return lifetime_end_; }
689
690 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
691 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
692
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100693 uint32_t GetDexPc() const { return dex_pc_; }
694
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000695 bool IsCatchBlock() const { return is_catch_block_; }
696 void SetIsCatchBlock() { is_catch_block_ = true; }
697
David Brazdil8d5b8b22015-03-24 10:51:52 +0000698 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +0000699 bool EndsWithIf() const;
700 bool HasSinglePhi() const;
701
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000702 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000703 HGraph* graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000704 GrowableArray<HBasicBlock*> predecessors_;
705 GrowableArray<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100706 HInstructionList instructions_;
707 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000708 HLoopInformation* loop_information_;
709 HBasicBlock* dominator_;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100710 GrowableArray<HBasicBlock*> dominated_blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000711 int block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100712 // The dex program counter of the first instruction of this block.
713 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100714 size_t lifetime_start_;
715 size_t lifetime_end_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000716 bool is_catch_block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000717
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000718 friend class HGraph;
719 friend class HInstruction;
720
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000721 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
722};
723
David Brazdilb2bd1c52015-03-25 11:17:37 +0000724// Iterates over the LoopInformation of all loops which contain 'block'
725// from the innermost to the outermost.
726class HLoopInformationOutwardIterator : public ValueObject {
727 public:
728 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
729 : current_(block.GetLoopInformation()) {}
730
731 bool Done() const { return current_ == nullptr; }
732
733 void Advance() {
734 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +0100735 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +0000736 }
737
738 HLoopInformation* Current() const {
739 DCHECK(!Done());
740 return current_;
741 }
742
743 private:
744 HLoopInformation* current_;
745
746 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
747};
748
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100749#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
750 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000751 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000752 M(ArrayGet, Instruction) \
753 M(ArrayLength, Instruction) \
754 M(ArraySet, Instruction) \
David Brazdil66d126e2015-04-03 16:02:44 +0100755 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000756 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +0000757 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000758 M(CheckCast, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100759 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000760 M(Compare, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100761 M(Condition, BinaryOperation) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700762 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000763 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +0000764 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000765 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100766 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000767 M(Exit, Instruction) \
768 M(FloatConstant, Constant) \
769 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100770 M(GreaterThan, Condition) \
771 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100772 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000773 M(InstanceFieldGet, Instruction) \
774 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000775 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100776 M(IntConstant, Constant) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +0000777 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000778 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100779 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000780 M(LessThan, Condition) \
781 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000782 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000783 M(LoadException, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100784 M(LoadLocal, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000785 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100786 M(Local, Instruction) \
787 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +0100788 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +0000789 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000790 M(Mul, BinaryOperation) \
791 M(Neg, UnaryOperation) \
792 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100793 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +0100794 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000795 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000796 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000797 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000798 M(Or, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100799 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000800 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100801 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000802 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100803 M(Return, Instruction) \
804 M(ReturnVoid, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000805 M(Shl, BinaryOperation) \
806 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100807 M(StaticFieldGet, Instruction) \
808 M(StaticFieldSet, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100809 M(StoreLocal, Instruction) \
810 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100811 M(SuspendCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000812 M(Temporary, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000813 M(Throw, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +0000814 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000815 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000816 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000817
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100818#define FOR_EACH_INSTRUCTION(M) \
819 FOR_EACH_CONCRETE_INSTRUCTION(M) \
820 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +0100821 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +0100822 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100823 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -0700824
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100825#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000826FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
827#undef FORWARD_DECLARATION
828
Roland Levillainccc07a92014-09-16 14:48:16 +0100829#define DECLARE_INSTRUCTION(type) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000830 InstructionKind GetKind() const OVERRIDE { return k##type; } \
831 const char* DebugName() const OVERRIDE { return #type; } \
832 const H##type* As##type() const OVERRIDE { return this; } \
833 H##type* As##type() OVERRIDE { return this; } \
834 bool InstructionTypeEquals(HInstruction* other) const OVERRIDE { \
Roland Levillainccc07a92014-09-16 14:48:16 +0100835 return other->Is##type(); \
836 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000837 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000838
David Brazdiled596192015-01-23 10:39:45 +0000839template <typename T> class HUseList;
840
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100841template <typename T>
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700842class HUseListNode : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000843 public:
David Brazdiled596192015-01-23 10:39:45 +0000844 HUseListNode* GetPrevious() const { return prev_; }
845 HUseListNode* GetNext() const { return next_; }
846 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100847 size_t GetIndex() const { return index_; }
848
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000849 private:
David Brazdiled596192015-01-23 10:39:45 +0000850 HUseListNode(T user, size_t index)
851 : user_(user), index_(index), prev_(nullptr), next_(nullptr) {}
852
853 T const user_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100854 const size_t index_;
David Brazdiled596192015-01-23 10:39:45 +0000855 HUseListNode<T>* prev_;
856 HUseListNode<T>* next_;
857
858 friend class HUseList<T>;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000859
860 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
861};
862
David Brazdiled596192015-01-23 10:39:45 +0000863template <typename T>
864class HUseList : public ValueObject {
865 public:
866 HUseList() : first_(nullptr) {}
867
868 void Clear() {
869 first_ = nullptr;
870 }
871
872 // Adds a new entry at the beginning of the use list and returns
873 // the newly created node.
874 HUseListNode<T>* AddUse(T user, size_t index, ArenaAllocator* arena) {
David Brazdilea55b932015-01-27 17:12:29 +0000875 HUseListNode<T>* new_node = new (arena) HUseListNode<T>(user, index);
David Brazdiled596192015-01-23 10:39:45 +0000876 if (IsEmpty()) {
877 first_ = new_node;
878 } else {
879 first_->prev_ = new_node;
880 new_node->next_ = first_;
881 first_ = new_node;
882 }
883 return new_node;
884 }
885
886 HUseListNode<T>* GetFirst() const {
887 return first_;
888 }
889
890 void Remove(HUseListNode<T>* node) {
David Brazdil1abb4192015-02-17 18:33:36 +0000891 DCHECK(node != nullptr);
892 DCHECK(Contains(node));
893
David Brazdiled596192015-01-23 10:39:45 +0000894 if (node->prev_ != nullptr) {
895 node->prev_->next_ = node->next_;
896 }
897 if (node->next_ != nullptr) {
898 node->next_->prev_ = node->prev_;
899 }
900 if (node == first_) {
901 first_ = node->next_;
902 }
903 }
904
David Brazdil1abb4192015-02-17 18:33:36 +0000905 bool Contains(const HUseListNode<T>* node) const {
906 if (node == nullptr) {
907 return false;
908 }
909 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
910 if (current == node) {
911 return true;
912 }
913 }
914 return false;
915 }
916
David Brazdiled596192015-01-23 10:39:45 +0000917 bool IsEmpty() const {
918 return first_ == nullptr;
919 }
920
921 bool HasOnlyOneUse() const {
922 return first_ != nullptr && first_->next_ == nullptr;
923 }
924
925 private:
926 HUseListNode<T>* first_;
927};
928
929template<typename T>
930class HUseIterator : public ValueObject {
931 public:
932 explicit HUseIterator(const HUseList<T>& uses) : current_(uses.GetFirst()) {}
933
934 bool Done() const { return current_ == nullptr; }
935
936 void Advance() {
937 DCHECK(!Done());
938 current_ = current_->GetNext();
939 }
940
941 HUseListNode<T>* Current() const {
942 DCHECK(!Done());
943 return current_;
944 }
945
946 private:
947 HUseListNode<T>* current_;
948
949 friend class HValue;
950};
951
David Brazdil1abb4192015-02-17 18:33:36 +0000952// This class is used by HEnvironment and HInstruction classes to record the
953// instructions they use and pointers to the corresponding HUseListNodes kept
954// by the used instructions.
955template <typename T>
956class HUserRecord : public ValueObject {
957 public:
958 HUserRecord() : instruction_(nullptr), use_node_(nullptr) {}
959 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), use_node_(nullptr) {}
960
961 HUserRecord(const HUserRecord<T>& old_record, HUseListNode<T>* use_node)
962 : instruction_(old_record.instruction_), use_node_(use_node) {
963 DCHECK(instruction_ != nullptr);
964 DCHECK(use_node_ != nullptr);
965 DCHECK(old_record.use_node_ == nullptr);
966 }
967
968 HInstruction* GetInstruction() const { return instruction_; }
969 HUseListNode<T>* GetUseNode() const { return use_node_; }
970
971 private:
972 // Instruction used by the user.
973 HInstruction* instruction_;
974
975 // Corresponding entry in the use list kept by 'instruction_'.
976 HUseListNode<T>* use_node_;
977};
978
Calin Juravle27df7582015-04-17 19:12:31 +0100979// TODO: Add better documentation to this class and maybe refactor with more suggestive names.
980// - Has(All)SideEffects suggests that all the side effects are present but only ChangesSomething
981// flag is consider.
982// - DependsOn suggests that there is a real dependency between side effects but it only
983// checks DependendsOnSomething flag.
984//
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100985// Represents the side effects an instruction may have.
986class SideEffects : public ValueObject {
987 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100988 SideEffects() : flags_(0) {}
989
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100990 static SideEffects None() {
991 return SideEffects(0);
992 }
993
994 static SideEffects All() {
995 return SideEffects(ChangesSomething().flags_ | DependsOnSomething().flags_);
996 }
997
998 static SideEffects ChangesSomething() {
999 return SideEffects((1 << kFlagChangesCount) - 1);
1000 }
1001
1002 static SideEffects DependsOnSomething() {
1003 int count = kFlagDependsOnCount - kFlagChangesCount;
1004 return SideEffects(((1 << count) - 1) << kFlagChangesCount);
1005 }
1006
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001007 SideEffects Union(SideEffects other) const {
1008 return SideEffects(flags_ | other.flags_);
1009 }
1010
Roland Levillain72bceff2014-09-15 18:29:00 +01001011 bool HasSideEffects() const {
1012 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
1013 return (flags_ & all_bits_set) != 0;
1014 }
1015
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001016 bool HasAllSideEffects() const {
1017 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
1018 return all_bits_set == (flags_ & all_bits_set);
1019 }
1020
1021 bool DependsOn(SideEffects other) const {
1022 size_t depends_flags = other.ComputeDependsFlags();
1023 return (flags_ & depends_flags) != 0;
1024 }
1025
1026 bool HasDependencies() const {
1027 int count = kFlagDependsOnCount - kFlagChangesCount;
1028 size_t all_bits_set = (1 << count) - 1;
1029 return ((flags_ >> kFlagChangesCount) & all_bits_set) != 0;
1030 }
1031
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001032 private:
1033 static constexpr int kFlagChangesSomething = 0;
1034 static constexpr int kFlagChangesCount = kFlagChangesSomething + 1;
1035
1036 static constexpr int kFlagDependsOnSomething = kFlagChangesCount;
1037 static constexpr int kFlagDependsOnCount = kFlagDependsOnSomething + 1;
1038
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001039 explicit SideEffects(size_t flags) : flags_(flags) {}
1040
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001041 size_t ComputeDependsFlags() const {
1042 return flags_ << kFlagChangesCount;
1043 }
1044
1045 size_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001046};
1047
David Brazdiled596192015-01-23 10:39:45 +00001048// A HEnvironment object contains the values of virtual registers at a given location.
1049class HEnvironment : public ArenaObject<kArenaAllocMisc> {
1050 public:
1051 HEnvironment(ArenaAllocator* arena, size_t number_of_vregs)
1052 : vregs_(arena, number_of_vregs) {
1053 vregs_.SetSize(number_of_vregs);
1054 for (size_t i = 0; i < number_of_vregs; i++) {
David Brazdil1abb4192015-02-17 18:33:36 +00001055 vregs_.Put(i, HUserRecord<HEnvironment*>());
David Brazdiled596192015-01-23 10:39:45 +00001056 }
1057 }
1058
1059 void CopyFrom(HEnvironment* env);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001060 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1061 // input to the loop phi instead. This is for inserting instructions that
1062 // require an environment (like HDeoptimization) in the loop pre-header.
1063 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001064
1065 void SetRawEnvAt(size_t index, HInstruction* instruction) {
David Brazdil1abb4192015-02-17 18:33:36 +00001066 vregs_.Put(index, HUserRecord<HEnvironment*>(instruction));
David Brazdiled596192015-01-23 10:39:45 +00001067 }
1068
1069 HInstruction* GetInstructionAt(size_t index) const {
David Brazdil1abb4192015-02-17 18:33:36 +00001070 return vregs_.Get(index).GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001071 }
1072
David Brazdil1abb4192015-02-17 18:33:36 +00001073 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001074
1075 size_t Size() const { return vregs_.Size(); }
1076
1077 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001078 // Record instructions' use entries of this environment for constant-time removal.
1079 // It should only be called by HInstruction when a new environment use is added.
1080 void RecordEnvUse(HUseListNode<HEnvironment*>* env_use) {
1081 DCHECK(env_use->GetUser() == this);
1082 size_t index = env_use->GetIndex();
1083 vregs_.Put(index, HUserRecord<HEnvironment*>(vregs_.Get(index), env_use));
1084 }
David Brazdiled596192015-01-23 10:39:45 +00001085
David Brazdil1abb4192015-02-17 18:33:36 +00001086 GrowableArray<HUserRecord<HEnvironment*> > vregs_;
David Brazdiled596192015-01-23 10:39:45 +00001087
David Brazdil1abb4192015-02-17 18:33:36 +00001088 friend HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001089
1090 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1091};
1092
Calin Juravleacf735c2015-02-12 15:25:22 +00001093class ReferenceTypeInfo : ValueObject {
1094 public:
Calin Juravleb1498f62015-02-16 13:13:29 +00001095 typedef Handle<mirror::Class> TypeHandle;
1096
1097 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact)
1098 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1099 if (type_handle->IsObjectClass()) {
1100 // Override the type handle to be consistent with the case when we get to
1101 // Top but don't have the Object class available. It avoids having to guess
1102 // what value the type_handle has when it's Top.
1103 return ReferenceTypeInfo(TypeHandle(), is_exact, true);
1104 } else {
1105 return ReferenceTypeInfo(type_handle, is_exact, false);
1106 }
1107 }
1108
1109 static ReferenceTypeInfo CreateTop(bool is_exact) {
1110 return ReferenceTypeInfo(TypeHandle(), is_exact, true);
Calin Juravleacf735c2015-02-12 15:25:22 +00001111 }
1112
1113 bool IsExact() const { return is_exact_; }
1114 bool IsTop() const { return is_top_; }
1115
1116 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
1117
Calin Juravleb1498f62015-02-16 13:13:29 +00001118 bool IsSupertypeOf(ReferenceTypeInfo rti) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Calin Juravleacf735c2015-02-12 15:25:22 +00001119 if (IsTop()) {
1120 // Top (equivalent for java.lang.Object) is supertype of anything.
1121 return true;
1122 }
1123 if (rti.IsTop()) {
1124 // If we get here `this` is not Top() so it can't be a supertype.
1125 return false;
1126 }
1127 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
1128 }
1129
1130 // Returns true if the type information provide the same amount of details.
1131 // Note that it does not mean that the instructions have the same actual type
1132 // (e.g. tops are equal but they can be the result of a merge).
1133 bool IsEqual(ReferenceTypeInfo rti) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1134 if (IsExact() != rti.IsExact()) {
1135 return false;
1136 }
1137 if (IsTop() && rti.IsTop()) {
1138 // `Top` means java.lang.Object, so the types are equivalent.
1139 return true;
1140 }
1141 if (IsTop() || rti.IsTop()) {
1142 // If only one is top or object than they are not equivalent.
1143 // NB: We need this extra check because the type_handle of `Top` is invalid
1144 // and we cannot inspect its reference.
1145 return false;
1146 }
1147
1148 // Finally check the types.
1149 return GetTypeHandle().Get() == rti.GetTypeHandle().Get();
1150 }
1151
1152 private:
Calin Juravleb1498f62015-02-16 13:13:29 +00001153 ReferenceTypeInfo() : ReferenceTypeInfo(TypeHandle(), false, true) {}
1154 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact, bool is_top)
1155 : type_handle_(type_handle), is_exact_(is_exact), is_top_(is_top) {}
1156
Calin Juravleacf735c2015-02-12 15:25:22 +00001157 // The class of the object.
Calin Juravleb1498f62015-02-16 13:13:29 +00001158 TypeHandle type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001159 // Whether or not the type is exact or a superclass of the actual type.
Calin Juravleb1498f62015-02-16 13:13:29 +00001160 // Whether or not we have any information about this type.
Calin Juravleacf735c2015-02-12 15:25:22 +00001161 bool is_exact_;
1162 // A true value here means that the object type should be java.lang.Object.
1163 // We don't have access to the corresponding mirror object every time so this
1164 // flag acts as a substitute. When true, the TypeHandle refers to a null
1165 // pointer and should not be used.
1166 bool is_top_;
1167};
1168
1169std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
1170
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001171class HInstruction : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001172 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001173 explicit HInstruction(SideEffects side_effects)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001174 : previous_(nullptr),
1175 next_(nullptr),
1176 block_(nullptr),
1177 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001178 ssa_index_(-1),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001179 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001180 locations_(nullptr),
1181 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001182 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001183 side_effects_(side_effects),
1184 reference_type_info_(ReferenceTypeInfo::CreateTop(/* is_exact */ false)) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001185
Dave Allison20dfc792014-06-16 20:44:29 -07001186 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001187
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001188#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001189 enum InstructionKind {
1190 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1191 };
1192#undef DECLARE_KIND
1193
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001194 HInstruction* GetNext() const { return next_; }
1195 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001196
Calin Juravle77520bc2015-01-12 18:45:46 +00001197 HInstruction* GetNextDisregardingMoves() const;
1198 HInstruction* GetPreviousDisregardingMoves() const;
1199
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001200 HBasicBlock* GetBlock() const { return block_; }
1201 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001202 bool IsInBlock() const { return block_ != nullptr; }
1203 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +01001204 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001205
Roland Levillain6b879dd2014-09-22 17:13:44 +01001206 virtual size_t InputCount() const = 0;
David Brazdil1abb4192015-02-17 18:33:36 +00001207 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001208
1209 virtual void Accept(HGraphVisitor* visitor) = 0;
1210 virtual const char* DebugName() const = 0;
1211
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001212 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
David Brazdil1abb4192015-02-17 18:33:36 +00001213 void SetRawInputAt(size_t index, HInstruction* input) {
1214 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1215 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001216
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001217 virtual bool NeedsEnvironment() const { return false; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001218 virtual bool IsControlFlow() const { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01001219 virtual bool CanThrow() const { return false; }
Roland Levillain72bceff2014-09-15 18:29:00 +01001220 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001221
Calin Juravle10e244f2015-01-26 18:54:32 +00001222 // Does not apply for all instructions, but having this at top level greatly
1223 // simplifies the null check elimination.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001224 virtual bool CanBeNull() const {
1225 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1226 return true;
1227 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001228
Calin Juravle641547a2015-04-21 22:08:51 +01001229 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj) const {
1230 UNUSED(obj);
1231 return false;
1232 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001233
Calin Juravleacf735c2015-02-12 15:25:22 +00001234 void SetReferenceTypeInfo(ReferenceTypeInfo reference_type_info) {
Calin Juravle61d544b2015-02-23 16:46:57 +00001235 DCHECK_EQ(GetType(), Primitive::kPrimNot);
Calin Juravleacf735c2015-02-12 15:25:22 +00001236 reference_type_info_ = reference_type_info;
1237 }
1238
Calin Juravle61d544b2015-02-23 16:46:57 +00001239 ReferenceTypeInfo GetReferenceTypeInfo() const {
1240 DCHECK_EQ(GetType(), Primitive::kPrimNot);
1241 return reference_type_info_;
1242 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001243
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001244 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001245 DCHECK(user != nullptr);
1246 HUseListNode<HInstruction*>* use =
1247 uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1248 user->SetRawInputRecordAt(index, HUserRecord<HInstruction*>(user->InputRecordAt(index), use));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001249 }
1250
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001251 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001252 DCHECK(user != nullptr);
David Brazdiled596192015-01-23 10:39:45 +00001253 HUseListNode<HEnvironment*>* env_use =
1254 env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1255 user->RecordEnvUse(env_use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001256 }
1257
David Brazdil1abb4192015-02-17 18:33:36 +00001258 void RemoveAsUserOfInput(size_t input) {
1259 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
1260 input_use.GetInstruction()->uses_.Remove(input_use.GetUseNode());
1261 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001262
David Brazdil1abb4192015-02-17 18:33:36 +00001263 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1264 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001265
David Brazdiled596192015-01-23 10:39:45 +00001266 bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); }
1267 bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); }
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001268 bool HasNonEnvironmentUses() const { return !uses_.IsEmpty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001269 bool HasOnlyOneNonEnvironmentUse() const {
1270 return !HasEnvironmentUses() && GetUses().HasOnlyOneUse();
1271 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001272
Roland Levillain6c82d402014-10-13 16:10:27 +01001273 // Does this instruction strictly dominate `other_instruction`?
1274 // Returns false if this instruction and `other_instruction` are the same.
1275 // Aborts if this instruction and `other_instruction` are both phis.
1276 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001277
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001278 int GetId() const { return id_; }
1279 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001280
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001281 int GetSsaIndex() const { return ssa_index_; }
1282 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1283 bool HasSsaIndex() const { return ssa_index_ != -1; }
1284
1285 bool HasEnvironment() const { return environment_ != nullptr; }
1286 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001287 // Set the `environment_` field. Raw because this method does not
1288 // update the uses lists.
1289 void SetRawEnvironment(HEnvironment* environment) { environment_ = environment; }
1290
1291 // Set the environment of this instruction, copying it from `environment`. While
1292 // copying, the uses lists are being updated.
1293 void CopyEnvironmentFrom(HEnvironment* environment) {
1294 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
1295 environment_ = new (allocator) HEnvironment(allocator, environment->Size());
1296 environment_->CopyFrom(environment);
1297 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001298
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001299 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
1300 HBasicBlock* block) {
1301 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
1302 environment_ = new (allocator) HEnvironment(allocator, environment->Size());
1303 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
1304 }
1305
Nicolas Geoffray39468442014-09-02 15:17:15 +01001306 // Returns the number of entries in the environment. Typically, that is the
1307 // number of dex registers in a method. It could be more in case of inlining.
1308 size_t EnvironmentSize() const;
1309
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001310 LocationSummary* GetLocations() const { return locations_; }
1311 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001312
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001313 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001314 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001315
Alexandre Rames188d4312015-04-09 18:30:21 +01001316 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
1317 // uses of this instruction by `other` are *not* updated.
1318 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
1319 ReplaceWith(other);
1320 other->ReplaceInput(this, use_index);
1321 }
1322
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001323 // Move `this` instruction before `cursor`.
1324 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001325
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001326#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01001327 bool Is##type() const { return (As##type() != nullptr); } \
1328 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001329 virtual H##type* As##type() { return nullptr; }
1330
1331 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
1332#undef INSTRUCTION_TYPE_CHECK
1333
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001334 // Returns whether the instruction can be moved within the graph.
1335 virtual bool CanBeMoved() const { return false; }
1336
1337 // Returns whether the two instructions are of the same kind.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001338 virtual bool InstructionTypeEquals(HInstruction* other) const {
1339 UNUSED(other);
1340 return false;
1341 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001342
1343 // Returns whether any data encoded in the two instructions is equal.
1344 // This method does not look at the inputs. Both instructions must be
1345 // of the same type, otherwise the method has undefined behavior.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001346 virtual bool InstructionDataEquals(HInstruction* other) const {
1347 UNUSED(other);
1348 return false;
1349 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001350
1351 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00001352 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001353 // 2) Their inputs are identical.
1354 bool Equals(HInstruction* other) const;
1355
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001356 virtual InstructionKind GetKind() const = 0;
1357
1358 virtual size_t ComputeHashCode() const {
1359 size_t result = GetKind();
1360 for (size_t i = 0, e = InputCount(); i < e; ++i) {
1361 result = (result * 31) + InputAt(i)->GetId();
1362 }
1363 return result;
1364 }
1365
1366 SideEffects GetSideEffects() const { return side_effects_; }
1367
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001368 size_t GetLifetimePosition() const { return lifetime_position_; }
1369 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
1370 LiveInterval* GetLiveInterval() const { return live_interval_; }
1371 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
1372 bool HasLiveInterval() const { return live_interval_ != nullptr; }
1373
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001374 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
1375
1376 // Returns whether the code generation of the instruction will require to have access
1377 // to the current method. Such instructions are:
1378 // (1): Instructions that require an environment, as calling the runtime requires
1379 // to walk the stack and have the current method stored at a specific stack address.
1380 // (2): Object literals like classes and strings, that are loaded from the dex cache
1381 // fields of the current method.
1382 bool NeedsCurrentMethod() const {
1383 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
1384 }
1385
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001386 virtual bool NeedsDexCache() const { return false; }
1387
David Brazdil1abb4192015-02-17 18:33:36 +00001388 protected:
1389 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
1390 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
1391
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001392 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001393 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); }
1394
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001395 HInstruction* previous_;
1396 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001397 HBasicBlock* block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001398
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001399 // An instruction gets an id when it is added to the graph.
1400 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01001401 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001402 int id_;
1403
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001404 // When doing liveness analysis, instructions that have uses get an SSA index.
1405 int ssa_index_;
1406
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001407 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00001408 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001409
1410 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00001411 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001412
Nicolas Geoffray39468442014-09-02 15:17:15 +01001413 // The environment associated with this instruction. Not null if the instruction
1414 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001415 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001416
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001417 // Set by the code generator.
1418 LocationSummary* locations_;
1419
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001420 // Set by the liveness analysis.
1421 LiveInterval* live_interval_;
1422
1423 // Set by the liveness analysis, this is the position in a linear
1424 // order of blocks where this instruction's live interval start.
1425 size_t lifetime_position_;
1426
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001427 const SideEffects side_effects_;
1428
Calin Juravleacf735c2015-02-12 15:25:22 +00001429 // TODO: for primitive types this should be marked as invalid.
1430 ReferenceTypeInfo reference_type_info_;
1431
David Brazdil1abb4192015-02-17 18:33:36 +00001432 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001433 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00001434 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001435 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001436 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001437
1438 DISALLOW_COPY_AND_ASSIGN(HInstruction);
1439};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001440std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001441
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001442class HInputIterator : public ValueObject {
1443 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001444 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001445
1446 bool Done() const { return index_ == instruction_->InputCount(); }
1447 HInstruction* Current() const { return instruction_->InputAt(index_); }
1448 void Advance() { index_++; }
1449
1450 private:
1451 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001452 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001453
1454 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
1455};
1456
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001457class HInstructionIterator : public ValueObject {
1458 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001459 explicit HInstructionIterator(const HInstructionList& instructions)
1460 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001461 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001462 }
1463
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001464 bool Done() const { return instruction_ == nullptr; }
1465 HInstruction* Current() const { return instruction_; }
1466 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001467 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001468 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001469 }
1470
1471 private:
1472 HInstruction* instruction_;
1473 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001474
1475 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001476};
1477
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001478class HBackwardInstructionIterator : public ValueObject {
1479 public:
1480 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
1481 : instruction_(instructions.last_instruction_) {
1482 next_ = Done() ? nullptr : instruction_->GetPrevious();
1483 }
1484
1485 bool Done() const { return instruction_ == nullptr; }
1486 HInstruction* Current() const { return instruction_; }
1487 void Advance() {
1488 instruction_ = next_;
1489 next_ = Done() ? nullptr : instruction_->GetPrevious();
1490 }
1491
1492 private:
1493 HInstruction* instruction_;
1494 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001495
1496 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001497};
1498
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001499// An embedded container with N elements of type T. Used (with partial
1500// specialization for N=0) because embedded arrays cannot have size 0.
1501template<typename T, intptr_t N>
1502class EmbeddedArray {
1503 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001504 EmbeddedArray() : elements_() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001505
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001506 intptr_t GetLength() const { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001507
1508 const T& operator[](intptr_t i) const {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001509 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001510 return elements_[i];
1511 }
1512
1513 T& operator[](intptr_t i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001514 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001515 return elements_[i];
1516 }
1517
1518 const T& At(intptr_t i) const {
1519 return (*this)[i];
1520 }
1521
1522 void SetAt(intptr_t i, const T& val) {
1523 (*this)[i] = val;
1524 }
1525
1526 private:
1527 T elements_[N];
1528};
1529
1530template<typename T>
1531class EmbeddedArray<T, 0> {
1532 public:
1533 intptr_t length() const { return 0; }
1534 const T& operator[](intptr_t i) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001535 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001536 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001537 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001538 }
1539 T& operator[](intptr_t i) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001540 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001541 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001542 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001543 }
1544};
1545
1546template<intptr_t N>
1547class HTemplateInstruction: public HInstruction {
1548 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001549 HTemplateInstruction<N>(SideEffects side_effects)
1550 : HInstruction(side_effects), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07001551 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001552
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001553 size_t InputCount() const OVERRIDE { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001554
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001555 protected:
David Brazdil1abb4192015-02-17 18:33:36 +00001556 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_[i]; }
1557
1558 void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE {
1559 inputs_[i] = input;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001560 }
1561
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001562 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001563 EmbeddedArray<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001564
1565 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001566};
1567
Dave Allison20dfc792014-06-16 20:44:29 -07001568template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001569class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07001570 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001571 HExpression<N>(Primitive::Type type, SideEffects side_effects)
1572 : HTemplateInstruction<N>(side_effects), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07001573 virtual ~HExpression() {}
1574
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001575 Primitive::Type GetType() const OVERRIDE { return type_; }
Dave Allison20dfc792014-06-16 20:44:29 -07001576
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001577 protected:
1578 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07001579};
1580
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001581// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
1582// instruction that branches to the exit block.
1583class HReturnVoid : public HTemplateInstruction<0> {
1584 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001585 HReturnVoid() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001586
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001587 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001588
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001589 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001590
1591 private:
1592 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
1593};
1594
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001595// Represents dex's RETURN opcodes. A HReturn is a control flow
1596// instruction that branches to the exit block.
1597class HReturn : public HTemplateInstruction<1> {
1598 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001599 explicit HReturn(HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001600 SetRawInputAt(0, value);
1601 }
1602
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001603 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001604
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001605 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001606
1607 private:
1608 DISALLOW_COPY_AND_ASSIGN(HReturn);
1609};
1610
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001611// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001612// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001613// exit block.
1614class HExit : public HTemplateInstruction<0> {
1615 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001616 HExit() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001617
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001618 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001619
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001620 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001621
1622 private:
1623 DISALLOW_COPY_AND_ASSIGN(HExit);
1624};
1625
1626// Jumps from one block to another.
1627class HGoto : public HTemplateInstruction<0> {
1628 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001629 HGoto() : HTemplateInstruction(SideEffects::None()) {}
1630
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001631 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001632
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001633 HBasicBlock* GetSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001634 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001635 }
1636
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001637 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001638
1639 private:
1640 DISALLOW_COPY_AND_ASSIGN(HGoto);
1641};
1642
Dave Allison20dfc792014-06-16 20:44:29 -07001643
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001644// Conditional branch. A block ending with an HIf instruction must have
1645// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001646class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001647 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001648 explicit HIf(HInstruction* input) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001649 SetRawInputAt(0, input);
1650 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001651
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001652 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001653
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001654 HBasicBlock* IfTrueSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001655 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001656 }
1657
1658 HBasicBlock* IfFalseSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001659 return GetBlock()->GetSuccessors().Get(1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001660 }
1661
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001662 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001663
1664 private:
1665 DISALLOW_COPY_AND_ASSIGN(HIf);
1666};
1667
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001668// Deoptimize to interpreter, upon checking a condition.
1669class HDeoptimize : public HTemplateInstruction<1> {
1670 public:
1671 HDeoptimize(HInstruction* cond, uint32_t dex_pc)
1672 : HTemplateInstruction(SideEffects::None()),
1673 dex_pc_(dex_pc) {
1674 SetRawInputAt(0, cond);
1675 }
1676
1677 bool NeedsEnvironment() const OVERRIDE { return true; }
1678 bool CanThrow() const OVERRIDE { return true; }
1679 uint32_t GetDexPc() const { return dex_pc_; }
1680
1681 DECLARE_INSTRUCTION(Deoptimize);
1682
1683 private:
1684 uint32_t dex_pc_;
1685
1686 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
1687};
1688
Roland Levillain88cb1752014-10-20 16:36:47 +01001689class HUnaryOperation : public HExpression<1> {
1690 public:
1691 HUnaryOperation(Primitive::Type result_type, HInstruction* input)
1692 : HExpression(result_type, SideEffects::None()) {
1693 SetRawInputAt(0, input);
1694 }
1695
1696 HInstruction* GetInput() const { return InputAt(0); }
1697 Primitive::Type GetResultType() const { return GetType(); }
1698
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001699 bool CanBeMoved() const OVERRIDE { return true; }
1700 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001701 UNUSED(other);
1702 return true;
1703 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001704
Roland Levillain9240d6a2014-10-20 16:47:04 +01001705 // Try to statically evaluate `operation` and return a HConstant
1706 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001707 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01001708 HConstant* TryStaticEvaluation() const;
1709
1710 // Apply this operation to `x`.
1711 virtual int32_t Evaluate(int32_t x) const = 0;
1712 virtual int64_t Evaluate(int64_t x) const = 0;
1713
Roland Levillain88cb1752014-10-20 16:36:47 +01001714 DECLARE_INSTRUCTION(UnaryOperation);
1715
1716 private:
1717 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
1718};
1719
Dave Allison20dfc792014-06-16 20:44:29 -07001720class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001721 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001722 HBinaryOperation(Primitive::Type result_type,
1723 HInstruction* left,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001724 HInstruction* right) : HExpression(result_type, SideEffects::None()) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001725 SetRawInputAt(0, left);
1726 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001727 }
1728
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001729 HInstruction* GetLeft() const { return InputAt(0); }
1730 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07001731 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001732
Mingyao Yangdc5ac732015-02-25 11:28:05 -08001733 virtual bool IsCommutative() const { return false; }
1734
1735 // Put constant on the right.
1736 // Returns whether order is changed.
1737 bool OrderInputsWithConstantOnTheRight() {
1738 HInstruction* left = InputAt(0);
1739 HInstruction* right = InputAt(1);
1740 if (left->IsConstant() && !right->IsConstant()) {
1741 ReplaceInput(right, 0);
1742 ReplaceInput(left, 1);
1743 return true;
1744 }
1745 return false;
1746 }
1747
1748 // Order inputs by instruction id, but favor constant on the right side.
1749 // This helps GVN for commutative ops.
1750 void OrderInputs() {
1751 DCHECK(IsCommutative());
1752 HInstruction* left = InputAt(0);
1753 HInstruction* right = InputAt(1);
1754 if (left == right || (!left->IsConstant() && right->IsConstant())) {
1755 return;
1756 }
1757 if (OrderInputsWithConstantOnTheRight()) {
1758 return;
1759 }
1760 // Order according to instruction id.
1761 if (left->GetId() > right->GetId()) {
1762 ReplaceInput(right, 0);
1763 ReplaceInput(left, 1);
1764 }
1765 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001766
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001767 bool CanBeMoved() const OVERRIDE { return true; }
1768 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001769 UNUSED(other);
1770 return true;
1771 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001772
Roland Levillain9240d6a2014-10-20 16:47:04 +01001773 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01001774 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001775 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01001776 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01001777
1778 // Apply this operation to `x` and `y`.
1779 virtual int32_t Evaluate(int32_t x, int32_t y) const = 0;
1780 virtual int64_t Evaluate(int64_t x, int64_t y) const = 0;
1781
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001782 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001783 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001784 HConstant* GetConstantRight() const;
1785
1786 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001787 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001788 HInstruction* GetLeastConstantLeft() const;
1789
Roland Levillainccc07a92014-09-16 14:48:16 +01001790 DECLARE_INSTRUCTION(BinaryOperation);
1791
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001792 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001793 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
1794};
1795
Dave Allison20dfc792014-06-16 20:44:29 -07001796class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001797 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001798 HCondition(HInstruction* first, HInstruction* second)
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001799 : HBinaryOperation(Primitive::kPrimBoolean, first, second),
1800 needs_materialization_(true) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001801
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001802 bool NeedsMaterialization() const { return needs_materialization_; }
1803 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001804
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001805 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001806 // `instruction`, and disregard moves in between.
1807 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001808
Dave Allison20dfc792014-06-16 20:44:29 -07001809 DECLARE_INSTRUCTION(Condition);
1810
1811 virtual IfCondition GetCondition() const = 0;
1812
1813 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001814 // For register allocation purposes, returns whether this instruction needs to be
1815 // materialized (that is, not just be in the processor flags).
1816 bool needs_materialization_;
1817
Dave Allison20dfc792014-06-16 20:44:29 -07001818 DISALLOW_COPY_AND_ASSIGN(HCondition);
1819};
1820
1821// Instruction to check if two inputs are equal to each other.
1822class HEqual : public HCondition {
1823 public:
1824 HEqual(HInstruction* first, HInstruction* second)
1825 : HCondition(first, second) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001826
Mingyao Yangdc5ac732015-02-25 11:28:05 -08001827 bool IsCommutative() const OVERRIDE { return true; }
1828
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001829 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001830 return x == y ? 1 : 0;
1831 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001832 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001833 return x == y ? 1 : 0;
1834 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001835
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001836 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001837
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001838 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001839 return kCondEQ;
1840 }
1841
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001842 private:
1843 DISALLOW_COPY_AND_ASSIGN(HEqual);
1844};
1845
Dave Allison20dfc792014-06-16 20:44:29 -07001846class HNotEqual : public HCondition {
1847 public:
1848 HNotEqual(HInstruction* first, HInstruction* second)
1849 : HCondition(first, second) {}
1850
Mingyao Yangdc5ac732015-02-25 11:28:05 -08001851 bool IsCommutative() const OVERRIDE { return true; }
1852
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001853 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001854 return x != y ? 1 : 0;
1855 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001856 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001857 return x != y ? 1 : 0;
1858 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001859
Dave Allison20dfc792014-06-16 20:44:29 -07001860 DECLARE_INSTRUCTION(NotEqual);
1861
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001862 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001863 return kCondNE;
1864 }
1865
1866 private:
1867 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
1868};
1869
1870class HLessThan : public HCondition {
1871 public:
1872 HLessThan(HInstruction* first, HInstruction* second)
1873 : HCondition(first, second) {}
1874
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001875 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001876 return x < y ? 1 : 0;
1877 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001878 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001879 return x < y ? 1 : 0;
1880 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001881
Dave Allison20dfc792014-06-16 20:44:29 -07001882 DECLARE_INSTRUCTION(LessThan);
1883
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001884 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001885 return kCondLT;
1886 }
1887
1888 private:
1889 DISALLOW_COPY_AND_ASSIGN(HLessThan);
1890};
1891
1892class HLessThanOrEqual : public HCondition {
1893 public:
1894 HLessThanOrEqual(HInstruction* first, HInstruction* second)
1895 : HCondition(first, second) {}
1896
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001897 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001898 return x <= y ? 1 : 0;
1899 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001900 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001901 return x <= y ? 1 : 0;
1902 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001903
Dave Allison20dfc792014-06-16 20:44:29 -07001904 DECLARE_INSTRUCTION(LessThanOrEqual);
1905
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001906 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001907 return kCondLE;
1908 }
1909
1910 private:
1911 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
1912};
1913
1914class HGreaterThan : public HCondition {
1915 public:
1916 HGreaterThan(HInstruction* first, HInstruction* second)
1917 : HCondition(first, second) {}
1918
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001919 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001920 return x > y ? 1 : 0;
1921 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001922 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001923 return x > y ? 1 : 0;
1924 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001925
Dave Allison20dfc792014-06-16 20:44:29 -07001926 DECLARE_INSTRUCTION(GreaterThan);
1927
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001928 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001929 return kCondGT;
1930 }
1931
1932 private:
1933 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
1934};
1935
1936class HGreaterThanOrEqual : public HCondition {
1937 public:
1938 HGreaterThanOrEqual(HInstruction* first, HInstruction* second)
1939 : HCondition(first, second) {}
1940
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001941 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001942 return x >= y ? 1 : 0;
1943 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001944 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001945 return x >= y ? 1 : 0;
1946 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001947
Dave Allison20dfc792014-06-16 20:44:29 -07001948 DECLARE_INSTRUCTION(GreaterThanOrEqual);
1949
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001950 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001951 return kCondGE;
1952 }
1953
1954 private:
1955 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
1956};
1957
1958
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001959// Instruction to check how two inputs compare to each other.
1960// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
1961class HCompare : public HBinaryOperation {
1962 public:
Calin Juravleddb7df22014-11-25 20:56:51 +00001963 // The bias applies for floating point operations and indicates how NaN
1964 // comparisons are treated:
1965 enum Bias {
1966 kNoBias, // bias is not applicable (i.e. for long operation)
1967 kGtBias, // return 1 for NaN comparisons
1968 kLtBias, // return -1 for NaN comparisons
1969 };
1970
1971 HCompare(Primitive::Type type, HInstruction* first, HInstruction* second, Bias bias)
1972 : HBinaryOperation(Primitive::kPrimInt, first, second), bias_(bias) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001973 DCHECK_EQ(type, first->GetType());
1974 DCHECK_EQ(type, second->GetType());
1975 }
1976
Calin Juravleddb7df22014-11-25 20:56:51 +00001977 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01001978 return
1979 x == y ? 0 :
1980 x > y ? 1 :
1981 -1;
1982 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001983
1984 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01001985 return
1986 x == y ? 0 :
1987 x > y ? 1 :
1988 -1;
1989 }
1990
Calin Juravleddb7df22014-11-25 20:56:51 +00001991 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
1992 return bias_ == other->AsCompare()->bias_;
1993 }
1994
1995 bool IsGtBias() { return bias_ == kGtBias; }
1996
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001997 DECLARE_INSTRUCTION(Compare);
1998
1999 private:
Calin Juravleddb7df22014-11-25 20:56:51 +00002000 const Bias bias_;
2001
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002002 DISALLOW_COPY_AND_ASSIGN(HCompare);
2003};
2004
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002005// A local in the graph. Corresponds to a Dex register.
2006class HLocal : public HTemplateInstruction<0> {
2007 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002008 explicit HLocal(uint16_t reg_number)
2009 : HTemplateInstruction(SideEffects::None()), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002010
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002011 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002012
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002013 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002014
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002015 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002016 // The Dex register number.
2017 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002018
2019 DISALLOW_COPY_AND_ASSIGN(HLocal);
2020};
2021
2022// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07002023class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002024 public:
Roland Levillain5799fc02014-09-25 12:15:20 +01002025 HLoadLocal(HLocal* local, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002026 : HExpression(type, SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002027 SetRawInputAt(0, local);
2028 }
2029
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002030 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
2031
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002032 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002033
2034 private:
2035 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
2036};
2037
2038// Store a value in a given local. This instruction has two inputs: the value
2039// and the local.
2040class HStoreLocal : public HTemplateInstruction<2> {
2041 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002042 HStoreLocal(HLocal* local, HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002043 SetRawInputAt(0, local);
2044 SetRawInputAt(1, value);
2045 }
2046
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002047 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
2048
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002049 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002050
2051 private:
2052 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
2053};
2054
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002055class HConstant : public HExpression<0> {
2056 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002057 explicit HConstant(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
2058
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002059 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002060
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002061 virtual bool IsMinusOne() const { return false; }
2062 virtual bool IsZero() const { return false; }
2063 virtual bool IsOne() const { return false; }
2064
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002065 DECLARE_INSTRUCTION(Constant);
2066
2067 private:
2068 DISALLOW_COPY_AND_ASSIGN(HConstant);
2069};
2070
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002071class HFloatConstant : public HConstant {
2072 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002073 float GetValue() const { return value_; }
2074
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002075 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002076 return bit_cast<uint32_t, float>(other->AsFloatConstant()->value_) ==
2077 bit_cast<uint32_t, float>(value_);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002078 }
2079
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002080 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002081
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002082 bool IsMinusOne() const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002083 return bit_cast<uint32_t, float>(AsFloatConstant()->GetValue()) ==
2084 bit_cast<uint32_t, float>((-1.0f));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002085 }
2086 bool IsZero() const OVERRIDE {
2087 return AsFloatConstant()->GetValue() == 0.0f;
2088 }
2089 bool IsOne() const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002090 return bit_cast<uint32_t, float>(AsFloatConstant()->GetValue()) ==
2091 bit_cast<uint32_t, float>(1.0f);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002092 }
2093
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002094 DECLARE_INSTRUCTION(FloatConstant);
2095
2096 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002097 explicit HFloatConstant(float value) : HConstant(Primitive::kPrimFloat), value_(value) {}
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002098 explicit HFloatConstant(int32_t value)
2099 : HConstant(Primitive::kPrimFloat), value_(bit_cast<float, int32_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00002100
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002101 const float value_;
2102
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002103 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00002104 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002105 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002106 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2107};
2108
2109class HDoubleConstant : public HConstant {
2110 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002111 double GetValue() const { return value_; }
2112
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002113 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002114 return bit_cast<uint64_t, double>(other->AsDoubleConstant()->value_) ==
2115 bit_cast<uint64_t, double>(value_);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002116 }
2117
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002118 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002119
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002120 bool IsMinusOne() const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002121 return bit_cast<uint64_t, double>(AsDoubleConstant()->GetValue()) ==
2122 bit_cast<uint64_t, double>((-1.0));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002123 }
2124 bool IsZero() const OVERRIDE {
2125 return AsDoubleConstant()->GetValue() == 0.0;
2126 }
2127 bool IsOne() const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002128 return bit_cast<uint64_t, double>(AsDoubleConstant()->GetValue()) ==
2129 bit_cast<uint64_t, double>(1.0);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002130 }
2131
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002132 DECLARE_INSTRUCTION(DoubleConstant);
2133
2134 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002135 explicit HDoubleConstant(double value) : HConstant(Primitive::kPrimDouble), value_(value) {}
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002136 explicit HDoubleConstant(int64_t value)
2137 : HConstant(Primitive::kPrimDouble), value_(bit_cast<double, int64_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00002138
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002139 const double value_;
2140
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002141 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00002142 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002143 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002144 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
2145};
2146
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002147class HNullConstant : public HConstant {
2148 public:
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002149 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2150 return true;
2151 }
2152
2153 size_t ComputeHashCode() const OVERRIDE { return 0; }
2154
2155 DECLARE_INSTRUCTION(NullConstant);
2156
2157 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002158 HNullConstant() : HConstant(Primitive::kPrimNot) {}
2159
2160 friend class HGraph;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002161 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2162};
2163
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002164// Constants of the type int. Those can be from Dex instructions, or
2165// synthesized (for example with the if-eqz instruction).
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002166class HIntConstant : public HConstant {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002167 public:
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002168 int32_t GetValue() const { return value_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002169
Calin Juravle61d544b2015-02-23 16:46:57 +00002170 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002171 return other->AsIntConstant()->value_ == value_;
2172 }
2173
Calin Juravle61d544b2015-02-23 16:46:57 +00002174 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2175
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002176 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2177 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2178 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2179
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002180 DECLARE_INSTRUCTION(IntConstant);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002181
2182 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002183 explicit HIntConstant(int32_t value) : HConstant(Primitive::kPrimInt), value_(value) {}
2184
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002185 const int32_t value_;
2186
David Brazdil8d5b8b22015-03-24 10:51:52 +00002187 friend class HGraph;
2188 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
Zheng Xuad4450e2015-04-17 18:48:56 +08002189 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002190 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2191};
2192
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002193class HLongConstant : public HConstant {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002194 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002195 int64_t GetValue() const { return value_; }
2196
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002197 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002198 return other->AsLongConstant()->value_ == value_;
2199 }
2200
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002201 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002202
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002203 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2204 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2205 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2206
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002207 DECLARE_INSTRUCTION(LongConstant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002208
2209 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002210 explicit HLongConstant(int64_t value) : HConstant(Primitive::kPrimLong), value_(value) {}
2211
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002212 const int64_t value_;
2213
David Brazdil8d5b8b22015-03-24 10:51:52 +00002214 friend class HGraph;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002215 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2216};
2217
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002218enum class Intrinsics {
2219#define OPTIMIZING_INTRINSICS(Name, IsStatic) k ## Name,
2220#include "intrinsics_list.h"
2221 kNone,
2222 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
2223#undef INTRINSICS_LIST
2224#undef OPTIMIZING_INTRINSICS
2225};
2226std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
2227
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002228class HInvoke : public HInstruction {
2229 public:
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002230 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002231
2232 // Runtime needs to walk the stack, so Dex -> Dex calls need to
2233 // know their environment.
Calin Juravle77520bc2015-01-12 18:45:46 +00002234 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002235
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01002236 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002237 SetRawInputAt(index, argument);
2238 }
2239
Roland Levillain3e3d7332015-04-28 11:00:54 +01002240 // Return the number of arguments. This number can be lower than
2241 // the number of inputs returned by InputCount(), as some invoke
2242 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
2243 // inputs at the end of their list of inputs.
2244 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
2245
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002246 Primitive::Type GetType() const OVERRIDE { return return_type_; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002247
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002248 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002249
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002250 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
2251
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01002252 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002253 return intrinsic_;
2254 }
2255
2256 void SetIntrinsic(Intrinsics intrinsic) {
2257 intrinsic_ = intrinsic;
2258 }
2259
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002260 DECLARE_INSTRUCTION(Invoke);
2261
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002262 protected:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002263 HInvoke(ArenaAllocator* arena,
2264 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01002265 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002266 Primitive::Type return_type,
2267 uint32_t dex_pc,
2268 uint32_t dex_method_index)
2269 : HInstruction(SideEffects::All()),
Roland Levillain3e3d7332015-04-28 11:00:54 +01002270 number_of_arguments_(number_of_arguments),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002271 inputs_(arena, number_of_arguments),
2272 return_type_(return_type),
2273 dex_pc_(dex_pc),
2274 dex_method_index_(dex_method_index),
2275 intrinsic_(Intrinsics::kNone) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01002276 uint32_t number_of_inputs = number_of_arguments + number_of_other_inputs;
2277 inputs_.SetSize(number_of_inputs);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002278 }
2279
David Brazdil1abb4192015-02-17 18:33:36 +00002280 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
2281 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
2282 inputs_.Put(index, input);
2283 }
2284
Roland Levillain3e3d7332015-04-28 11:00:54 +01002285 uint32_t number_of_arguments_;
David Brazdil1abb4192015-02-17 18:33:36 +00002286 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002287 const Primitive::Type return_type_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002288 const uint32_t dex_pc_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002289 const uint32_t dex_method_index_;
2290 Intrinsics intrinsic_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002291
2292 private:
2293 DISALLOW_COPY_AND_ASSIGN(HInvoke);
2294};
2295
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002296class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002297 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01002298 // Requirements of this method call regarding the class
2299 // initialization (clinit) check of its declaring class.
2300 enum class ClinitCheckRequirement {
2301 kNone, // Class already initialized.
2302 kExplicit, // Static call having explicit clinit check as last input.
2303 kImplicit, // Static call implicitly requiring a clinit check.
2304 };
2305
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002306 HInvokeStaticOrDirect(ArenaAllocator* arena,
2307 uint32_t number_of_arguments,
2308 Primitive::Type return_type,
2309 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002310 uint32_t dex_method_index,
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002311 bool is_recursive,
Jeff Hao848f70a2014-01-15 13:49:50 -08002312 int32_t string_init_offset,
Nicolas Geoffray79041292015-03-26 10:05:54 +00002313 InvokeType original_invoke_type,
Roland Levillain4c0eb422015-04-24 16:43:49 +01002314 InvokeType invoke_type,
2315 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01002316 : HInvoke(arena,
2317 number_of_arguments,
2318 clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u,
2319 return_type,
2320 dex_pc,
2321 dex_method_index),
Nicolas Geoffray79041292015-03-26 10:05:54 +00002322 original_invoke_type_(original_invoke_type),
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002323 invoke_type_(invoke_type),
Roland Levillain4c0eb422015-04-24 16:43:49 +01002324 is_recursive_(is_recursive),
Jeff Hao848f70a2014-01-15 13:49:50 -08002325 clinit_check_requirement_(clinit_check_requirement),
2326 string_init_offset_(string_init_offset) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002327
Calin Juravle641547a2015-04-21 22:08:51 +01002328 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
2329 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00002330 // We access the method via the dex cache so we can't do an implicit null check.
2331 // TODO: for intrinsics we can generate implicit null checks.
2332 return false;
2333 }
2334
Nicolas Geoffray79041292015-03-26 10:05:54 +00002335 InvokeType GetOriginalInvokeType() const { return original_invoke_type_; }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002336 InvokeType GetInvokeType() const { return invoke_type_; }
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002337 bool IsRecursive() const { return is_recursive_; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002338 bool NeedsDexCache() const OVERRIDE { return !IsRecursive(); }
Jeff Hao848f70a2014-01-15 13:49:50 -08002339 bool IsStringInit() const { return string_init_offset_ != 0; }
2340 int32_t GetStringInitOffset() const { return string_init_offset_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002341
Roland Levillain4c0eb422015-04-24 16:43:49 +01002342 // Is this instruction a call to a static method?
2343 bool IsStatic() const {
2344 return GetInvokeType() == kStatic;
2345 }
2346
Roland Levillain3e3d7332015-04-28 11:00:54 +01002347 // Remove the art::HLoadClass instruction set as last input by
2348 // art::PrepareForRegisterAllocation::VisitClinitCheck in lieu of
2349 // the initial art::HClinitCheck instruction (only relevant for
2350 // static calls with explicit clinit check).
2351 void RemoveLoadClassAsLastInput() {
Roland Levillain4c0eb422015-04-24 16:43:49 +01002352 DCHECK(IsStaticWithExplicitClinitCheck());
2353 size_t last_input_index = InputCount() - 1;
2354 HInstruction* last_input = InputAt(last_input_index);
2355 DCHECK(last_input != nullptr);
Roland Levillain3e3d7332015-04-28 11:00:54 +01002356 DCHECK(last_input->IsLoadClass()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01002357 RemoveAsUserOfInput(last_input_index);
2358 inputs_.DeleteAt(last_input_index);
2359 clinit_check_requirement_ = ClinitCheckRequirement::kImplicit;
2360 DCHECK(IsStaticWithImplicitClinitCheck());
2361 }
2362
2363 // Is this a call to a static method whose declaring class has an
2364 // explicit intialization check in the graph?
2365 bool IsStaticWithExplicitClinitCheck() const {
2366 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kExplicit);
2367 }
2368
2369 // Is this a call to a static method whose declaring class has an
2370 // implicit intialization check requirement?
2371 bool IsStaticWithImplicitClinitCheck() const {
2372 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kImplicit);
2373 }
2374
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002375 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002376
Roland Levillain4c0eb422015-04-24 16:43:49 +01002377 protected:
2378 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
2379 const HUserRecord<HInstruction*> input_record = HInvoke::InputRecordAt(i);
2380 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck() && (i == InputCount() - 1)) {
2381 HInstruction* input = input_record.GetInstruction();
2382 // `input` is the last input of a static invoke marked as having
2383 // an explicit clinit check. It must either be:
2384 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
2385 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
2386 DCHECK(input != nullptr);
2387 DCHECK(input->IsClinitCheck() || input->IsLoadClass()) << input->DebugName();
2388 }
2389 return input_record;
2390 }
2391
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002392 private:
Nicolas Geoffray79041292015-03-26 10:05:54 +00002393 const InvokeType original_invoke_type_;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002394 const InvokeType invoke_type_;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002395 const bool is_recursive_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01002396 ClinitCheckRequirement clinit_check_requirement_;
Jeff Hao848f70a2014-01-15 13:49:50 -08002397 // Thread entrypoint offset for string init method if this is a string init invoke.
2398 // Note that there are multiple string init methods, each having its own offset.
2399 int32_t string_init_offset_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002400
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002401 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002402};
2403
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002404class HInvokeVirtual : public HInvoke {
2405 public:
2406 HInvokeVirtual(ArenaAllocator* arena,
2407 uint32_t number_of_arguments,
2408 Primitive::Type return_type,
2409 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002410 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002411 uint32_t vtable_index)
Roland Levillain3e3d7332015-04-28 11:00:54 +01002412 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002413 vtable_index_(vtable_index) {}
2414
Calin Juravle641547a2015-04-21 22:08:51 +01002415 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00002416 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01002417 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00002418 }
2419
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002420 uint32_t GetVTableIndex() const { return vtable_index_; }
2421
2422 DECLARE_INSTRUCTION(InvokeVirtual);
2423
2424 private:
2425 const uint32_t vtable_index_;
2426
2427 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
2428};
2429
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002430class HInvokeInterface : public HInvoke {
2431 public:
2432 HInvokeInterface(ArenaAllocator* arena,
2433 uint32_t number_of_arguments,
2434 Primitive::Type return_type,
2435 uint32_t dex_pc,
2436 uint32_t dex_method_index,
2437 uint32_t imt_index)
Roland Levillain3e3d7332015-04-28 11:00:54 +01002438 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002439 imt_index_(imt_index) {}
2440
Calin Juravle641547a2015-04-21 22:08:51 +01002441 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00002442 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01002443 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00002444 }
2445
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002446 uint32_t GetImtIndex() const { return imt_index_; }
2447 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
2448
2449 DECLARE_INSTRUCTION(InvokeInterface);
2450
2451 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002452 const uint32_t imt_index_;
2453
2454 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
2455};
2456
Dave Allison20dfc792014-06-16 20:44:29 -07002457class HNewInstance : public HExpression<0> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002458 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002459 HNewInstance(uint32_t dex_pc, uint16_t type_index, QuickEntrypointEnum entrypoint)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002460 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2461 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002462 type_index_(type_index),
2463 entrypoint_(entrypoint) {}
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002464
2465 uint32_t GetDexPc() const { return dex_pc_; }
2466 uint16_t GetTypeIndex() const { return type_index_; }
2467
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002468 // Calls runtime so needs an environment.
Calin Juravle92a6ed22014-12-02 18:58:03 +00002469 bool NeedsEnvironment() const OVERRIDE { return true; }
2470 // It may throw when called on:
2471 // - interfaces
2472 // - abstract/innaccessible/unknown classes
2473 // TODO: optimize when possible.
2474 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002475
Calin Juravle10e244f2015-01-26 18:54:32 +00002476 bool CanBeNull() const OVERRIDE { return false; }
2477
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002478 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
2479
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002480 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002481
2482 private:
2483 const uint32_t dex_pc_;
2484 const uint16_t type_index_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002485 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002486
2487 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
2488};
2489
Roland Levillain88cb1752014-10-20 16:36:47 +01002490class HNeg : public HUnaryOperation {
2491 public:
2492 explicit HNeg(Primitive::Type result_type, HInstruction* input)
2493 : HUnaryOperation(result_type, input) {}
2494
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002495 int32_t Evaluate(int32_t x) const OVERRIDE { return -x; }
2496 int64_t Evaluate(int64_t x) const OVERRIDE { return -x; }
Roland Levillain9240d6a2014-10-20 16:47:04 +01002497
Roland Levillain88cb1752014-10-20 16:36:47 +01002498 DECLARE_INSTRUCTION(Neg);
2499
2500 private:
2501 DISALLOW_COPY_AND_ASSIGN(HNeg);
2502};
2503
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002504class HNewArray : public HExpression<1> {
2505 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002506 HNewArray(HInstruction* length,
2507 uint32_t dex_pc,
2508 uint16_t type_index,
2509 QuickEntrypointEnum entrypoint)
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002510 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2511 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002512 type_index_(type_index),
2513 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002514 SetRawInputAt(0, length);
2515 }
2516
2517 uint32_t GetDexPc() const { return dex_pc_; }
2518 uint16_t GetTypeIndex() const { return type_index_; }
2519
2520 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00002521 bool NeedsEnvironment() const OVERRIDE { return true; }
2522
Mingyao Yang0c365e62015-03-31 15:09:29 -07002523 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
2524 bool CanThrow() const OVERRIDE { return true; }
2525
Calin Juravle10e244f2015-01-26 18:54:32 +00002526 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002527
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002528 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
2529
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002530 DECLARE_INSTRUCTION(NewArray);
2531
2532 private:
2533 const uint32_t dex_pc_;
2534 const uint16_t type_index_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002535 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002536
2537 DISALLOW_COPY_AND_ASSIGN(HNewArray);
2538};
2539
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002540class HAdd : public HBinaryOperation {
2541 public:
2542 HAdd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2543 : HBinaryOperation(result_type, left, right) {}
2544
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002545 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002546
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002547 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002548 return x + y;
2549 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002550 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002551 return x + y;
2552 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002553
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002554 DECLARE_INSTRUCTION(Add);
2555
2556 private:
2557 DISALLOW_COPY_AND_ASSIGN(HAdd);
2558};
2559
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002560class HSub : public HBinaryOperation {
2561 public:
2562 HSub(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2563 : HBinaryOperation(result_type, left, right) {}
2564
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002565 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002566 return x - y;
2567 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002568 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002569 return x - y;
2570 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002571
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002572 DECLARE_INSTRUCTION(Sub);
2573
2574 private:
2575 DISALLOW_COPY_AND_ASSIGN(HSub);
2576};
2577
Calin Juravle34bacdf2014-10-07 20:23:36 +01002578class HMul : public HBinaryOperation {
2579 public:
2580 HMul(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2581 : HBinaryOperation(result_type, left, right) {}
2582
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002583 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002584
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002585 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x * y; }
2586 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x * y; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002587
2588 DECLARE_INSTRUCTION(Mul);
2589
2590 private:
2591 DISALLOW_COPY_AND_ASSIGN(HMul);
2592};
2593
Calin Juravle7c4954d2014-10-28 16:57:40 +00002594class HDiv : public HBinaryOperation {
2595 public:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002596 HDiv(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
2597 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00002598
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002599 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00002600 // Our graph structure ensures we never have 0 for `y` during constant folding.
2601 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00002602 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00002603 return (y == -1) ? -x : x / y;
2604 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002605
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002606 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002607 DCHECK_NE(y, 0);
2608 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2609 return (y == -1) ? -x : x / y;
2610 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002611
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002612 uint32_t GetDexPc() const { return dex_pc_; }
2613
Calin Juravle7c4954d2014-10-28 16:57:40 +00002614 DECLARE_INSTRUCTION(Div);
2615
2616 private:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002617 const uint32_t dex_pc_;
2618
Calin Juravle7c4954d2014-10-28 16:57:40 +00002619 DISALLOW_COPY_AND_ASSIGN(HDiv);
2620};
2621
Calin Juravlebacfec32014-11-14 15:54:36 +00002622class HRem : public HBinaryOperation {
2623 public:
2624 HRem(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
2625 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
2626
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002627 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002628 DCHECK_NE(y, 0);
2629 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2630 return (y == -1) ? 0 : x % y;
2631 }
2632
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002633 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002634 DCHECK_NE(y, 0);
2635 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2636 return (y == -1) ? 0 : x % y;
2637 }
2638
2639 uint32_t GetDexPc() const { return dex_pc_; }
2640
2641 DECLARE_INSTRUCTION(Rem);
2642
2643 private:
2644 const uint32_t dex_pc_;
2645
2646 DISALLOW_COPY_AND_ASSIGN(HRem);
2647};
2648
Calin Juravled0d48522014-11-04 16:40:20 +00002649class HDivZeroCheck : public HExpression<1> {
2650 public:
2651 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
2652 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
2653 SetRawInputAt(0, value);
2654 }
2655
2656 bool CanBeMoved() const OVERRIDE { return true; }
2657
2658 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2659 UNUSED(other);
2660 return true;
2661 }
2662
2663 bool NeedsEnvironment() const OVERRIDE { return true; }
2664 bool CanThrow() const OVERRIDE { return true; }
2665
2666 uint32_t GetDexPc() const { return dex_pc_; }
2667
2668 DECLARE_INSTRUCTION(DivZeroCheck);
2669
2670 private:
2671 const uint32_t dex_pc_;
2672
2673 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
2674};
2675
Calin Juravle9aec02f2014-11-18 23:06:35 +00002676class HShl : public HBinaryOperation {
2677 public:
2678 HShl(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2679 : HBinaryOperation(result_type, left, right) {}
2680
2681 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x << (y & kMaxIntShiftValue); }
2682 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x << (y & kMaxLongShiftValue); }
2683
2684 DECLARE_INSTRUCTION(Shl);
2685
2686 private:
2687 DISALLOW_COPY_AND_ASSIGN(HShl);
2688};
2689
2690class HShr : public HBinaryOperation {
2691 public:
2692 HShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2693 : HBinaryOperation(result_type, left, right) {}
2694
2695 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x >> (y & kMaxIntShiftValue); }
2696 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x >> (y & kMaxLongShiftValue); }
2697
2698 DECLARE_INSTRUCTION(Shr);
2699
2700 private:
2701 DISALLOW_COPY_AND_ASSIGN(HShr);
2702};
2703
2704class HUShr : public HBinaryOperation {
2705 public:
2706 HUShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2707 : HBinaryOperation(result_type, left, right) {}
2708
2709 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
2710 uint32_t ux = static_cast<uint32_t>(x);
2711 uint32_t uy = static_cast<uint32_t>(y) & kMaxIntShiftValue;
2712 return static_cast<int32_t>(ux >> uy);
2713 }
2714
2715 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
2716 uint64_t ux = static_cast<uint64_t>(x);
2717 uint64_t uy = static_cast<uint64_t>(y) & kMaxLongShiftValue;
2718 return static_cast<int64_t>(ux >> uy);
2719 }
2720
2721 DECLARE_INSTRUCTION(UShr);
2722
2723 private:
2724 DISALLOW_COPY_AND_ASSIGN(HUShr);
2725};
2726
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002727class HAnd : public HBinaryOperation {
2728 public:
2729 HAnd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2730 : HBinaryOperation(result_type, left, right) {}
2731
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002732 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002733
2734 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x & y; }
2735 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x & y; }
2736
2737 DECLARE_INSTRUCTION(And);
2738
2739 private:
2740 DISALLOW_COPY_AND_ASSIGN(HAnd);
2741};
2742
2743class HOr : public HBinaryOperation {
2744 public:
2745 HOr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2746 : HBinaryOperation(result_type, left, right) {}
2747
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002748 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002749
2750 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x | y; }
2751 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x | y; }
2752
2753 DECLARE_INSTRUCTION(Or);
2754
2755 private:
2756 DISALLOW_COPY_AND_ASSIGN(HOr);
2757};
2758
2759class HXor : public HBinaryOperation {
2760 public:
2761 HXor(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2762 : HBinaryOperation(result_type, left, right) {}
2763
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002764 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002765
2766 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x ^ y; }
2767 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x ^ y; }
2768
2769 DECLARE_INSTRUCTION(Xor);
2770
2771 private:
2772 DISALLOW_COPY_AND_ASSIGN(HXor);
2773};
2774
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002775// The value of a parameter in this method. Its location depends on
2776// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07002777class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002778 public:
Calin Juravle10e244f2015-01-26 18:54:32 +00002779 HParameterValue(uint8_t index, Primitive::Type parameter_type, bool is_this = false)
2780 : HExpression(parameter_type, SideEffects::None()), index_(index), is_this_(is_this) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002781
2782 uint8_t GetIndex() const { return index_; }
2783
Calin Juravle10e244f2015-01-26 18:54:32 +00002784 bool CanBeNull() const OVERRIDE { return !is_this_; }
2785
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002786 DECLARE_INSTRUCTION(ParameterValue);
2787
2788 private:
2789 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00002790 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002791 const uint8_t index_;
2792
Calin Juravle10e244f2015-01-26 18:54:32 +00002793 // Whether or not the parameter value corresponds to 'this' argument.
2794 const bool is_this_;
2795
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002796 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
2797};
2798
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002799class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002800 public:
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002801 explicit HNot(Primitive::Type result_type, HInstruction* input)
2802 : HUnaryOperation(result_type, input) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002803
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002804 bool CanBeMoved() const OVERRIDE { return true; }
2805 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002806 UNUSED(other);
2807 return true;
2808 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002809
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002810 int32_t Evaluate(int32_t x) const OVERRIDE { return ~x; }
2811 int64_t Evaluate(int64_t x) const OVERRIDE { return ~x; }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002812
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002813 DECLARE_INSTRUCTION(Not);
2814
2815 private:
2816 DISALLOW_COPY_AND_ASSIGN(HNot);
2817};
2818
David Brazdil66d126e2015-04-03 16:02:44 +01002819class HBooleanNot : public HUnaryOperation {
2820 public:
2821 explicit HBooleanNot(HInstruction* input)
2822 : HUnaryOperation(Primitive::Type::kPrimBoolean, input) {}
2823
2824 bool CanBeMoved() const OVERRIDE { return true; }
2825 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2826 UNUSED(other);
2827 return true;
2828 }
2829
2830 int32_t Evaluate(int32_t x) const OVERRIDE {
2831 DCHECK(IsUint<1>(x));
2832 return !x;
2833 }
2834
2835 int64_t Evaluate(int64_t x ATTRIBUTE_UNUSED) const OVERRIDE {
2836 LOG(FATAL) << DebugName() << " cannot be used with 64-bit values";
2837 UNREACHABLE();
2838 }
2839
2840 DECLARE_INSTRUCTION(BooleanNot);
2841
2842 private:
2843 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
2844};
2845
Roland Levillaindff1f282014-11-05 14:15:05 +00002846class HTypeConversion : public HExpression<1> {
2847 public:
2848 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00002849 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
2850 : HExpression(result_type, SideEffects::None()), dex_pc_(dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002851 SetRawInputAt(0, input);
2852 DCHECK_NE(input->GetType(), result_type);
2853 }
2854
2855 HInstruction* GetInput() const { return InputAt(0); }
2856 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
2857 Primitive::Type GetResultType() const { return GetType(); }
2858
Roland Levillain624279f2014-12-04 11:54:28 +00002859 // Required by the x86 and ARM code generators when producing calls
2860 // to the runtime.
2861 uint32_t GetDexPc() const { return dex_pc_; }
2862
Roland Levillaindff1f282014-11-05 14:15:05 +00002863 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00002864 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00002865
2866 DECLARE_INSTRUCTION(TypeConversion);
2867
2868 private:
Roland Levillain624279f2014-12-04 11:54:28 +00002869 const uint32_t dex_pc_;
2870
Roland Levillaindff1f282014-11-05 14:15:05 +00002871 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
2872};
2873
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00002874static constexpr uint32_t kNoRegNumber = -1;
2875
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002876class HPhi : public HInstruction {
2877 public:
2878 HPhi(ArenaAllocator* arena, uint32_t reg_number, size_t number_of_inputs, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002879 : HInstruction(SideEffects::None()),
2880 inputs_(arena, number_of_inputs),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002881 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002882 type_(type),
Calin Juravle10e244f2015-01-26 18:54:32 +00002883 is_live_(false),
2884 can_be_null_(true) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002885 inputs_.SetSize(number_of_inputs);
2886 }
2887
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00002888 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
2889 static Primitive::Type ToPhiType(Primitive::Type type) {
2890 switch (type) {
2891 case Primitive::kPrimBoolean:
2892 case Primitive::kPrimByte:
2893 case Primitive::kPrimShort:
2894 case Primitive::kPrimChar:
2895 return Primitive::kPrimInt;
2896 default:
2897 return type;
2898 }
2899 }
2900
Calin Juravle10e244f2015-01-26 18:54:32 +00002901 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002902
2903 void AddInput(HInstruction* input);
David Brazdil2d7352b2015-04-20 14:52:42 +01002904 void RemoveInputAt(size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002905
Calin Juravle10e244f2015-01-26 18:54:32 +00002906 Primitive::Type GetType() const OVERRIDE { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002907 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002908
Calin Juravle10e244f2015-01-26 18:54:32 +00002909 bool CanBeNull() const OVERRIDE { return can_be_null_; }
2910 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
2911
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002912 uint32_t GetRegNumber() const { return reg_number_; }
2913
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002914 void SetDead() { is_live_ = false; }
2915 void SetLive() { is_live_ = true; }
2916 bool IsDead() const { return !is_live_; }
2917 bool IsLive() const { return is_live_; }
2918
Calin Juravlea4f88312015-04-16 12:57:19 +01002919 // Returns the next equivalent phi (starting from the current one) or null if there is none.
2920 // An equivalent phi is a phi having the same dex register and type.
2921 // It assumes that phis with the same dex register are adjacent.
2922 HPhi* GetNextEquivalentPhiWithSameType() {
2923 HInstruction* next = GetNext();
2924 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
2925 if (next->GetType() == GetType()) {
2926 return next->AsPhi();
2927 }
2928 next = next->GetNext();
2929 }
2930 return nullptr;
2931 }
2932
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002933 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002934
David Brazdil1abb4192015-02-17 18:33:36 +00002935 protected:
2936 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
2937
2938 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
2939 inputs_.Put(index, input);
2940 }
2941
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002942 private:
David Brazdil1abb4192015-02-17 18:33:36 +00002943 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002944 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002945 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002946 bool is_live_;
Calin Juravle10e244f2015-01-26 18:54:32 +00002947 bool can_be_null_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002948
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002949 DISALLOW_COPY_AND_ASSIGN(HPhi);
2950};
2951
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002952class HNullCheck : public HExpression<1> {
2953 public:
2954 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002955 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002956 SetRawInputAt(0, value);
2957 }
2958
Calin Juravle10e244f2015-01-26 18:54:32 +00002959 bool CanBeMoved() const OVERRIDE { return true; }
2960 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002961 UNUSED(other);
2962 return true;
2963 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002964
Calin Juravle10e244f2015-01-26 18:54:32 +00002965 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002966
Calin Juravle10e244f2015-01-26 18:54:32 +00002967 bool CanThrow() const OVERRIDE { return true; }
2968
2969 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01002970
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002971 uint32_t GetDexPc() const { return dex_pc_; }
2972
2973 DECLARE_INSTRUCTION(NullCheck);
2974
2975 private:
2976 const uint32_t dex_pc_;
2977
2978 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
2979};
2980
2981class FieldInfo : public ValueObject {
2982 public:
Calin Juravle52c48962014-12-16 17:02:57 +00002983 FieldInfo(MemberOffset field_offset, Primitive::Type field_type, bool is_volatile)
2984 : field_offset_(field_offset), field_type_(field_type), is_volatile_(is_volatile) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002985
2986 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002987 Primitive::Type GetFieldType() const { return field_type_; }
Calin Juravle52c48962014-12-16 17:02:57 +00002988 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002989
2990 private:
2991 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01002992 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00002993 const bool is_volatile_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002994};
2995
2996class HInstanceFieldGet : public HExpression<1> {
2997 public:
2998 HInstanceFieldGet(HInstruction* value,
2999 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003000 MemberOffset field_offset,
3001 bool is_volatile)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003002 : HExpression(field_type, SideEffects::DependsOnSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00003003 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003004 SetRawInputAt(0, value);
3005 }
3006
Calin Juravle10c9cbe2014-12-19 10:50:19 +00003007 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003008
3009 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3010 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
3011 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003012 }
3013
Calin Juravle641547a2015-04-21 22:08:51 +01003014 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3015 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00003016 }
3017
3018 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01003019 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
3020 }
3021
Calin Juravle52c48962014-12-16 17:02:57 +00003022 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003023 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003024 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003025 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003026
3027 DECLARE_INSTRUCTION(InstanceFieldGet);
3028
3029 private:
3030 const FieldInfo field_info_;
3031
3032 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
3033};
3034
3035class HInstanceFieldSet : public HTemplateInstruction<2> {
3036 public:
3037 HInstanceFieldSet(HInstruction* object,
3038 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01003039 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003040 MemberOffset field_offset,
3041 bool is_volatile)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003042 : HTemplateInstruction(SideEffects::ChangesSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00003043 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003044 SetRawInputAt(0, object);
3045 SetRawInputAt(1, value);
3046 }
3047
Calin Juravle641547a2015-04-21 22:08:51 +01003048 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3049 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00003050 }
3051
Calin Juravle52c48962014-12-16 17:02:57 +00003052 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003053 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003054 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003055 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003056 HInstruction* GetValue() const { return InputAt(1); }
3057
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003058 DECLARE_INSTRUCTION(InstanceFieldSet);
3059
3060 private:
3061 const FieldInfo field_info_;
3062
3063 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
3064};
3065
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003066class HArrayGet : public HExpression<2> {
3067 public:
3068 HArrayGet(HInstruction* array, HInstruction* index, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003069 : HExpression(type, SideEffects::DependsOnSomething()) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003070 SetRawInputAt(0, array);
3071 SetRawInputAt(1, index);
3072 }
3073
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003074 bool CanBeMoved() const OVERRIDE { return true; }
3075 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003076 UNUSED(other);
3077 return true;
3078 }
Calin Juravle641547a2015-04-21 22:08:51 +01003079 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3080 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00003081 // TODO: We can be smarter here.
3082 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
3083 // which generates the implicit null check. There are cases when these can be removed
3084 // to produce better code. If we ever add optimizations to do so we should allow an
3085 // implicit check here (as long as the address falls in the first page).
3086 return false;
3087 }
3088
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003089 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003090
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003091 HInstruction* GetArray() const { return InputAt(0); }
3092 HInstruction* GetIndex() const { return InputAt(1); }
3093
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003094 DECLARE_INSTRUCTION(ArrayGet);
3095
3096 private:
3097 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
3098};
3099
3100class HArraySet : public HTemplateInstruction<3> {
3101 public:
3102 HArraySet(HInstruction* array,
3103 HInstruction* index,
3104 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003105 Primitive::Type expected_component_type,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003106 uint32_t dex_pc)
3107 : HTemplateInstruction(SideEffects::ChangesSomething()),
3108 dex_pc_(dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003109 expected_component_type_(expected_component_type),
3110 needs_type_check_(value->GetType() == Primitive::kPrimNot) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003111 SetRawInputAt(0, array);
3112 SetRawInputAt(1, index);
3113 SetRawInputAt(2, value);
3114 }
3115
Calin Juravle77520bc2015-01-12 18:45:46 +00003116 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003117 // We currently always call a runtime method to catch array store
3118 // exceptions.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003119 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003120 }
3121
Calin Juravle641547a2015-04-21 22:08:51 +01003122 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3123 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00003124 // TODO: Same as for ArrayGet.
3125 return false;
3126 }
3127
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003128 void ClearNeedsTypeCheck() {
3129 needs_type_check_ = false;
3130 }
3131
3132 bool NeedsTypeCheck() const { return needs_type_check_; }
3133
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003134 uint32_t GetDexPc() const { return dex_pc_; }
3135
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003136 HInstruction* GetArray() const { return InputAt(0); }
3137 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003138 HInstruction* GetValue() const { return InputAt(2); }
3139
3140 Primitive::Type GetComponentType() const {
3141 // The Dex format does not type floating point index operations. Since the
3142 // `expected_component_type_` is set during building and can therefore not
3143 // be correct, we also check what is the value type. If it is a floating
3144 // point type, we must use that type.
3145 Primitive::Type value_type = GetValue()->GetType();
3146 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
3147 ? value_type
3148 : expected_component_type_;
3149 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003150
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003151 DECLARE_INSTRUCTION(ArraySet);
3152
3153 private:
3154 const uint32_t dex_pc_;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003155 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003156 bool needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003157
3158 DISALLOW_COPY_AND_ASSIGN(HArraySet);
3159};
3160
3161class HArrayLength : public HExpression<1> {
3162 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003163 explicit HArrayLength(HInstruction* array)
3164 : HExpression(Primitive::kPrimInt, SideEffects::None()) {
3165 // Note that arrays do not change length, so the instruction does not
3166 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003167 SetRawInputAt(0, array);
3168 }
3169
Calin Juravle77520bc2015-01-12 18:45:46 +00003170 bool CanBeMoved() const OVERRIDE { return true; }
3171 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003172 UNUSED(other);
3173 return true;
3174 }
Calin Juravle641547a2015-04-21 22:08:51 +01003175 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3176 return obj == InputAt(0);
3177 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003178
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003179 DECLARE_INSTRUCTION(ArrayLength);
3180
3181 private:
3182 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
3183};
3184
3185class HBoundsCheck : public HExpression<2> {
3186 public:
3187 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003188 : HExpression(index->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003189 DCHECK(index->GetType() == Primitive::kPrimInt);
3190 SetRawInputAt(0, index);
3191 SetRawInputAt(1, length);
3192 }
3193
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003194 bool CanBeMoved() const OVERRIDE { return true; }
3195 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003196 UNUSED(other);
3197 return true;
3198 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003199
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003200 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003201
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003202 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01003203
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003204 uint32_t GetDexPc() const { return dex_pc_; }
3205
3206 DECLARE_INSTRUCTION(BoundsCheck);
3207
3208 private:
3209 const uint32_t dex_pc_;
3210
3211 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
3212};
3213
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003214/**
3215 * Some DEX instructions are folded into multiple HInstructions that need
3216 * to stay live until the last HInstruction. This class
3217 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00003218 * HInstruction stays live. `index` represents the stack location index of the
3219 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003220 */
3221class HTemporary : public HTemplateInstruction<0> {
3222 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003223 explicit HTemporary(size_t index) : HTemplateInstruction(SideEffects::None()), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003224
3225 size_t GetIndex() const { return index_; }
3226
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00003227 Primitive::Type GetType() const OVERRIDE {
3228 // The previous instruction is the one that will be stored in the temporary location.
3229 DCHECK(GetPrevious() != nullptr);
3230 return GetPrevious()->GetType();
3231 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00003232
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003233 DECLARE_INSTRUCTION(Temporary);
3234
3235 private:
3236 const size_t index_;
3237
3238 DISALLOW_COPY_AND_ASSIGN(HTemporary);
3239};
3240
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003241class HSuspendCheck : public HTemplateInstruction<0> {
3242 public:
3243 explicit HSuspendCheck(uint32_t dex_pc)
Nicolas Geoffray9ebc72c2014-09-25 16:33:42 +01003244 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003245
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003246 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003247 return true;
3248 }
3249
3250 uint32_t GetDexPc() const { return dex_pc_; }
3251
3252 DECLARE_INSTRUCTION(SuspendCheck);
3253
3254 private:
3255 const uint32_t dex_pc_;
3256
3257 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
3258};
3259
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003260/**
3261 * Instruction to load a Class object.
3262 */
3263class HLoadClass : public HExpression<0> {
3264 public:
3265 HLoadClass(uint16_t type_index,
3266 bool is_referrers_class,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003267 uint32_t dex_pc)
3268 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3269 type_index_(type_index),
3270 is_referrers_class_(is_referrers_class),
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003271 dex_pc_(dex_pc),
Calin Juravleb1498f62015-02-16 13:13:29 +00003272 generate_clinit_check_(false),
3273 loaded_class_rti_(ReferenceTypeInfo::CreateTop(/* is_exact */ false)) {}
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003274
3275 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003276
3277 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3278 return other->AsLoadClass()->type_index_ == type_index_;
3279 }
3280
3281 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
3282
3283 uint32_t GetDexPc() const { return dex_pc_; }
3284 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003285 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003286
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003287 bool NeedsEnvironment() const OVERRIDE {
3288 // Will call runtime and load the class if the class is not loaded yet.
3289 // TODO: finer grain decision.
3290 return !is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003291 }
3292
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003293 bool MustGenerateClinitCheck() const {
3294 return generate_clinit_check_;
3295 }
3296
3297 void SetMustGenerateClinitCheck() {
3298 generate_clinit_check_ = true;
3299 }
3300
3301 bool CanCallRuntime() const {
3302 return MustGenerateClinitCheck() || !is_referrers_class_;
3303 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003304
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003305 bool CanThrow() const OVERRIDE {
3306 // May call runtime and and therefore can throw.
3307 // TODO: finer grain decision.
3308 return !is_referrers_class_;
3309 }
3310
Calin Juravleacf735c2015-02-12 15:25:22 +00003311 ReferenceTypeInfo GetLoadedClassRTI() {
3312 return loaded_class_rti_;
3313 }
3314
3315 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
3316 // Make sure we only set exact types (the loaded class should never be merged).
3317 DCHECK(rti.IsExact());
3318 loaded_class_rti_ = rti;
3319 }
3320
3321 bool IsResolved() {
3322 return loaded_class_rti_.IsExact();
3323 }
3324
Nicolas Geoffray9437b782015-03-25 10:08:51 +00003325 bool NeedsDexCache() const OVERRIDE { return !is_referrers_class_; }
3326
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003327 DECLARE_INSTRUCTION(LoadClass);
3328
3329 private:
3330 const uint16_t type_index_;
3331 const bool is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003332 const uint32_t dex_pc_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003333 // Whether this instruction must generate the initialization check.
3334 // Used for code generation.
3335 bool generate_clinit_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003336
Calin Juravleacf735c2015-02-12 15:25:22 +00003337 ReferenceTypeInfo loaded_class_rti_;
3338
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003339 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
3340};
3341
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003342class HLoadString : public HExpression<0> {
3343 public:
3344 HLoadString(uint32_t string_index, uint32_t dex_pc)
3345 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3346 string_index_(string_index),
3347 dex_pc_(dex_pc) {}
3348
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003349 bool CanBeMoved() const OVERRIDE { return true; }
3350
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003351 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3352 return other->AsLoadString()->string_index_ == string_index_;
3353 }
3354
3355 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
3356
3357 uint32_t GetDexPc() const { return dex_pc_; }
3358 uint32_t GetStringIndex() const { return string_index_; }
3359
3360 // TODO: Can we deopt or debug when we resolve a string?
3361 bool NeedsEnvironment() const OVERRIDE { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00003362 bool NeedsDexCache() const OVERRIDE { return true; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003363
3364 DECLARE_INSTRUCTION(LoadString);
3365
3366 private:
3367 const uint32_t string_index_;
3368 const uint32_t dex_pc_;
3369
3370 DISALLOW_COPY_AND_ASSIGN(HLoadString);
3371};
3372
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003373/**
3374 * Performs an initialization check on its Class object input.
3375 */
3376class HClinitCheck : public HExpression<1> {
3377 public:
3378 explicit HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Nicolas Geoffraya0466e12015-03-27 15:00:40 +00003379 : HExpression(Primitive::kPrimNot, SideEffects::ChangesSomething()),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003380 dex_pc_(dex_pc) {
3381 SetRawInputAt(0, constant);
3382 }
3383
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003384 bool CanBeMoved() const OVERRIDE { return true; }
3385 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3386 UNUSED(other);
3387 return true;
3388 }
3389
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003390 bool NeedsEnvironment() const OVERRIDE {
3391 // May call runtime to initialize the class.
3392 return true;
3393 }
3394
3395 uint32_t GetDexPc() const { return dex_pc_; }
3396
3397 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
3398
3399 DECLARE_INSTRUCTION(ClinitCheck);
3400
3401 private:
3402 const uint32_t dex_pc_;
3403
3404 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
3405};
3406
3407class HStaticFieldGet : public HExpression<1> {
3408 public:
3409 HStaticFieldGet(HInstruction* cls,
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 : HExpression(field_type, SideEffects::DependsOnSomething()),
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 }
3417
Calin Juravle52c48962014-12-16 17:02:57 +00003418
Calin Juravle10c9cbe2014-12-19 10:50:19 +00003419 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003420
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003421 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00003422 HStaticFieldGet* other_get = other->AsStaticFieldGet();
3423 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003424 }
3425
3426 size_t ComputeHashCode() const OVERRIDE {
3427 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
3428 }
3429
Calin Juravle52c48962014-12-16 17:02:57 +00003430 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003431 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
3432 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003433 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003434
3435 DECLARE_INSTRUCTION(StaticFieldGet);
3436
3437 private:
3438 const FieldInfo field_info_;
3439
3440 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
3441};
3442
3443class HStaticFieldSet : public HTemplateInstruction<2> {
3444 public:
3445 HStaticFieldSet(HInstruction* cls,
3446 HInstruction* value,
3447 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003448 MemberOffset field_offset,
3449 bool is_volatile)
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003450 : HTemplateInstruction(SideEffects::ChangesSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00003451 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003452 SetRawInputAt(0, cls);
3453 SetRawInputAt(1, value);
3454 }
3455
Calin Juravle52c48962014-12-16 17:02:57 +00003456 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003457 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
3458 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003459 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003460
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003461 HInstruction* GetValue() const { return InputAt(1); }
3462
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003463 DECLARE_INSTRUCTION(StaticFieldSet);
3464
3465 private:
3466 const FieldInfo field_info_;
3467
3468 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
3469};
3470
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003471// Implement the move-exception DEX instruction.
3472class HLoadException : public HExpression<0> {
3473 public:
3474 HLoadException() : HExpression(Primitive::kPrimNot, SideEffects::None()) {}
3475
3476 DECLARE_INSTRUCTION(LoadException);
3477
3478 private:
3479 DISALLOW_COPY_AND_ASSIGN(HLoadException);
3480};
3481
3482class HThrow : public HTemplateInstruction<1> {
3483 public:
3484 HThrow(HInstruction* exception, uint32_t dex_pc)
3485 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {
3486 SetRawInputAt(0, exception);
3487 }
3488
3489 bool IsControlFlow() const OVERRIDE { return true; }
3490
3491 bool NeedsEnvironment() const OVERRIDE { return true; }
3492
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003493 bool CanThrow() const OVERRIDE { return true; }
3494
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003495 uint32_t GetDexPc() const { return dex_pc_; }
3496
3497 DECLARE_INSTRUCTION(Throw);
3498
3499 private:
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003500 const uint32_t dex_pc_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003501
3502 DISALLOW_COPY_AND_ASSIGN(HThrow);
3503};
3504
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003505class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003506 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003507 HInstanceOf(HInstruction* object,
3508 HLoadClass* constant,
3509 bool class_is_final,
3510 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003511 : HExpression(Primitive::kPrimBoolean, SideEffects::None()),
3512 class_is_final_(class_is_final),
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003513 must_do_null_check_(true),
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003514 dex_pc_(dex_pc) {
3515 SetRawInputAt(0, object);
3516 SetRawInputAt(1, constant);
3517 }
3518
3519 bool CanBeMoved() const OVERRIDE { return true; }
3520
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003521 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003522 return true;
3523 }
3524
3525 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003526 return false;
3527 }
3528
3529 uint32_t GetDexPc() const { return dex_pc_; }
3530
3531 bool IsClassFinal() const { return class_is_final_; }
3532
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003533 // Used only in code generation.
3534 bool MustDoNullCheck() const { return must_do_null_check_; }
3535 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
3536
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003537 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003538
3539 private:
3540 const bool class_is_final_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003541 bool must_do_null_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003542 const uint32_t dex_pc_;
3543
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003544 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
3545};
3546
Calin Juravleb1498f62015-02-16 13:13:29 +00003547class HBoundType : public HExpression<1> {
3548 public:
3549 HBoundType(HInstruction* input, ReferenceTypeInfo bound_type)
3550 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3551 bound_type_(bound_type) {
Calin Juravle61d544b2015-02-23 16:46:57 +00003552 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00003553 SetRawInputAt(0, input);
3554 }
3555
3556 const ReferenceTypeInfo& GetBoundType() const { return bound_type_; }
3557
3558 bool CanBeNull() const OVERRIDE {
3559 // `null instanceof ClassX` always return false so we can't be null.
3560 return false;
3561 }
3562
3563 DECLARE_INSTRUCTION(BoundType);
3564
3565 private:
3566 // Encodes the most upper class that this instruction can have. In other words
3567 // it is always the case that GetBoundType().IsSupertypeOf(GetReferenceType()).
3568 // It is used to bound the type in cases like `if (x instanceof ClassX) {}`
3569 const ReferenceTypeInfo bound_type_;
3570
3571 DISALLOW_COPY_AND_ASSIGN(HBoundType);
3572};
3573
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003574class HCheckCast : public HTemplateInstruction<2> {
3575 public:
3576 HCheckCast(HInstruction* object,
3577 HLoadClass* constant,
3578 bool class_is_final,
3579 uint32_t dex_pc)
3580 : HTemplateInstruction(SideEffects::None()),
3581 class_is_final_(class_is_final),
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003582 must_do_null_check_(true),
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003583 dex_pc_(dex_pc) {
3584 SetRawInputAt(0, object);
3585 SetRawInputAt(1, constant);
3586 }
3587
3588 bool CanBeMoved() const OVERRIDE { return true; }
3589
3590 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
3591 return true;
3592 }
3593
3594 bool NeedsEnvironment() const OVERRIDE {
3595 // Instruction may throw a CheckCastError.
3596 return true;
3597 }
3598
3599 bool CanThrow() const OVERRIDE { return true; }
3600
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003601 bool MustDoNullCheck() const { return must_do_null_check_; }
3602 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
3603
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003604 uint32_t GetDexPc() const { return dex_pc_; }
3605
3606 bool IsClassFinal() const { return class_is_final_; }
3607
3608 DECLARE_INSTRUCTION(CheckCast);
3609
3610 private:
3611 const bool class_is_final_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003612 bool must_do_null_check_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003613 const uint32_t dex_pc_;
3614
3615 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003616};
3617
Calin Juravle27df7582015-04-17 19:12:31 +01003618class HMemoryBarrier : public HTemplateInstruction<0> {
3619 public:
3620 explicit HMemoryBarrier(MemBarrierKind barrier_kind)
3621 : HTemplateInstruction(SideEffects::None()),
3622 barrier_kind_(barrier_kind) {}
3623
3624 MemBarrierKind GetBarrierKind() { return barrier_kind_; }
3625
3626 DECLARE_INSTRUCTION(MemoryBarrier);
3627
3628 private:
3629 const MemBarrierKind barrier_kind_;
3630
3631 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
3632};
3633
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003634class HMonitorOperation : public HTemplateInstruction<1> {
3635 public:
3636 enum OperationKind {
3637 kEnter,
3638 kExit,
3639 };
3640
3641 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
3642 : HTemplateInstruction(SideEffects::None()), kind_(kind), dex_pc_(dex_pc) {
3643 SetRawInputAt(0, object);
3644 }
3645
3646 // Instruction may throw a Java exception, so we need an environment.
3647 bool NeedsEnvironment() const OVERRIDE { return true; }
3648 bool CanThrow() const OVERRIDE { return true; }
3649
3650 uint32_t GetDexPc() const { return dex_pc_; }
3651
3652 bool IsEnter() const { return kind_ == kEnter; }
3653
3654 DECLARE_INSTRUCTION(MonitorOperation);
3655
Calin Juravle52c48962014-12-16 17:02:57 +00003656 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003657 const OperationKind kind_;
3658 const uint32_t dex_pc_;
3659
3660 private:
3661 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
3662};
3663
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003664class MoveOperands : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003665 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01003666 MoveOperands(Location source,
3667 Location destination,
3668 Primitive::Type type,
3669 HInstruction* instruction)
3670 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003671
3672 Location GetSource() const { return source_; }
3673 Location GetDestination() const { return destination_; }
3674
3675 void SetSource(Location value) { source_ = value; }
3676 void SetDestination(Location value) { destination_ = value; }
3677
3678 // The parallel move resolver marks moves as "in-progress" by clearing the
3679 // destination (but not the source).
3680 Location MarkPending() {
3681 DCHECK(!IsPending());
3682 Location dest = destination_;
3683 destination_ = Location::NoLocation();
3684 return dest;
3685 }
3686
3687 void ClearPending(Location dest) {
3688 DCHECK(IsPending());
3689 destination_ = dest;
3690 }
3691
3692 bool IsPending() const {
3693 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
3694 return destination_.IsInvalid() && !source_.IsInvalid();
3695 }
3696
3697 // True if this blocks a move from the given location.
3698 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08003699 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003700 }
3701
3702 // A move is redundant if it's been eliminated, if its source and
3703 // destination are the same, or if its destination is unneeded.
3704 bool IsRedundant() const {
3705 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
3706 }
3707
3708 // We clear both operands to indicate move that's been eliminated.
3709 void Eliminate() {
3710 source_ = destination_ = Location::NoLocation();
3711 }
3712
3713 bool IsEliminated() const {
3714 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
3715 return source_.IsInvalid();
3716 }
3717
Nicolas Geoffray90218252015-04-15 11:56:51 +01003718 bool Is64BitMove() const {
3719 return Primitive::Is64BitType(type_);
3720 }
3721
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003722 HInstruction* GetInstruction() const { return instruction_; }
3723
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003724 private:
3725 Location source_;
3726 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01003727 // The type this move is for.
3728 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003729 // The instruction this move is assocatied with. Null when this move is
3730 // for moving an input in the expected locations of user (including a phi user).
3731 // This is only used in debug mode, to ensure we do not connect interval siblings
3732 // in the same parallel move.
3733 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003734};
3735
3736static constexpr size_t kDefaultNumberOfMoves = 4;
3737
3738class HParallelMove : public HTemplateInstruction<0> {
3739 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003740 explicit HParallelMove(ArenaAllocator* arena)
3741 : HTemplateInstruction(SideEffects::None()), moves_(arena, kDefaultNumberOfMoves) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003742
Nicolas Geoffray90218252015-04-15 11:56:51 +01003743 void AddMove(Location source,
3744 Location destination,
3745 Primitive::Type type,
3746 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003747 DCHECK(source.IsValid());
3748 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003749 if (kIsDebugBuild) {
3750 if (instruction != nullptr) {
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00003751 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003752 if (moves_.Get(i).GetInstruction() == instruction) {
3753 // Special case the situation where the move is for the spill slot
3754 // of the instruction.
3755 if ((GetPrevious() == instruction)
3756 || ((GetPrevious() == nullptr)
3757 && instruction->IsPhi()
3758 && instruction->GetBlock() == GetBlock())) {
3759 DCHECK_NE(destination.GetKind(), moves_.Get(i).GetDestination().GetKind())
3760 << "Doing parallel moves for the same instruction.";
3761 } else {
3762 DCHECK(false) << "Doing parallel moves for the same instruction.";
3763 }
3764 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00003765 }
3766 }
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003767 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Zheng Xuad4450e2015-04-17 18:48:56 +08003768 DCHECK(!destination.OverlapsWith(moves_.Get(i).GetDestination()))
3769 << "Overlapped destination for two moves in a parallel move.";
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003770 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003771 }
Nicolas Geoffray90218252015-04-15 11:56:51 +01003772 moves_.Add(MoveOperands(source, destination, type, instruction));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003773 }
3774
3775 MoveOperands* MoveOperandsAt(size_t index) const {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003776 return moves_.GetRawStorage() + index;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003777 }
3778
3779 size_t NumMoves() const { return moves_.Size(); }
3780
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003781 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003782
3783 private:
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003784 GrowableArray<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003785
3786 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
3787};
3788
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003789class HGraphVisitor : public ValueObject {
3790 public:
Dave Allison20dfc792014-06-16 20:44:29 -07003791 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
3792 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003793
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003794 virtual void VisitInstruction(HInstruction* instruction) { UNUSED(instruction); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003795 virtual void VisitBasicBlock(HBasicBlock* block);
3796
Roland Levillain633021e2014-10-01 14:12:25 +01003797 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003798 void VisitInsertionOrder();
3799
Roland Levillain633021e2014-10-01 14:12:25 +01003800 // Visit the graph following dominator tree reverse post-order.
3801 void VisitReversePostOrder();
3802
Nicolas Geoffray787c3072014-03-17 10:20:19 +00003803 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00003804
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003805 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003806#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003807 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
3808
3809 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
3810
3811#undef DECLARE_VISIT_INSTRUCTION
3812
3813 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07003814 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003815
3816 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
3817};
3818
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003819class HGraphDelegateVisitor : public HGraphVisitor {
3820 public:
3821 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
3822 virtual ~HGraphDelegateVisitor() {}
3823
3824 // Visit functions that delegate to to super class.
3825#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003826 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003827
3828 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
3829
3830#undef DECLARE_VISIT_INSTRUCTION
3831
3832 private:
3833 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
3834};
3835
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003836class HInsertionOrderIterator : public ValueObject {
3837 public:
3838 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
3839
3840 bool Done() const { return index_ == graph_.GetBlocks().Size(); }
3841 HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); }
3842 void Advance() { ++index_; }
3843
3844 private:
3845 const HGraph& graph_;
3846 size_t index_;
3847
3848 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
3849};
3850
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003851class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003852 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00003853 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
3854 // Check that reverse post order of the graph has been built.
3855 DCHECK(!graph.GetReversePostOrder().IsEmpty());
3856 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003857
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003858 bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); }
3859 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003860 void Advance() { ++index_; }
3861
3862 private:
3863 const HGraph& graph_;
3864 size_t index_;
3865
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003866 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003867};
3868
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003869class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003870 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003871 explicit HPostOrderIterator(const HGraph& graph)
David Brazdil10f56cb2015-03-24 18:49:14 +00003872 : graph_(graph), index_(graph_.GetReversePostOrder().Size()) {
3873 // Check that reverse post order of the graph has been built.
3874 DCHECK(!graph.GetReversePostOrder().IsEmpty());
3875 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003876
3877 bool Done() const { return index_ == 0; }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003878 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003879 void Advance() { --index_; }
3880
3881 private:
3882 const HGraph& graph_;
3883 size_t index_;
3884
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003885 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003886};
3887
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01003888class HLinearPostOrderIterator : public ValueObject {
3889 public:
3890 explicit HLinearPostOrderIterator(const HGraph& graph)
3891 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().Size()) {}
3892
3893 bool Done() const { return index_ == 0; }
3894
3895 HBasicBlock* Current() const { return order_.Get(index_ -1); }
3896
3897 void Advance() {
3898 --index_;
3899 DCHECK_GE(index_, 0U);
3900 }
3901
3902 private:
3903 const GrowableArray<HBasicBlock*>& order_;
3904 size_t index_;
3905
3906 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
3907};
3908
3909class HLinearOrderIterator : public ValueObject {
3910 public:
3911 explicit HLinearOrderIterator(const HGraph& graph)
3912 : order_(graph.GetLinearOrder()), index_(0) {}
3913
3914 bool Done() const { return index_ == order_.Size(); }
3915 HBasicBlock* Current() const { return order_.Get(index_); }
3916 void Advance() { ++index_; }
3917
3918 private:
3919 const GrowableArray<HBasicBlock*>& order_;
3920 size_t index_;
3921
3922 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
3923};
3924
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003925// Iterator over the blocks that art part of the loop. Includes blocks part
3926// of an inner loop. The order in which the blocks are iterated is on their
3927// block id.
3928class HBlocksInLoopIterator : public ValueObject {
3929 public:
3930 explicit HBlocksInLoopIterator(const HLoopInformation& info)
3931 : blocks_in_loop_(info.GetBlocks()),
3932 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
3933 index_(0) {
3934 if (!blocks_in_loop_.IsBitSet(index_)) {
3935 Advance();
3936 }
3937 }
3938
3939 bool Done() const { return index_ == blocks_.Size(); }
3940 HBasicBlock* Current() const { return blocks_.Get(index_); }
3941 void Advance() {
3942 ++index_;
3943 for (size_t e = blocks_.Size(); index_ < e; ++index_) {
3944 if (blocks_in_loop_.IsBitSet(index_)) {
3945 break;
3946 }
3947 }
3948 }
3949
3950 private:
3951 const BitVector& blocks_in_loop_;
3952 const GrowableArray<HBasicBlock*>& blocks_;
3953 size_t index_;
3954
3955 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
3956};
3957
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003958inline int64_t Int64FromConstant(HConstant* constant) {
3959 DCHECK(constant->IsIntConstant() || constant->IsLongConstant());
3960 return constant->IsIntConstant() ? constant->AsIntConstant()->GetValue()
3961 : constant->AsLongConstant()->GetValue();
3962}
3963
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003964} // namespace art
3965
3966#endif // ART_COMPILER_OPTIMIZING_NODES_H_