blob: 53bff91a473d32ea712da6eaca34c3da1ed5c02b [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;
Nicolas Geoffraydb216f42015-05-05 17:02:20 +010051class SlowPathCode;
David Brazdil8d5b8b22015-03-24 10:51:52 +000052class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000053
54static const int kDefaultNumberOfBlocks = 8;
55static const int kDefaultNumberOfSuccessors = 2;
56static const int kDefaultNumberOfPredecessors = 2;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010057static const int kDefaultNumberOfDominatedBlocks = 1;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000058static const int kDefaultNumberOfBackEdges = 1;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000059
Calin Juravle9aec02f2014-11-18 23:06:35 +000060static constexpr uint32_t kMaxIntShiftValue = 0x1f;
61static constexpr uint64_t kMaxLongShiftValue = 0x3f;
62
Dave Allison20dfc792014-06-16 20:44:29 -070063enum IfCondition {
64 kCondEQ,
65 kCondNE,
66 kCondLT,
67 kCondLE,
68 kCondGT,
69 kCondGE,
70};
71
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010072class HInstructionList {
73 public:
74 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
75
76 void AddInstruction(HInstruction* instruction);
77 void RemoveInstruction(HInstruction* instruction);
78
David Brazdilc3d743f2015-04-22 13:40:50 +010079 // Insert `instruction` before/after an existing instruction `cursor`.
80 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
81 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
82
Roland Levillain6b469232014-09-25 10:10:38 +010083 // Return true if this list contains `instruction`.
84 bool Contains(HInstruction* instruction) const;
85
Roland Levillainccc07a92014-09-16 14:48:16 +010086 // Return true if `instruction1` is found before `instruction2` in
87 // this instruction list and false otherwise. Abort if none
88 // of these instructions is found.
89 bool FoundBefore(const HInstruction* instruction1,
90 const HInstruction* instruction2) const;
91
Nicolas Geoffray276d9da2015-02-02 18:24:11 +000092 bool IsEmpty() const { return first_instruction_ == nullptr; }
93 void Clear() { first_instruction_ = last_instruction_ = nullptr; }
94
95 // Update the block of all instructions to be `block`.
96 void SetBlockOfInstructions(HBasicBlock* block) const;
97
98 void AddAfter(HInstruction* cursor, const HInstructionList& instruction_list);
99 void Add(const HInstructionList& instruction_list);
100
David Brazdil2d7352b2015-04-20 14:52:42 +0100101 // Return the number of instructions in the list. This is an expensive operation.
102 size_t CountSize() const;
103
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100104 private:
105 HInstruction* first_instruction_;
106 HInstruction* last_instruction_;
107
108 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000109 friend class HGraph;
110 friend class HInstruction;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100111 friend class HInstructionIterator;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100112 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100113
114 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
115};
116
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000117// Control-flow graph of a method. Contains a list of basic blocks.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700118class HGraph : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000119 public:
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000120 HGraph(ArenaAllocator* arena, bool debuggable = false, int start_instruction_id = 0)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000121 : arena_(arena),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000122 blocks_(arena, kDefaultNumberOfBlocks),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100123 reverse_post_order_(arena, kDefaultNumberOfBlocks),
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100124 linear_order_(arena, kDefaultNumberOfBlocks),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700125 entry_block_(nullptr),
126 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100127 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100128 number_of_vregs_(0),
129 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000130 temporaries_vreg_slots_(0),
Mark Mendell1152c922015-04-24 17:06:35 -0400131 has_bounds_checks_(false),
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000132 debuggable_(debuggable),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000133 current_instruction_id_(start_instruction_id),
134 cached_null_constant_(nullptr),
135 cached_int_constants_(std::less<int32_t>(), arena->Adapter()),
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000136 cached_float_constants_(std::less<int32_t>(), arena->Adapter()),
137 cached_long_constants_(std::less<int64_t>(), arena->Adapter()),
138 cached_double_constants_(std::less<int64_t>(), arena->Adapter()) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000139
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000140 ArenaAllocator* GetArena() const { return arena_; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100141 const GrowableArray<HBasicBlock*>& GetBlocks() const { return blocks_; }
Roland Levillain93445682014-10-06 19:24:02 +0100142 HBasicBlock* GetBlock(size_t id) const { return blocks_.Get(id); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000143
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000144 HBasicBlock* GetEntryBlock() const { return entry_block_; }
145 HBasicBlock* GetExitBlock() const { return exit_block_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000146
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000147 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
148 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000149
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000150 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100151
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000152 // Try building the SSA form of this graph, with dominance computation and loop
153 // recognition. Returns whether it was successful in doing all these steps.
154 bool TryBuildingSsa() {
155 BuildDominatorTree();
Nicolas Geoffrayd3350832015-03-12 11:16:23 +0000156 // The SSA builder requires loops to all be natural. Specifically, the dead phi
157 // elimination phase checks the consistency of the graph when doing a post-order
158 // visit for eliminating dead phis: a dead phi can only have loop header phi
159 // users remaining when being visited.
160 if (!AnalyzeNaturalLoops()) return false;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000161 TransformToSsa();
Nicolas Geoffrayd3350832015-03-12 11:16:23 +0000162 return true;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000163 }
164
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000165 void BuildDominatorTree();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000166 void TransformToSsa();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100167 void SimplifyCFG();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000168
Nicolas Geoffrayf5370122014-12-02 11:51:19 +0000169 // Analyze all natural loops in this graph. Returns false if one
170 // loop is not natural, that is the header does not dominate the
171 // back edge.
172 bool AnalyzeNaturalLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100173
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000174 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
175 void InlineInto(HGraph* outer_graph, HInvoke* invoke);
176
David Brazdil2d7352b2015-04-20 14:52:42 +0100177 // Removes `block` from the graph.
178 void DeleteDeadBlock(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000179
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100180 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
181 void SimplifyLoop(HBasicBlock* header);
182
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000183 int32_t GetNextInstructionId() {
184 DCHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000185 return current_instruction_id_++;
186 }
187
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000188 int32_t GetCurrentInstructionId() const {
189 return current_instruction_id_;
190 }
191
192 void SetCurrentInstructionId(int32_t id) {
193 current_instruction_id_ = id;
194 }
195
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100196 uint16_t GetMaximumNumberOfOutVRegs() const {
197 return maximum_number_of_out_vregs_;
198 }
199
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000200 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
201 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100202 }
203
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000204 void UpdateTemporariesVRegSlots(size_t slots) {
205 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100206 }
207
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000208 size_t GetTemporariesVRegSlots() const {
209 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100210 }
211
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100212 void SetNumberOfVRegs(uint16_t number_of_vregs) {
213 number_of_vregs_ = number_of_vregs;
214 }
215
216 uint16_t GetNumberOfVRegs() const {
217 return number_of_vregs_;
218 }
219
220 void SetNumberOfInVRegs(uint16_t value) {
221 number_of_in_vregs_ = value;
222 }
223
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100224 uint16_t GetNumberOfLocalVRegs() const {
225 return number_of_vregs_ - number_of_in_vregs_;
226 }
227
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100228 const GrowableArray<HBasicBlock*>& GetReversePostOrder() const {
229 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100230 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100231
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100232 const GrowableArray<HBasicBlock*>& GetLinearOrder() const {
233 return linear_order_;
234 }
235
Mark Mendell1152c922015-04-24 17:06:35 -0400236 bool HasBoundsChecks() const {
237 return has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800238 }
239
Mark Mendell1152c922015-04-24 17:06:35 -0400240 void SetHasBoundsChecks(bool value) {
241 has_bounds_checks_ = value;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800242 }
243
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000244 bool IsDebuggable() const { return debuggable_; }
245
David Brazdil8d5b8b22015-03-24 10:51:52 +0000246 // Returns a constant of the given type and value. If it does not exist
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000247 // already, it is created and inserted into the graph. This method is only for
248 // integral types.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000249 HConstant* GetConstant(Primitive::Type type, int64_t value);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000250 HNullConstant* GetNullConstant();
David Brazdil8d5b8b22015-03-24 10:51:52 +0000251 HIntConstant* GetIntConstant(int32_t value) {
252 return CreateConstant(value, &cached_int_constants_);
253 }
254 HLongConstant* GetLongConstant(int64_t value) {
255 return CreateConstant(value, &cached_long_constants_);
256 }
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000257 HFloatConstant* GetFloatConstant(float value) {
258 return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_);
259 }
260 HDoubleConstant* GetDoubleConstant(double value) {
261 return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_);
262 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000263
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000264 HBasicBlock* FindCommonDominator(HBasicBlock* first, HBasicBlock* second) const;
David Brazdil2d7352b2015-04-20 14:52:42 +0100265
266 private:
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000267 void VisitBlockForDominatorTree(HBasicBlock* block,
268 HBasicBlock* predecessor,
269 GrowableArray<size_t>* visits);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100270 void FindBackEdges(ArenaBitVector* visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000271 void VisitBlockForBackEdges(HBasicBlock* block,
272 ArenaBitVector* visited,
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100273 ArenaBitVector* visiting);
Roland Levillainfc600dc2014-12-02 17:16:31 +0000274 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100275 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000276
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000277 template <class InstructionType, typename ValueType>
278 InstructionType* CreateConstant(ValueType value,
279 ArenaSafeMap<ValueType, InstructionType*>* cache) {
280 // Try to find an existing constant of the given value.
281 InstructionType* constant = nullptr;
282 auto cached_constant = cache->find(value);
283 if (cached_constant != cache->end()) {
284 constant = cached_constant->second;
285 }
286
287 // If not found or previously deleted, create and cache a new instruction.
288 if (constant == nullptr || constant->GetBlock() == nullptr) {
289 constant = new (arena_) InstructionType(value);
290 cache->Overwrite(value, constant);
291 InsertConstant(constant);
292 }
293 return constant;
294 }
295
David Brazdil8d5b8b22015-03-24 10:51:52 +0000296 void InsertConstant(HConstant* instruction);
297
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000298 // Cache a float constant into the graph. This method should only be
299 // called by the SsaBuilder when creating "equivalent" instructions.
300 void CacheFloatConstant(HFloatConstant* constant);
301
302 // See CacheFloatConstant comment.
303 void CacheDoubleConstant(HDoubleConstant* constant);
304
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000305 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000306
307 // List of blocks in insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000308 GrowableArray<HBasicBlock*> blocks_;
309
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100310 // List of blocks to perform a reverse post order tree traversal.
311 GrowableArray<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000312
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100313 // List of blocks to perform a linear order tree traversal.
314 GrowableArray<HBasicBlock*> linear_order_;
315
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000316 HBasicBlock* entry_block_;
317 HBasicBlock* exit_block_;
318
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100319 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100320 uint16_t maximum_number_of_out_vregs_;
321
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100322 // The number of virtual registers in this method. Contains the parameters.
323 uint16_t number_of_vregs_;
324
325 // The number of virtual registers used by parameters of this method.
326 uint16_t number_of_in_vregs_;
327
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000328 // Number of vreg size slots that the temporaries use (used in baseline compiler).
329 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100330
Mark Mendell1152c922015-04-24 17:06:35 -0400331 // Has bounds checks. We can totally skip BCE if it's false.
332 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800333
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000334 // Indicates whether the graph should be compiled in a way that
335 // ensures full debuggability. If false, we can apply more
336 // aggressive optimizations that may limit the level of debugging.
337 const bool debuggable_;
338
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000339 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000340 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000341
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000342 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000343 HNullConstant* cached_null_constant_;
344 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000345 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000346 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000347 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000348
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000349 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100350 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000351 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000352 DISALLOW_COPY_AND_ASSIGN(HGraph);
353};
354
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700355class HLoopInformation : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000356 public:
357 HLoopInformation(HBasicBlock* header, HGraph* graph)
358 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100359 suspend_check_(nullptr),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100360 back_edges_(graph->GetArena(), kDefaultNumberOfBackEdges),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100361 // Make bit vector growable, as the number of blocks may change.
362 blocks_(graph->GetArena(), graph->GetBlocks().Size(), true) {}
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100363
364 HBasicBlock* GetHeader() const {
365 return header_;
366 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000367
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000368 void SetHeader(HBasicBlock* block) {
369 header_ = block;
370 }
371
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100372 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
373 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
374 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
375
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000376 void AddBackEdge(HBasicBlock* back_edge) {
377 back_edges_.Add(back_edge);
378 }
379
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100380 void RemoveBackEdge(HBasicBlock* back_edge) {
381 back_edges_.Delete(back_edge);
382 }
383
David Brazdil46e2a392015-03-16 17:31:52 +0000384 bool IsBackEdge(const HBasicBlock& block) const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100385 for (size_t i = 0, e = back_edges_.Size(); i < e; ++i) {
David Brazdil46e2a392015-03-16 17:31:52 +0000386 if (back_edges_.Get(i) == &block) return true;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100387 }
388 return false;
389 }
390
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000391 size_t NumberOfBackEdges() const {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000392 return back_edges_.Size();
393 }
394
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100395 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100396
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100397 const GrowableArray<HBasicBlock*>& GetBackEdges() const {
398 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100399 }
400
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100401 // Returns the lifetime position of the back edge that has the
402 // greatest lifetime position.
403 size_t GetLifetimeEnd() const;
404
405 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
406 for (size_t i = 0, e = back_edges_.Size(); i < e; ++i) {
407 if (back_edges_.Get(i) == existing) {
408 back_edges_.Put(i, new_back_edge);
409 return;
410 }
411 }
412 UNREACHABLE();
Nicolas Geoffray57902602015-04-21 14:28:41 +0100413 }
414
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100415 // Finds blocks that are part of this loop. Returns whether the loop is a natural loop,
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100416 // that is the header dominates the back edge.
417 bool Populate();
418
419 // Returns whether this loop information contains `block`.
420 // Note that this loop information *must* be populated before entering this function.
421 bool Contains(const HBasicBlock& block) const;
422
423 // Returns whether this loop information is an inner loop of `other`.
424 // Note that `other` *must* be populated before entering this function.
425 bool IsIn(const HLoopInformation& other) const;
426
427 const ArenaBitVector& GetBlocks() const { return blocks_; }
428
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000429 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000430 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000431
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000432 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100433 // Internal recursive implementation of `Populate`.
434 void PopulateRecursive(HBasicBlock* block);
435
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000436 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100437 HSuspendCheck* suspend_check_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000438 GrowableArray<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100439 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000440
441 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
442};
443
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100444static constexpr size_t kNoLifetime = -1;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100445static constexpr uint32_t kNoDexPc = -1;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100446
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000447// A block in a method. Contains the list of instructions represented
448// as a double linked list. Each block knows its predecessors and
449// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100450
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700451class HBasicBlock : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000452 public:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100453 explicit HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000454 : graph_(graph),
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000455 predecessors_(graph->GetArena(), kDefaultNumberOfPredecessors),
456 successors_(graph->GetArena(), kDefaultNumberOfSuccessors),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000457 loop_information_(nullptr),
458 dominator_(nullptr),
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100459 dominated_blocks_(graph->GetArena(), kDefaultNumberOfDominatedBlocks),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100460 block_id_(-1),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100461 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100462 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000463 lifetime_end_(kNoLifetime),
464 is_catch_block_(false) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000465
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100466 const GrowableArray<HBasicBlock*>& GetPredecessors() const {
467 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000468 }
469
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100470 const GrowableArray<HBasicBlock*>& GetSuccessors() const {
471 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000472 }
473
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100474 const GrowableArray<HBasicBlock*>& GetDominatedBlocks() const {
475 return dominated_blocks_;
476 }
477
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100478 bool IsEntryBlock() const {
479 return graph_->GetEntryBlock() == this;
480 }
481
482 bool IsExitBlock() const {
483 return graph_->GetExitBlock() == this;
484 }
485
David Brazdil46e2a392015-03-16 17:31:52 +0000486 bool IsSingleGoto() const;
487
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000488 void AddBackEdge(HBasicBlock* back_edge) {
489 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000490 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000491 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100492 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000493 loop_information_->AddBackEdge(back_edge);
494 }
495
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000496 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000497 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000498
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000499 int GetBlockId() const { return block_id_; }
500 void SetBlockId(int id) { block_id_ = id; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000501
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000502 HBasicBlock* GetDominator() const { return dominator_; }
503 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100504 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.Add(block); }
David Brazdil2d7352b2015-04-20 14:52:42 +0100505 void RemoveDominatedBlock(HBasicBlock* block) { dominated_blocks_.Delete(block); }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000506 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
507 for (size_t i = 0, e = dominated_blocks_.Size(); i < e; ++i) {
508 if (dominated_blocks_.Get(i) == existing) {
509 dominated_blocks_.Put(i, new_block);
510 return;
511 }
512 }
513 LOG(FATAL) << "Unreachable";
514 UNREACHABLE();
515 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000516
517 int NumberOfBackEdges() const {
518 return loop_information_ == nullptr
519 ? 0
520 : loop_information_->NumberOfBackEdges();
521 }
522
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100523 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
524 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100525 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100526 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +0100527 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
528 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000529
530 void AddSuccessor(HBasicBlock* block) {
531 successors_.Add(block);
532 block->predecessors_.Add(this);
533 }
534
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100535 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
536 size_t successor_index = GetSuccessorIndexOf(existing);
537 DCHECK_NE(successor_index, static_cast<size_t>(-1));
538 existing->RemovePredecessor(this);
539 new_block->predecessors_.Add(this);
540 successors_.Put(successor_index, new_block);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000541 }
542
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000543 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
544 size_t predecessor_index = GetPredecessorIndexOf(existing);
545 DCHECK_NE(predecessor_index, static_cast<size_t>(-1));
546 existing->RemoveSuccessor(this);
547 new_block->successors_.Add(this);
548 predecessors_.Put(predecessor_index, new_block);
549 }
550
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100551 void RemovePredecessor(HBasicBlock* block) {
552 predecessors_.Delete(block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100553 }
554
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000555 void RemoveSuccessor(HBasicBlock* block) {
556 successors_.Delete(block);
557 }
558
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100559 void ClearAllPredecessors() {
560 predecessors_.Reset();
561 }
562
563 void AddPredecessor(HBasicBlock* block) {
564 predecessors_.Add(block);
565 block->successors_.Add(this);
566 }
567
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100568 void SwapPredecessors() {
Nicolas Geoffrayc83d4412014-09-18 16:46:20 +0100569 DCHECK_EQ(predecessors_.Size(), 2u);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100570 HBasicBlock* temp = predecessors_.Get(0);
571 predecessors_.Put(0, predecessors_.Get(1));
572 predecessors_.Put(1, temp);
573 }
574
David Brazdil769c9e52015-04-27 13:54:09 +0100575 void SwapSuccessors() {
576 DCHECK_EQ(successors_.Size(), 2u);
577 HBasicBlock* temp = successors_.Get(0);
578 successors_.Put(0, successors_.Get(1));
579 successors_.Put(1, temp);
580 }
581
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100582 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) {
583 for (size_t i = 0, e = predecessors_.Size(); i < e; ++i) {
584 if (predecessors_.Get(i) == predecessor) {
585 return i;
586 }
587 }
588 return -1;
589 }
590
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100591 size_t GetSuccessorIndexOf(HBasicBlock* successor) {
592 for (size_t i = 0, e = successors_.Size(); i < e; ++i) {
593 if (successors_.Get(i) == successor) {
594 return i;
595 }
596 }
597 return -1;
598 }
599
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000600 // Split the block into two blocks just after `cursor`. Returns the newly
601 // created block. Note that this method just updates raw block information,
602 // like predecessors, successors, dominators, and instruction list. It does not
603 // update the graph, reverse post order, loop information, nor make sure the
604 // blocks are consistent (for example ending with a control flow instruction).
605 HBasicBlock* SplitAfter(HInstruction* cursor);
606
607 // Merge `other` at the end of `this`. Successors and dominated blocks of
608 // `other` are changed to be successors and dominated blocks of `this`. Note
609 // that this method does not update the graph, reverse post order, loop
610 // information, nor make sure the blocks are consistent (for example ending
611 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +0100612 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000613
614 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
615 // of `this` are moved to `other`.
616 // Note that this method does not update the graph, reverse post order, loop
617 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +0000618 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000619 void ReplaceWith(HBasicBlock* other);
620
David Brazdil2d7352b2015-04-20 14:52:42 +0100621 // Merge `other` at the end of `this`. This method updates loops, reverse post
622 // order, links to predecessors, successors, dominators and deletes the block
623 // from the graph. The two blocks must be successive, i.e. `this` the only
624 // predecessor of `other` and vice versa.
625 void MergeWith(HBasicBlock* other);
626
627 // Disconnects `this` from all its predecessors, successors and dominator,
628 // removes it from all loops it is included in and eventually from the graph.
629 // The block must not dominate any other block. Predecessors and successors
630 // are safely updated.
631 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +0000632
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000633 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +0100634 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100635 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +0100636 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +0100637 // Replace instruction `initial` with `replacement` within this block.
638 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
639 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100640 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100641 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +0000642 // RemoveInstruction and RemovePhi delete a given instruction from the respective
643 // instruction list. With 'ensure_safety' set to true, it verifies that the
644 // instruction is not in use and removes it from the use lists of its inputs.
645 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
646 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +0100647 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100648
649 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +0100650 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100651 }
652
Roland Levillain6b879dd2014-09-22 17:13:44 +0100653 bool IsLoopPreHeaderFirstPredecessor() const {
654 DCHECK(IsLoopHeader());
655 DCHECK(!GetPredecessors().IsEmpty());
656 return GetPredecessors().Get(0) == GetLoopInformation()->GetPreHeader();
657 }
658
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100659 HLoopInformation* GetLoopInformation() const {
660 return loop_information_;
661 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000662
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000663 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100664 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000665 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100666 void SetInLoop(HLoopInformation* info) {
667 if (IsLoopHeader()) {
668 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +0100669 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100670 loop_information_ = info;
671 } else if (loop_information_->Contains(*info->GetHeader())) {
672 // Block is currently part of an outer loop. Make it part of this inner loop.
673 // Note that a non loop header having a loop information means this loop information
674 // has already been populated
675 loop_information_ = info;
676 } else {
677 // Block is part of an inner loop. Do not update the loop information.
678 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
679 // at this point, because this method is being called while populating `info`.
680 }
681 }
682
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000683 // Raw update of the loop information.
684 void SetLoopInformation(HLoopInformation* info) {
685 loop_information_ = info;
686 }
687
David Brazdil69a28042015-04-29 17:16:07 +0100688 // Checks if the loop information points to a valid loop. If the loop has been
689 // dismantled (does not have a back edge any more), loop information is
690 // removed or replaced with the information of the first valid outer loop.
691 void UpdateLoopInformation();
692
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100693 bool IsInLoop() const { return loop_information_ != nullptr; }
694
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100695 // Returns wheter this block dominates the blocked passed as parameter.
696 bool Dominates(HBasicBlock* block) const;
697
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100698 size_t GetLifetimeStart() const { return lifetime_start_; }
699 size_t GetLifetimeEnd() const { return lifetime_end_; }
700
701 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
702 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
703
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100704 uint32_t GetDexPc() const { return dex_pc_; }
705
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000706 bool IsCatchBlock() const { return is_catch_block_; }
707 void SetIsCatchBlock() { is_catch_block_ = true; }
708
David Brazdil8d5b8b22015-03-24 10:51:52 +0000709 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +0000710 bool EndsWithIf() const;
711 bool HasSinglePhi() const;
712
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000713 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000714 HGraph* graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000715 GrowableArray<HBasicBlock*> predecessors_;
716 GrowableArray<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100717 HInstructionList instructions_;
718 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000719 HLoopInformation* loop_information_;
720 HBasicBlock* dominator_;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100721 GrowableArray<HBasicBlock*> dominated_blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000722 int block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100723 // The dex program counter of the first instruction of this block.
724 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100725 size_t lifetime_start_;
726 size_t lifetime_end_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000727 bool is_catch_block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000728
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000729 friend class HGraph;
730 friend class HInstruction;
731
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000732 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
733};
734
David Brazdilb2bd1c52015-03-25 11:17:37 +0000735// Iterates over the LoopInformation of all loops which contain 'block'
736// from the innermost to the outermost.
737class HLoopInformationOutwardIterator : public ValueObject {
738 public:
739 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
740 : current_(block.GetLoopInformation()) {}
741
742 bool Done() const { return current_ == nullptr; }
743
744 void Advance() {
745 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +0100746 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +0000747 }
748
749 HLoopInformation* Current() const {
750 DCHECK(!Done());
751 return current_;
752 }
753
754 private:
755 HLoopInformation* current_;
756
757 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
758};
759
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100760#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
761 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000762 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000763 M(ArrayGet, Instruction) \
764 M(ArrayLength, Instruction) \
765 M(ArraySet, Instruction) \
David Brazdil66d126e2015-04-03 16:02:44 +0100766 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000767 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +0000768 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000769 M(CheckCast, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100770 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000771 M(Compare, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100772 M(Condition, BinaryOperation) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700773 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000774 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +0000775 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000776 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100777 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000778 M(Exit, Instruction) \
779 M(FloatConstant, Constant) \
780 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100781 M(GreaterThan, Condition) \
782 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100783 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000784 M(InstanceFieldGet, Instruction) \
785 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000786 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100787 M(IntConstant, Constant) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +0000788 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000789 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100790 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000791 M(LessThan, Condition) \
792 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000793 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000794 M(LoadException, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100795 M(LoadLocal, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000796 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100797 M(Local, Instruction) \
798 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +0100799 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +0000800 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000801 M(Mul, BinaryOperation) \
802 M(Neg, UnaryOperation) \
803 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100804 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +0100805 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000806 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000807 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000808 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000809 M(Or, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100810 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000811 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100812 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000813 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100814 M(Return, Instruction) \
815 M(ReturnVoid, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000816 M(Shl, BinaryOperation) \
817 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100818 M(StaticFieldGet, Instruction) \
819 M(StaticFieldSet, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100820 M(StoreLocal, Instruction) \
821 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100822 M(SuspendCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000823 M(Temporary, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000824 M(Throw, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +0000825 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000826 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000827 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000828
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100829#define FOR_EACH_INSTRUCTION(M) \
830 FOR_EACH_CONCRETE_INSTRUCTION(M) \
831 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +0100832 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +0100833 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100834 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -0700835
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100836#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000837FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
838#undef FORWARD_DECLARATION
839
Roland Levillainccc07a92014-09-16 14:48:16 +0100840#define DECLARE_INSTRUCTION(type) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000841 InstructionKind GetKind() const OVERRIDE { return k##type; } \
842 const char* DebugName() const OVERRIDE { return #type; } \
843 const H##type* As##type() const OVERRIDE { return this; } \
844 H##type* As##type() OVERRIDE { return this; } \
845 bool InstructionTypeEquals(HInstruction* other) const OVERRIDE { \
Roland Levillainccc07a92014-09-16 14:48:16 +0100846 return other->Is##type(); \
847 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000848 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000849
David Brazdiled596192015-01-23 10:39:45 +0000850template <typename T> class HUseList;
851
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100852template <typename T>
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700853class HUseListNode : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000854 public:
David Brazdiled596192015-01-23 10:39:45 +0000855 HUseListNode* GetPrevious() const { return prev_; }
856 HUseListNode* GetNext() const { return next_; }
857 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100858 size_t GetIndex() const { return index_; }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +0100859 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100860
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000861 private:
David Brazdiled596192015-01-23 10:39:45 +0000862 HUseListNode(T user, size_t index)
863 : user_(user), index_(index), prev_(nullptr), next_(nullptr) {}
864
865 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +0100866 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +0000867 HUseListNode<T>* prev_;
868 HUseListNode<T>* next_;
869
870 friend class HUseList<T>;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000871
872 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
873};
874
David Brazdiled596192015-01-23 10:39:45 +0000875template <typename T>
876class HUseList : public ValueObject {
877 public:
878 HUseList() : first_(nullptr) {}
879
880 void Clear() {
881 first_ = nullptr;
882 }
883
884 // Adds a new entry at the beginning of the use list and returns
885 // the newly created node.
886 HUseListNode<T>* AddUse(T user, size_t index, ArenaAllocator* arena) {
David Brazdilea55b932015-01-27 17:12:29 +0000887 HUseListNode<T>* new_node = new (arena) HUseListNode<T>(user, index);
David Brazdiled596192015-01-23 10:39:45 +0000888 if (IsEmpty()) {
889 first_ = new_node;
890 } else {
891 first_->prev_ = new_node;
892 new_node->next_ = first_;
893 first_ = new_node;
894 }
895 return new_node;
896 }
897
898 HUseListNode<T>* GetFirst() const {
899 return first_;
900 }
901
902 void Remove(HUseListNode<T>* node) {
David Brazdil1abb4192015-02-17 18:33:36 +0000903 DCHECK(node != nullptr);
904 DCHECK(Contains(node));
905
David Brazdiled596192015-01-23 10:39:45 +0000906 if (node->prev_ != nullptr) {
907 node->prev_->next_ = node->next_;
908 }
909 if (node->next_ != nullptr) {
910 node->next_->prev_ = node->prev_;
911 }
912 if (node == first_) {
913 first_ = node->next_;
914 }
915 }
916
David Brazdil1abb4192015-02-17 18:33:36 +0000917 bool Contains(const HUseListNode<T>* node) const {
918 if (node == nullptr) {
919 return false;
920 }
921 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
922 if (current == node) {
923 return true;
924 }
925 }
926 return false;
927 }
928
David Brazdiled596192015-01-23 10:39:45 +0000929 bool IsEmpty() const {
930 return first_ == nullptr;
931 }
932
933 bool HasOnlyOneUse() const {
934 return first_ != nullptr && first_->next_ == nullptr;
935 }
936
937 private:
938 HUseListNode<T>* first_;
939};
940
941template<typename T>
942class HUseIterator : public ValueObject {
943 public:
944 explicit HUseIterator(const HUseList<T>& uses) : current_(uses.GetFirst()) {}
945
946 bool Done() const { return current_ == nullptr; }
947
948 void Advance() {
949 DCHECK(!Done());
950 current_ = current_->GetNext();
951 }
952
953 HUseListNode<T>* Current() const {
954 DCHECK(!Done());
955 return current_;
956 }
957
958 private:
959 HUseListNode<T>* current_;
960
961 friend class HValue;
962};
963
David Brazdil1abb4192015-02-17 18:33:36 +0000964// This class is used by HEnvironment and HInstruction classes to record the
965// instructions they use and pointers to the corresponding HUseListNodes kept
966// by the used instructions.
967template <typename T>
968class HUserRecord : public ValueObject {
969 public:
970 HUserRecord() : instruction_(nullptr), use_node_(nullptr) {}
971 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), use_node_(nullptr) {}
972
973 HUserRecord(const HUserRecord<T>& old_record, HUseListNode<T>* use_node)
974 : instruction_(old_record.instruction_), use_node_(use_node) {
975 DCHECK(instruction_ != nullptr);
976 DCHECK(use_node_ != nullptr);
977 DCHECK(old_record.use_node_ == nullptr);
978 }
979
980 HInstruction* GetInstruction() const { return instruction_; }
981 HUseListNode<T>* GetUseNode() const { return use_node_; }
982
983 private:
984 // Instruction used by the user.
985 HInstruction* instruction_;
986
987 // Corresponding entry in the use list kept by 'instruction_'.
988 HUseListNode<T>* use_node_;
989};
990
Calin Juravle27df7582015-04-17 19:12:31 +0100991// TODO: Add better documentation to this class and maybe refactor with more suggestive names.
992// - Has(All)SideEffects suggests that all the side effects are present but only ChangesSomething
993// flag is consider.
994// - DependsOn suggests that there is a real dependency between side effects but it only
995// checks DependendsOnSomething flag.
996//
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100997// Represents the side effects an instruction may have.
998class SideEffects : public ValueObject {
999 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001000 SideEffects() : flags_(0) {}
1001
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001002 static SideEffects None() {
1003 return SideEffects(0);
1004 }
1005
1006 static SideEffects All() {
1007 return SideEffects(ChangesSomething().flags_ | DependsOnSomething().flags_);
1008 }
1009
1010 static SideEffects ChangesSomething() {
1011 return SideEffects((1 << kFlagChangesCount) - 1);
1012 }
1013
1014 static SideEffects DependsOnSomething() {
1015 int count = kFlagDependsOnCount - kFlagChangesCount;
1016 return SideEffects(((1 << count) - 1) << kFlagChangesCount);
1017 }
1018
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001019 SideEffects Union(SideEffects other) const {
1020 return SideEffects(flags_ | other.flags_);
1021 }
1022
Roland Levillain72bceff2014-09-15 18:29:00 +01001023 bool HasSideEffects() const {
1024 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
1025 return (flags_ & all_bits_set) != 0;
1026 }
1027
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001028 bool HasAllSideEffects() const {
1029 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
1030 return all_bits_set == (flags_ & all_bits_set);
1031 }
1032
1033 bool DependsOn(SideEffects other) const {
1034 size_t depends_flags = other.ComputeDependsFlags();
1035 return (flags_ & depends_flags) != 0;
1036 }
1037
1038 bool HasDependencies() const {
1039 int count = kFlagDependsOnCount - kFlagChangesCount;
1040 size_t all_bits_set = (1 << count) - 1;
1041 return ((flags_ >> kFlagChangesCount) & all_bits_set) != 0;
1042 }
1043
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001044 private:
1045 static constexpr int kFlagChangesSomething = 0;
1046 static constexpr int kFlagChangesCount = kFlagChangesSomething + 1;
1047
1048 static constexpr int kFlagDependsOnSomething = kFlagChangesCount;
1049 static constexpr int kFlagDependsOnCount = kFlagDependsOnSomething + 1;
1050
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001051 explicit SideEffects(size_t flags) : flags_(flags) {}
1052
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001053 size_t ComputeDependsFlags() const {
1054 return flags_ << kFlagChangesCount;
1055 }
1056
1057 size_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001058};
1059
David Brazdiled596192015-01-23 10:39:45 +00001060// A HEnvironment object contains the values of virtual registers at a given location.
1061class HEnvironment : public ArenaObject<kArenaAllocMisc> {
1062 public:
1063 HEnvironment(ArenaAllocator* arena, size_t number_of_vregs)
1064 : vregs_(arena, number_of_vregs) {
1065 vregs_.SetSize(number_of_vregs);
1066 for (size_t i = 0; i < number_of_vregs; i++) {
David Brazdil1abb4192015-02-17 18:33:36 +00001067 vregs_.Put(i, HUserRecord<HEnvironment*>());
David Brazdiled596192015-01-23 10:39:45 +00001068 }
1069 }
1070
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001071 void CopyFrom(const GrowableArray<HInstruction*>& locals);
1072 void CopyFrom(HEnvironment* environment);
1073
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001074 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1075 // input to the loop phi instead. This is for inserting instructions that
1076 // require an environment (like HDeoptimization) in the loop pre-header.
1077 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001078
1079 void SetRawEnvAt(size_t index, HInstruction* instruction) {
David Brazdil1abb4192015-02-17 18:33:36 +00001080 vregs_.Put(index, HUserRecord<HEnvironment*>(instruction));
David Brazdiled596192015-01-23 10:39:45 +00001081 }
1082
1083 HInstruction* GetInstructionAt(size_t index) const {
David Brazdil1abb4192015-02-17 18:33:36 +00001084 return vregs_.Get(index).GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001085 }
1086
David Brazdil1abb4192015-02-17 18:33:36 +00001087 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001088
1089 size_t Size() const { return vregs_.Size(); }
1090
1091 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001092 // Record instructions' use entries of this environment for constant-time removal.
1093 // It should only be called by HInstruction when a new environment use is added.
1094 void RecordEnvUse(HUseListNode<HEnvironment*>* env_use) {
1095 DCHECK(env_use->GetUser() == this);
1096 size_t index = env_use->GetIndex();
1097 vregs_.Put(index, HUserRecord<HEnvironment*>(vregs_.Get(index), env_use));
1098 }
David Brazdiled596192015-01-23 10:39:45 +00001099
David Brazdil1abb4192015-02-17 18:33:36 +00001100 GrowableArray<HUserRecord<HEnvironment*> > vregs_;
David Brazdiled596192015-01-23 10:39:45 +00001101
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001102 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001103
1104 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1105};
1106
Calin Juravleacf735c2015-02-12 15:25:22 +00001107class ReferenceTypeInfo : ValueObject {
1108 public:
Calin Juravleb1498f62015-02-16 13:13:29 +00001109 typedef Handle<mirror::Class> TypeHandle;
1110
1111 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact)
1112 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1113 if (type_handle->IsObjectClass()) {
1114 // Override the type handle to be consistent with the case when we get to
1115 // Top but don't have the Object class available. It avoids having to guess
1116 // what value the type_handle has when it's Top.
1117 return ReferenceTypeInfo(TypeHandle(), is_exact, true);
1118 } else {
1119 return ReferenceTypeInfo(type_handle, is_exact, false);
1120 }
1121 }
1122
1123 static ReferenceTypeInfo CreateTop(bool is_exact) {
1124 return ReferenceTypeInfo(TypeHandle(), is_exact, true);
Calin Juravleacf735c2015-02-12 15:25:22 +00001125 }
1126
1127 bool IsExact() const { return is_exact_; }
1128 bool IsTop() const { return is_top_; }
1129
1130 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
1131
Calin Juravleb1498f62015-02-16 13:13:29 +00001132 bool IsSupertypeOf(ReferenceTypeInfo rti) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Calin Juravleacf735c2015-02-12 15:25:22 +00001133 if (IsTop()) {
1134 // Top (equivalent for java.lang.Object) is supertype of anything.
1135 return true;
1136 }
1137 if (rti.IsTop()) {
1138 // If we get here `this` is not Top() so it can't be a supertype.
1139 return false;
1140 }
1141 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
1142 }
1143
1144 // Returns true if the type information provide the same amount of details.
1145 // Note that it does not mean that the instructions have the same actual type
1146 // (e.g. tops are equal but they can be the result of a merge).
1147 bool IsEqual(ReferenceTypeInfo rti) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1148 if (IsExact() != rti.IsExact()) {
1149 return false;
1150 }
1151 if (IsTop() && rti.IsTop()) {
1152 // `Top` means java.lang.Object, so the types are equivalent.
1153 return true;
1154 }
1155 if (IsTop() || rti.IsTop()) {
1156 // If only one is top or object than they are not equivalent.
1157 // NB: We need this extra check because the type_handle of `Top` is invalid
1158 // and we cannot inspect its reference.
1159 return false;
1160 }
1161
1162 // Finally check the types.
1163 return GetTypeHandle().Get() == rti.GetTypeHandle().Get();
1164 }
1165
1166 private:
Calin Juravleb1498f62015-02-16 13:13:29 +00001167 ReferenceTypeInfo() : ReferenceTypeInfo(TypeHandle(), false, true) {}
1168 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact, bool is_top)
1169 : type_handle_(type_handle), is_exact_(is_exact), is_top_(is_top) {}
1170
Calin Juravleacf735c2015-02-12 15:25:22 +00001171 // The class of the object.
Calin Juravleb1498f62015-02-16 13:13:29 +00001172 TypeHandle type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001173 // Whether or not the type is exact or a superclass of the actual type.
Calin Juravleb1498f62015-02-16 13:13:29 +00001174 // Whether or not we have any information about this type.
Calin Juravleacf735c2015-02-12 15:25:22 +00001175 bool is_exact_;
1176 // A true value here means that the object type should be java.lang.Object.
1177 // We don't have access to the corresponding mirror object every time so this
1178 // flag acts as a substitute. When true, the TypeHandle refers to a null
1179 // pointer and should not be used.
1180 bool is_top_;
1181};
1182
1183std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
1184
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001185class HInstruction : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001186 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001187 explicit HInstruction(SideEffects side_effects)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001188 : previous_(nullptr),
1189 next_(nullptr),
1190 block_(nullptr),
1191 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001192 ssa_index_(-1),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001193 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001194 locations_(nullptr),
1195 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001196 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001197 side_effects_(side_effects),
1198 reference_type_info_(ReferenceTypeInfo::CreateTop(/* is_exact */ false)) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001199
Dave Allison20dfc792014-06-16 20:44:29 -07001200 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001201
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001202#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001203 enum InstructionKind {
1204 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1205 };
1206#undef DECLARE_KIND
1207
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001208 HInstruction* GetNext() const { return next_; }
1209 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001210
Calin Juravle77520bc2015-01-12 18:45:46 +00001211 HInstruction* GetNextDisregardingMoves() const;
1212 HInstruction* GetPreviousDisregardingMoves() const;
1213
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001214 HBasicBlock* GetBlock() const { return block_; }
1215 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001216 bool IsInBlock() const { return block_ != nullptr; }
1217 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +01001218 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001219
Roland Levillain6b879dd2014-09-22 17:13:44 +01001220 virtual size_t InputCount() const = 0;
David Brazdil1abb4192015-02-17 18:33:36 +00001221 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001222
1223 virtual void Accept(HGraphVisitor* visitor) = 0;
1224 virtual const char* DebugName() const = 0;
1225
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001226 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
David Brazdil1abb4192015-02-17 18:33:36 +00001227 void SetRawInputAt(size_t index, HInstruction* input) {
1228 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1229 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001230
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001231 virtual bool NeedsEnvironment() const { return false; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001232 virtual bool IsControlFlow() const { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01001233 virtual bool CanThrow() const { return false; }
Roland Levillain72bceff2014-09-15 18:29:00 +01001234 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001235
Calin Juravle10e244f2015-01-26 18:54:32 +00001236 // Does not apply for all instructions, but having this at top level greatly
1237 // simplifies the null check elimination.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001238 virtual bool CanBeNull() const {
1239 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1240 return true;
1241 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001242
Calin Juravle641547a2015-04-21 22:08:51 +01001243 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj) const {
1244 UNUSED(obj);
1245 return false;
1246 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001247
Calin Juravleacf735c2015-02-12 15:25:22 +00001248 void SetReferenceTypeInfo(ReferenceTypeInfo reference_type_info) {
Calin Juravle61d544b2015-02-23 16:46:57 +00001249 DCHECK_EQ(GetType(), Primitive::kPrimNot);
Calin Juravleacf735c2015-02-12 15:25:22 +00001250 reference_type_info_ = reference_type_info;
1251 }
1252
Calin Juravle61d544b2015-02-23 16:46:57 +00001253 ReferenceTypeInfo GetReferenceTypeInfo() const {
1254 DCHECK_EQ(GetType(), Primitive::kPrimNot);
1255 return reference_type_info_;
1256 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001257
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001258 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001259 DCHECK(user != nullptr);
1260 HUseListNode<HInstruction*>* use =
1261 uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1262 user->SetRawInputRecordAt(index, HUserRecord<HInstruction*>(user->InputRecordAt(index), use));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001263 }
1264
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001265 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001266 DCHECK(user != nullptr);
David Brazdiled596192015-01-23 10:39:45 +00001267 HUseListNode<HEnvironment*>* env_use =
1268 env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1269 user->RecordEnvUse(env_use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001270 }
1271
David Brazdil1abb4192015-02-17 18:33:36 +00001272 void RemoveAsUserOfInput(size_t input) {
1273 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
1274 input_use.GetInstruction()->uses_.Remove(input_use.GetUseNode());
1275 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001276
David Brazdil1abb4192015-02-17 18:33:36 +00001277 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1278 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001279
David Brazdiled596192015-01-23 10:39:45 +00001280 bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); }
1281 bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); }
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001282 bool HasNonEnvironmentUses() const { return !uses_.IsEmpty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001283 bool HasOnlyOneNonEnvironmentUse() const {
1284 return !HasEnvironmentUses() && GetUses().HasOnlyOneUse();
1285 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001286
Roland Levillain6c82d402014-10-13 16:10:27 +01001287 // Does this instruction strictly dominate `other_instruction`?
1288 // Returns false if this instruction and `other_instruction` are the same.
1289 // Aborts if this instruction and `other_instruction` are both phis.
1290 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001291
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001292 int GetId() const { return id_; }
1293 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001294
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001295 int GetSsaIndex() const { return ssa_index_; }
1296 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1297 bool HasSsaIndex() const { return ssa_index_ != -1; }
1298
1299 bool HasEnvironment() const { return environment_ != nullptr; }
1300 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001301 // Set the `environment_` field. Raw because this method does not
1302 // update the uses lists.
1303 void SetRawEnvironment(HEnvironment* environment) { environment_ = environment; }
1304
1305 // Set the environment of this instruction, copying it from `environment`. While
1306 // copying, the uses lists are being updated.
1307 void CopyEnvironmentFrom(HEnvironment* environment) {
1308 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
1309 environment_ = new (allocator) HEnvironment(allocator, environment->Size());
1310 environment_->CopyFrom(environment);
1311 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001312
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001313 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
1314 HBasicBlock* block) {
1315 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
1316 environment_ = new (allocator) HEnvironment(allocator, environment->Size());
1317 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
1318 }
1319
Nicolas Geoffray39468442014-09-02 15:17:15 +01001320 // Returns the number of entries in the environment. Typically, that is the
1321 // number of dex registers in a method. It could be more in case of inlining.
1322 size_t EnvironmentSize() const;
1323
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001324 LocationSummary* GetLocations() const { return locations_; }
1325 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001326
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001327 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001328 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001329
Alexandre Rames188d4312015-04-09 18:30:21 +01001330 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
1331 // uses of this instruction by `other` are *not* updated.
1332 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
1333 ReplaceWith(other);
1334 other->ReplaceInput(this, use_index);
1335 }
1336
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001337 // Move `this` instruction before `cursor`.
1338 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001339
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001340#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01001341 bool Is##type() const { return (As##type() != nullptr); } \
1342 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001343 virtual H##type* As##type() { return nullptr; }
1344
1345 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
1346#undef INSTRUCTION_TYPE_CHECK
1347
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001348 // Returns whether the instruction can be moved within the graph.
1349 virtual bool CanBeMoved() const { return false; }
1350
1351 // Returns whether the two instructions are of the same kind.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001352 virtual bool InstructionTypeEquals(HInstruction* other) const {
1353 UNUSED(other);
1354 return false;
1355 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001356
1357 // Returns whether any data encoded in the two instructions is equal.
1358 // This method does not look at the inputs. Both instructions must be
1359 // of the same type, otherwise the method has undefined behavior.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001360 virtual bool InstructionDataEquals(HInstruction* other) const {
1361 UNUSED(other);
1362 return false;
1363 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001364
1365 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00001366 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001367 // 2) Their inputs are identical.
1368 bool Equals(HInstruction* other) const;
1369
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001370 virtual InstructionKind GetKind() const = 0;
1371
1372 virtual size_t ComputeHashCode() const {
1373 size_t result = GetKind();
1374 for (size_t i = 0, e = InputCount(); i < e; ++i) {
1375 result = (result * 31) + InputAt(i)->GetId();
1376 }
1377 return result;
1378 }
1379
1380 SideEffects GetSideEffects() const { return side_effects_; }
1381
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001382 size_t GetLifetimePosition() const { return lifetime_position_; }
1383 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
1384 LiveInterval* GetLiveInterval() const { return live_interval_; }
1385 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
1386 bool HasLiveInterval() const { return live_interval_ != nullptr; }
1387
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001388 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
1389
1390 // Returns whether the code generation of the instruction will require to have access
1391 // to the current method. Such instructions are:
1392 // (1): Instructions that require an environment, as calling the runtime requires
1393 // to walk the stack and have the current method stored at a specific stack address.
1394 // (2): Object literals like classes and strings, that are loaded from the dex cache
1395 // fields of the current method.
1396 bool NeedsCurrentMethod() const {
1397 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
1398 }
1399
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001400 virtual bool NeedsDexCache() const { return false; }
1401
David Brazdil1abb4192015-02-17 18:33:36 +00001402 protected:
1403 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
1404 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
1405
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001406 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001407 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); }
1408
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001409 HInstruction* previous_;
1410 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001411 HBasicBlock* block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001412
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001413 // An instruction gets an id when it is added to the graph.
1414 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01001415 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001416 int id_;
1417
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001418 // When doing liveness analysis, instructions that have uses get an SSA index.
1419 int ssa_index_;
1420
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001421 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00001422 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001423
1424 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00001425 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001426
Nicolas Geoffray39468442014-09-02 15:17:15 +01001427 // The environment associated with this instruction. Not null if the instruction
1428 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001429 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001430
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001431 // Set by the code generator.
1432 LocationSummary* locations_;
1433
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001434 // Set by the liveness analysis.
1435 LiveInterval* live_interval_;
1436
1437 // Set by the liveness analysis, this is the position in a linear
1438 // order of blocks where this instruction's live interval start.
1439 size_t lifetime_position_;
1440
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001441 const SideEffects side_effects_;
1442
Calin Juravleacf735c2015-02-12 15:25:22 +00001443 // TODO: for primitive types this should be marked as invalid.
1444 ReferenceTypeInfo reference_type_info_;
1445
David Brazdil1abb4192015-02-17 18:33:36 +00001446 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001447 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00001448 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001449 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001450 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001451
1452 DISALLOW_COPY_AND_ASSIGN(HInstruction);
1453};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001454std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001455
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001456class HInputIterator : public ValueObject {
1457 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001458 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001459
1460 bool Done() const { return index_ == instruction_->InputCount(); }
1461 HInstruction* Current() const { return instruction_->InputAt(index_); }
1462 void Advance() { index_++; }
1463
1464 private:
1465 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001466 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001467
1468 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
1469};
1470
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001471class HInstructionIterator : public ValueObject {
1472 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001473 explicit HInstructionIterator(const HInstructionList& instructions)
1474 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001475 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001476 }
1477
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001478 bool Done() const { return instruction_ == nullptr; }
1479 HInstruction* Current() const { return instruction_; }
1480 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001481 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001482 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001483 }
1484
1485 private:
1486 HInstruction* instruction_;
1487 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001488
1489 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001490};
1491
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001492class HBackwardInstructionIterator : public ValueObject {
1493 public:
1494 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
1495 : instruction_(instructions.last_instruction_) {
1496 next_ = Done() ? nullptr : instruction_->GetPrevious();
1497 }
1498
1499 bool Done() const { return instruction_ == nullptr; }
1500 HInstruction* Current() const { return instruction_; }
1501 void Advance() {
1502 instruction_ = next_;
1503 next_ = Done() ? nullptr : instruction_->GetPrevious();
1504 }
1505
1506 private:
1507 HInstruction* instruction_;
1508 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001509
1510 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001511};
1512
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001513// An embedded container with N elements of type T. Used (with partial
1514// specialization for N=0) because embedded arrays cannot have size 0.
1515template<typename T, intptr_t N>
1516class EmbeddedArray {
1517 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001518 EmbeddedArray() : elements_() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001519
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001520 intptr_t GetLength() const { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001521
1522 const T& operator[](intptr_t i) const {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001523 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001524 return elements_[i];
1525 }
1526
1527 T& operator[](intptr_t i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001528 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001529 return elements_[i];
1530 }
1531
1532 const T& At(intptr_t i) const {
1533 return (*this)[i];
1534 }
1535
1536 void SetAt(intptr_t i, const T& val) {
1537 (*this)[i] = val;
1538 }
1539
1540 private:
1541 T elements_[N];
1542};
1543
1544template<typename T>
1545class EmbeddedArray<T, 0> {
1546 public:
1547 intptr_t length() const { return 0; }
1548 const T& operator[](intptr_t i) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001549 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001550 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001551 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001552 }
1553 T& operator[](intptr_t i) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001554 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001555 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001556 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001557 }
1558};
1559
1560template<intptr_t N>
1561class HTemplateInstruction: public HInstruction {
1562 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001563 HTemplateInstruction<N>(SideEffects side_effects)
1564 : HInstruction(side_effects), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07001565 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001566
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001567 size_t InputCount() const OVERRIDE { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001568
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001569 protected:
David Brazdil1abb4192015-02-17 18:33:36 +00001570 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_[i]; }
1571
1572 void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE {
1573 inputs_[i] = input;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001574 }
1575
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001576 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001577 EmbeddedArray<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001578
1579 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001580};
1581
Dave Allison20dfc792014-06-16 20:44:29 -07001582template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001583class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07001584 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001585 HExpression<N>(Primitive::Type type, SideEffects side_effects)
1586 : HTemplateInstruction<N>(side_effects), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07001587 virtual ~HExpression() {}
1588
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001589 Primitive::Type GetType() const OVERRIDE { return type_; }
Dave Allison20dfc792014-06-16 20:44:29 -07001590
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001591 protected:
1592 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07001593};
1594
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001595// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
1596// instruction that branches to the exit block.
1597class HReturnVoid : public HTemplateInstruction<0> {
1598 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001599 HReturnVoid() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001600
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001601 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001602
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001603 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001604
1605 private:
1606 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
1607};
1608
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001609// Represents dex's RETURN opcodes. A HReturn is a control flow
1610// instruction that branches to the exit block.
1611class HReturn : public HTemplateInstruction<1> {
1612 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001613 explicit HReturn(HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001614 SetRawInputAt(0, value);
1615 }
1616
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001617 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001618
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001619 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001620
1621 private:
1622 DISALLOW_COPY_AND_ASSIGN(HReturn);
1623};
1624
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001625// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001626// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001627// exit block.
1628class HExit : public HTemplateInstruction<0> {
1629 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001630 HExit() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001631
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001632 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001633
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001634 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001635
1636 private:
1637 DISALLOW_COPY_AND_ASSIGN(HExit);
1638};
1639
1640// Jumps from one block to another.
1641class HGoto : public HTemplateInstruction<0> {
1642 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001643 HGoto() : HTemplateInstruction(SideEffects::None()) {}
1644
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001645 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001646
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001647 HBasicBlock* GetSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001648 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001649 }
1650
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001651 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001652
1653 private:
1654 DISALLOW_COPY_AND_ASSIGN(HGoto);
1655};
1656
Dave Allison20dfc792014-06-16 20:44:29 -07001657
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001658// Conditional branch. A block ending with an HIf instruction must have
1659// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001660class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001661 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001662 explicit HIf(HInstruction* input) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001663 SetRawInputAt(0, input);
1664 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001665
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001666 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001667
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001668 HBasicBlock* IfTrueSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001669 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001670 }
1671
1672 HBasicBlock* IfFalseSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001673 return GetBlock()->GetSuccessors().Get(1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001674 }
1675
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001676 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001677
1678 private:
1679 DISALLOW_COPY_AND_ASSIGN(HIf);
1680};
1681
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001682// Deoptimize to interpreter, upon checking a condition.
1683class HDeoptimize : public HTemplateInstruction<1> {
1684 public:
1685 HDeoptimize(HInstruction* cond, uint32_t dex_pc)
1686 : HTemplateInstruction(SideEffects::None()),
1687 dex_pc_(dex_pc) {
1688 SetRawInputAt(0, cond);
1689 }
1690
1691 bool NeedsEnvironment() const OVERRIDE { return true; }
1692 bool CanThrow() const OVERRIDE { return true; }
1693 uint32_t GetDexPc() const { return dex_pc_; }
1694
1695 DECLARE_INSTRUCTION(Deoptimize);
1696
1697 private:
1698 uint32_t dex_pc_;
1699
1700 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
1701};
1702
Roland Levillain88cb1752014-10-20 16:36:47 +01001703class HUnaryOperation : public HExpression<1> {
1704 public:
1705 HUnaryOperation(Primitive::Type result_type, HInstruction* input)
1706 : HExpression(result_type, SideEffects::None()) {
1707 SetRawInputAt(0, input);
1708 }
1709
1710 HInstruction* GetInput() const { return InputAt(0); }
1711 Primitive::Type GetResultType() const { return GetType(); }
1712
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001713 bool CanBeMoved() const OVERRIDE { return true; }
1714 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001715 UNUSED(other);
1716 return true;
1717 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001718
Roland Levillain9240d6a2014-10-20 16:47:04 +01001719 // Try to statically evaluate `operation` and return a HConstant
1720 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001721 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01001722 HConstant* TryStaticEvaluation() const;
1723
1724 // Apply this operation to `x`.
1725 virtual int32_t Evaluate(int32_t x) const = 0;
1726 virtual int64_t Evaluate(int64_t x) const = 0;
1727
Roland Levillain88cb1752014-10-20 16:36:47 +01001728 DECLARE_INSTRUCTION(UnaryOperation);
1729
1730 private:
1731 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
1732};
1733
Dave Allison20dfc792014-06-16 20:44:29 -07001734class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001735 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001736 HBinaryOperation(Primitive::Type result_type,
1737 HInstruction* left,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001738 HInstruction* right) : HExpression(result_type, SideEffects::None()) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001739 SetRawInputAt(0, left);
1740 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001741 }
1742
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001743 HInstruction* GetLeft() const { return InputAt(0); }
1744 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07001745 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001746
Mingyao Yangdc5ac732015-02-25 11:28:05 -08001747 virtual bool IsCommutative() const { return false; }
1748
1749 // Put constant on the right.
1750 // Returns whether order is changed.
1751 bool OrderInputsWithConstantOnTheRight() {
1752 HInstruction* left = InputAt(0);
1753 HInstruction* right = InputAt(1);
1754 if (left->IsConstant() && !right->IsConstant()) {
1755 ReplaceInput(right, 0);
1756 ReplaceInput(left, 1);
1757 return true;
1758 }
1759 return false;
1760 }
1761
1762 // Order inputs by instruction id, but favor constant on the right side.
1763 // This helps GVN for commutative ops.
1764 void OrderInputs() {
1765 DCHECK(IsCommutative());
1766 HInstruction* left = InputAt(0);
1767 HInstruction* right = InputAt(1);
1768 if (left == right || (!left->IsConstant() && right->IsConstant())) {
1769 return;
1770 }
1771 if (OrderInputsWithConstantOnTheRight()) {
1772 return;
1773 }
1774 // Order according to instruction id.
1775 if (left->GetId() > right->GetId()) {
1776 ReplaceInput(right, 0);
1777 ReplaceInput(left, 1);
1778 }
1779 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001780
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001781 bool CanBeMoved() const OVERRIDE { return true; }
1782 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001783 UNUSED(other);
1784 return true;
1785 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001786
Roland Levillain9240d6a2014-10-20 16:47:04 +01001787 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01001788 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001789 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01001790 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01001791
1792 // Apply this operation to `x` and `y`.
1793 virtual int32_t Evaluate(int32_t x, int32_t y) const = 0;
1794 virtual int64_t Evaluate(int64_t x, int64_t y) const = 0;
1795
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001796 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001797 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001798 HConstant* GetConstantRight() const;
1799
1800 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001801 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001802 HInstruction* GetLeastConstantLeft() const;
1803
Roland Levillainccc07a92014-09-16 14:48:16 +01001804 DECLARE_INSTRUCTION(BinaryOperation);
1805
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001806 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001807 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
1808};
1809
Dave Allison20dfc792014-06-16 20:44:29 -07001810class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001811 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001812 HCondition(HInstruction* first, HInstruction* second)
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001813 : HBinaryOperation(Primitive::kPrimBoolean, first, second),
1814 needs_materialization_(true) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001815
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001816 bool NeedsMaterialization() const { return needs_materialization_; }
1817 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001818
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001819 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001820 // `instruction`, and disregard moves in between.
1821 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001822
Dave Allison20dfc792014-06-16 20:44:29 -07001823 DECLARE_INSTRUCTION(Condition);
1824
1825 virtual IfCondition GetCondition() const = 0;
1826
1827 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001828 // For register allocation purposes, returns whether this instruction needs to be
1829 // materialized (that is, not just be in the processor flags).
1830 bool needs_materialization_;
1831
Dave Allison20dfc792014-06-16 20:44:29 -07001832 DISALLOW_COPY_AND_ASSIGN(HCondition);
1833};
1834
1835// Instruction to check if two inputs are equal to each other.
1836class HEqual : public HCondition {
1837 public:
1838 HEqual(HInstruction* first, HInstruction* second)
1839 : HCondition(first, second) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001840
Mingyao Yangdc5ac732015-02-25 11:28:05 -08001841 bool IsCommutative() const OVERRIDE { return true; }
1842
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001843 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001844 return x == y ? 1 : 0;
1845 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001846 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001847 return x == y ? 1 : 0;
1848 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001849
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001850 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001851
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001852 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001853 return kCondEQ;
1854 }
1855
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001856 private:
1857 DISALLOW_COPY_AND_ASSIGN(HEqual);
1858};
1859
Dave Allison20dfc792014-06-16 20:44:29 -07001860class HNotEqual : public HCondition {
1861 public:
1862 HNotEqual(HInstruction* first, HInstruction* second)
1863 : HCondition(first, second) {}
1864
Mingyao Yangdc5ac732015-02-25 11:28:05 -08001865 bool IsCommutative() const OVERRIDE { return true; }
1866
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001867 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001868 return x != y ? 1 : 0;
1869 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001870 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001871 return x != y ? 1 : 0;
1872 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001873
Dave Allison20dfc792014-06-16 20:44:29 -07001874 DECLARE_INSTRUCTION(NotEqual);
1875
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001876 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001877 return kCondNE;
1878 }
1879
1880 private:
1881 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
1882};
1883
1884class HLessThan : public HCondition {
1885 public:
1886 HLessThan(HInstruction* first, HInstruction* second)
1887 : HCondition(first, second) {}
1888
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001889 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001890 return x < y ? 1 : 0;
1891 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001892 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001893 return x < y ? 1 : 0;
1894 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001895
Dave Allison20dfc792014-06-16 20:44:29 -07001896 DECLARE_INSTRUCTION(LessThan);
1897
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001898 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001899 return kCondLT;
1900 }
1901
1902 private:
1903 DISALLOW_COPY_AND_ASSIGN(HLessThan);
1904};
1905
1906class HLessThanOrEqual : public HCondition {
1907 public:
1908 HLessThanOrEqual(HInstruction* first, HInstruction* second)
1909 : HCondition(first, second) {}
1910
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001911 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001912 return x <= y ? 1 : 0;
1913 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001914 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001915 return x <= y ? 1 : 0;
1916 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001917
Dave Allison20dfc792014-06-16 20:44:29 -07001918 DECLARE_INSTRUCTION(LessThanOrEqual);
1919
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001920 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001921 return kCondLE;
1922 }
1923
1924 private:
1925 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
1926};
1927
1928class HGreaterThan : public HCondition {
1929 public:
1930 HGreaterThan(HInstruction* first, HInstruction* second)
1931 : HCondition(first, second) {}
1932
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001933 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001934 return x > y ? 1 : 0;
1935 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001936 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001937 return x > y ? 1 : 0;
1938 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001939
Dave Allison20dfc792014-06-16 20:44:29 -07001940 DECLARE_INSTRUCTION(GreaterThan);
1941
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001942 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001943 return kCondGT;
1944 }
1945
1946 private:
1947 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
1948};
1949
1950class HGreaterThanOrEqual : public HCondition {
1951 public:
1952 HGreaterThanOrEqual(HInstruction* first, HInstruction* second)
1953 : HCondition(first, second) {}
1954
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001955 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001956 return x >= y ? 1 : 0;
1957 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001958 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001959 return x >= y ? 1 : 0;
1960 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001961
Dave Allison20dfc792014-06-16 20:44:29 -07001962 DECLARE_INSTRUCTION(GreaterThanOrEqual);
1963
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001964 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001965 return kCondGE;
1966 }
1967
1968 private:
1969 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
1970};
1971
1972
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001973// Instruction to check how two inputs compare to each other.
1974// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
1975class HCompare : public HBinaryOperation {
1976 public:
Calin Juravleddb7df22014-11-25 20:56:51 +00001977 // The bias applies for floating point operations and indicates how NaN
1978 // comparisons are treated:
1979 enum Bias {
1980 kNoBias, // bias is not applicable (i.e. for long operation)
1981 kGtBias, // return 1 for NaN comparisons
1982 kLtBias, // return -1 for NaN comparisons
1983 };
1984
1985 HCompare(Primitive::Type type, HInstruction* first, HInstruction* second, Bias bias)
1986 : HBinaryOperation(Primitive::kPrimInt, first, second), bias_(bias) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001987 DCHECK_EQ(type, first->GetType());
1988 DCHECK_EQ(type, second->GetType());
1989 }
1990
Calin Juravleddb7df22014-11-25 20:56:51 +00001991 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01001992 return
1993 x == y ? 0 :
1994 x > y ? 1 :
1995 -1;
1996 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001997
1998 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01001999 return
2000 x == y ? 0 :
2001 x > y ? 1 :
2002 -1;
2003 }
2004
Calin Juravleddb7df22014-11-25 20:56:51 +00002005 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2006 return bias_ == other->AsCompare()->bias_;
2007 }
2008
2009 bool IsGtBias() { return bias_ == kGtBias; }
2010
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002011 DECLARE_INSTRUCTION(Compare);
2012
2013 private:
Calin Juravleddb7df22014-11-25 20:56:51 +00002014 const Bias bias_;
2015
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002016 DISALLOW_COPY_AND_ASSIGN(HCompare);
2017};
2018
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002019// A local in the graph. Corresponds to a Dex register.
2020class HLocal : public HTemplateInstruction<0> {
2021 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002022 explicit HLocal(uint16_t reg_number)
2023 : HTemplateInstruction(SideEffects::None()), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002024
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002025 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002026
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002027 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002028
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002029 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002030 // The Dex register number.
2031 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002032
2033 DISALLOW_COPY_AND_ASSIGN(HLocal);
2034};
2035
2036// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07002037class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002038 public:
Roland Levillain5799fc02014-09-25 12:15:20 +01002039 HLoadLocal(HLocal* local, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002040 : HExpression(type, SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002041 SetRawInputAt(0, local);
2042 }
2043
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002044 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
2045
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002046 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002047
2048 private:
2049 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
2050};
2051
2052// Store a value in a given local. This instruction has two inputs: the value
2053// and the local.
2054class HStoreLocal : public HTemplateInstruction<2> {
2055 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002056 HStoreLocal(HLocal* local, HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002057 SetRawInputAt(0, local);
2058 SetRawInputAt(1, value);
2059 }
2060
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002061 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
2062
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002063 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002064
2065 private:
2066 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
2067};
2068
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002069class HConstant : public HExpression<0> {
2070 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002071 explicit HConstant(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
2072
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002073 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002074
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002075 virtual bool IsMinusOne() const { return false; }
2076 virtual bool IsZero() const { return false; }
2077 virtual bool IsOne() const { return false; }
2078
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002079 DECLARE_INSTRUCTION(Constant);
2080
2081 private:
2082 DISALLOW_COPY_AND_ASSIGN(HConstant);
2083};
2084
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002085class HFloatConstant : public HConstant {
2086 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002087 float GetValue() const { return value_; }
2088
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002089 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002090 return bit_cast<uint32_t, float>(other->AsFloatConstant()->value_) ==
2091 bit_cast<uint32_t, float>(value_);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002092 }
2093
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002094 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002095
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002096 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002097 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002098 }
2099 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002100 return value_ == 0.0f;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002101 }
2102 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002103 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
2104 }
2105 bool IsNaN() const {
2106 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002107 }
2108
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002109 DECLARE_INSTRUCTION(FloatConstant);
2110
2111 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002112 explicit HFloatConstant(float value) : HConstant(Primitive::kPrimFloat), value_(value) {}
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002113 explicit HFloatConstant(int32_t value)
2114 : HConstant(Primitive::kPrimFloat), value_(bit_cast<float, int32_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00002115
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002116 const float value_;
2117
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002118 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00002119 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002120 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002121 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2122};
2123
2124class HDoubleConstant : public HConstant {
2125 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002126 double GetValue() const { return value_; }
2127
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002128 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002129 return bit_cast<uint64_t, double>(other->AsDoubleConstant()->value_) ==
2130 bit_cast<uint64_t, double>(value_);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002131 }
2132
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002133 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002134
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002135 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002136 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002137 }
2138 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002139 return value_ == 0.0;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002140 }
2141 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002142 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
2143 }
2144 bool IsNaN() const {
2145 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002146 }
2147
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002148 DECLARE_INSTRUCTION(DoubleConstant);
2149
2150 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002151 explicit HDoubleConstant(double value) : HConstant(Primitive::kPrimDouble), value_(value) {}
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002152 explicit HDoubleConstant(int64_t value)
2153 : HConstant(Primitive::kPrimDouble), value_(bit_cast<double, int64_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00002154
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002155 const double value_;
2156
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002157 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00002158 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002159 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002160 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
2161};
2162
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002163class HNullConstant : public HConstant {
2164 public:
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002165 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2166 return true;
2167 }
2168
2169 size_t ComputeHashCode() const OVERRIDE { return 0; }
2170
2171 DECLARE_INSTRUCTION(NullConstant);
2172
2173 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002174 HNullConstant() : HConstant(Primitive::kPrimNot) {}
2175
2176 friend class HGraph;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002177 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2178};
2179
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002180// Constants of the type int. Those can be from Dex instructions, or
2181// synthesized (for example with the if-eqz instruction).
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002182class HIntConstant : public HConstant {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002183 public:
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002184 int32_t GetValue() const { return value_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002185
Calin Juravle61d544b2015-02-23 16:46:57 +00002186 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002187 return other->AsIntConstant()->value_ == value_;
2188 }
2189
Calin Juravle61d544b2015-02-23 16:46:57 +00002190 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2191
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002192 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2193 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2194 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2195
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002196 DECLARE_INSTRUCTION(IntConstant);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002197
2198 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002199 explicit HIntConstant(int32_t value) : HConstant(Primitive::kPrimInt), value_(value) {}
2200
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002201 const int32_t value_;
2202
David Brazdil8d5b8b22015-03-24 10:51:52 +00002203 friend class HGraph;
2204 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
Zheng Xuad4450e2015-04-17 18:48:56 +08002205 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002206 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2207};
2208
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002209class HLongConstant : public HConstant {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002210 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002211 int64_t GetValue() const { return value_; }
2212
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002213 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002214 return other->AsLongConstant()->value_ == value_;
2215 }
2216
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002217 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002218
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002219 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2220 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2221 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2222
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002223 DECLARE_INSTRUCTION(LongConstant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002224
2225 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002226 explicit HLongConstant(int64_t value) : HConstant(Primitive::kPrimLong), value_(value) {}
2227
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002228 const int64_t value_;
2229
David Brazdil8d5b8b22015-03-24 10:51:52 +00002230 friend class HGraph;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002231 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2232};
2233
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002234enum class Intrinsics {
2235#define OPTIMIZING_INTRINSICS(Name, IsStatic) k ## Name,
2236#include "intrinsics_list.h"
2237 kNone,
2238 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
2239#undef INTRINSICS_LIST
2240#undef OPTIMIZING_INTRINSICS
2241};
2242std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
2243
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002244class HInvoke : public HInstruction {
2245 public:
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002246 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002247
2248 // Runtime needs to walk the stack, so Dex -> Dex calls need to
2249 // know their environment.
Calin Juravle77520bc2015-01-12 18:45:46 +00002250 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002251
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01002252 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002253 SetRawInputAt(index, argument);
2254 }
2255
Roland Levillain3e3d7332015-04-28 11:00:54 +01002256 // Return the number of arguments. This number can be lower than
2257 // the number of inputs returned by InputCount(), as some invoke
2258 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
2259 // inputs at the end of their list of inputs.
2260 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
2261
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002262 Primitive::Type GetType() const OVERRIDE { return return_type_; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002263
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002264 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002265
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002266 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
2267
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01002268 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002269 return intrinsic_;
2270 }
2271
2272 void SetIntrinsic(Intrinsics intrinsic) {
2273 intrinsic_ = intrinsic;
2274 }
2275
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002276 DECLARE_INSTRUCTION(Invoke);
2277
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002278 protected:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002279 HInvoke(ArenaAllocator* arena,
2280 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01002281 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002282 Primitive::Type return_type,
2283 uint32_t dex_pc,
2284 uint32_t dex_method_index)
2285 : HInstruction(SideEffects::All()),
Roland Levillain3e3d7332015-04-28 11:00:54 +01002286 number_of_arguments_(number_of_arguments),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002287 inputs_(arena, number_of_arguments),
2288 return_type_(return_type),
2289 dex_pc_(dex_pc),
2290 dex_method_index_(dex_method_index),
2291 intrinsic_(Intrinsics::kNone) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01002292 uint32_t number_of_inputs = number_of_arguments + number_of_other_inputs;
2293 inputs_.SetSize(number_of_inputs);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002294 }
2295
David Brazdil1abb4192015-02-17 18:33:36 +00002296 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
2297 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
2298 inputs_.Put(index, input);
2299 }
2300
Roland Levillain3e3d7332015-04-28 11:00:54 +01002301 uint32_t number_of_arguments_;
David Brazdil1abb4192015-02-17 18:33:36 +00002302 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002303 const Primitive::Type return_type_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002304 const uint32_t dex_pc_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002305 const uint32_t dex_method_index_;
2306 Intrinsics intrinsic_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002307
2308 private:
2309 DISALLOW_COPY_AND_ASSIGN(HInvoke);
2310};
2311
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002312class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002313 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01002314 // Requirements of this method call regarding the class
2315 // initialization (clinit) check of its declaring class.
2316 enum class ClinitCheckRequirement {
2317 kNone, // Class already initialized.
2318 kExplicit, // Static call having explicit clinit check as last input.
2319 kImplicit, // Static call implicitly requiring a clinit check.
2320 };
2321
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002322 HInvokeStaticOrDirect(ArenaAllocator* arena,
2323 uint32_t number_of_arguments,
2324 Primitive::Type return_type,
2325 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002326 uint32_t dex_method_index,
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002327 bool is_recursive,
Jeff Hao848f70a2014-01-15 13:49:50 -08002328 int32_t string_init_offset,
Nicolas Geoffray79041292015-03-26 10:05:54 +00002329 InvokeType original_invoke_type,
Roland Levillain4c0eb422015-04-24 16:43:49 +01002330 InvokeType invoke_type,
2331 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01002332 : HInvoke(arena,
2333 number_of_arguments,
2334 clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u,
2335 return_type,
2336 dex_pc,
2337 dex_method_index),
Nicolas Geoffray79041292015-03-26 10:05:54 +00002338 original_invoke_type_(original_invoke_type),
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002339 invoke_type_(invoke_type),
Roland Levillain4c0eb422015-04-24 16:43:49 +01002340 is_recursive_(is_recursive),
Jeff Hao848f70a2014-01-15 13:49:50 -08002341 clinit_check_requirement_(clinit_check_requirement),
2342 string_init_offset_(string_init_offset) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002343
Calin Juravle641547a2015-04-21 22:08:51 +01002344 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
2345 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00002346 // We access the method via the dex cache so we can't do an implicit null check.
2347 // TODO: for intrinsics we can generate implicit null checks.
2348 return false;
2349 }
2350
Nicolas Geoffray79041292015-03-26 10:05:54 +00002351 InvokeType GetOriginalInvokeType() const { return original_invoke_type_; }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002352 InvokeType GetInvokeType() const { return invoke_type_; }
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002353 bool IsRecursive() const { return is_recursive_; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002354 bool NeedsDexCache() const OVERRIDE { return !IsRecursive(); }
Jeff Hao848f70a2014-01-15 13:49:50 -08002355 bool IsStringInit() const { return string_init_offset_ != 0; }
2356 int32_t GetStringInitOffset() const { return string_init_offset_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002357
Roland Levillain4c0eb422015-04-24 16:43:49 +01002358 // Is this instruction a call to a static method?
2359 bool IsStatic() const {
2360 return GetInvokeType() == kStatic;
2361 }
2362
Roland Levillain3e3d7332015-04-28 11:00:54 +01002363 // Remove the art::HLoadClass instruction set as last input by
2364 // art::PrepareForRegisterAllocation::VisitClinitCheck in lieu of
2365 // the initial art::HClinitCheck instruction (only relevant for
2366 // static calls with explicit clinit check).
2367 void RemoveLoadClassAsLastInput() {
Roland Levillain4c0eb422015-04-24 16:43:49 +01002368 DCHECK(IsStaticWithExplicitClinitCheck());
2369 size_t last_input_index = InputCount() - 1;
2370 HInstruction* last_input = InputAt(last_input_index);
2371 DCHECK(last_input != nullptr);
Roland Levillain3e3d7332015-04-28 11:00:54 +01002372 DCHECK(last_input->IsLoadClass()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01002373 RemoveAsUserOfInput(last_input_index);
2374 inputs_.DeleteAt(last_input_index);
2375 clinit_check_requirement_ = ClinitCheckRequirement::kImplicit;
2376 DCHECK(IsStaticWithImplicitClinitCheck());
2377 }
2378
2379 // Is this a call to a static method whose declaring class has an
2380 // explicit intialization check in the graph?
2381 bool IsStaticWithExplicitClinitCheck() const {
2382 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kExplicit);
2383 }
2384
2385 // Is this a call to a static method whose declaring class has an
2386 // implicit intialization check requirement?
2387 bool IsStaticWithImplicitClinitCheck() const {
2388 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kImplicit);
2389 }
2390
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002391 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002392
Roland Levillain4c0eb422015-04-24 16:43:49 +01002393 protected:
2394 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
2395 const HUserRecord<HInstruction*> input_record = HInvoke::InputRecordAt(i);
2396 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck() && (i == InputCount() - 1)) {
2397 HInstruction* input = input_record.GetInstruction();
2398 // `input` is the last input of a static invoke marked as having
2399 // an explicit clinit check. It must either be:
2400 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
2401 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
2402 DCHECK(input != nullptr);
2403 DCHECK(input->IsClinitCheck() || input->IsLoadClass()) << input->DebugName();
2404 }
2405 return input_record;
2406 }
2407
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002408 private:
Nicolas Geoffray79041292015-03-26 10:05:54 +00002409 const InvokeType original_invoke_type_;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002410 const InvokeType invoke_type_;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002411 const bool is_recursive_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01002412 ClinitCheckRequirement clinit_check_requirement_;
Jeff Hao848f70a2014-01-15 13:49:50 -08002413 // Thread entrypoint offset for string init method if this is a string init invoke.
2414 // Note that there are multiple string init methods, each having its own offset.
2415 int32_t string_init_offset_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002416
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002417 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002418};
2419
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002420class HInvokeVirtual : public HInvoke {
2421 public:
2422 HInvokeVirtual(ArenaAllocator* arena,
2423 uint32_t number_of_arguments,
2424 Primitive::Type return_type,
2425 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002426 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002427 uint32_t vtable_index)
Roland Levillain3e3d7332015-04-28 11:00:54 +01002428 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002429 vtable_index_(vtable_index) {}
2430
Calin Juravle641547a2015-04-21 22:08:51 +01002431 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00002432 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01002433 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00002434 }
2435
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002436 uint32_t GetVTableIndex() const { return vtable_index_; }
2437
2438 DECLARE_INSTRUCTION(InvokeVirtual);
2439
2440 private:
2441 const uint32_t vtable_index_;
2442
2443 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
2444};
2445
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002446class HInvokeInterface : public HInvoke {
2447 public:
2448 HInvokeInterface(ArenaAllocator* arena,
2449 uint32_t number_of_arguments,
2450 Primitive::Type return_type,
2451 uint32_t dex_pc,
2452 uint32_t dex_method_index,
2453 uint32_t imt_index)
Roland Levillain3e3d7332015-04-28 11:00:54 +01002454 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002455 imt_index_(imt_index) {}
2456
Calin Juravle641547a2015-04-21 22:08:51 +01002457 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00002458 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01002459 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00002460 }
2461
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002462 uint32_t GetImtIndex() const { return imt_index_; }
2463 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
2464
2465 DECLARE_INSTRUCTION(InvokeInterface);
2466
2467 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002468 const uint32_t imt_index_;
2469
2470 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
2471};
2472
Dave Allison20dfc792014-06-16 20:44:29 -07002473class HNewInstance : public HExpression<0> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002474 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002475 HNewInstance(uint32_t dex_pc, uint16_t type_index, QuickEntrypointEnum entrypoint)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002476 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2477 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002478 type_index_(type_index),
2479 entrypoint_(entrypoint) {}
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002480
2481 uint32_t GetDexPc() const { return dex_pc_; }
2482 uint16_t GetTypeIndex() const { return type_index_; }
2483
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002484 // Calls runtime so needs an environment.
Calin Juravle92a6ed22014-12-02 18:58:03 +00002485 bool NeedsEnvironment() const OVERRIDE { return true; }
2486 // It may throw when called on:
2487 // - interfaces
2488 // - abstract/innaccessible/unknown classes
2489 // TODO: optimize when possible.
2490 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002491
Calin Juravle10e244f2015-01-26 18:54:32 +00002492 bool CanBeNull() const OVERRIDE { return false; }
2493
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002494 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
2495
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002496 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002497
2498 private:
2499 const uint32_t dex_pc_;
2500 const uint16_t type_index_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002501 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002502
2503 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
2504};
2505
Roland Levillain88cb1752014-10-20 16:36:47 +01002506class HNeg : public HUnaryOperation {
2507 public:
2508 explicit HNeg(Primitive::Type result_type, HInstruction* input)
2509 : HUnaryOperation(result_type, input) {}
2510
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002511 int32_t Evaluate(int32_t x) const OVERRIDE { return -x; }
2512 int64_t Evaluate(int64_t x) const OVERRIDE { return -x; }
Roland Levillain9240d6a2014-10-20 16:47:04 +01002513
Roland Levillain88cb1752014-10-20 16:36:47 +01002514 DECLARE_INSTRUCTION(Neg);
2515
2516 private:
2517 DISALLOW_COPY_AND_ASSIGN(HNeg);
2518};
2519
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002520class HNewArray : public HExpression<1> {
2521 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002522 HNewArray(HInstruction* length,
2523 uint32_t dex_pc,
2524 uint16_t type_index,
2525 QuickEntrypointEnum entrypoint)
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002526 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2527 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002528 type_index_(type_index),
2529 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002530 SetRawInputAt(0, length);
2531 }
2532
2533 uint32_t GetDexPc() const { return dex_pc_; }
2534 uint16_t GetTypeIndex() const { return type_index_; }
2535
2536 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00002537 bool NeedsEnvironment() const OVERRIDE { return true; }
2538
Mingyao Yang0c365e62015-03-31 15:09:29 -07002539 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
2540 bool CanThrow() const OVERRIDE { return true; }
2541
Calin Juravle10e244f2015-01-26 18:54:32 +00002542 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002543
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002544 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
2545
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002546 DECLARE_INSTRUCTION(NewArray);
2547
2548 private:
2549 const uint32_t dex_pc_;
2550 const uint16_t type_index_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002551 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002552
2553 DISALLOW_COPY_AND_ASSIGN(HNewArray);
2554};
2555
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002556class HAdd : public HBinaryOperation {
2557 public:
2558 HAdd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2559 : HBinaryOperation(result_type, left, right) {}
2560
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002561 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002562
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002563 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002564 return x + y;
2565 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002566 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002567 return x + y;
2568 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002569
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002570 DECLARE_INSTRUCTION(Add);
2571
2572 private:
2573 DISALLOW_COPY_AND_ASSIGN(HAdd);
2574};
2575
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002576class HSub : public HBinaryOperation {
2577 public:
2578 HSub(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2579 : HBinaryOperation(result_type, left, right) {}
2580
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002581 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002582 return x - y;
2583 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002584 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002585 return x - y;
2586 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002587
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002588 DECLARE_INSTRUCTION(Sub);
2589
2590 private:
2591 DISALLOW_COPY_AND_ASSIGN(HSub);
2592};
2593
Calin Juravle34bacdf2014-10-07 20:23:36 +01002594class HMul : public HBinaryOperation {
2595 public:
2596 HMul(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2597 : HBinaryOperation(result_type, left, right) {}
2598
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002599 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002600
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002601 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x * y; }
2602 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x * y; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002603
2604 DECLARE_INSTRUCTION(Mul);
2605
2606 private:
2607 DISALLOW_COPY_AND_ASSIGN(HMul);
2608};
2609
Calin Juravle7c4954d2014-10-28 16:57:40 +00002610class HDiv : public HBinaryOperation {
2611 public:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002612 HDiv(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
2613 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00002614
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002615 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00002616 // Our graph structure ensures we never have 0 for `y` during constant folding.
2617 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00002618 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00002619 return (y == -1) ? -x : x / y;
2620 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002621
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002622 int64_t Evaluate(int64_t x, int64_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) ? -x : x / y;
2626 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002627
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002628 uint32_t GetDexPc() const { return dex_pc_; }
2629
Calin Juravle7c4954d2014-10-28 16:57:40 +00002630 DECLARE_INSTRUCTION(Div);
2631
2632 private:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002633 const uint32_t dex_pc_;
2634
Calin Juravle7c4954d2014-10-28 16:57:40 +00002635 DISALLOW_COPY_AND_ASSIGN(HDiv);
2636};
2637
Calin Juravlebacfec32014-11-14 15:54:36 +00002638class HRem : public HBinaryOperation {
2639 public:
2640 HRem(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
2641 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
2642
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002643 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002644 DCHECK_NE(y, 0);
2645 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2646 return (y == -1) ? 0 : x % y;
2647 }
2648
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002649 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002650 DCHECK_NE(y, 0);
2651 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2652 return (y == -1) ? 0 : x % y;
2653 }
2654
2655 uint32_t GetDexPc() const { return dex_pc_; }
2656
2657 DECLARE_INSTRUCTION(Rem);
2658
2659 private:
2660 const uint32_t dex_pc_;
2661
2662 DISALLOW_COPY_AND_ASSIGN(HRem);
2663};
2664
Calin Juravled0d48522014-11-04 16:40:20 +00002665class HDivZeroCheck : public HExpression<1> {
2666 public:
2667 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
2668 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
2669 SetRawInputAt(0, value);
2670 }
2671
2672 bool CanBeMoved() const OVERRIDE { return true; }
2673
2674 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2675 UNUSED(other);
2676 return true;
2677 }
2678
2679 bool NeedsEnvironment() const OVERRIDE { return true; }
2680 bool CanThrow() const OVERRIDE { return true; }
2681
2682 uint32_t GetDexPc() const { return dex_pc_; }
2683
2684 DECLARE_INSTRUCTION(DivZeroCheck);
2685
2686 private:
2687 const uint32_t dex_pc_;
2688
2689 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
2690};
2691
Calin Juravle9aec02f2014-11-18 23:06:35 +00002692class HShl : public HBinaryOperation {
2693 public:
2694 HShl(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2695 : HBinaryOperation(result_type, left, right) {}
2696
2697 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x << (y & kMaxIntShiftValue); }
2698 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x << (y & kMaxLongShiftValue); }
2699
2700 DECLARE_INSTRUCTION(Shl);
2701
2702 private:
2703 DISALLOW_COPY_AND_ASSIGN(HShl);
2704};
2705
2706class HShr : public HBinaryOperation {
2707 public:
2708 HShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2709 : HBinaryOperation(result_type, left, right) {}
2710
2711 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x >> (y & kMaxIntShiftValue); }
2712 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x >> (y & kMaxLongShiftValue); }
2713
2714 DECLARE_INSTRUCTION(Shr);
2715
2716 private:
2717 DISALLOW_COPY_AND_ASSIGN(HShr);
2718};
2719
2720class HUShr : public HBinaryOperation {
2721 public:
2722 HUShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2723 : HBinaryOperation(result_type, left, right) {}
2724
2725 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
2726 uint32_t ux = static_cast<uint32_t>(x);
2727 uint32_t uy = static_cast<uint32_t>(y) & kMaxIntShiftValue;
2728 return static_cast<int32_t>(ux >> uy);
2729 }
2730
2731 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
2732 uint64_t ux = static_cast<uint64_t>(x);
2733 uint64_t uy = static_cast<uint64_t>(y) & kMaxLongShiftValue;
2734 return static_cast<int64_t>(ux >> uy);
2735 }
2736
2737 DECLARE_INSTRUCTION(UShr);
2738
2739 private:
2740 DISALLOW_COPY_AND_ASSIGN(HUShr);
2741};
2742
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002743class HAnd : public HBinaryOperation {
2744 public:
2745 HAnd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2746 : HBinaryOperation(result_type, left, right) {}
2747
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002748 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002749
2750 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x & y; }
2751 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x & y; }
2752
2753 DECLARE_INSTRUCTION(And);
2754
2755 private:
2756 DISALLOW_COPY_AND_ASSIGN(HAnd);
2757};
2758
2759class HOr : public HBinaryOperation {
2760 public:
2761 HOr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2762 : HBinaryOperation(result_type, left, right) {}
2763
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002764 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002765
2766 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x | y; }
2767 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x | y; }
2768
2769 DECLARE_INSTRUCTION(Or);
2770
2771 private:
2772 DISALLOW_COPY_AND_ASSIGN(HOr);
2773};
2774
2775class HXor : public HBinaryOperation {
2776 public:
2777 HXor(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2778 : HBinaryOperation(result_type, left, right) {}
2779
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002780 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002781
2782 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x ^ y; }
2783 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x ^ y; }
2784
2785 DECLARE_INSTRUCTION(Xor);
2786
2787 private:
2788 DISALLOW_COPY_AND_ASSIGN(HXor);
2789};
2790
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002791// The value of a parameter in this method. Its location depends on
2792// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07002793class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002794 public:
Calin Juravle10e244f2015-01-26 18:54:32 +00002795 HParameterValue(uint8_t index, Primitive::Type parameter_type, bool is_this = false)
2796 : HExpression(parameter_type, SideEffects::None()), index_(index), is_this_(is_this) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002797
2798 uint8_t GetIndex() const { return index_; }
2799
Calin Juravle10e244f2015-01-26 18:54:32 +00002800 bool CanBeNull() const OVERRIDE { return !is_this_; }
2801
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002802 DECLARE_INSTRUCTION(ParameterValue);
2803
2804 private:
2805 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00002806 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002807 const uint8_t index_;
2808
Calin Juravle10e244f2015-01-26 18:54:32 +00002809 // Whether or not the parameter value corresponds to 'this' argument.
2810 const bool is_this_;
2811
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002812 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
2813};
2814
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002815class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002816 public:
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002817 explicit HNot(Primitive::Type result_type, HInstruction* input)
2818 : HUnaryOperation(result_type, input) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002819
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002820 bool CanBeMoved() const OVERRIDE { return true; }
2821 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002822 UNUSED(other);
2823 return true;
2824 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002825
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002826 int32_t Evaluate(int32_t x) const OVERRIDE { return ~x; }
2827 int64_t Evaluate(int64_t x) const OVERRIDE { return ~x; }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002828
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002829 DECLARE_INSTRUCTION(Not);
2830
2831 private:
2832 DISALLOW_COPY_AND_ASSIGN(HNot);
2833};
2834
David Brazdil66d126e2015-04-03 16:02:44 +01002835class HBooleanNot : public HUnaryOperation {
2836 public:
2837 explicit HBooleanNot(HInstruction* input)
2838 : HUnaryOperation(Primitive::Type::kPrimBoolean, input) {}
2839
2840 bool CanBeMoved() const OVERRIDE { return true; }
2841 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2842 UNUSED(other);
2843 return true;
2844 }
2845
2846 int32_t Evaluate(int32_t x) const OVERRIDE {
2847 DCHECK(IsUint<1>(x));
2848 return !x;
2849 }
2850
2851 int64_t Evaluate(int64_t x ATTRIBUTE_UNUSED) const OVERRIDE {
2852 LOG(FATAL) << DebugName() << " cannot be used with 64-bit values";
2853 UNREACHABLE();
2854 }
2855
2856 DECLARE_INSTRUCTION(BooleanNot);
2857
2858 private:
2859 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
2860};
2861
Roland Levillaindff1f282014-11-05 14:15:05 +00002862class HTypeConversion : public HExpression<1> {
2863 public:
2864 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00002865 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
2866 : HExpression(result_type, SideEffects::None()), dex_pc_(dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002867 SetRawInputAt(0, input);
2868 DCHECK_NE(input->GetType(), result_type);
2869 }
2870
2871 HInstruction* GetInput() const { return InputAt(0); }
2872 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
2873 Primitive::Type GetResultType() const { return GetType(); }
2874
Roland Levillain624279f2014-12-04 11:54:28 +00002875 // Required by the x86 and ARM code generators when producing calls
2876 // to the runtime.
2877 uint32_t GetDexPc() const { return dex_pc_; }
2878
Roland Levillaindff1f282014-11-05 14:15:05 +00002879 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00002880 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00002881
2882 DECLARE_INSTRUCTION(TypeConversion);
2883
2884 private:
Roland Levillain624279f2014-12-04 11:54:28 +00002885 const uint32_t dex_pc_;
2886
Roland Levillaindff1f282014-11-05 14:15:05 +00002887 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
2888};
2889
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00002890static constexpr uint32_t kNoRegNumber = -1;
2891
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002892class HPhi : public HInstruction {
2893 public:
2894 HPhi(ArenaAllocator* arena, uint32_t reg_number, size_t number_of_inputs, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002895 : HInstruction(SideEffects::None()),
2896 inputs_(arena, number_of_inputs),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002897 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002898 type_(type),
Calin Juravle10e244f2015-01-26 18:54:32 +00002899 is_live_(false),
2900 can_be_null_(true) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002901 inputs_.SetSize(number_of_inputs);
2902 }
2903
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00002904 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
2905 static Primitive::Type ToPhiType(Primitive::Type type) {
2906 switch (type) {
2907 case Primitive::kPrimBoolean:
2908 case Primitive::kPrimByte:
2909 case Primitive::kPrimShort:
2910 case Primitive::kPrimChar:
2911 return Primitive::kPrimInt;
2912 default:
2913 return type;
2914 }
2915 }
2916
Calin Juravle10e244f2015-01-26 18:54:32 +00002917 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002918
2919 void AddInput(HInstruction* input);
David Brazdil2d7352b2015-04-20 14:52:42 +01002920 void RemoveInputAt(size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002921
Calin Juravle10e244f2015-01-26 18:54:32 +00002922 Primitive::Type GetType() const OVERRIDE { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002923 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002924
Calin Juravle10e244f2015-01-26 18:54:32 +00002925 bool CanBeNull() const OVERRIDE { return can_be_null_; }
2926 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
2927
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002928 uint32_t GetRegNumber() const { return reg_number_; }
2929
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002930 void SetDead() { is_live_ = false; }
2931 void SetLive() { is_live_ = true; }
2932 bool IsDead() const { return !is_live_; }
2933 bool IsLive() const { return is_live_; }
2934
Calin Juravlea4f88312015-04-16 12:57:19 +01002935 // Returns the next equivalent phi (starting from the current one) or null if there is none.
2936 // An equivalent phi is a phi having the same dex register and type.
2937 // It assumes that phis with the same dex register are adjacent.
2938 HPhi* GetNextEquivalentPhiWithSameType() {
2939 HInstruction* next = GetNext();
2940 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
2941 if (next->GetType() == GetType()) {
2942 return next->AsPhi();
2943 }
2944 next = next->GetNext();
2945 }
2946 return nullptr;
2947 }
2948
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002949 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002950
David Brazdil1abb4192015-02-17 18:33:36 +00002951 protected:
2952 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
2953
2954 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
2955 inputs_.Put(index, input);
2956 }
2957
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002958 private:
David Brazdil1abb4192015-02-17 18:33:36 +00002959 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002960 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002961 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002962 bool is_live_;
Calin Juravle10e244f2015-01-26 18:54:32 +00002963 bool can_be_null_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002964
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002965 DISALLOW_COPY_AND_ASSIGN(HPhi);
2966};
2967
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002968class HNullCheck : public HExpression<1> {
2969 public:
2970 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002971 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002972 SetRawInputAt(0, value);
2973 }
2974
Calin Juravle10e244f2015-01-26 18:54:32 +00002975 bool CanBeMoved() const OVERRIDE { return true; }
2976 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002977 UNUSED(other);
2978 return true;
2979 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002980
Calin Juravle10e244f2015-01-26 18:54:32 +00002981 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002982
Calin Juravle10e244f2015-01-26 18:54:32 +00002983 bool CanThrow() const OVERRIDE { return true; }
2984
2985 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01002986
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002987 uint32_t GetDexPc() const { return dex_pc_; }
2988
2989 DECLARE_INSTRUCTION(NullCheck);
2990
2991 private:
2992 const uint32_t dex_pc_;
2993
2994 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
2995};
2996
2997class FieldInfo : public ValueObject {
2998 public:
Calin Juravle52c48962014-12-16 17:02:57 +00002999 FieldInfo(MemberOffset field_offset, Primitive::Type field_type, bool is_volatile)
3000 : field_offset_(field_offset), field_type_(field_type), is_volatile_(is_volatile) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003001
3002 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003003 Primitive::Type GetFieldType() const { return field_type_; }
Calin Juravle52c48962014-12-16 17:02:57 +00003004 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003005
3006 private:
3007 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01003008 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00003009 const bool is_volatile_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003010};
3011
3012class HInstanceFieldGet : public HExpression<1> {
3013 public:
3014 HInstanceFieldGet(HInstruction* value,
3015 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003016 MemberOffset field_offset,
3017 bool is_volatile)
Nicolas Geoffray2af23072015-04-30 11:15:40 +00003018 : HExpression(field_type, SideEffects::DependsOnSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00003019 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003020 SetRawInputAt(0, value);
3021 }
3022
Calin Juravle10c9cbe2014-12-19 10:50:19 +00003023 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003024
3025 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3026 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
3027 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003028 }
3029
Calin Juravle641547a2015-04-21 22:08:51 +01003030 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3031 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00003032 }
3033
3034 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01003035 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
3036 }
3037
Calin Juravle52c48962014-12-16 17:02:57 +00003038 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003039 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003040 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003041 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003042
3043 DECLARE_INSTRUCTION(InstanceFieldGet);
3044
3045 private:
3046 const FieldInfo field_info_;
3047
3048 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
3049};
3050
3051class HInstanceFieldSet : public HTemplateInstruction<2> {
3052 public:
3053 HInstanceFieldSet(HInstruction* object,
3054 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01003055 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003056 MemberOffset field_offset,
3057 bool is_volatile)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003058 : HTemplateInstruction(SideEffects::ChangesSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00003059 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003060 SetRawInputAt(0, object);
3061 SetRawInputAt(1, value);
3062 }
3063
Calin Juravle641547a2015-04-21 22:08:51 +01003064 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3065 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00003066 }
3067
Calin Juravle52c48962014-12-16 17:02:57 +00003068 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003069 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003070 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003071 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003072 HInstruction* GetValue() const { return InputAt(1); }
3073
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003074 DECLARE_INSTRUCTION(InstanceFieldSet);
3075
3076 private:
3077 const FieldInfo field_info_;
3078
3079 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
3080};
3081
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003082class HArrayGet : public HExpression<2> {
3083 public:
3084 HArrayGet(HInstruction* array, HInstruction* index, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003085 : HExpression(type, SideEffects::DependsOnSomething()) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003086 SetRawInputAt(0, array);
3087 SetRawInputAt(1, index);
3088 }
3089
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003090 bool CanBeMoved() const OVERRIDE { return true; }
3091 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003092 UNUSED(other);
3093 return true;
3094 }
Calin Juravle641547a2015-04-21 22:08:51 +01003095 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3096 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00003097 // TODO: We can be smarter here.
3098 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
3099 // which generates the implicit null check. There are cases when these can be removed
3100 // to produce better code. If we ever add optimizations to do so we should allow an
3101 // implicit check here (as long as the address falls in the first page).
3102 return false;
3103 }
3104
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003105 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003106
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003107 HInstruction* GetArray() const { return InputAt(0); }
3108 HInstruction* GetIndex() const { return InputAt(1); }
3109
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003110 DECLARE_INSTRUCTION(ArrayGet);
3111
3112 private:
3113 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
3114};
3115
3116class HArraySet : public HTemplateInstruction<3> {
3117 public:
3118 HArraySet(HInstruction* array,
3119 HInstruction* index,
3120 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003121 Primitive::Type expected_component_type,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003122 uint32_t dex_pc)
3123 : HTemplateInstruction(SideEffects::ChangesSomething()),
3124 dex_pc_(dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003125 expected_component_type_(expected_component_type),
3126 needs_type_check_(value->GetType() == Primitive::kPrimNot) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003127 SetRawInputAt(0, array);
3128 SetRawInputAt(1, index);
3129 SetRawInputAt(2, value);
3130 }
3131
Calin Juravle77520bc2015-01-12 18:45:46 +00003132 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003133 // We currently always call a runtime method to catch array store
3134 // exceptions.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003135 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003136 }
3137
Calin Juravle641547a2015-04-21 22:08:51 +01003138 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3139 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00003140 // TODO: Same as for ArrayGet.
3141 return false;
3142 }
3143
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003144 void ClearNeedsTypeCheck() {
3145 needs_type_check_ = false;
3146 }
3147
3148 bool NeedsTypeCheck() const { return needs_type_check_; }
3149
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003150 uint32_t GetDexPc() const { return dex_pc_; }
3151
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003152 HInstruction* GetArray() const { return InputAt(0); }
3153 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003154 HInstruction* GetValue() const { return InputAt(2); }
3155
3156 Primitive::Type GetComponentType() const {
3157 // The Dex format does not type floating point index operations. Since the
3158 // `expected_component_type_` is set during building and can therefore not
3159 // be correct, we also check what is the value type. If it is a floating
3160 // point type, we must use that type.
3161 Primitive::Type value_type = GetValue()->GetType();
3162 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
3163 ? value_type
3164 : expected_component_type_;
3165 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003166
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003167 DECLARE_INSTRUCTION(ArraySet);
3168
3169 private:
3170 const uint32_t dex_pc_;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003171 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003172 bool needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003173
3174 DISALLOW_COPY_AND_ASSIGN(HArraySet);
3175};
3176
3177class HArrayLength : public HExpression<1> {
3178 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003179 explicit HArrayLength(HInstruction* array)
3180 : HExpression(Primitive::kPrimInt, SideEffects::None()) {
3181 // Note that arrays do not change length, so the instruction does not
3182 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003183 SetRawInputAt(0, array);
3184 }
3185
Calin Juravle77520bc2015-01-12 18:45:46 +00003186 bool CanBeMoved() const OVERRIDE { return true; }
3187 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003188 UNUSED(other);
3189 return true;
3190 }
Calin Juravle641547a2015-04-21 22:08:51 +01003191 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3192 return obj == InputAt(0);
3193 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003194
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003195 DECLARE_INSTRUCTION(ArrayLength);
3196
3197 private:
3198 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
3199};
3200
3201class HBoundsCheck : public HExpression<2> {
3202 public:
3203 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003204 : HExpression(index->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003205 DCHECK(index->GetType() == Primitive::kPrimInt);
3206 SetRawInputAt(0, index);
3207 SetRawInputAt(1, length);
3208 }
3209
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003210 bool CanBeMoved() const OVERRIDE { return true; }
3211 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003212 UNUSED(other);
3213 return true;
3214 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003215
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003216 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003217
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003218 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01003219
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003220 uint32_t GetDexPc() const { return dex_pc_; }
3221
3222 DECLARE_INSTRUCTION(BoundsCheck);
3223
3224 private:
3225 const uint32_t dex_pc_;
3226
3227 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
3228};
3229
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003230/**
3231 * Some DEX instructions are folded into multiple HInstructions that need
3232 * to stay live until the last HInstruction. This class
3233 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00003234 * HInstruction stays live. `index` represents the stack location index of the
3235 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003236 */
3237class HTemporary : public HTemplateInstruction<0> {
3238 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003239 explicit HTemporary(size_t index) : HTemplateInstruction(SideEffects::None()), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003240
3241 size_t GetIndex() const { return index_; }
3242
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00003243 Primitive::Type GetType() const OVERRIDE {
3244 // The previous instruction is the one that will be stored in the temporary location.
3245 DCHECK(GetPrevious() != nullptr);
3246 return GetPrevious()->GetType();
3247 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00003248
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003249 DECLARE_INSTRUCTION(Temporary);
3250
3251 private:
3252 const size_t index_;
3253
3254 DISALLOW_COPY_AND_ASSIGN(HTemporary);
3255};
3256
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003257class HSuspendCheck : public HTemplateInstruction<0> {
3258 public:
3259 explicit HSuspendCheck(uint32_t dex_pc)
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01003260 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003261
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003262 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003263 return true;
3264 }
3265
3266 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01003267 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
3268 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003269
3270 DECLARE_INSTRUCTION(SuspendCheck);
3271
3272 private:
3273 const uint32_t dex_pc_;
3274
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01003275 // Only used for code generation, in order to share the same slow path between back edges
3276 // of a same loop.
3277 SlowPathCode* slow_path_;
3278
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003279 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
3280};
3281
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003282/**
3283 * Instruction to load a Class object.
3284 */
3285class HLoadClass : public HExpression<0> {
3286 public:
3287 HLoadClass(uint16_t type_index,
3288 bool is_referrers_class,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003289 uint32_t dex_pc)
3290 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3291 type_index_(type_index),
3292 is_referrers_class_(is_referrers_class),
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003293 dex_pc_(dex_pc),
Calin Juravleb1498f62015-02-16 13:13:29 +00003294 generate_clinit_check_(false),
3295 loaded_class_rti_(ReferenceTypeInfo::CreateTop(/* is_exact */ false)) {}
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003296
3297 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003298
3299 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3300 return other->AsLoadClass()->type_index_ == type_index_;
3301 }
3302
3303 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
3304
3305 uint32_t GetDexPc() const { return dex_pc_; }
3306 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003307 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003308
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003309 bool NeedsEnvironment() const OVERRIDE {
3310 // Will call runtime and load the class if the class is not loaded yet.
3311 // TODO: finer grain decision.
3312 return !is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003313 }
3314
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003315 bool MustGenerateClinitCheck() const {
3316 return generate_clinit_check_;
3317 }
3318
3319 void SetMustGenerateClinitCheck() {
3320 generate_clinit_check_ = true;
3321 }
3322
3323 bool CanCallRuntime() const {
3324 return MustGenerateClinitCheck() || !is_referrers_class_;
3325 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003326
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003327 bool CanThrow() const OVERRIDE {
3328 // May call runtime and and therefore can throw.
3329 // TODO: finer grain decision.
3330 return !is_referrers_class_;
3331 }
3332
Calin Juravleacf735c2015-02-12 15:25:22 +00003333 ReferenceTypeInfo GetLoadedClassRTI() {
3334 return loaded_class_rti_;
3335 }
3336
3337 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
3338 // Make sure we only set exact types (the loaded class should never be merged).
3339 DCHECK(rti.IsExact());
3340 loaded_class_rti_ = rti;
3341 }
3342
3343 bool IsResolved() {
3344 return loaded_class_rti_.IsExact();
3345 }
3346
Nicolas Geoffray9437b782015-03-25 10:08:51 +00003347 bool NeedsDexCache() const OVERRIDE { return !is_referrers_class_; }
3348
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003349 DECLARE_INSTRUCTION(LoadClass);
3350
3351 private:
3352 const uint16_t type_index_;
3353 const bool is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003354 const uint32_t dex_pc_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003355 // Whether this instruction must generate the initialization check.
3356 // Used for code generation.
3357 bool generate_clinit_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003358
Calin Juravleacf735c2015-02-12 15:25:22 +00003359 ReferenceTypeInfo loaded_class_rti_;
3360
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003361 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
3362};
3363
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003364class HLoadString : public HExpression<0> {
3365 public:
3366 HLoadString(uint32_t string_index, uint32_t dex_pc)
3367 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3368 string_index_(string_index),
3369 dex_pc_(dex_pc) {}
3370
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003371 bool CanBeMoved() const OVERRIDE { return true; }
3372
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003373 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3374 return other->AsLoadString()->string_index_ == string_index_;
3375 }
3376
3377 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
3378
3379 uint32_t GetDexPc() const { return dex_pc_; }
3380 uint32_t GetStringIndex() const { return string_index_; }
3381
3382 // TODO: Can we deopt or debug when we resolve a string?
3383 bool NeedsEnvironment() const OVERRIDE { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00003384 bool NeedsDexCache() const OVERRIDE { return true; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003385
3386 DECLARE_INSTRUCTION(LoadString);
3387
3388 private:
3389 const uint32_t string_index_;
3390 const uint32_t dex_pc_;
3391
3392 DISALLOW_COPY_AND_ASSIGN(HLoadString);
3393};
3394
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003395/**
3396 * Performs an initialization check on its Class object input.
3397 */
3398class HClinitCheck : public HExpression<1> {
3399 public:
3400 explicit HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Nicolas Geoffraya0466e12015-03-27 15:00:40 +00003401 : HExpression(Primitive::kPrimNot, SideEffects::ChangesSomething()),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003402 dex_pc_(dex_pc) {
3403 SetRawInputAt(0, constant);
3404 }
3405
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003406 bool CanBeMoved() const OVERRIDE { return true; }
3407 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3408 UNUSED(other);
3409 return true;
3410 }
3411
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003412 bool NeedsEnvironment() const OVERRIDE {
3413 // May call runtime to initialize the class.
3414 return true;
3415 }
3416
3417 uint32_t GetDexPc() const { return dex_pc_; }
3418
3419 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
3420
3421 DECLARE_INSTRUCTION(ClinitCheck);
3422
3423 private:
3424 const uint32_t dex_pc_;
3425
3426 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
3427};
3428
3429class HStaticFieldGet : public HExpression<1> {
3430 public:
3431 HStaticFieldGet(HInstruction* cls,
3432 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003433 MemberOffset field_offset,
3434 bool is_volatile)
Nicolas Geoffray2af23072015-04-30 11:15:40 +00003435 : HExpression(field_type, SideEffects::DependsOnSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00003436 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003437 SetRawInputAt(0, cls);
3438 }
3439
Calin Juravle52c48962014-12-16 17:02:57 +00003440
Calin Juravle10c9cbe2014-12-19 10:50:19 +00003441 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003442
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003443 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00003444 HStaticFieldGet* other_get = other->AsStaticFieldGet();
3445 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003446 }
3447
3448 size_t ComputeHashCode() const OVERRIDE {
3449 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
3450 }
3451
Calin Juravle52c48962014-12-16 17:02:57 +00003452 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003453 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
3454 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003455 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003456
3457 DECLARE_INSTRUCTION(StaticFieldGet);
3458
3459 private:
3460 const FieldInfo field_info_;
3461
3462 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
3463};
3464
3465class HStaticFieldSet : public HTemplateInstruction<2> {
3466 public:
3467 HStaticFieldSet(HInstruction* cls,
3468 HInstruction* value,
3469 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003470 MemberOffset field_offset,
3471 bool is_volatile)
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003472 : HTemplateInstruction(SideEffects::ChangesSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00003473 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003474 SetRawInputAt(0, cls);
3475 SetRawInputAt(1, value);
3476 }
3477
Calin Juravle52c48962014-12-16 17:02:57 +00003478 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003479 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
3480 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003481 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003482
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003483 HInstruction* GetValue() const { return InputAt(1); }
3484
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003485 DECLARE_INSTRUCTION(StaticFieldSet);
3486
3487 private:
3488 const FieldInfo field_info_;
3489
3490 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
3491};
3492
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003493// Implement the move-exception DEX instruction.
3494class HLoadException : public HExpression<0> {
3495 public:
3496 HLoadException() : HExpression(Primitive::kPrimNot, SideEffects::None()) {}
3497
3498 DECLARE_INSTRUCTION(LoadException);
3499
3500 private:
3501 DISALLOW_COPY_AND_ASSIGN(HLoadException);
3502};
3503
3504class HThrow : public HTemplateInstruction<1> {
3505 public:
3506 HThrow(HInstruction* exception, uint32_t dex_pc)
3507 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {
3508 SetRawInputAt(0, exception);
3509 }
3510
3511 bool IsControlFlow() const OVERRIDE { return true; }
3512
3513 bool NeedsEnvironment() const OVERRIDE { return true; }
3514
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003515 bool CanThrow() const OVERRIDE { return true; }
3516
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003517 uint32_t GetDexPc() const { return dex_pc_; }
3518
3519 DECLARE_INSTRUCTION(Throw);
3520
3521 private:
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003522 const uint32_t dex_pc_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003523
3524 DISALLOW_COPY_AND_ASSIGN(HThrow);
3525};
3526
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003527class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003528 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003529 HInstanceOf(HInstruction* object,
3530 HLoadClass* constant,
3531 bool class_is_final,
3532 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003533 : HExpression(Primitive::kPrimBoolean, SideEffects::None()),
3534 class_is_final_(class_is_final),
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003535 must_do_null_check_(true),
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003536 dex_pc_(dex_pc) {
3537 SetRawInputAt(0, object);
3538 SetRawInputAt(1, constant);
3539 }
3540
3541 bool CanBeMoved() const OVERRIDE { return true; }
3542
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003543 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003544 return true;
3545 }
3546
3547 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003548 return false;
3549 }
3550
3551 uint32_t GetDexPc() const { return dex_pc_; }
3552
3553 bool IsClassFinal() const { return class_is_final_; }
3554
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003555 // Used only in code generation.
3556 bool MustDoNullCheck() const { return must_do_null_check_; }
3557 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
3558
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003559 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003560
3561 private:
3562 const bool class_is_final_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003563 bool must_do_null_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003564 const uint32_t dex_pc_;
3565
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003566 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
3567};
3568
Calin Juravleb1498f62015-02-16 13:13:29 +00003569class HBoundType : public HExpression<1> {
3570 public:
3571 HBoundType(HInstruction* input, ReferenceTypeInfo bound_type)
3572 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3573 bound_type_(bound_type) {
Calin Juravle61d544b2015-02-23 16:46:57 +00003574 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00003575 SetRawInputAt(0, input);
3576 }
3577
3578 const ReferenceTypeInfo& GetBoundType() const { return bound_type_; }
3579
3580 bool CanBeNull() const OVERRIDE {
3581 // `null instanceof ClassX` always return false so we can't be null.
3582 return false;
3583 }
3584
3585 DECLARE_INSTRUCTION(BoundType);
3586
3587 private:
3588 // Encodes the most upper class that this instruction can have. In other words
3589 // it is always the case that GetBoundType().IsSupertypeOf(GetReferenceType()).
3590 // It is used to bound the type in cases like `if (x instanceof ClassX) {}`
3591 const ReferenceTypeInfo bound_type_;
3592
3593 DISALLOW_COPY_AND_ASSIGN(HBoundType);
3594};
3595
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003596class HCheckCast : public HTemplateInstruction<2> {
3597 public:
3598 HCheckCast(HInstruction* object,
3599 HLoadClass* constant,
3600 bool class_is_final,
3601 uint32_t dex_pc)
3602 : HTemplateInstruction(SideEffects::None()),
3603 class_is_final_(class_is_final),
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003604 must_do_null_check_(true),
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003605 dex_pc_(dex_pc) {
3606 SetRawInputAt(0, object);
3607 SetRawInputAt(1, constant);
3608 }
3609
3610 bool CanBeMoved() const OVERRIDE { return true; }
3611
3612 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
3613 return true;
3614 }
3615
3616 bool NeedsEnvironment() const OVERRIDE {
3617 // Instruction may throw a CheckCastError.
3618 return true;
3619 }
3620
3621 bool CanThrow() const OVERRIDE { return true; }
3622
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003623 bool MustDoNullCheck() const { return must_do_null_check_; }
3624 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
3625
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003626 uint32_t GetDexPc() const { return dex_pc_; }
3627
3628 bool IsClassFinal() const { return class_is_final_; }
3629
3630 DECLARE_INSTRUCTION(CheckCast);
3631
3632 private:
3633 const bool class_is_final_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003634 bool must_do_null_check_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003635 const uint32_t dex_pc_;
3636
3637 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003638};
3639
Calin Juravle27df7582015-04-17 19:12:31 +01003640class HMemoryBarrier : public HTemplateInstruction<0> {
3641 public:
3642 explicit HMemoryBarrier(MemBarrierKind barrier_kind)
3643 : HTemplateInstruction(SideEffects::None()),
3644 barrier_kind_(barrier_kind) {}
3645
3646 MemBarrierKind GetBarrierKind() { return barrier_kind_; }
3647
3648 DECLARE_INSTRUCTION(MemoryBarrier);
3649
3650 private:
3651 const MemBarrierKind barrier_kind_;
3652
3653 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
3654};
3655
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003656class HMonitorOperation : public HTemplateInstruction<1> {
3657 public:
3658 enum OperationKind {
3659 kEnter,
3660 kExit,
3661 };
3662
3663 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
3664 : HTemplateInstruction(SideEffects::None()), kind_(kind), dex_pc_(dex_pc) {
3665 SetRawInputAt(0, object);
3666 }
3667
3668 // Instruction may throw a Java exception, so we need an environment.
3669 bool NeedsEnvironment() const OVERRIDE { return true; }
3670 bool CanThrow() const OVERRIDE { return true; }
3671
3672 uint32_t GetDexPc() const { return dex_pc_; }
3673
3674 bool IsEnter() const { return kind_ == kEnter; }
3675
3676 DECLARE_INSTRUCTION(MonitorOperation);
3677
Calin Juravle52c48962014-12-16 17:02:57 +00003678 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003679 const OperationKind kind_;
3680 const uint32_t dex_pc_;
3681
3682 private:
3683 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
3684};
3685
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003686class MoveOperands : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003687 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01003688 MoveOperands(Location source,
3689 Location destination,
3690 Primitive::Type type,
3691 HInstruction* instruction)
3692 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003693
3694 Location GetSource() const { return source_; }
3695 Location GetDestination() const { return destination_; }
3696
3697 void SetSource(Location value) { source_ = value; }
3698 void SetDestination(Location value) { destination_ = value; }
3699
3700 // The parallel move resolver marks moves as "in-progress" by clearing the
3701 // destination (but not the source).
3702 Location MarkPending() {
3703 DCHECK(!IsPending());
3704 Location dest = destination_;
3705 destination_ = Location::NoLocation();
3706 return dest;
3707 }
3708
3709 void ClearPending(Location dest) {
3710 DCHECK(IsPending());
3711 destination_ = dest;
3712 }
3713
3714 bool IsPending() const {
3715 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
3716 return destination_.IsInvalid() && !source_.IsInvalid();
3717 }
3718
3719 // True if this blocks a move from the given location.
3720 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08003721 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003722 }
3723
3724 // A move is redundant if it's been eliminated, if its source and
3725 // destination are the same, or if its destination is unneeded.
3726 bool IsRedundant() const {
3727 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
3728 }
3729
3730 // We clear both operands to indicate move that's been eliminated.
3731 void Eliminate() {
3732 source_ = destination_ = Location::NoLocation();
3733 }
3734
3735 bool IsEliminated() const {
3736 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
3737 return source_.IsInvalid();
3738 }
3739
Nicolas Geoffray90218252015-04-15 11:56:51 +01003740 bool Is64BitMove() const {
3741 return Primitive::Is64BitType(type_);
3742 }
3743
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003744 HInstruction* GetInstruction() const { return instruction_; }
3745
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003746 private:
3747 Location source_;
3748 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01003749 // The type this move is for.
3750 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003751 // The instruction this move is assocatied with. Null when this move is
3752 // for moving an input in the expected locations of user (including a phi user).
3753 // This is only used in debug mode, to ensure we do not connect interval siblings
3754 // in the same parallel move.
3755 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003756};
3757
3758static constexpr size_t kDefaultNumberOfMoves = 4;
3759
3760class HParallelMove : public HTemplateInstruction<0> {
3761 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003762 explicit HParallelMove(ArenaAllocator* arena)
3763 : HTemplateInstruction(SideEffects::None()), moves_(arena, kDefaultNumberOfMoves) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003764
Nicolas Geoffray90218252015-04-15 11:56:51 +01003765 void AddMove(Location source,
3766 Location destination,
3767 Primitive::Type type,
3768 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003769 DCHECK(source.IsValid());
3770 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003771 if (kIsDebugBuild) {
3772 if (instruction != nullptr) {
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00003773 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003774 if (moves_.Get(i).GetInstruction() == instruction) {
3775 // Special case the situation where the move is for the spill slot
3776 // of the instruction.
3777 if ((GetPrevious() == instruction)
3778 || ((GetPrevious() == nullptr)
3779 && instruction->IsPhi()
3780 && instruction->GetBlock() == GetBlock())) {
3781 DCHECK_NE(destination.GetKind(), moves_.Get(i).GetDestination().GetKind())
3782 << "Doing parallel moves for the same instruction.";
3783 } else {
3784 DCHECK(false) << "Doing parallel moves for the same instruction.";
3785 }
3786 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00003787 }
3788 }
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003789 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Zheng Xuad4450e2015-04-17 18:48:56 +08003790 DCHECK(!destination.OverlapsWith(moves_.Get(i).GetDestination()))
3791 << "Overlapped destination for two moves in a parallel move.";
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003792 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003793 }
Nicolas Geoffray90218252015-04-15 11:56:51 +01003794 moves_.Add(MoveOperands(source, destination, type, instruction));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003795 }
3796
3797 MoveOperands* MoveOperandsAt(size_t index) const {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003798 return moves_.GetRawStorage() + index;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003799 }
3800
3801 size_t NumMoves() const { return moves_.Size(); }
3802
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003803 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003804
3805 private:
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003806 GrowableArray<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003807
3808 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
3809};
3810
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003811class HGraphVisitor : public ValueObject {
3812 public:
Dave Allison20dfc792014-06-16 20:44:29 -07003813 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
3814 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003815
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003816 virtual void VisitInstruction(HInstruction* instruction) { UNUSED(instruction); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003817 virtual void VisitBasicBlock(HBasicBlock* block);
3818
Roland Levillain633021e2014-10-01 14:12:25 +01003819 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003820 void VisitInsertionOrder();
3821
Roland Levillain633021e2014-10-01 14:12:25 +01003822 // Visit the graph following dominator tree reverse post-order.
3823 void VisitReversePostOrder();
3824
Nicolas Geoffray787c3072014-03-17 10:20:19 +00003825 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00003826
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003827 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003828#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003829 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
3830
3831 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
3832
3833#undef DECLARE_VISIT_INSTRUCTION
3834
3835 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07003836 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003837
3838 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
3839};
3840
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003841class HGraphDelegateVisitor : public HGraphVisitor {
3842 public:
3843 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
3844 virtual ~HGraphDelegateVisitor() {}
3845
3846 // Visit functions that delegate to to super class.
3847#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003848 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003849
3850 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
3851
3852#undef DECLARE_VISIT_INSTRUCTION
3853
3854 private:
3855 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
3856};
3857
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003858class HInsertionOrderIterator : public ValueObject {
3859 public:
3860 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
3861
3862 bool Done() const { return index_ == graph_.GetBlocks().Size(); }
3863 HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); }
3864 void Advance() { ++index_; }
3865
3866 private:
3867 const HGraph& graph_;
3868 size_t index_;
3869
3870 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
3871};
3872
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003873class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003874 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00003875 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
3876 // Check that reverse post order of the graph has been built.
3877 DCHECK(!graph.GetReversePostOrder().IsEmpty());
3878 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003879
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003880 bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); }
3881 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003882 void Advance() { ++index_; }
3883
3884 private:
3885 const HGraph& graph_;
3886 size_t index_;
3887
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003888 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003889};
3890
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003891class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003892 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003893 explicit HPostOrderIterator(const HGraph& graph)
David Brazdil10f56cb2015-03-24 18:49:14 +00003894 : graph_(graph), index_(graph_.GetReversePostOrder().Size()) {
3895 // Check that reverse post order of the graph has been built.
3896 DCHECK(!graph.GetReversePostOrder().IsEmpty());
3897 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003898
3899 bool Done() const { return index_ == 0; }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003900 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003901 void Advance() { --index_; }
3902
3903 private:
3904 const HGraph& graph_;
3905 size_t index_;
3906
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003907 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003908};
3909
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01003910class HLinearPostOrderIterator : public ValueObject {
3911 public:
3912 explicit HLinearPostOrderIterator(const HGraph& graph)
3913 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().Size()) {}
3914
3915 bool Done() const { return index_ == 0; }
3916
3917 HBasicBlock* Current() const { return order_.Get(index_ -1); }
3918
3919 void Advance() {
3920 --index_;
3921 DCHECK_GE(index_, 0U);
3922 }
3923
3924 private:
3925 const GrowableArray<HBasicBlock*>& order_;
3926 size_t index_;
3927
3928 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
3929};
3930
3931class HLinearOrderIterator : public ValueObject {
3932 public:
3933 explicit HLinearOrderIterator(const HGraph& graph)
3934 : order_(graph.GetLinearOrder()), index_(0) {}
3935
3936 bool Done() const { return index_ == order_.Size(); }
3937 HBasicBlock* Current() const { return order_.Get(index_); }
3938 void Advance() { ++index_; }
3939
3940 private:
3941 const GrowableArray<HBasicBlock*>& order_;
3942 size_t index_;
3943
3944 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
3945};
3946
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003947// Iterator over the blocks that art part of the loop. Includes blocks part
3948// of an inner loop. The order in which the blocks are iterated is on their
3949// block id.
3950class HBlocksInLoopIterator : public ValueObject {
3951 public:
3952 explicit HBlocksInLoopIterator(const HLoopInformation& info)
3953 : blocks_in_loop_(info.GetBlocks()),
3954 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
3955 index_(0) {
3956 if (!blocks_in_loop_.IsBitSet(index_)) {
3957 Advance();
3958 }
3959 }
3960
3961 bool Done() const { return index_ == blocks_.Size(); }
3962 HBasicBlock* Current() const { return blocks_.Get(index_); }
3963 void Advance() {
3964 ++index_;
3965 for (size_t e = blocks_.Size(); index_ < e; ++index_) {
3966 if (blocks_in_loop_.IsBitSet(index_)) {
3967 break;
3968 }
3969 }
3970 }
3971
3972 private:
3973 const BitVector& blocks_in_loop_;
3974 const GrowableArray<HBasicBlock*>& blocks_;
3975 size_t index_;
3976
3977 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
3978};
3979
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003980inline int64_t Int64FromConstant(HConstant* constant) {
3981 DCHECK(constant->IsIntConstant() || constant->IsLongConstant());
3982 return constant->IsIntConstant() ? constant->AsIntConstant()->GetValue()
3983 : constant->AsLongConstant()->GetValue();
3984}
3985
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003986} // namespace art
3987
3988#endif // ART_COMPILER_OPTIMIZING_NODES_H_