blob: 3fe23e18162a57e180c960e7d06ea783019f8295 [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 {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100639 return (loop_information_ != nullptr) && (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.
658 } else if (loop_information_ == nullptr) {
659 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
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100677 bool IsInLoop() const { return loop_information_ != nullptr; }
678
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100679 // Returns wheter this block dominates the blocked passed as parameter.
680 bool Dominates(HBasicBlock* block) const;
681
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100682 size_t GetLifetimeStart() const { return lifetime_start_; }
683 size_t GetLifetimeEnd() const { return lifetime_end_; }
684
685 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
686 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
687
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100688 uint32_t GetDexPc() const { return dex_pc_; }
689
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000690 bool IsCatchBlock() const { return is_catch_block_; }
691 void SetIsCatchBlock() { is_catch_block_ = true; }
692
David Brazdil8d5b8b22015-03-24 10:51:52 +0000693 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +0000694 bool EndsWithIf() const;
695 bool HasSinglePhi() const;
696
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000697 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000698 HGraph* graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000699 GrowableArray<HBasicBlock*> predecessors_;
700 GrowableArray<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100701 HInstructionList instructions_;
702 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000703 HLoopInformation* loop_information_;
704 HBasicBlock* dominator_;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100705 GrowableArray<HBasicBlock*> dominated_blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000706 int block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100707 // The dex program counter of the first instruction of this block.
708 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100709 size_t lifetime_start_;
710 size_t lifetime_end_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000711 bool is_catch_block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000712
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000713 friend class HGraph;
714 friend class HInstruction;
715
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000716 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
717};
718
David Brazdilb2bd1c52015-03-25 11:17:37 +0000719// Iterates over the LoopInformation of all loops which contain 'block'
720// from the innermost to the outermost.
721class HLoopInformationOutwardIterator : public ValueObject {
722 public:
723 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
724 : current_(block.GetLoopInformation()) {}
725
726 bool Done() const { return current_ == nullptr; }
727
728 void Advance() {
729 DCHECK(!Done());
730 current_ = current_->GetHeader()->GetDominator()->GetLoopInformation();
731 }
732
733 HLoopInformation* Current() const {
734 DCHECK(!Done());
735 return current_;
736 }
737
738 private:
739 HLoopInformation* current_;
740
741 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
742};
743
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100744#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
745 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000746 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000747 M(ArrayGet, Instruction) \
748 M(ArrayLength, Instruction) \
749 M(ArraySet, Instruction) \
David Brazdil66d126e2015-04-03 16:02:44 +0100750 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000751 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +0000752 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000753 M(CheckCast, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100754 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000755 M(Compare, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100756 M(Condition, BinaryOperation) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700757 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000758 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +0000759 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000760 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100761 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000762 M(Exit, Instruction) \
763 M(FloatConstant, Constant) \
764 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100765 M(GreaterThan, Condition) \
766 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100767 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000768 M(InstanceFieldGet, Instruction) \
769 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000770 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100771 M(IntConstant, Constant) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +0000772 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000773 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100774 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000775 M(LessThan, Condition) \
776 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000777 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000778 M(LoadException, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100779 M(LoadLocal, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000780 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100781 M(Local, Instruction) \
782 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +0100783 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +0000784 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000785 M(Mul, BinaryOperation) \
786 M(Neg, UnaryOperation) \
787 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100788 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +0100789 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000790 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000791 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000792 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000793 M(Or, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100794 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000795 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100796 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000797 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100798 M(Return, Instruction) \
799 M(ReturnVoid, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000800 M(Shl, BinaryOperation) \
801 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100802 M(StaticFieldGet, Instruction) \
803 M(StaticFieldSet, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100804 M(StoreLocal, Instruction) \
805 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100806 M(SuspendCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000807 M(Temporary, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000808 M(Throw, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +0000809 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000810 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000811 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000812
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100813#define FOR_EACH_INSTRUCTION(M) \
814 FOR_EACH_CONCRETE_INSTRUCTION(M) \
815 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +0100816 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +0100817 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100818 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -0700819
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100820#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000821FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
822#undef FORWARD_DECLARATION
823
Roland Levillainccc07a92014-09-16 14:48:16 +0100824#define DECLARE_INSTRUCTION(type) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000825 InstructionKind GetKind() const OVERRIDE { return k##type; } \
826 const char* DebugName() const OVERRIDE { return #type; } \
827 const H##type* As##type() const OVERRIDE { return this; } \
828 H##type* As##type() OVERRIDE { return this; } \
829 bool InstructionTypeEquals(HInstruction* other) const OVERRIDE { \
Roland Levillainccc07a92014-09-16 14:48:16 +0100830 return other->Is##type(); \
831 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000832 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000833
David Brazdiled596192015-01-23 10:39:45 +0000834template <typename T> class HUseList;
835
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100836template <typename T>
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700837class HUseListNode : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000838 public:
David Brazdiled596192015-01-23 10:39:45 +0000839 HUseListNode* GetPrevious() const { return prev_; }
840 HUseListNode* GetNext() const { return next_; }
841 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100842 size_t GetIndex() const { return index_; }
843
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000844 private:
David Brazdiled596192015-01-23 10:39:45 +0000845 HUseListNode(T user, size_t index)
846 : user_(user), index_(index), prev_(nullptr), next_(nullptr) {}
847
848 T const user_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100849 const size_t index_;
David Brazdiled596192015-01-23 10:39:45 +0000850 HUseListNode<T>* prev_;
851 HUseListNode<T>* next_;
852
853 friend class HUseList<T>;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000854
855 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
856};
857
David Brazdiled596192015-01-23 10:39:45 +0000858template <typename T>
859class HUseList : public ValueObject {
860 public:
861 HUseList() : first_(nullptr) {}
862
863 void Clear() {
864 first_ = nullptr;
865 }
866
867 // Adds a new entry at the beginning of the use list and returns
868 // the newly created node.
869 HUseListNode<T>* AddUse(T user, size_t index, ArenaAllocator* arena) {
David Brazdilea55b932015-01-27 17:12:29 +0000870 HUseListNode<T>* new_node = new (arena) HUseListNode<T>(user, index);
David Brazdiled596192015-01-23 10:39:45 +0000871 if (IsEmpty()) {
872 first_ = new_node;
873 } else {
874 first_->prev_ = new_node;
875 new_node->next_ = first_;
876 first_ = new_node;
877 }
878 return new_node;
879 }
880
881 HUseListNode<T>* GetFirst() const {
882 return first_;
883 }
884
885 void Remove(HUseListNode<T>* node) {
David Brazdil1abb4192015-02-17 18:33:36 +0000886 DCHECK(node != nullptr);
887 DCHECK(Contains(node));
888
David Brazdiled596192015-01-23 10:39:45 +0000889 if (node->prev_ != nullptr) {
890 node->prev_->next_ = node->next_;
891 }
892 if (node->next_ != nullptr) {
893 node->next_->prev_ = node->prev_;
894 }
895 if (node == first_) {
896 first_ = node->next_;
897 }
898 }
899
David Brazdil1abb4192015-02-17 18:33:36 +0000900 bool Contains(const HUseListNode<T>* node) const {
901 if (node == nullptr) {
902 return false;
903 }
904 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
905 if (current == node) {
906 return true;
907 }
908 }
909 return false;
910 }
911
David Brazdiled596192015-01-23 10:39:45 +0000912 bool IsEmpty() const {
913 return first_ == nullptr;
914 }
915
916 bool HasOnlyOneUse() const {
917 return first_ != nullptr && first_->next_ == nullptr;
918 }
919
920 private:
921 HUseListNode<T>* first_;
922};
923
924template<typename T>
925class HUseIterator : public ValueObject {
926 public:
927 explicit HUseIterator(const HUseList<T>& uses) : current_(uses.GetFirst()) {}
928
929 bool Done() const { return current_ == nullptr; }
930
931 void Advance() {
932 DCHECK(!Done());
933 current_ = current_->GetNext();
934 }
935
936 HUseListNode<T>* Current() const {
937 DCHECK(!Done());
938 return current_;
939 }
940
941 private:
942 HUseListNode<T>* current_;
943
944 friend class HValue;
945};
946
David Brazdil1abb4192015-02-17 18:33:36 +0000947// This class is used by HEnvironment and HInstruction classes to record the
948// instructions they use and pointers to the corresponding HUseListNodes kept
949// by the used instructions.
950template <typename T>
951class HUserRecord : public ValueObject {
952 public:
953 HUserRecord() : instruction_(nullptr), use_node_(nullptr) {}
954 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), use_node_(nullptr) {}
955
956 HUserRecord(const HUserRecord<T>& old_record, HUseListNode<T>* use_node)
957 : instruction_(old_record.instruction_), use_node_(use_node) {
958 DCHECK(instruction_ != nullptr);
959 DCHECK(use_node_ != nullptr);
960 DCHECK(old_record.use_node_ == nullptr);
961 }
962
963 HInstruction* GetInstruction() const { return instruction_; }
964 HUseListNode<T>* GetUseNode() const { return use_node_; }
965
966 private:
967 // Instruction used by the user.
968 HInstruction* instruction_;
969
970 // Corresponding entry in the use list kept by 'instruction_'.
971 HUseListNode<T>* use_node_;
972};
973
Calin Juravle27df7582015-04-17 19:12:31 +0100974// TODO: Add better documentation to this class and maybe refactor with more suggestive names.
975// - Has(All)SideEffects suggests that all the side effects are present but only ChangesSomething
976// flag is consider.
977// - DependsOn suggests that there is a real dependency between side effects but it only
978// checks DependendsOnSomething flag.
979//
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100980// Represents the side effects an instruction may have.
981class SideEffects : public ValueObject {
982 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100983 SideEffects() : flags_(0) {}
984
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100985 static SideEffects None() {
986 return SideEffects(0);
987 }
988
989 static SideEffects All() {
990 return SideEffects(ChangesSomething().flags_ | DependsOnSomething().flags_);
991 }
992
993 static SideEffects ChangesSomething() {
994 return SideEffects((1 << kFlagChangesCount) - 1);
995 }
996
997 static SideEffects DependsOnSomething() {
998 int count = kFlagDependsOnCount - kFlagChangesCount;
999 return SideEffects(((1 << count) - 1) << kFlagChangesCount);
1000 }
1001
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001002 SideEffects Union(SideEffects other) const {
1003 return SideEffects(flags_ | other.flags_);
1004 }
1005
Roland Levillain72bceff2014-09-15 18:29:00 +01001006 bool HasSideEffects() const {
1007 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
1008 return (flags_ & all_bits_set) != 0;
1009 }
1010
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001011 bool HasAllSideEffects() const {
1012 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
1013 return all_bits_set == (flags_ & all_bits_set);
1014 }
1015
1016 bool DependsOn(SideEffects other) const {
1017 size_t depends_flags = other.ComputeDependsFlags();
1018 return (flags_ & depends_flags) != 0;
1019 }
1020
1021 bool HasDependencies() const {
1022 int count = kFlagDependsOnCount - kFlagChangesCount;
1023 size_t all_bits_set = (1 << count) - 1;
1024 return ((flags_ >> kFlagChangesCount) & all_bits_set) != 0;
1025 }
1026
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001027 private:
1028 static constexpr int kFlagChangesSomething = 0;
1029 static constexpr int kFlagChangesCount = kFlagChangesSomething + 1;
1030
1031 static constexpr int kFlagDependsOnSomething = kFlagChangesCount;
1032 static constexpr int kFlagDependsOnCount = kFlagDependsOnSomething + 1;
1033
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001034 explicit SideEffects(size_t flags) : flags_(flags) {}
1035
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001036 size_t ComputeDependsFlags() const {
1037 return flags_ << kFlagChangesCount;
1038 }
1039
1040 size_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001041};
1042
David Brazdiled596192015-01-23 10:39:45 +00001043// A HEnvironment object contains the values of virtual registers at a given location.
1044class HEnvironment : public ArenaObject<kArenaAllocMisc> {
1045 public:
1046 HEnvironment(ArenaAllocator* arena, size_t number_of_vregs)
1047 : vregs_(arena, number_of_vregs) {
1048 vregs_.SetSize(number_of_vregs);
1049 for (size_t i = 0; i < number_of_vregs; i++) {
David Brazdil1abb4192015-02-17 18:33:36 +00001050 vregs_.Put(i, HUserRecord<HEnvironment*>());
David Brazdiled596192015-01-23 10:39:45 +00001051 }
1052 }
1053
1054 void CopyFrom(HEnvironment* env);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001055 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1056 // input to the loop phi instead. This is for inserting instructions that
1057 // require an environment (like HDeoptimization) in the loop pre-header.
1058 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001059
1060 void SetRawEnvAt(size_t index, HInstruction* instruction) {
David Brazdil1abb4192015-02-17 18:33:36 +00001061 vregs_.Put(index, HUserRecord<HEnvironment*>(instruction));
David Brazdiled596192015-01-23 10:39:45 +00001062 }
1063
1064 HInstruction* GetInstructionAt(size_t index) const {
David Brazdil1abb4192015-02-17 18:33:36 +00001065 return vregs_.Get(index).GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001066 }
1067
David Brazdil1abb4192015-02-17 18:33:36 +00001068 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001069
1070 size_t Size() const { return vregs_.Size(); }
1071
1072 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001073 // Record instructions' use entries of this environment for constant-time removal.
1074 // It should only be called by HInstruction when a new environment use is added.
1075 void RecordEnvUse(HUseListNode<HEnvironment*>* env_use) {
1076 DCHECK(env_use->GetUser() == this);
1077 size_t index = env_use->GetIndex();
1078 vregs_.Put(index, HUserRecord<HEnvironment*>(vregs_.Get(index), env_use));
1079 }
David Brazdiled596192015-01-23 10:39:45 +00001080
David Brazdil1abb4192015-02-17 18:33:36 +00001081 GrowableArray<HUserRecord<HEnvironment*> > vregs_;
David Brazdiled596192015-01-23 10:39:45 +00001082
David Brazdil1abb4192015-02-17 18:33:36 +00001083 friend HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001084
1085 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1086};
1087
Calin Juravleacf735c2015-02-12 15:25:22 +00001088class ReferenceTypeInfo : ValueObject {
1089 public:
Calin Juravleb1498f62015-02-16 13:13:29 +00001090 typedef Handle<mirror::Class> TypeHandle;
1091
1092 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact)
1093 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1094 if (type_handle->IsObjectClass()) {
1095 // Override the type handle to be consistent with the case when we get to
1096 // Top but don't have the Object class available. It avoids having to guess
1097 // what value the type_handle has when it's Top.
1098 return ReferenceTypeInfo(TypeHandle(), is_exact, true);
1099 } else {
1100 return ReferenceTypeInfo(type_handle, is_exact, false);
1101 }
1102 }
1103
1104 static ReferenceTypeInfo CreateTop(bool is_exact) {
1105 return ReferenceTypeInfo(TypeHandle(), is_exact, true);
Calin Juravleacf735c2015-02-12 15:25:22 +00001106 }
1107
1108 bool IsExact() const { return is_exact_; }
1109 bool IsTop() const { return is_top_; }
1110
1111 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
1112
Calin Juravleb1498f62015-02-16 13:13:29 +00001113 bool IsSupertypeOf(ReferenceTypeInfo rti) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Calin Juravleacf735c2015-02-12 15:25:22 +00001114 if (IsTop()) {
1115 // Top (equivalent for java.lang.Object) is supertype of anything.
1116 return true;
1117 }
1118 if (rti.IsTop()) {
1119 // If we get here `this` is not Top() so it can't be a supertype.
1120 return false;
1121 }
1122 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
1123 }
1124
1125 // Returns true if the type information provide the same amount of details.
1126 // Note that it does not mean that the instructions have the same actual type
1127 // (e.g. tops are equal but they can be the result of a merge).
1128 bool IsEqual(ReferenceTypeInfo rti) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1129 if (IsExact() != rti.IsExact()) {
1130 return false;
1131 }
1132 if (IsTop() && rti.IsTop()) {
1133 // `Top` means java.lang.Object, so the types are equivalent.
1134 return true;
1135 }
1136 if (IsTop() || rti.IsTop()) {
1137 // If only one is top or object than they are not equivalent.
1138 // NB: We need this extra check because the type_handle of `Top` is invalid
1139 // and we cannot inspect its reference.
1140 return false;
1141 }
1142
1143 // Finally check the types.
1144 return GetTypeHandle().Get() == rti.GetTypeHandle().Get();
1145 }
1146
1147 private:
Calin Juravleb1498f62015-02-16 13:13:29 +00001148 ReferenceTypeInfo() : ReferenceTypeInfo(TypeHandle(), false, true) {}
1149 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact, bool is_top)
1150 : type_handle_(type_handle), is_exact_(is_exact), is_top_(is_top) {}
1151
Calin Juravleacf735c2015-02-12 15:25:22 +00001152 // The class of the object.
Calin Juravleb1498f62015-02-16 13:13:29 +00001153 TypeHandle type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001154 // Whether or not the type is exact or a superclass of the actual type.
Calin Juravleb1498f62015-02-16 13:13:29 +00001155 // Whether or not we have any information about this type.
Calin Juravleacf735c2015-02-12 15:25:22 +00001156 bool is_exact_;
1157 // A true value here means that the object type should be java.lang.Object.
1158 // We don't have access to the corresponding mirror object every time so this
1159 // flag acts as a substitute. When true, the TypeHandle refers to a null
1160 // pointer and should not be used.
1161 bool is_top_;
1162};
1163
1164std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
1165
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001166class HInstruction : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001167 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001168 explicit HInstruction(SideEffects side_effects)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001169 : previous_(nullptr),
1170 next_(nullptr),
1171 block_(nullptr),
1172 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001173 ssa_index_(-1),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001174 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001175 locations_(nullptr),
1176 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001177 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001178 side_effects_(side_effects),
1179 reference_type_info_(ReferenceTypeInfo::CreateTop(/* is_exact */ false)) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001180
Dave Allison20dfc792014-06-16 20:44:29 -07001181 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001182
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001183#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001184 enum InstructionKind {
1185 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1186 };
1187#undef DECLARE_KIND
1188
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001189 HInstruction* GetNext() const { return next_; }
1190 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001191
Calin Juravle77520bc2015-01-12 18:45:46 +00001192 HInstruction* GetNextDisregardingMoves() const;
1193 HInstruction* GetPreviousDisregardingMoves() const;
1194
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001195 HBasicBlock* GetBlock() const { return block_; }
1196 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001197 bool IsInBlock() const { return block_ != nullptr; }
1198 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +01001199 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001200
Roland Levillain6b879dd2014-09-22 17:13:44 +01001201 virtual size_t InputCount() const = 0;
David Brazdil1abb4192015-02-17 18:33:36 +00001202 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001203
1204 virtual void Accept(HGraphVisitor* visitor) = 0;
1205 virtual const char* DebugName() const = 0;
1206
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001207 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
David Brazdil1abb4192015-02-17 18:33:36 +00001208 void SetRawInputAt(size_t index, HInstruction* input) {
1209 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1210 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001211
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001212 virtual bool NeedsEnvironment() const { return false; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001213 virtual bool IsControlFlow() const { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01001214 virtual bool CanThrow() const { return false; }
Roland Levillain72bceff2014-09-15 18:29:00 +01001215 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001216
Calin Juravle10e244f2015-01-26 18:54:32 +00001217 // Does not apply for all instructions, but having this at top level greatly
1218 // simplifies the null check elimination.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001219 virtual bool CanBeNull() const {
1220 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1221 return true;
1222 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001223
Calin Juravle641547a2015-04-21 22:08:51 +01001224 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj) const {
1225 UNUSED(obj);
1226 return false;
1227 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001228
Calin Juravleacf735c2015-02-12 15:25:22 +00001229 void SetReferenceTypeInfo(ReferenceTypeInfo reference_type_info) {
Calin Juravle61d544b2015-02-23 16:46:57 +00001230 DCHECK_EQ(GetType(), Primitive::kPrimNot);
Calin Juravleacf735c2015-02-12 15:25:22 +00001231 reference_type_info_ = reference_type_info;
1232 }
1233
Calin Juravle61d544b2015-02-23 16:46:57 +00001234 ReferenceTypeInfo GetReferenceTypeInfo() const {
1235 DCHECK_EQ(GetType(), Primitive::kPrimNot);
1236 return reference_type_info_;
1237 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001238
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001239 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001240 DCHECK(user != nullptr);
1241 HUseListNode<HInstruction*>* use =
1242 uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1243 user->SetRawInputRecordAt(index, HUserRecord<HInstruction*>(user->InputRecordAt(index), use));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001244 }
1245
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001246 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001247 DCHECK(user != nullptr);
David Brazdiled596192015-01-23 10:39:45 +00001248 HUseListNode<HEnvironment*>* env_use =
1249 env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1250 user->RecordEnvUse(env_use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001251 }
1252
David Brazdil1abb4192015-02-17 18:33:36 +00001253 void RemoveAsUserOfInput(size_t input) {
1254 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
1255 input_use.GetInstruction()->uses_.Remove(input_use.GetUseNode());
1256 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001257
David Brazdil1abb4192015-02-17 18:33:36 +00001258 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1259 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001260
David Brazdiled596192015-01-23 10:39:45 +00001261 bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); }
1262 bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); }
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001263 bool HasNonEnvironmentUses() const { return !uses_.IsEmpty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001264 bool HasOnlyOneNonEnvironmentUse() const {
1265 return !HasEnvironmentUses() && GetUses().HasOnlyOneUse();
1266 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001267
Roland Levillain6c82d402014-10-13 16:10:27 +01001268 // Does this instruction strictly dominate `other_instruction`?
1269 // Returns false if this instruction and `other_instruction` are the same.
1270 // Aborts if this instruction and `other_instruction` are both phis.
1271 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001272
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001273 int GetId() const { return id_; }
1274 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001275
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001276 int GetSsaIndex() const { return ssa_index_; }
1277 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1278 bool HasSsaIndex() const { return ssa_index_ != -1; }
1279
1280 bool HasEnvironment() const { return environment_ != nullptr; }
1281 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001282 // Set the `environment_` field. Raw because this method does not
1283 // update the uses lists.
1284 void SetRawEnvironment(HEnvironment* environment) { environment_ = environment; }
1285
1286 // Set the environment of this instruction, copying it from `environment`. While
1287 // copying, the uses lists are being updated.
1288 void CopyEnvironmentFrom(HEnvironment* environment) {
1289 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
1290 environment_ = new (allocator) HEnvironment(allocator, environment->Size());
1291 environment_->CopyFrom(environment);
1292 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001293
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001294 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
1295 HBasicBlock* block) {
1296 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
1297 environment_ = new (allocator) HEnvironment(allocator, environment->Size());
1298 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
1299 }
1300
Nicolas Geoffray39468442014-09-02 15:17:15 +01001301 // Returns the number of entries in the environment. Typically, that is the
1302 // number of dex registers in a method. It could be more in case of inlining.
1303 size_t EnvironmentSize() const;
1304
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001305 LocationSummary* GetLocations() const { return locations_; }
1306 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001307
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001308 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001309 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001310
Alexandre Rames188d4312015-04-09 18:30:21 +01001311 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
1312 // uses of this instruction by `other` are *not* updated.
1313 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
1314 ReplaceWith(other);
1315 other->ReplaceInput(this, use_index);
1316 }
1317
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001318 // Move `this` instruction before `cursor`.
1319 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001320
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001321#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01001322 bool Is##type() const { return (As##type() != nullptr); } \
1323 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001324 virtual H##type* As##type() { return nullptr; }
1325
1326 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
1327#undef INSTRUCTION_TYPE_CHECK
1328
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001329 // Returns whether the instruction can be moved within the graph.
1330 virtual bool CanBeMoved() const { return false; }
1331
1332 // Returns whether the two instructions are of the same kind.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001333 virtual bool InstructionTypeEquals(HInstruction* other) const {
1334 UNUSED(other);
1335 return false;
1336 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001337
1338 // Returns whether any data encoded in the two instructions is equal.
1339 // This method does not look at the inputs. Both instructions must be
1340 // of the same type, otherwise the method has undefined behavior.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001341 virtual bool InstructionDataEquals(HInstruction* other) const {
1342 UNUSED(other);
1343 return false;
1344 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001345
1346 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00001347 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001348 // 2) Their inputs are identical.
1349 bool Equals(HInstruction* other) const;
1350
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001351 virtual InstructionKind GetKind() const = 0;
1352
1353 virtual size_t ComputeHashCode() const {
1354 size_t result = GetKind();
1355 for (size_t i = 0, e = InputCount(); i < e; ++i) {
1356 result = (result * 31) + InputAt(i)->GetId();
1357 }
1358 return result;
1359 }
1360
1361 SideEffects GetSideEffects() const { return side_effects_; }
1362
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001363 size_t GetLifetimePosition() const { return lifetime_position_; }
1364 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
1365 LiveInterval* GetLiveInterval() const { return live_interval_; }
1366 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
1367 bool HasLiveInterval() const { return live_interval_ != nullptr; }
1368
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001369 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
1370
1371 // Returns whether the code generation of the instruction will require to have access
1372 // to the current method. Such instructions are:
1373 // (1): Instructions that require an environment, as calling the runtime requires
1374 // to walk the stack and have the current method stored at a specific stack address.
1375 // (2): Object literals like classes and strings, that are loaded from the dex cache
1376 // fields of the current method.
1377 bool NeedsCurrentMethod() const {
1378 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
1379 }
1380
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001381 virtual bool NeedsDexCache() const { return false; }
1382
David Brazdil1abb4192015-02-17 18:33:36 +00001383 protected:
1384 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
1385 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
1386
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001387 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001388 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); }
1389
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001390 HInstruction* previous_;
1391 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001392 HBasicBlock* block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001393
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001394 // An instruction gets an id when it is added to the graph.
1395 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01001396 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001397 int id_;
1398
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001399 // When doing liveness analysis, instructions that have uses get an SSA index.
1400 int ssa_index_;
1401
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001402 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00001403 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001404
1405 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00001406 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001407
Nicolas Geoffray39468442014-09-02 15:17:15 +01001408 // The environment associated with this instruction. Not null if the instruction
1409 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001410 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001411
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001412 // Set by the code generator.
1413 LocationSummary* locations_;
1414
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001415 // Set by the liveness analysis.
1416 LiveInterval* live_interval_;
1417
1418 // Set by the liveness analysis, this is the position in a linear
1419 // order of blocks where this instruction's live interval start.
1420 size_t lifetime_position_;
1421
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001422 const SideEffects side_effects_;
1423
Calin Juravleacf735c2015-02-12 15:25:22 +00001424 // TODO: for primitive types this should be marked as invalid.
1425 ReferenceTypeInfo reference_type_info_;
1426
David Brazdil1abb4192015-02-17 18:33:36 +00001427 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001428 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00001429 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001430 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001431 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001432
1433 DISALLOW_COPY_AND_ASSIGN(HInstruction);
1434};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001435std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001436
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001437class HInputIterator : public ValueObject {
1438 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001439 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001440
1441 bool Done() const { return index_ == instruction_->InputCount(); }
1442 HInstruction* Current() const { return instruction_->InputAt(index_); }
1443 void Advance() { index_++; }
1444
1445 private:
1446 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001447 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001448
1449 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
1450};
1451
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001452class HInstructionIterator : public ValueObject {
1453 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001454 explicit HInstructionIterator(const HInstructionList& instructions)
1455 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001456 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001457 }
1458
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001459 bool Done() const { return instruction_ == nullptr; }
1460 HInstruction* Current() const { return instruction_; }
1461 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001462 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001463 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001464 }
1465
1466 private:
1467 HInstruction* instruction_;
1468 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001469
1470 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001471};
1472
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001473class HBackwardInstructionIterator : public ValueObject {
1474 public:
1475 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
1476 : instruction_(instructions.last_instruction_) {
1477 next_ = Done() ? nullptr : instruction_->GetPrevious();
1478 }
1479
1480 bool Done() const { return instruction_ == nullptr; }
1481 HInstruction* Current() const { return instruction_; }
1482 void Advance() {
1483 instruction_ = next_;
1484 next_ = Done() ? nullptr : instruction_->GetPrevious();
1485 }
1486
1487 private:
1488 HInstruction* instruction_;
1489 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001490
1491 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001492};
1493
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001494// An embedded container with N elements of type T. Used (with partial
1495// specialization for N=0) because embedded arrays cannot have size 0.
1496template<typename T, intptr_t N>
1497class EmbeddedArray {
1498 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001499 EmbeddedArray() : elements_() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001500
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001501 intptr_t GetLength() const { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001502
1503 const T& operator[](intptr_t i) const {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001504 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001505 return elements_[i];
1506 }
1507
1508 T& operator[](intptr_t i) {
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 const T& At(intptr_t i) const {
1514 return (*this)[i];
1515 }
1516
1517 void SetAt(intptr_t i, const T& val) {
1518 (*this)[i] = val;
1519 }
1520
1521 private:
1522 T elements_[N];
1523};
1524
1525template<typename T>
1526class EmbeddedArray<T, 0> {
1527 public:
1528 intptr_t length() const { return 0; }
1529 const T& operator[](intptr_t i) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001530 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001531 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001532 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001533 }
1534 T& operator[](intptr_t i) {
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};
1540
1541template<intptr_t N>
1542class HTemplateInstruction: public HInstruction {
1543 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001544 HTemplateInstruction<N>(SideEffects side_effects)
1545 : HInstruction(side_effects), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07001546 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001547
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001548 size_t InputCount() const OVERRIDE { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001549
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001550 protected:
David Brazdil1abb4192015-02-17 18:33:36 +00001551 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_[i]; }
1552
1553 void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE {
1554 inputs_[i] = input;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001555 }
1556
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001557 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001558 EmbeddedArray<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001559
1560 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001561};
1562
Dave Allison20dfc792014-06-16 20:44:29 -07001563template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001564class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07001565 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001566 HExpression<N>(Primitive::Type type, SideEffects side_effects)
1567 : HTemplateInstruction<N>(side_effects), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07001568 virtual ~HExpression() {}
1569
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001570 Primitive::Type GetType() const OVERRIDE { return type_; }
Dave Allison20dfc792014-06-16 20:44:29 -07001571
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001572 protected:
1573 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07001574};
1575
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001576// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
1577// instruction that branches to the exit block.
1578class HReturnVoid : public HTemplateInstruction<0> {
1579 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001580 HReturnVoid() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001581
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001582 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001583
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001584 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001585
1586 private:
1587 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
1588};
1589
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001590// Represents dex's RETURN opcodes. A HReturn is a control flow
1591// instruction that branches to the exit block.
1592class HReturn : public HTemplateInstruction<1> {
1593 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001594 explicit HReturn(HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001595 SetRawInputAt(0, value);
1596 }
1597
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001598 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001599
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001600 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001601
1602 private:
1603 DISALLOW_COPY_AND_ASSIGN(HReturn);
1604};
1605
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001606// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001607// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001608// exit block.
1609class HExit : public HTemplateInstruction<0> {
1610 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001611 HExit() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001612
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001613 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001614
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001615 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001616
1617 private:
1618 DISALLOW_COPY_AND_ASSIGN(HExit);
1619};
1620
1621// Jumps from one block to another.
1622class HGoto : public HTemplateInstruction<0> {
1623 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001624 HGoto() : HTemplateInstruction(SideEffects::None()) {}
1625
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001626 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001627
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001628 HBasicBlock* GetSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001629 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001630 }
1631
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001632 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001633
1634 private:
1635 DISALLOW_COPY_AND_ASSIGN(HGoto);
1636};
1637
Dave Allison20dfc792014-06-16 20:44:29 -07001638
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001639// Conditional branch. A block ending with an HIf instruction must have
1640// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001641class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001642 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001643 explicit HIf(HInstruction* input) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001644 SetRawInputAt(0, input);
1645 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001646
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001647 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001648
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001649 HBasicBlock* IfTrueSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001650 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001651 }
1652
1653 HBasicBlock* IfFalseSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001654 return GetBlock()->GetSuccessors().Get(1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001655 }
1656
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001657 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001658
1659 private:
1660 DISALLOW_COPY_AND_ASSIGN(HIf);
1661};
1662
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001663// Deoptimize to interpreter, upon checking a condition.
1664class HDeoptimize : public HTemplateInstruction<1> {
1665 public:
1666 HDeoptimize(HInstruction* cond, uint32_t dex_pc)
1667 : HTemplateInstruction(SideEffects::None()),
1668 dex_pc_(dex_pc) {
1669 SetRawInputAt(0, cond);
1670 }
1671
1672 bool NeedsEnvironment() const OVERRIDE { return true; }
1673 bool CanThrow() const OVERRIDE { return true; }
1674 uint32_t GetDexPc() const { return dex_pc_; }
1675
1676 DECLARE_INSTRUCTION(Deoptimize);
1677
1678 private:
1679 uint32_t dex_pc_;
1680
1681 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
1682};
1683
Roland Levillain88cb1752014-10-20 16:36:47 +01001684class HUnaryOperation : public HExpression<1> {
1685 public:
1686 HUnaryOperation(Primitive::Type result_type, HInstruction* input)
1687 : HExpression(result_type, SideEffects::None()) {
1688 SetRawInputAt(0, input);
1689 }
1690
1691 HInstruction* GetInput() const { return InputAt(0); }
1692 Primitive::Type GetResultType() const { return GetType(); }
1693
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001694 bool CanBeMoved() const OVERRIDE { return true; }
1695 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001696 UNUSED(other);
1697 return true;
1698 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001699
Roland Levillain9240d6a2014-10-20 16:47:04 +01001700 // Try to statically evaluate `operation` and return a HConstant
1701 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001702 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01001703 HConstant* TryStaticEvaluation() const;
1704
1705 // Apply this operation to `x`.
1706 virtual int32_t Evaluate(int32_t x) const = 0;
1707 virtual int64_t Evaluate(int64_t x) const = 0;
1708
Roland Levillain88cb1752014-10-20 16:36:47 +01001709 DECLARE_INSTRUCTION(UnaryOperation);
1710
1711 private:
1712 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
1713};
1714
Dave Allison20dfc792014-06-16 20:44:29 -07001715class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001716 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001717 HBinaryOperation(Primitive::Type result_type,
1718 HInstruction* left,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001719 HInstruction* right) : HExpression(result_type, SideEffects::None()) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001720 SetRawInputAt(0, left);
1721 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001722 }
1723
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001724 HInstruction* GetLeft() const { return InputAt(0); }
1725 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07001726 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001727
Mingyao Yangdc5ac732015-02-25 11:28:05 -08001728 virtual bool IsCommutative() const { return false; }
1729
1730 // Put constant on the right.
1731 // Returns whether order is changed.
1732 bool OrderInputsWithConstantOnTheRight() {
1733 HInstruction* left = InputAt(0);
1734 HInstruction* right = InputAt(1);
1735 if (left->IsConstant() && !right->IsConstant()) {
1736 ReplaceInput(right, 0);
1737 ReplaceInput(left, 1);
1738 return true;
1739 }
1740 return false;
1741 }
1742
1743 // Order inputs by instruction id, but favor constant on the right side.
1744 // This helps GVN for commutative ops.
1745 void OrderInputs() {
1746 DCHECK(IsCommutative());
1747 HInstruction* left = InputAt(0);
1748 HInstruction* right = InputAt(1);
1749 if (left == right || (!left->IsConstant() && right->IsConstant())) {
1750 return;
1751 }
1752 if (OrderInputsWithConstantOnTheRight()) {
1753 return;
1754 }
1755 // Order according to instruction id.
1756 if (left->GetId() > right->GetId()) {
1757 ReplaceInput(right, 0);
1758 ReplaceInput(left, 1);
1759 }
1760 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001761
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001762 bool CanBeMoved() const OVERRIDE { return true; }
1763 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001764 UNUSED(other);
1765 return true;
1766 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001767
Roland Levillain9240d6a2014-10-20 16:47:04 +01001768 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01001769 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001770 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01001771 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01001772
1773 // Apply this operation to `x` and `y`.
1774 virtual int32_t Evaluate(int32_t x, int32_t y) const = 0;
1775 virtual int64_t Evaluate(int64_t x, int64_t y) const = 0;
1776
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001777 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001778 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001779 HConstant* GetConstantRight() const;
1780
1781 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001782 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001783 HInstruction* GetLeastConstantLeft() const;
1784
Roland Levillainccc07a92014-09-16 14:48:16 +01001785 DECLARE_INSTRUCTION(BinaryOperation);
1786
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001787 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001788 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
1789};
1790
Dave Allison20dfc792014-06-16 20:44:29 -07001791class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001792 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001793 HCondition(HInstruction* first, HInstruction* second)
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001794 : HBinaryOperation(Primitive::kPrimBoolean, first, second),
1795 needs_materialization_(true) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001796
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001797 bool NeedsMaterialization() const { return needs_materialization_; }
1798 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001799
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001800 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001801 // `instruction`, and disregard moves in between.
1802 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001803
Dave Allison20dfc792014-06-16 20:44:29 -07001804 DECLARE_INSTRUCTION(Condition);
1805
1806 virtual IfCondition GetCondition() const = 0;
1807
1808 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001809 // For register allocation purposes, returns whether this instruction needs to be
1810 // materialized (that is, not just be in the processor flags).
1811 bool needs_materialization_;
1812
Dave Allison20dfc792014-06-16 20:44:29 -07001813 DISALLOW_COPY_AND_ASSIGN(HCondition);
1814};
1815
1816// Instruction to check if two inputs are equal to each other.
1817class HEqual : public HCondition {
1818 public:
1819 HEqual(HInstruction* first, HInstruction* second)
1820 : HCondition(first, second) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001821
Mingyao Yangdc5ac732015-02-25 11:28:05 -08001822 bool IsCommutative() const OVERRIDE { return true; }
1823
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001824 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001825 return x == y ? 1 : 0;
1826 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001827 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001828 return x == y ? 1 : 0;
1829 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001830
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001831 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001832
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001833 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001834 return kCondEQ;
1835 }
1836
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001837 private:
1838 DISALLOW_COPY_AND_ASSIGN(HEqual);
1839};
1840
Dave Allison20dfc792014-06-16 20:44:29 -07001841class HNotEqual : public HCondition {
1842 public:
1843 HNotEqual(HInstruction* first, HInstruction* second)
1844 : HCondition(first, second) {}
1845
Mingyao Yangdc5ac732015-02-25 11:28:05 -08001846 bool IsCommutative() const OVERRIDE { return true; }
1847
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001848 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001849 return x != y ? 1 : 0;
1850 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001851 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001852 return x != y ? 1 : 0;
1853 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001854
Dave Allison20dfc792014-06-16 20:44:29 -07001855 DECLARE_INSTRUCTION(NotEqual);
1856
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001857 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001858 return kCondNE;
1859 }
1860
1861 private:
1862 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
1863};
1864
1865class HLessThan : public HCondition {
1866 public:
1867 HLessThan(HInstruction* first, HInstruction* second)
1868 : HCondition(first, second) {}
1869
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001870 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001871 return x < y ? 1 : 0;
1872 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001873 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001874 return x < y ? 1 : 0;
1875 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001876
Dave Allison20dfc792014-06-16 20:44:29 -07001877 DECLARE_INSTRUCTION(LessThan);
1878
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001879 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001880 return kCondLT;
1881 }
1882
1883 private:
1884 DISALLOW_COPY_AND_ASSIGN(HLessThan);
1885};
1886
1887class HLessThanOrEqual : public HCondition {
1888 public:
1889 HLessThanOrEqual(HInstruction* first, HInstruction* second)
1890 : HCondition(first, second) {}
1891
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001892 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001893 return x <= y ? 1 : 0;
1894 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001895 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001896 return x <= y ? 1 : 0;
1897 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001898
Dave Allison20dfc792014-06-16 20:44:29 -07001899 DECLARE_INSTRUCTION(LessThanOrEqual);
1900
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001901 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001902 return kCondLE;
1903 }
1904
1905 private:
1906 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
1907};
1908
1909class HGreaterThan : public HCondition {
1910 public:
1911 HGreaterThan(HInstruction* first, HInstruction* second)
1912 : HCondition(first, second) {}
1913
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001914 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001915 return x > y ? 1 : 0;
1916 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001917 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001918 return x > y ? 1 : 0;
1919 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001920
Dave Allison20dfc792014-06-16 20:44:29 -07001921 DECLARE_INSTRUCTION(GreaterThan);
1922
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001923 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001924 return kCondGT;
1925 }
1926
1927 private:
1928 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
1929};
1930
1931class HGreaterThanOrEqual : public HCondition {
1932 public:
1933 HGreaterThanOrEqual(HInstruction* first, HInstruction* second)
1934 : HCondition(first, second) {}
1935
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001936 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001937 return x >= y ? 1 : 0;
1938 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001939 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001940 return x >= y ? 1 : 0;
1941 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001942
Dave Allison20dfc792014-06-16 20:44:29 -07001943 DECLARE_INSTRUCTION(GreaterThanOrEqual);
1944
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001945 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001946 return kCondGE;
1947 }
1948
1949 private:
1950 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
1951};
1952
1953
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001954// Instruction to check how two inputs compare to each other.
1955// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
1956class HCompare : public HBinaryOperation {
1957 public:
Calin Juravleddb7df22014-11-25 20:56:51 +00001958 // The bias applies for floating point operations and indicates how NaN
1959 // comparisons are treated:
1960 enum Bias {
1961 kNoBias, // bias is not applicable (i.e. for long operation)
1962 kGtBias, // return 1 for NaN comparisons
1963 kLtBias, // return -1 for NaN comparisons
1964 };
1965
1966 HCompare(Primitive::Type type, HInstruction* first, HInstruction* second, Bias bias)
1967 : HBinaryOperation(Primitive::kPrimInt, first, second), bias_(bias) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001968 DCHECK_EQ(type, first->GetType());
1969 DCHECK_EQ(type, second->GetType());
1970 }
1971
Calin Juravleddb7df22014-11-25 20:56:51 +00001972 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01001973 return
1974 x == y ? 0 :
1975 x > y ? 1 :
1976 -1;
1977 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001978
1979 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01001980 return
1981 x == y ? 0 :
1982 x > y ? 1 :
1983 -1;
1984 }
1985
Calin Juravleddb7df22014-11-25 20:56:51 +00001986 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
1987 return bias_ == other->AsCompare()->bias_;
1988 }
1989
1990 bool IsGtBias() { return bias_ == kGtBias; }
1991
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001992 DECLARE_INSTRUCTION(Compare);
1993
1994 private:
Calin Juravleddb7df22014-11-25 20:56:51 +00001995 const Bias bias_;
1996
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001997 DISALLOW_COPY_AND_ASSIGN(HCompare);
1998};
1999
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002000// A local in the graph. Corresponds to a Dex register.
2001class HLocal : public HTemplateInstruction<0> {
2002 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002003 explicit HLocal(uint16_t reg_number)
2004 : HTemplateInstruction(SideEffects::None()), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002005
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002006 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002007
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002008 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002009
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002010 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002011 // The Dex register number.
2012 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002013
2014 DISALLOW_COPY_AND_ASSIGN(HLocal);
2015};
2016
2017// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07002018class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002019 public:
Roland Levillain5799fc02014-09-25 12:15:20 +01002020 HLoadLocal(HLocal* local, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002021 : HExpression(type, SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002022 SetRawInputAt(0, local);
2023 }
2024
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002025 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
2026
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002027 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002028
2029 private:
2030 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
2031};
2032
2033// Store a value in a given local. This instruction has two inputs: the value
2034// and the local.
2035class HStoreLocal : public HTemplateInstruction<2> {
2036 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002037 HStoreLocal(HLocal* local, HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002038 SetRawInputAt(0, local);
2039 SetRawInputAt(1, value);
2040 }
2041
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002042 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
2043
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002044 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002045
2046 private:
2047 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
2048};
2049
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002050class HConstant : public HExpression<0> {
2051 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002052 explicit HConstant(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
2053
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002054 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002055
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002056 virtual bool IsMinusOne() const { return false; }
2057 virtual bool IsZero() const { return false; }
2058 virtual bool IsOne() const { return false; }
2059
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002060 DECLARE_INSTRUCTION(Constant);
2061
2062 private:
2063 DISALLOW_COPY_AND_ASSIGN(HConstant);
2064};
2065
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002066class HFloatConstant : public HConstant {
2067 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002068 float GetValue() const { return value_; }
2069
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002070 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002071 return bit_cast<uint32_t, float>(other->AsFloatConstant()->value_) ==
2072 bit_cast<uint32_t, float>(value_);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002073 }
2074
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002075 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002076
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002077 bool IsMinusOne() const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002078 return bit_cast<uint32_t, float>(AsFloatConstant()->GetValue()) ==
2079 bit_cast<uint32_t, float>((-1.0f));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002080 }
2081 bool IsZero() const OVERRIDE {
2082 return AsFloatConstant()->GetValue() == 0.0f;
2083 }
2084 bool IsOne() const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002085 return bit_cast<uint32_t, float>(AsFloatConstant()->GetValue()) ==
2086 bit_cast<uint32_t, float>(1.0f);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002087 }
2088
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002089 DECLARE_INSTRUCTION(FloatConstant);
2090
2091 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002092 explicit HFloatConstant(float value) : HConstant(Primitive::kPrimFloat), value_(value) {}
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002093 explicit HFloatConstant(int32_t value)
2094 : HConstant(Primitive::kPrimFloat), value_(bit_cast<float, int32_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00002095
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002096 const float value_;
2097
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002098 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00002099 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002100 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002101 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2102};
2103
2104class HDoubleConstant : public HConstant {
2105 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002106 double GetValue() const { return value_; }
2107
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002108 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002109 return bit_cast<uint64_t, double>(other->AsDoubleConstant()->value_) ==
2110 bit_cast<uint64_t, double>(value_);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002111 }
2112
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002113 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002114
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002115 bool IsMinusOne() const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002116 return bit_cast<uint64_t, double>(AsDoubleConstant()->GetValue()) ==
2117 bit_cast<uint64_t, double>((-1.0));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002118 }
2119 bool IsZero() const OVERRIDE {
2120 return AsDoubleConstant()->GetValue() == 0.0;
2121 }
2122 bool IsOne() const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002123 return bit_cast<uint64_t, double>(AsDoubleConstant()->GetValue()) ==
2124 bit_cast<uint64_t, double>(1.0);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002125 }
2126
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002127 DECLARE_INSTRUCTION(DoubleConstant);
2128
2129 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002130 explicit HDoubleConstant(double value) : HConstant(Primitive::kPrimDouble), value_(value) {}
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002131 explicit HDoubleConstant(int64_t value)
2132 : HConstant(Primitive::kPrimDouble), value_(bit_cast<double, int64_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00002133
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002134 const double value_;
2135
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002136 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00002137 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002138 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002139 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
2140};
2141
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002142class HNullConstant : public HConstant {
2143 public:
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002144 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2145 return true;
2146 }
2147
2148 size_t ComputeHashCode() const OVERRIDE { return 0; }
2149
2150 DECLARE_INSTRUCTION(NullConstant);
2151
2152 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002153 HNullConstant() : HConstant(Primitive::kPrimNot) {}
2154
2155 friend class HGraph;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002156 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2157};
2158
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002159// Constants of the type int. Those can be from Dex instructions, or
2160// synthesized (for example with the if-eqz instruction).
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002161class HIntConstant : public HConstant {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002162 public:
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002163 int32_t GetValue() const { return value_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002164
Calin Juravle61d544b2015-02-23 16:46:57 +00002165 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002166 return other->AsIntConstant()->value_ == value_;
2167 }
2168
Calin Juravle61d544b2015-02-23 16:46:57 +00002169 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2170
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002171 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2172 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2173 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2174
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002175 DECLARE_INSTRUCTION(IntConstant);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002176
2177 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002178 explicit HIntConstant(int32_t value) : HConstant(Primitive::kPrimInt), value_(value) {}
2179
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002180 const int32_t value_;
2181
David Brazdil8d5b8b22015-03-24 10:51:52 +00002182 friend class HGraph;
2183 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
Zheng Xuad4450e2015-04-17 18:48:56 +08002184 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002185 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2186};
2187
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002188class HLongConstant : public HConstant {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002189 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002190 int64_t GetValue() const { return value_; }
2191
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002192 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002193 return other->AsLongConstant()->value_ == value_;
2194 }
2195
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002196 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002197
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002198 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2199 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2200 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2201
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002202 DECLARE_INSTRUCTION(LongConstant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002203
2204 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002205 explicit HLongConstant(int64_t value) : HConstant(Primitive::kPrimLong), value_(value) {}
2206
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002207 const int64_t value_;
2208
David Brazdil8d5b8b22015-03-24 10:51:52 +00002209 friend class HGraph;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002210 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2211};
2212
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002213enum class Intrinsics {
2214#define OPTIMIZING_INTRINSICS(Name, IsStatic) k ## Name,
2215#include "intrinsics_list.h"
2216 kNone,
2217 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
2218#undef INTRINSICS_LIST
2219#undef OPTIMIZING_INTRINSICS
2220};
2221std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
2222
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002223class HInvoke : public HInstruction {
2224 public:
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002225 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002226
2227 // Runtime needs to walk the stack, so Dex -> Dex calls need to
2228 // know their environment.
Calin Juravle77520bc2015-01-12 18:45:46 +00002229 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002230
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01002231 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002232 SetRawInputAt(index, argument);
2233 }
2234
Roland Levillain3e3d7332015-04-28 11:00:54 +01002235 // Return the number of arguments. This number can be lower than
2236 // the number of inputs returned by InputCount(), as some invoke
2237 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
2238 // inputs at the end of their list of inputs.
2239 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
2240
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002241 Primitive::Type GetType() const OVERRIDE { return return_type_; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002242
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002243 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002244
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002245 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
2246
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01002247 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002248 return intrinsic_;
2249 }
2250
2251 void SetIntrinsic(Intrinsics intrinsic) {
2252 intrinsic_ = intrinsic;
2253 }
2254
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002255 DECLARE_INSTRUCTION(Invoke);
2256
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002257 protected:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002258 HInvoke(ArenaAllocator* arena,
2259 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01002260 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002261 Primitive::Type return_type,
2262 uint32_t dex_pc,
2263 uint32_t dex_method_index)
2264 : HInstruction(SideEffects::All()),
Roland Levillain3e3d7332015-04-28 11:00:54 +01002265 number_of_arguments_(number_of_arguments),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002266 inputs_(arena, number_of_arguments),
2267 return_type_(return_type),
2268 dex_pc_(dex_pc),
2269 dex_method_index_(dex_method_index),
2270 intrinsic_(Intrinsics::kNone) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01002271 uint32_t number_of_inputs = number_of_arguments + number_of_other_inputs;
2272 inputs_.SetSize(number_of_inputs);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002273 }
2274
David Brazdil1abb4192015-02-17 18:33:36 +00002275 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
2276 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
2277 inputs_.Put(index, input);
2278 }
2279
Roland Levillain3e3d7332015-04-28 11:00:54 +01002280 uint32_t number_of_arguments_;
David Brazdil1abb4192015-02-17 18:33:36 +00002281 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002282 const Primitive::Type return_type_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002283 const uint32_t dex_pc_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002284 const uint32_t dex_method_index_;
2285 Intrinsics intrinsic_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002286
2287 private:
2288 DISALLOW_COPY_AND_ASSIGN(HInvoke);
2289};
2290
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002291class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002292 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01002293 // Requirements of this method call regarding the class
2294 // initialization (clinit) check of its declaring class.
2295 enum class ClinitCheckRequirement {
2296 kNone, // Class already initialized.
2297 kExplicit, // Static call having explicit clinit check as last input.
2298 kImplicit, // Static call implicitly requiring a clinit check.
2299 };
2300
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002301 HInvokeStaticOrDirect(ArenaAllocator* arena,
2302 uint32_t number_of_arguments,
2303 Primitive::Type return_type,
2304 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002305 uint32_t dex_method_index,
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002306 bool is_recursive,
Jeff Hao848f70a2014-01-15 13:49:50 -08002307 int32_t string_init_offset,
Nicolas Geoffray79041292015-03-26 10:05:54 +00002308 InvokeType original_invoke_type,
Roland Levillain4c0eb422015-04-24 16:43:49 +01002309 InvokeType invoke_type,
2310 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01002311 : HInvoke(arena,
2312 number_of_arguments,
2313 clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u,
2314 return_type,
2315 dex_pc,
2316 dex_method_index),
Nicolas Geoffray79041292015-03-26 10:05:54 +00002317 original_invoke_type_(original_invoke_type),
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002318 invoke_type_(invoke_type),
Roland Levillain4c0eb422015-04-24 16:43:49 +01002319 is_recursive_(is_recursive),
Jeff Hao848f70a2014-01-15 13:49:50 -08002320 clinit_check_requirement_(clinit_check_requirement),
2321 string_init_offset_(string_init_offset) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002322
Calin Juravle641547a2015-04-21 22:08:51 +01002323 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
2324 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00002325 // We access the method via the dex cache so we can't do an implicit null check.
2326 // TODO: for intrinsics we can generate implicit null checks.
2327 return false;
2328 }
2329
Nicolas Geoffray79041292015-03-26 10:05:54 +00002330 InvokeType GetOriginalInvokeType() const { return original_invoke_type_; }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002331 InvokeType GetInvokeType() const { return invoke_type_; }
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002332 bool IsRecursive() const { return is_recursive_; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002333 bool NeedsDexCache() const OVERRIDE { return !IsRecursive(); }
Jeff Hao848f70a2014-01-15 13:49:50 -08002334 bool IsStringInit() const { return string_init_offset_ != 0; }
2335 int32_t GetStringInitOffset() const { return string_init_offset_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002336
Roland Levillain4c0eb422015-04-24 16:43:49 +01002337 // Is this instruction a call to a static method?
2338 bool IsStatic() const {
2339 return GetInvokeType() == kStatic;
2340 }
2341
Roland Levillain3e3d7332015-04-28 11:00:54 +01002342 // Remove the art::HLoadClass instruction set as last input by
2343 // art::PrepareForRegisterAllocation::VisitClinitCheck in lieu of
2344 // the initial art::HClinitCheck instruction (only relevant for
2345 // static calls with explicit clinit check).
2346 void RemoveLoadClassAsLastInput() {
Roland Levillain4c0eb422015-04-24 16:43:49 +01002347 DCHECK(IsStaticWithExplicitClinitCheck());
2348 size_t last_input_index = InputCount() - 1;
2349 HInstruction* last_input = InputAt(last_input_index);
2350 DCHECK(last_input != nullptr);
Roland Levillain3e3d7332015-04-28 11:00:54 +01002351 DCHECK(last_input->IsLoadClass()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01002352 RemoveAsUserOfInput(last_input_index);
2353 inputs_.DeleteAt(last_input_index);
2354 clinit_check_requirement_ = ClinitCheckRequirement::kImplicit;
2355 DCHECK(IsStaticWithImplicitClinitCheck());
2356 }
2357
2358 // Is this a call to a static method whose declaring class has an
2359 // explicit intialization check in the graph?
2360 bool IsStaticWithExplicitClinitCheck() const {
2361 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kExplicit);
2362 }
2363
2364 // Is this a call to a static method whose declaring class has an
2365 // implicit intialization check requirement?
2366 bool IsStaticWithImplicitClinitCheck() const {
2367 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kImplicit);
2368 }
2369
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002370 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002371
Roland Levillain4c0eb422015-04-24 16:43:49 +01002372 protected:
2373 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
2374 const HUserRecord<HInstruction*> input_record = HInvoke::InputRecordAt(i);
2375 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck() && (i == InputCount() - 1)) {
2376 HInstruction* input = input_record.GetInstruction();
2377 // `input` is the last input of a static invoke marked as having
2378 // an explicit clinit check. It must either be:
2379 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
2380 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
2381 DCHECK(input != nullptr);
2382 DCHECK(input->IsClinitCheck() || input->IsLoadClass()) << input->DebugName();
2383 }
2384 return input_record;
2385 }
2386
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002387 private:
Nicolas Geoffray79041292015-03-26 10:05:54 +00002388 const InvokeType original_invoke_type_;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002389 const InvokeType invoke_type_;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002390 const bool is_recursive_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01002391 ClinitCheckRequirement clinit_check_requirement_;
Jeff Hao848f70a2014-01-15 13:49:50 -08002392 // Thread entrypoint offset for string init method if this is a string init invoke.
2393 // Note that there are multiple string init methods, each having its own offset.
2394 int32_t string_init_offset_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002395
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002396 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002397};
2398
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002399class HInvokeVirtual : public HInvoke {
2400 public:
2401 HInvokeVirtual(ArenaAllocator* arena,
2402 uint32_t number_of_arguments,
2403 Primitive::Type return_type,
2404 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002405 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002406 uint32_t vtable_index)
Roland Levillain3e3d7332015-04-28 11:00:54 +01002407 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002408 vtable_index_(vtable_index) {}
2409
Calin Juravle641547a2015-04-21 22:08:51 +01002410 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00002411 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01002412 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00002413 }
2414
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002415 uint32_t GetVTableIndex() const { return vtable_index_; }
2416
2417 DECLARE_INSTRUCTION(InvokeVirtual);
2418
2419 private:
2420 const uint32_t vtable_index_;
2421
2422 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
2423};
2424
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002425class HInvokeInterface : public HInvoke {
2426 public:
2427 HInvokeInterface(ArenaAllocator* arena,
2428 uint32_t number_of_arguments,
2429 Primitive::Type return_type,
2430 uint32_t dex_pc,
2431 uint32_t dex_method_index,
2432 uint32_t imt_index)
Roland Levillain3e3d7332015-04-28 11:00:54 +01002433 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002434 imt_index_(imt_index) {}
2435
Calin Juravle641547a2015-04-21 22:08:51 +01002436 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00002437 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01002438 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00002439 }
2440
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002441 uint32_t GetImtIndex() const { return imt_index_; }
2442 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
2443
2444 DECLARE_INSTRUCTION(InvokeInterface);
2445
2446 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002447 const uint32_t imt_index_;
2448
2449 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
2450};
2451
Dave Allison20dfc792014-06-16 20:44:29 -07002452class HNewInstance : public HExpression<0> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002453 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002454 HNewInstance(uint32_t dex_pc, uint16_t type_index, QuickEntrypointEnum entrypoint)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002455 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2456 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002457 type_index_(type_index),
2458 entrypoint_(entrypoint) {}
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002459
2460 uint32_t GetDexPc() const { return dex_pc_; }
2461 uint16_t GetTypeIndex() const { return type_index_; }
2462
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002463 // Calls runtime so needs an environment.
Calin Juravle92a6ed22014-12-02 18:58:03 +00002464 bool NeedsEnvironment() const OVERRIDE { return true; }
2465 // It may throw when called on:
2466 // - interfaces
2467 // - abstract/innaccessible/unknown classes
2468 // TODO: optimize when possible.
2469 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002470
Calin Juravle10e244f2015-01-26 18:54:32 +00002471 bool CanBeNull() const OVERRIDE { return false; }
2472
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002473 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
2474
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002475 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002476
2477 private:
2478 const uint32_t dex_pc_;
2479 const uint16_t type_index_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002480 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002481
2482 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
2483};
2484
Roland Levillain88cb1752014-10-20 16:36:47 +01002485class HNeg : public HUnaryOperation {
2486 public:
2487 explicit HNeg(Primitive::Type result_type, HInstruction* input)
2488 : HUnaryOperation(result_type, input) {}
2489
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002490 int32_t Evaluate(int32_t x) const OVERRIDE { return -x; }
2491 int64_t Evaluate(int64_t x) const OVERRIDE { return -x; }
Roland Levillain9240d6a2014-10-20 16:47:04 +01002492
Roland Levillain88cb1752014-10-20 16:36:47 +01002493 DECLARE_INSTRUCTION(Neg);
2494
2495 private:
2496 DISALLOW_COPY_AND_ASSIGN(HNeg);
2497};
2498
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002499class HNewArray : public HExpression<1> {
2500 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002501 HNewArray(HInstruction* length,
2502 uint32_t dex_pc,
2503 uint16_t type_index,
2504 QuickEntrypointEnum entrypoint)
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002505 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2506 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002507 type_index_(type_index),
2508 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002509 SetRawInputAt(0, length);
2510 }
2511
2512 uint32_t GetDexPc() const { return dex_pc_; }
2513 uint16_t GetTypeIndex() const { return type_index_; }
2514
2515 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00002516 bool NeedsEnvironment() const OVERRIDE { return true; }
2517
Mingyao Yang0c365e62015-03-31 15:09:29 -07002518 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
2519 bool CanThrow() const OVERRIDE { return true; }
2520
Calin Juravle10e244f2015-01-26 18:54:32 +00002521 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002522
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002523 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
2524
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002525 DECLARE_INSTRUCTION(NewArray);
2526
2527 private:
2528 const uint32_t dex_pc_;
2529 const uint16_t type_index_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002530 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002531
2532 DISALLOW_COPY_AND_ASSIGN(HNewArray);
2533};
2534
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002535class HAdd : public HBinaryOperation {
2536 public:
2537 HAdd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2538 : HBinaryOperation(result_type, left, right) {}
2539
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002540 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002541
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002542 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002543 return x + y;
2544 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002545 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002546 return x + y;
2547 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002548
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002549 DECLARE_INSTRUCTION(Add);
2550
2551 private:
2552 DISALLOW_COPY_AND_ASSIGN(HAdd);
2553};
2554
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002555class HSub : public HBinaryOperation {
2556 public:
2557 HSub(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2558 : HBinaryOperation(result_type, left, right) {}
2559
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002560 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002561 return x - y;
2562 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002563 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002564 return x - y;
2565 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002566
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002567 DECLARE_INSTRUCTION(Sub);
2568
2569 private:
2570 DISALLOW_COPY_AND_ASSIGN(HSub);
2571};
2572
Calin Juravle34bacdf2014-10-07 20:23:36 +01002573class HMul : public HBinaryOperation {
2574 public:
2575 HMul(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2576 : HBinaryOperation(result_type, left, right) {}
2577
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002578 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002579
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002580 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x * y; }
2581 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x * y; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002582
2583 DECLARE_INSTRUCTION(Mul);
2584
2585 private:
2586 DISALLOW_COPY_AND_ASSIGN(HMul);
2587};
2588
Calin Juravle7c4954d2014-10-28 16:57:40 +00002589class HDiv : public HBinaryOperation {
2590 public:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002591 HDiv(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
2592 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00002593
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002594 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00002595 // Our graph structure ensures we never have 0 for `y` during constant folding.
2596 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00002597 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00002598 return (y == -1) ? -x : x / y;
2599 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002600
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002601 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002602 DCHECK_NE(y, 0);
2603 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2604 return (y == -1) ? -x : x / y;
2605 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002606
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002607 uint32_t GetDexPc() const { return dex_pc_; }
2608
Calin Juravle7c4954d2014-10-28 16:57:40 +00002609 DECLARE_INSTRUCTION(Div);
2610
2611 private:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002612 const uint32_t dex_pc_;
2613
Calin Juravle7c4954d2014-10-28 16:57:40 +00002614 DISALLOW_COPY_AND_ASSIGN(HDiv);
2615};
2616
Calin Juravlebacfec32014-11-14 15:54:36 +00002617class HRem : public HBinaryOperation {
2618 public:
2619 HRem(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
2620 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
2621
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002622 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002623 DCHECK_NE(y, 0);
2624 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2625 return (y == -1) ? 0 : x % y;
2626 }
2627
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002628 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002629 DCHECK_NE(y, 0);
2630 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2631 return (y == -1) ? 0 : x % y;
2632 }
2633
2634 uint32_t GetDexPc() const { return dex_pc_; }
2635
2636 DECLARE_INSTRUCTION(Rem);
2637
2638 private:
2639 const uint32_t dex_pc_;
2640
2641 DISALLOW_COPY_AND_ASSIGN(HRem);
2642};
2643
Calin Juravled0d48522014-11-04 16:40:20 +00002644class HDivZeroCheck : public HExpression<1> {
2645 public:
2646 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
2647 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
2648 SetRawInputAt(0, value);
2649 }
2650
2651 bool CanBeMoved() const OVERRIDE { return true; }
2652
2653 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2654 UNUSED(other);
2655 return true;
2656 }
2657
2658 bool NeedsEnvironment() const OVERRIDE { return true; }
2659 bool CanThrow() const OVERRIDE { return true; }
2660
2661 uint32_t GetDexPc() const { return dex_pc_; }
2662
2663 DECLARE_INSTRUCTION(DivZeroCheck);
2664
2665 private:
2666 const uint32_t dex_pc_;
2667
2668 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
2669};
2670
Calin Juravle9aec02f2014-11-18 23:06:35 +00002671class HShl : public HBinaryOperation {
2672 public:
2673 HShl(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2674 : HBinaryOperation(result_type, left, right) {}
2675
2676 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x << (y & kMaxIntShiftValue); }
2677 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x << (y & kMaxLongShiftValue); }
2678
2679 DECLARE_INSTRUCTION(Shl);
2680
2681 private:
2682 DISALLOW_COPY_AND_ASSIGN(HShl);
2683};
2684
2685class HShr : public HBinaryOperation {
2686 public:
2687 HShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2688 : HBinaryOperation(result_type, left, right) {}
2689
2690 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x >> (y & kMaxIntShiftValue); }
2691 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x >> (y & kMaxLongShiftValue); }
2692
2693 DECLARE_INSTRUCTION(Shr);
2694
2695 private:
2696 DISALLOW_COPY_AND_ASSIGN(HShr);
2697};
2698
2699class HUShr : public HBinaryOperation {
2700 public:
2701 HUShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2702 : HBinaryOperation(result_type, left, right) {}
2703
2704 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
2705 uint32_t ux = static_cast<uint32_t>(x);
2706 uint32_t uy = static_cast<uint32_t>(y) & kMaxIntShiftValue;
2707 return static_cast<int32_t>(ux >> uy);
2708 }
2709
2710 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
2711 uint64_t ux = static_cast<uint64_t>(x);
2712 uint64_t uy = static_cast<uint64_t>(y) & kMaxLongShiftValue;
2713 return static_cast<int64_t>(ux >> uy);
2714 }
2715
2716 DECLARE_INSTRUCTION(UShr);
2717
2718 private:
2719 DISALLOW_COPY_AND_ASSIGN(HUShr);
2720};
2721
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002722class HAnd : public HBinaryOperation {
2723 public:
2724 HAnd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2725 : HBinaryOperation(result_type, left, right) {}
2726
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002727 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002728
2729 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x & y; }
2730 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x & y; }
2731
2732 DECLARE_INSTRUCTION(And);
2733
2734 private:
2735 DISALLOW_COPY_AND_ASSIGN(HAnd);
2736};
2737
2738class HOr : public HBinaryOperation {
2739 public:
2740 HOr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2741 : HBinaryOperation(result_type, left, right) {}
2742
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002743 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002744
2745 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x | y; }
2746 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x | y; }
2747
2748 DECLARE_INSTRUCTION(Or);
2749
2750 private:
2751 DISALLOW_COPY_AND_ASSIGN(HOr);
2752};
2753
2754class HXor : public HBinaryOperation {
2755 public:
2756 HXor(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2757 : HBinaryOperation(result_type, left, right) {}
2758
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002759 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002760
2761 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x ^ y; }
2762 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x ^ y; }
2763
2764 DECLARE_INSTRUCTION(Xor);
2765
2766 private:
2767 DISALLOW_COPY_AND_ASSIGN(HXor);
2768};
2769
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002770// The value of a parameter in this method. Its location depends on
2771// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07002772class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002773 public:
Calin Juravle10e244f2015-01-26 18:54:32 +00002774 HParameterValue(uint8_t index, Primitive::Type parameter_type, bool is_this = false)
2775 : HExpression(parameter_type, SideEffects::None()), index_(index), is_this_(is_this) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002776
2777 uint8_t GetIndex() const { return index_; }
2778
Calin Juravle10e244f2015-01-26 18:54:32 +00002779 bool CanBeNull() const OVERRIDE { return !is_this_; }
2780
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002781 DECLARE_INSTRUCTION(ParameterValue);
2782
2783 private:
2784 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00002785 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002786 const uint8_t index_;
2787
Calin Juravle10e244f2015-01-26 18:54:32 +00002788 // Whether or not the parameter value corresponds to 'this' argument.
2789 const bool is_this_;
2790
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002791 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
2792};
2793
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002794class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002795 public:
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002796 explicit HNot(Primitive::Type result_type, HInstruction* input)
2797 : HUnaryOperation(result_type, input) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002798
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002799 bool CanBeMoved() const OVERRIDE { return true; }
2800 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002801 UNUSED(other);
2802 return true;
2803 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002804
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002805 int32_t Evaluate(int32_t x) const OVERRIDE { return ~x; }
2806 int64_t Evaluate(int64_t x) const OVERRIDE { return ~x; }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002807
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002808 DECLARE_INSTRUCTION(Not);
2809
2810 private:
2811 DISALLOW_COPY_AND_ASSIGN(HNot);
2812};
2813
David Brazdil66d126e2015-04-03 16:02:44 +01002814class HBooleanNot : public HUnaryOperation {
2815 public:
2816 explicit HBooleanNot(HInstruction* input)
2817 : HUnaryOperation(Primitive::Type::kPrimBoolean, input) {}
2818
2819 bool CanBeMoved() const OVERRIDE { return true; }
2820 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2821 UNUSED(other);
2822 return true;
2823 }
2824
2825 int32_t Evaluate(int32_t x) const OVERRIDE {
2826 DCHECK(IsUint<1>(x));
2827 return !x;
2828 }
2829
2830 int64_t Evaluate(int64_t x ATTRIBUTE_UNUSED) const OVERRIDE {
2831 LOG(FATAL) << DebugName() << " cannot be used with 64-bit values";
2832 UNREACHABLE();
2833 }
2834
2835 DECLARE_INSTRUCTION(BooleanNot);
2836
2837 private:
2838 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
2839};
2840
Roland Levillaindff1f282014-11-05 14:15:05 +00002841class HTypeConversion : public HExpression<1> {
2842 public:
2843 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00002844 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
2845 : HExpression(result_type, SideEffects::None()), dex_pc_(dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002846 SetRawInputAt(0, input);
2847 DCHECK_NE(input->GetType(), result_type);
2848 }
2849
2850 HInstruction* GetInput() const { return InputAt(0); }
2851 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
2852 Primitive::Type GetResultType() const { return GetType(); }
2853
Roland Levillain624279f2014-12-04 11:54:28 +00002854 // Required by the x86 and ARM code generators when producing calls
2855 // to the runtime.
2856 uint32_t GetDexPc() const { return dex_pc_; }
2857
Roland Levillaindff1f282014-11-05 14:15:05 +00002858 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00002859 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00002860
2861 DECLARE_INSTRUCTION(TypeConversion);
2862
2863 private:
Roland Levillain624279f2014-12-04 11:54:28 +00002864 const uint32_t dex_pc_;
2865
Roland Levillaindff1f282014-11-05 14:15:05 +00002866 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
2867};
2868
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00002869static constexpr uint32_t kNoRegNumber = -1;
2870
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002871class HPhi : public HInstruction {
2872 public:
2873 HPhi(ArenaAllocator* arena, uint32_t reg_number, size_t number_of_inputs, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002874 : HInstruction(SideEffects::None()),
2875 inputs_(arena, number_of_inputs),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002876 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002877 type_(type),
Calin Juravle10e244f2015-01-26 18:54:32 +00002878 is_live_(false),
2879 can_be_null_(true) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002880 inputs_.SetSize(number_of_inputs);
2881 }
2882
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00002883 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
2884 static Primitive::Type ToPhiType(Primitive::Type type) {
2885 switch (type) {
2886 case Primitive::kPrimBoolean:
2887 case Primitive::kPrimByte:
2888 case Primitive::kPrimShort:
2889 case Primitive::kPrimChar:
2890 return Primitive::kPrimInt;
2891 default:
2892 return type;
2893 }
2894 }
2895
Calin Juravle10e244f2015-01-26 18:54:32 +00002896 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002897
2898 void AddInput(HInstruction* input);
David Brazdil2d7352b2015-04-20 14:52:42 +01002899 void RemoveInputAt(size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002900
Calin Juravle10e244f2015-01-26 18:54:32 +00002901 Primitive::Type GetType() const OVERRIDE { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002902 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002903
Calin Juravle10e244f2015-01-26 18:54:32 +00002904 bool CanBeNull() const OVERRIDE { return can_be_null_; }
2905 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
2906
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002907 uint32_t GetRegNumber() const { return reg_number_; }
2908
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002909 void SetDead() { is_live_ = false; }
2910 void SetLive() { is_live_ = true; }
2911 bool IsDead() const { return !is_live_; }
2912 bool IsLive() const { return is_live_; }
2913
Calin Juravlea4f88312015-04-16 12:57:19 +01002914 // Returns the next equivalent phi (starting from the current one) or null if there is none.
2915 // An equivalent phi is a phi having the same dex register and type.
2916 // It assumes that phis with the same dex register are adjacent.
2917 HPhi* GetNextEquivalentPhiWithSameType() {
2918 HInstruction* next = GetNext();
2919 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
2920 if (next->GetType() == GetType()) {
2921 return next->AsPhi();
2922 }
2923 next = next->GetNext();
2924 }
2925 return nullptr;
2926 }
2927
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002928 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002929
David Brazdil1abb4192015-02-17 18:33:36 +00002930 protected:
2931 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
2932
2933 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
2934 inputs_.Put(index, input);
2935 }
2936
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002937 private:
David Brazdil1abb4192015-02-17 18:33:36 +00002938 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002939 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002940 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002941 bool is_live_;
Calin Juravle10e244f2015-01-26 18:54:32 +00002942 bool can_be_null_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002943
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002944 DISALLOW_COPY_AND_ASSIGN(HPhi);
2945};
2946
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002947class HNullCheck : public HExpression<1> {
2948 public:
2949 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002950 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002951 SetRawInputAt(0, value);
2952 }
2953
Calin Juravle10e244f2015-01-26 18:54:32 +00002954 bool CanBeMoved() const OVERRIDE { return true; }
2955 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002956 UNUSED(other);
2957 return true;
2958 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002959
Calin Juravle10e244f2015-01-26 18:54:32 +00002960 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002961
Calin Juravle10e244f2015-01-26 18:54:32 +00002962 bool CanThrow() const OVERRIDE { return true; }
2963
2964 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01002965
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002966 uint32_t GetDexPc() const { return dex_pc_; }
2967
2968 DECLARE_INSTRUCTION(NullCheck);
2969
2970 private:
2971 const uint32_t dex_pc_;
2972
2973 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
2974};
2975
2976class FieldInfo : public ValueObject {
2977 public:
Calin Juravle52c48962014-12-16 17:02:57 +00002978 FieldInfo(MemberOffset field_offset, Primitive::Type field_type, bool is_volatile)
2979 : field_offset_(field_offset), field_type_(field_type), is_volatile_(is_volatile) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002980
2981 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002982 Primitive::Type GetFieldType() const { return field_type_; }
Calin Juravle52c48962014-12-16 17:02:57 +00002983 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002984
2985 private:
2986 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01002987 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00002988 const bool is_volatile_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002989};
2990
2991class HInstanceFieldGet : public HExpression<1> {
2992 public:
2993 HInstanceFieldGet(HInstruction* value,
2994 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00002995 MemberOffset field_offset,
2996 bool is_volatile)
Nicolas Geoffray2af23072015-04-30 11:15:40 +00002997 : HExpression(field_type, SideEffects::DependsOnSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00002998 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002999 SetRawInputAt(0, value);
3000 }
3001
Calin Juravle10c9cbe2014-12-19 10:50:19 +00003002 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003003
3004 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3005 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
3006 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003007 }
3008
Calin Juravle641547a2015-04-21 22:08:51 +01003009 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3010 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00003011 }
3012
3013 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01003014 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
3015 }
3016
Calin Juravle52c48962014-12-16 17:02:57 +00003017 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003018 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003019 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003020 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003021
3022 DECLARE_INSTRUCTION(InstanceFieldGet);
3023
3024 private:
3025 const FieldInfo field_info_;
3026
3027 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
3028};
3029
3030class HInstanceFieldSet : public HTemplateInstruction<2> {
3031 public:
3032 HInstanceFieldSet(HInstruction* object,
3033 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01003034 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003035 MemberOffset field_offset,
3036 bool is_volatile)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003037 : HTemplateInstruction(SideEffects::ChangesSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00003038 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003039 SetRawInputAt(0, object);
3040 SetRawInputAt(1, value);
3041 }
3042
Calin Juravle641547a2015-04-21 22:08:51 +01003043 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3044 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00003045 }
3046
Calin Juravle52c48962014-12-16 17:02:57 +00003047 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003048 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003049 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003050 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003051 HInstruction* GetValue() const { return InputAt(1); }
3052
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003053 DECLARE_INSTRUCTION(InstanceFieldSet);
3054
3055 private:
3056 const FieldInfo field_info_;
3057
3058 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
3059};
3060
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003061class HArrayGet : public HExpression<2> {
3062 public:
3063 HArrayGet(HInstruction* array, HInstruction* index, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003064 : HExpression(type, SideEffects::DependsOnSomething()) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003065 SetRawInputAt(0, array);
3066 SetRawInputAt(1, index);
3067 }
3068
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003069 bool CanBeMoved() const OVERRIDE { return true; }
3070 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003071 UNUSED(other);
3072 return true;
3073 }
Calin Juravle641547a2015-04-21 22:08:51 +01003074 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3075 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00003076 // TODO: We can be smarter here.
3077 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
3078 // which generates the implicit null check. There are cases when these can be removed
3079 // to produce better code. If we ever add optimizations to do so we should allow an
3080 // implicit check here (as long as the address falls in the first page).
3081 return false;
3082 }
3083
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003084 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003085
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003086 HInstruction* GetArray() const { return InputAt(0); }
3087 HInstruction* GetIndex() const { return InputAt(1); }
3088
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003089 DECLARE_INSTRUCTION(ArrayGet);
3090
3091 private:
3092 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
3093};
3094
3095class HArraySet : public HTemplateInstruction<3> {
3096 public:
3097 HArraySet(HInstruction* array,
3098 HInstruction* index,
3099 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003100 Primitive::Type expected_component_type,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003101 uint32_t dex_pc)
3102 : HTemplateInstruction(SideEffects::ChangesSomething()),
3103 dex_pc_(dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003104 expected_component_type_(expected_component_type),
3105 needs_type_check_(value->GetType() == Primitive::kPrimNot) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003106 SetRawInputAt(0, array);
3107 SetRawInputAt(1, index);
3108 SetRawInputAt(2, value);
3109 }
3110
Calin Juravle77520bc2015-01-12 18:45:46 +00003111 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003112 // We currently always call a runtime method to catch array store
3113 // exceptions.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003114 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003115 }
3116
Calin Juravle641547a2015-04-21 22:08:51 +01003117 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3118 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00003119 // TODO: Same as for ArrayGet.
3120 return false;
3121 }
3122
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003123 void ClearNeedsTypeCheck() {
3124 needs_type_check_ = false;
3125 }
3126
3127 bool NeedsTypeCheck() const { return needs_type_check_; }
3128
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003129 uint32_t GetDexPc() const { return dex_pc_; }
3130
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003131 HInstruction* GetArray() const { return InputAt(0); }
3132 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003133 HInstruction* GetValue() const { return InputAt(2); }
3134
3135 Primitive::Type GetComponentType() const {
3136 // The Dex format does not type floating point index operations. Since the
3137 // `expected_component_type_` is set during building and can therefore not
3138 // be correct, we also check what is the value type. If it is a floating
3139 // point type, we must use that type.
3140 Primitive::Type value_type = GetValue()->GetType();
3141 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
3142 ? value_type
3143 : expected_component_type_;
3144 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003145
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003146 DECLARE_INSTRUCTION(ArraySet);
3147
3148 private:
3149 const uint32_t dex_pc_;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003150 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003151 bool needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003152
3153 DISALLOW_COPY_AND_ASSIGN(HArraySet);
3154};
3155
3156class HArrayLength : public HExpression<1> {
3157 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003158 explicit HArrayLength(HInstruction* array)
3159 : HExpression(Primitive::kPrimInt, SideEffects::None()) {
3160 // Note that arrays do not change length, so the instruction does not
3161 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003162 SetRawInputAt(0, array);
3163 }
3164
Calin Juravle77520bc2015-01-12 18:45:46 +00003165 bool CanBeMoved() const OVERRIDE { return true; }
3166 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003167 UNUSED(other);
3168 return true;
3169 }
Calin Juravle641547a2015-04-21 22:08:51 +01003170 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3171 return obj == InputAt(0);
3172 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003173
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003174 DECLARE_INSTRUCTION(ArrayLength);
3175
3176 private:
3177 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
3178};
3179
3180class HBoundsCheck : public HExpression<2> {
3181 public:
3182 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003183 : HExpression(index->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003184 DCHECK(index->GetType() == Primitive::kPrimInt);
3185 SetRawInputAt(0, index);
3186 SetRawInputAt(1, length);
3187 }
3188
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003189 bool CanBeMoved() const OVERRIDE { return true; }
3190 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003191 UNUSED(other);
3192 return true;
3193 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003194
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003195 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003196
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003197 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01003198
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003199 uint32_t GetDexPc() const { return dex_pc_; }
3200
3201 DECLARE_INSTRUCTION(BoundsCheck);
3202
3203 private:
3204 const uint32_t dex_pc_;
3205
3206 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
3207};
3208
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003209/**
3210 * Some DEX instructions are folded into multiple HInstructions that need
3211 * to stay live until the last HInstruction. This class
3212 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00003213 * HInstruction stays live. `index` represents the stack location index of the
3214 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003215 */
3216class HTemporary : public HTemplateInstruction<0> {
3217 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003218 explicit HTemporary(size_t index) : HTemplateInstruction(SideEffects::None()), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003219
3220 size_t GetIndex() const { return index_; }
3221
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00003222 Primitive::Type GetType() const OVERRIDE {
3223 // The previous instruction is the one that will be stored in the temporary location.
3224 DCHECK(GetPrevious() != nullptr);
3225 return GetPrevious()->GetType();
3226 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00003227
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003228 DECLARE_INSTRUCTION(Temporary);
3229
3230 private:
3231 const size_t index_;
3232
3233 DISALLOW_COPY_AND_ASSIGN(HTemporary);
3234};
3235
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003236class HSuspendCheck : public HTemplateInstruction<0> {
3237 public:
3238 explicit HSuspendCheck(uint32_t dex_pc)
Nicolas Geoffray9ebc72c2014-09-25 16:33:42 +01003239 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003240
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003241 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003242 return true;
3243 }
3244
3245 uint32_t GetDexPc() const { return dex_pc_; }
3246
3247 DECLARE_INSTRUCTION(SuspendCheck);
3248
3249 private:
3250 const uint32_t dex_pc_;
3251
3252 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
3253};
3254
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003255/**
3256 * Instruction to load a Class object.
3257 */
3258class HLoadClass : public HExpression<0> {
3259 public:
3260 HLoadClass(uint16_t type_index,
3261 bool is_referrers_class,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003262 uint32_t dex_pc)
3263 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3264 type_index_(type_index),
3265 is_referrers_class_(is_referrers_class),
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003266 dex_pc_(dex_pc),
Calin Juravleb1498f62015-02-16 13:13:29 +00003267 generate_clinit_check_(false),
3268 loaded_class_rti_(ReferenceTypeInfo::CreateTop(/* is_exact */ false)) {}
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003269
3270 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003271
3272 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3273 return other->AsLoadClass()->type_index_ == type_index_;
3274 }
3275
3276 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
3277
3278 uint32_t GetDexPc() const { return dex_pc_; }
3279 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003280 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003281
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003282 bool NeedsEnvironment() const OVERRIDE {
3283 // Will call runtime and load the class if the class is not loaded yet.
3284 // TODO: finer grain decision.
3285 return !is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003286 }
3287
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003288 bool MustGenerateClinitCheck() const {
3289 return generate_clinit_check_;
3290 }
3291
3292 void SetMustGenerateClinitCheck() {
3293 generate_clinit_check_ = true;
3294 }
3295
3296 bool CanCallRuntime() const {
3297 return MustGenerateClinitCheck() || !is_referrers_class_;
3298 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003299
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003300 bool CanThrow() const OVERRIDE {
3301 // May call runtime and and therefore can throw.
3302 // TODO: finer grain decision.
3303 return !is_referrers_class_;
3304 }
3305
Calin Juravleacf735c2015-02-12 15:25:22 +00003306 ReferenceTypeInfo GetLoadedClassRTI() {
3307 return loaded_class_rti_;
3308 }
3309
3310 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
3311 // Make sure we only set exact types (the loaded class should never be merged).
3312 DCHECK(rti.IsExact());
3313 loaded_class_rti_ = rti;
3314 }
3315
3316 bool IsResolved() {
3317 return loaded_class_rti_.IsExact();
3318 }
3319
Nicolas Geoffray9437b782015-03-25 10:08:51 +00003320 bool NeedsDexCache() const OVERRIDE { return !is_referrers_class_; }
3321
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003322 DECLARE_INSTRUCTION(LoadClass);
3323
3324 private:
3325 const uint16_t type_index_;
3326 const bool is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003327 const uint32_t dex_pc_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003328 // Whether this instruction must generate the initialization check.
3329 // Used for code generation.
3330 bool generate_clinit_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003331
Calin Juravleacf735c2015-02-12 15:25:22 +00003332 ReferenceTypeInfo loaded_class_rti_;
3333
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003334 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
3335};
3336
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003337class HLoadString : public HExpression<0> {
3338 public:
3339 HLoadString(uint32_t string_index, uint32_t dex_pc)
3340 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3341 string_index_(string_index),
3342 dex_pc_(dex_pc) {}
3343
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003344 bool CanBeMoved() const OVERRIDE { return true; }
3345
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003346 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3347 return other->AsLoadString()->string_index_ == string_index_;
3348 }
3349
3350 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
3351
3352 uint32_t GetDexPc() const { return dex_pc_; }
3353 uint32_t GetStringIndex() const { return string_index_; }
3354
3355 // TODO: Can we deopt or debug when we resolve a string?
3356 bool NeedsEnvironment() const OVERRIDE { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00003357 bool NeedsDexCache() const OVERRIDE { return true; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003358
3359 DECLARE_INSTRUCTION(LoadString);
3360
3361 private:
3362 const uint32_t string_index_;
3363 const uint32_t dex_pc_;
3364
3365 DISALLOW_COPY_AND_ASSIGN(HLoadString);
3366};
3367
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003368/**
3369 * Performs an initialization check on its Class object input.
3370 */
3371class HClinitCheck : public HExpression<1> {
3372 public:
3373 explicit HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Nicolas Geoffraya0466e12015-03-27 15:00:40 +00003374 : HExpression(Primitive::kPrimNot, SideEffects::ChangesSomething()),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003375 dex_pc_(dex_pc) {
3376 SetRawInputAt(0, constant);
3377 }
3378
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003379 bool CanBeMoved() const OVERRIDE { return true; }
3380 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3381 UNUSED(other);
3382 return true;
3383 }
3384
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003385 bool NeedsEnvironment() const OVERRIDE {
3386 // May call runtime to initialize the class.
3387 return true;
3388 }
3389
3390 uint32_t GetDexPc() const { return dex_pc_; }
3391
3392 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
3393
3394 DECLARE_INSTRUCTION(ClinitCheck);
3395
3396 private:
3397 const uint32_t dex_pc_;
3398
3399 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
3400};
3401
3402class HStaticFieldGet : public HExpression<1> {
3403 public:
3404 HStaticFieldGet(HInstruction* cls,
3405 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003406 MemberOffset field_offset,
3407 bool is_volatile)
Nicolas Geoffray2af23072015-04-30 11:15:40 +00003408 : HExpression(field_type, SideEffects::DependsOnSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00003409 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003410 SetRawInputAt(0, cls);
3411 }
3412
Calin Juravle52c48962014-12-16 17:02:57 +00003413
Calin Juravle10c9cbe2014-12-19 10:50:19 +00003414 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003415
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003416 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00003417 HStaticFieldGet* other_get = other->AsStaticFieldGet();
3418 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003419 }
3420
3421 size_t ComputeHashCode() const OVERRIDE {
3422 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
3423 }
3424
Calin Juravle52c48962014-12-16 17:02:57 +00003425 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003426 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
3427 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003428 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003429
3430 DECLARE_INSTRUCTION(StaticFieldGet);
3431
3432 private:
3433 const FieldInfo field_info_;
3434
3435 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
3436};
3437
3438class HStaticFieldSet : public HTemplateInstruction<2> {
3439 public:
3440 HStaticFieldSet(HInstruction* cls,
3441 HInstruction* value,
3442 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003443 MemberOffset field_offset,
3444 bool is_volatile)
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003445 : HTemplateInstruction(SideEffects::ChangesSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00003446 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003447 SetRawInputAt(0, cls);
3448 SetRawInputAt(1, value);
3449 }
3450
Calin Juravle52c48962014-12-16 17:02:57 +00003451 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003452 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
3453 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003454 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003455
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003456 HInstruction* GetValue() const { return InputAt(1); }
3457
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003458 DECLARE_INSTRUCTION(StaticFieldSet);
3459
3460 private:
3461 const FieldInfo field_info_;
3462
3463 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
3464};
3465
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003466// Implement the move-exception DEX instruction.
3467class HLoadException : public HExpression<0> {
3468 public:
3469 HLoadException() : HExpression(Primitive::kPrimNot, SideEffects::None()) {}
3470
3471 DECLARE_INSTRUCTION(LoadException);
3472
3473 private:
3474 DISALLOW_COPY_AND_ASSIGN(HLoadException);
3475};
3476
3477class HThrow : public HTemplateInstruction<1> {
3478 public:
3479 HThrow(HInstruction* exception, uint32_t dex_pc)
3480 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {
3481 SetRawInputAt(0, exception);
3482 }
3483
3484 bool IsControlFlow() const OVERRIDE { return true; }
3485
3486 bool NeedsEnvironment() const OVERRIDE { return true; }
3487
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003488 bool CanThrow() const OVERRIDE { return true; }
3489
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003490 uint32_t GetDexPc() const { return dex_pc_; }
3491
3492 DECLARE_INSTRUCTION(Throw);
3493
3494 private:
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003495 const uint32_t dex_pc_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003496
3497 DISALLOW_COPY_AND_ASSIGN(HThrow);
3498};
3499
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003500class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003501 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003502 HInstanceOf(HInstruction* object,
3503 HLoadClass* constant,
3504 bool class_is_final,
3505 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003506 : HExpression(Primitive::kPrimBoolean, SideEffects::None()),
3507 class_is_final_(class_is_final),
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003508 must_do_null_check_(true),
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003509 dex_pc_(dex_pc) {
3510 SetRawInputAt(0, object);
3511 SetRawInputAt(1, constant);
3512 }
3513
3514 bool CanBeMoved() const OVERRIDE { return true; }
3515
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003516 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003517 return true;
3518 }
3519
3520 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003521 return false;
3522 }
3523
3524 uint32_t GetDexPc() const { return dex_pc_; }
3525
3526 bool IsClassFinal() const { return class_is_final_; }
3527
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003528 // Used only in code generation.
3529 bool MustDoNullCheck() const { return must_do_null_check_; }
3530 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
3531
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003532 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003533
3534 private:
3535 const bool class_is_final_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003536 bool must_do_null_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003537 const uint32_t dex_pc_;
3538
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003539 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
3540};
3541
Calin Juravleb1498f62015-02-16 13:13:29 +00003542class HBoundType : public HExpression<1> {
3543 public:
3544 HBoundType(HInstruction* input, ReferenceTypeInfo bound_type)
3545 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3546 bound_type_(bound_type) {
Calin Juravle61d544b2015-02-23 16:46:57 +00003547 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00003548 SetRawInputAt(0, input);
3549 }
3550
3551 const ReferenceTypeInfo& GetBoundType() const { return bound_type_; }
3552
3553 bool CanBeNull() const OVERRIDE {
3554 // `null instanceof ClassX` always return false so we can't be null.
3555 return false;
3556 }
3557
3558 DECLARE_INSTRUCTION(BoundType);
3559
3560 private:
3561 // Encodes the most upper class that this instruction can have. In other words
3562 // it is always the case that GetBoundType().IsSupertypeOf(GetReferenceType()).
3563 // It is used to bound the type in cases like `if (x instanceof ClassX) {}`
3564 const ReferenceTypeInfo bound_type_;
3565
3566 DISALLOW_COPY_AND_ASSIGN(HBoundType);
3567};
3568
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003569class HCheckCast : public HTemplateInstruction<2> {
3570 public:
3571 HCheckCast(HInstruction* object,
3572 HLoadClass* constant,
3573 bool class_is_final,
3574 uint32_t dex_pc)
3575 : HTemplateInstruction(SideEffects::None()),
3576 class_is_final_(class_is_final),
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003577 must_do_null_check_(true),
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003578 dex_pc_(dex_pc) {
3579 SetRawInputAt(0, object);
3580 SetRawInputAt(1, constant);
3581 }
3582
3583 bool CanBeMoved() const OVERRIDE { return true; }
3584
3585 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
3586 return true;
3587 }
3588
3589 bool NeedsEnvironment() const OVERRIDE {
3590 // Instruction may throw a CheckCastError.
3591 return true;
3592 }
3593
3594 bool CanThrow() const OVERRIDE { return true; }
3595
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003596 bool MustDoNullCheck() const { return must_do_null_check_; }
3597 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
3598
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003599 uint32_t GetDexPc() const { return dex_pc_; }
3600
3601 bool IsClassFinal() const { return class_is_final_; }
3602
3603 DECLARE_INSTRUCTION(CheckCast);
3604
3605 private:
3606 const bool class_is_final_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003607 bool must_do_null_check_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003608 const uint32_t dex_pc_;
3609
3610 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003611};
3612
Calin Juravle27df7582015-04-17 19:12:31 +01003613class HMemoryBarrier : public HTemplateInstruction<0> {
3614 public:
3615 explicit HMemoryBarrier(MemBarrierKind barrier_kind)
3616 : HTemplateInstruction(SideEffects::None()),
3617 barrier_kind_(barrier_kind) {}
3618
3619 MemBarrierKind GetBarrierKind() { return barrier_kind_; }
3620
3621 DECLARE_INSTRUCTION(MemoryBarrier);
3622
3623 private:
3624 const MemBarrierKind barrier_kind_;
3625
3626 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
3627};
3628
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003629class HMonitorOperation : public HTemplateInstruction<1> {
3630 public:
3631 enum OperationKind {
3632 kEnter,
3633 kExit,
3634 };
3635
3636 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
3637 : HTemplateInstruction(SideEffects::None()), kind_(kind), dex_pc_(dex_pc) {
3638 SetRawInputAt(0, object);
3639 }
3640
3641 // Instruction may throw a Java exception, so we need an environment.
3642 bool NeedsEnvironment() const OVERRIDE { return true; }
3643 bool CanThrow() const OVERRIDE { return true; }
3644
3645 uint32_t GetDexPc() const { return dex_pc_; }
3646
3647 bool IsEnter() const { return kind_ == kEnter; }
3648
3649 DECLARE_INSTRUCTION(MonitorOperation);
3650
Calin Juravle52c48962014-12-16 17:02:57 +00003651 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003652 const OperationKind kind_;
3653 const uint32_t dex_pc_;
3654
3655 private:
3656 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
3657};
3658
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003659class MoveOperands : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003660 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01003661 MoveOperands(Location source,
3662 Location destination,
3663 Primitive::Type type,
3664 HInstruction* instruction)
3665 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003666
3667 Location GetSource() const { return source_; }
3668 Location GetDestination() const { return destination_; }
3669
3670 void SetSource(Location value) { source_ = value; }
3671 void SetDestination(Location value) { destination_ = value; }
3672
3673 // The parallel move resolver marks moves as "in-progress" by clearing the
3674 // destination (but not the source).
3675 Location MarkPending() {
3676 DCHECK(!IsPending());
3677 Location dest = destination_;
3678 destination_ = Location::NoLocation();
3679 return dest;
3680 }
3681
3682 void ClearPending(Location dest) {
3683 DCHECK(IsPending());
3684 destination_ = dest;
3685 }
3686
3687 bool IsPending() const {
3688 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
3689 return destination_.IsInvalid() && !source_.IsInvalid();
3690 }
3691
3692 // True if this blocks a move from the given location.
3693 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08003694 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003695 }
3696
3697 // A move is redundant if it's been eliminated, if its source and
3698 // destination are the same, or if its destination is unneeded.
3699 bool IsRedundant() const {
3700 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
3701 }
3702
3703 // We clear both operands to indicate move that's been eliminated.
3704 void Eliminate() {
3705 source_ = destination_ = Location::NoLocation();
3706 }
3707
3708 bool IsEliminated() const {
3709 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
3710 return source_.IsInvalid();
3711 }
3712
Nicolas Geoffray90218252015-04-15 11:56:51 +01003713 bool Is64BitMove() const {
3714 return Primitive::Is64BitType(type_);
3715 }
3716
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003717 HInstruction* GetInstruction() const { return instruction_; }
3718
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003719 private:
3720 Location source_;
3721 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01003722 // The type this move is for.
3723 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003724 // The instruction this move is assocatied with. Null when this move is
3725 // for moving an input in the expected locations of user (including a phi user).
3726 // This is only used in debug mode, to ensure we do not connect interval siblings
3727 // in the same parallel move.
3728 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003729};
3730
3731static constexpr size_t kDefaultNumberOfMoves = 4;
3732
3733class HParallelMove : public HTemplateInstruction<0> {
3734 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003735 explicit HParallelMove(ArenaAllocator* arena)
3736 : HTemplateInstruction(SideEffects::None()), moves_(arena, kDefaultNumberOfMoves) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003737
Nicolas Geoffray90218252015-04-15 11:56:51 +01003738 void AddMove(Location source,
3739 Location destination,
3740 Primitive::Type type,
3741 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003742 DCHECK(source.IsValid());
3743 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003744 if (kIsDebugBuild) {
3745 if (instruction != nullptr) {
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00003746 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003747 if (moves_.Get(i).GetInstruction() == instruction) {
3748 // Special case the situation where the move is for the spill slot
3749 // of the instruction.
3750 if ((GetPrevious() == instruction)
3751 || ((GetPrevious() == nullptr)
3752 && instruction->IsPhi()
3753 && instruction->GetBlock() == GetBlock())) {
3754 DCHECK_NE(destination.GetKind(), moves_.Get(i).GetDestination().GetKind())
3755 << "Doing parallel moves for the same instruction.";
3756 } else {
3757 DCHECK(false) << "Doing parallel moves for the same instruction.";
3758 }
3759 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00003760 }
3761 }
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003762 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Zheng Xuad4450e2015-04-17 18:48:56 +08003763 DCHECK(!destination.OverlapsWith(moves_.Get(i).GetDestination()))
3764 << "Overlapped destination for two moves in a parallel move.";
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003765 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003766 }
Nicolas Geoffray90218252015-04-15 11:56:51 +01003767 moves_.Add(MoveOperands(source, destination, type, instruction));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003768 }
3769
3770 MoveOperands* MoveOperandsAt(size_t index) const {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003771 return moves_.GetRawStorage() + index;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003772 }
3773
3774 size_t NumMoves() const { return moves_.Size(); }
3775
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003776 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003777
3778 private:
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003779 GrowableArray<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003780
3781 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
3782};
3783
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003784class HGraphVisitor : public ValueObject {
3785 public:
Dave Allison20dfc792014-06-16 20:44:29 -07003786 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
3787 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003788
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003789 virtual void VisitInstruction(HInstruction* instruction) { UNUSED(instruction); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003790 virtual void VisitBasicBlock(HBasicBlock* block);
3791
Roland Levillain633021e2014-10-01 14:12:25 +01003792 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003793 void VisitInsertionOrder();
3794
Roland Levillain633021e2014-10-01 14:12:25 +01003795 // Visit the graph following dominator tree reverse post-order.
3796 void VisitReversePostOrder();
3797
Nicolas Geoffray787c3072014-03-17 10:20:19 +00003798 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00003799
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003800 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003801#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003802 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
3803
3804 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
3805
3806#undef DECLARE_VISIT_INSTRUCTION
3807
3808 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07003809 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003810
3811 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
3812};
3813
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003814class HGraphDelegateVisitor : public HGraphVisitor {
3815 public:
3816 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
3817 virtual ~HGraphDelegateVisitor() {}
3818
3819 // Visit functions that delegate to to super class.
3820#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003821 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003822
3823 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
3824
3825#undef DECLARE_VISIT_INSTRUCTION
3826
3827 private:
3828 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
3829};
3830
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003831class HInsertionOrderIterator : public ValueObject {
3832 public:
3833 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
3834
3835 bool Done() const { return index_ == graph_.GetBlocks().Size(); }
3836 HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); }
3837 void Advance() { ++index_; }
3838
3839 private:
3840 const HGraph& graph_;
3841 size_t index_;
3842
3843 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
3844};
3845
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003846class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003847 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00003848 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
3849 // Check that reverse post order of the graph has been built.
3850 DCHECK(!graph.GetReversePostOrder().IsEmpty());
3851 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003852
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003853 bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); }
3854 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003855 void Advance() { ++index_; }
3856
3857 private:
3858 const HGraph& graph_;
3859 size_t index_;
3860
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003861 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003862};
3863
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003864class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003865 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003866 explicit HPostOrderIterator(const HGraph& graph)
David Brazdil10f56cb2015-03-24 18:49:14 +00003867 : graph_(graph), index_(graph_.GetReversePostOrder().Size()) {
3868 // Check that reverse post order of the graph has been built.
3869 DCHECK(!graph.GetReversePostOrder().IsEmpty());
3870 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003871
3872 bool Done() const { return index_ == 0; }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003873 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003874 void Advance() { --index_; }
3875
3876 private:
3877 const HGraph& graph_;
3878 size_t index_;
3879
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003880 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003881};
3882
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01003883class HLinearPostOrderIterator : public ValueObject {
3884 public:
3885 explicit HLinearPostOrderIterator(const HGraph& graph)
3886 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().Size()) {}
3887
3888 bool Done() const { return index_ == 0; }
3889
3890 HBasicBlock* Current() const { return order_.Get(index_ -1); }
3891
3892 void Advance() {
3893 --index_;
3894 DCHECK_GE(index_, 0U);
3895 }
3896
3897 private:
3898 const GrowableArray<HBasicBlock*>& order_;
3899 size_t index_;
3900
3901 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
3902};
3903
3904class HLinearOrderIterator : public ValueObject {
3905 public:
3906 explicit HLinearOrderIterator(const HGraph& graph)
3907 : order_(graph.GetLinearOrder()), index_(0) {}
3908
3909 bool Done() const { return index_ == order_.Size(); }
3910 HBasicBlock* Current() const { return order_.Get(index_); }
3911 void Advance() { ++index_; }
3912
3913 private:
3914 const GrowableArray<HBasicBlock*>& order_;
3915 size_t index_;
3916
3917 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
3918};
3919
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003920// Iterator over the blocks that art part of the loop. Includes blocks part
3921// of an inner loop. The order in which the blocks are iterated is on their
3922// block id.
3923class HBlocksInLoopIterator : public ValueObject {
3924 public:
3925 explicit HBlocksInLoopIterator(const HLoopInformation& info)
3926 : blocks_in_loop_(info.GetBlocks()),
3927 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
3928 index_(0) {
3929 if (!blocks_in_loop_.IsBitSet(index_)) {
3930 Advance();
3931 }
3932 }
3933
3934 bool Done() const { return index_ == blocks_.Size(); }
3935 HBasicBlock* Current() const { return blocks_.Get(index_); }
3936 void Advance() {
3937 ++index_;
3938 for (size_t e = blocks_.Size(); index_ < e; ++index_) {
3939 if (blocks_in_loop_.IsBitSet(index_)) {
3940 break;
3941 }
3942 }
3943 }
3944
3945 private:
3946 const BitVector& blocks_in_loop_;
3947 const GrowableArray<HBasicBlock*>& blocks_;
3948 size_t index_;
3949
3950 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
3951};
3952
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003953inline int64_t Int64FromConstant(HConstant* constant) {
3954 DCHECK(constant->IsIntConstant() || constant->IsLongConstant());
3955 return constant->IsIntConstant() ? constant->AsIntConstant()->GetValue()
3956 : constant->AsLongConstant()->GetValue();
3957}
3958
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003959} // namespace art
3960
3961#endif // ART_COMPILER_OPTIMIZING_NODES_H_