blob: 5fc047031065ce0af0715efe28c2c4c60570b856 [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 Levillainda4d79b2015-03-24 14:36:11 +00002097 return bit_cast<uint32_t, float>(AsFloatConstant()->GetValue()) ==
2098 bit_cast<uint32_t, float>((-1.0f));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002099 }
2100 bool IsZero() const OVERRIDE {
2101 return AsFloatConstant()->GetValue() == 0.0f;
2102 }
2103 bool IsOne() const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002104 return bit_cast<uint32_t, float>(AsFloatConstant()->GetValue()) ==
2105 bit_cast<uint32_t, float>(1.0f);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002106 }
2107
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002108 DECLARE_INSTRUCTION(FloatConstant);
2109
2110 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002111 explicit HFloatConstant(float value) : HConstant(Primitive::kPrimFloat), value_(value) {}
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002112 explicit HFloatConstant(int32_t value)
2113 : HConstant(Primitive::kPrimFloat), value_(bit_cast<float, int32_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00002114
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002115 const float value_;
2116
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002117 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00002118 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002119 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002120 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2121};
2122
2123class HDoubleConstant : public HConstant {
2124 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002125 double GetValue() const { return value_; }
2126
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002127 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002128 return bit_cast<uint64_t, double>(other->AsDoubleConstant()->value_) ==
2129 bit_cast<uint64_t, double>(value_);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002130 }
2131
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002132 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002133
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002134 bool IsMinusOne() const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002135 return bit_cast<uint64_t, double>(AsDoubleConstant()->GetValue()) ==
2136 bit_cast<uint64_t, double>((-1.0));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002137 }
2138 bool IsZero() const OVERRIDE {
2139 return AsDoubleConstant()->GetValue() == 0.0;
2140 }
2141 bool IsOne() const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002142 return bit_cast<uint64_t, double>(AsDoubleConstant()->GetValue()) ==
2143 bit_cast<uint64_t, double>(1.0);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002144 }
2145
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002146 DECLARE_INSTRUCTION(DoubleConstant);
2147
2148 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002149 explicit HDoubleConstant(double value) : HConstant(Primitive::kPrimDouble), value_(value) {}
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002150 explicit HDoubleConstant(int64_t value)
2151 : HConstant(Primitive::kPrimDouble), value_(bit_cast<double, int64_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00002152
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002153 const double value_;
2154
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002155 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00002156 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002157 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002158 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
2159};
2160
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002161class HNullConstant : public HConstant {
2162 public:
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002163 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2164 return true;
2165 }
2166
2167 size_t ComputeHashCode() const OVERRIDE { return 0; }
2168
2169 DECLARE_INSTRUCTION(NullConstant);
2170
2171 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002172 HNullConstant() : HConstant(Primitive::kPrimNot) {}
2173
2174 friend class HGraph;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002175 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2176};
2177
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002178// Constants of the type int. Those can be from Dex instructions, or
2179// synthesized (for example with the if-eqz instruction).
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002180class HIntConstant : public HConstant {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002181 public:
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002182 int32_t GetValue() const { return value_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002183
Calin Juravle61d544b2015-02-23 16:46:57 +00002184 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002185 return other->AsIntConstant()->value_ == value_;
2186 }
2187
Calin Juravle61d544b2015-02-23 16:46:57 +00002188 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2189
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002190 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2191 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2192 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2193
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002194 DECLARE_INSTRUCTION(IntConstant);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002195
2196 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002197 explicit HIntConstant(int32_t value) : HConstant(Primitive::kPrimInt), value_(value) {}
2198
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002199 const int32_t value_;
2200
David Brazdil8d5b8b22015-03-24 10:51:52 +00002201 friend class HGraph;
2202 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
Zheng Xuad4450e2015-04-17 18:48:56 +08002203 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002204 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2205};
2206
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002207class HLongConstant : public HConstant {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002208 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002209 int64_t GetValue() const { return value_; }
2210
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002211 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002212 return other->AsLongConstant()->value_ == value_;
2213 }
2214
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002215 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002216
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002217 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2218 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2219 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2220
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002221 DECLARE_INSTRUCTION(LongConstant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002222
2223 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002224 explicit HLongConstant(int64_t value) : HConstant(Primitive::kPrimLong), value_(value) {}
2225
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002226 const int64_t value_;
2227
David Brazdil8d5b8b22015-03-24 10:51:52 +00002228 friend class HGraph;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002229 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2230};
2231
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002232enum class Intrinsics {
2233#define OPTIMIZING_INTRINSICS(Name, IsStatic) k ## Name,
2234#include "intrinsics_list.h"
2235 kNone,
2236 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
2237#undef INTRINSICS_LIST
2238#undef OPTIMIZING_INTRINSICS
2239};
2240std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
2241
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002242class HInvoke : public HInstruction {
2243 public:
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002244 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002245
2246 // Runtime needs to walk the stack, so Dex -> Dex calls need to
2247 // know their environment.
Calin Juravle77520bc2015-01-12 18:45:46 +00002248 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002249
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01002250 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002251 SetRawInputAt(index, argument);
2252 }
2253
Roland Levillain3e3d7332015-04-28 11:00:54 +01002254 // Return the number of arguments. This number can be lower than
2255 // the number of inputs returned by InputCount(), as some invoke
2256 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
2257 // inputs at the end of their list of inputs.
2258 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
2259
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002260 Primitive::Type GetType() const OVERRIDE { return return_type_; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002261
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002262 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002263
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002264 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
2265
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01002266 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002267 return intrinsic_;
2268 }
2269
2270 void SetIntrinsic(Intrinsics intrinsic) {
2271 intrinsic_ = intrinsic;
2272 }
2273
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002274 DECLARE_INSTRUCTION(Invoke);
2275
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002276 protected:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002277 HInvoke(ArenaAllocator* arena,
2278 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01002279 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002280 Primitive::Type return_type,
2281 uint32_t dex_pc,
2282 uint32_t dex_method_index)
2283 : HInstruction(SideEffects::All()),
Roland Levillain3e3d7332015-04-28 11:00:54 +01002284 number_of_arguments_(number_of_arguments),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002285 inputs_(arena, number_of_arguments),
2286 return_type_(return_type),
2287 dex_pc_(dex_pc),
2288 dex_method_index_(dex_method_index),
2289 intrinsic_(Intrinsics::kNone) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01002290 uint32_t number_of_inputs = number_of_arguments + number_of_other_inputs;
2291 inputs_.SetSize(number_of_inputs);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002292 }
2293
David Brazdil1abb4192015-02-17 18:33:36 +00002294 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
2295 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
2296 inputs_.Put(index, input);
2297 }
2298
Roland Levillain3e3d7332015-04-28 11:00:54 +01002299 uint32_t number_of_arguments_;
David Brazdil1abb4192015-02-17 18:33:36 +00002300 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002301 const Primitive::Type return_type_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002302 const uint32_t dex_pc_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002303 const uint32_t dex_method_index_;
2304 Intrinsics intrinsic_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002305
2306 private:
2307 DISALLOW_COPY_AND_ASSIGN(HInvoke);
2308};
2309
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002310class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002311 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01002312 // Requirements of this method call regarding the class
2313 // initialization (clinit) check of its declaring class.
2314 enum class ClinitCheckRequirement {
2315 kNone, // Class already initialized.
2316 kExplicit, // Static call having explicit clinit check as last input.
2317 kImplicit, // Static call implicitly requiring a clinit check.
2318 };
2319
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002320 HInvokeStaticOrDirect(ArenaAllocator* arena,
2321 uint32_t number_of_arguments,
2322 Primitive::Type return_type,
2323 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002324 uint32_t dex_method_index,
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002325 bool is_recursive,
Jeff Hao848f70a2014-01-15 13:49:50 -08002326 int32_t string_init_offset,
Nicolas Geoffray79041292015-03-26 10:05:54 +00002327 InvokeType original_invoke_type,
Roland Levillain4c0eb422015-04-24 16:43:49 +01002328 InvokeType invoke_type,
2329 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01002330 : HInvoke(arena,
2331 number_of_arguments,
2332 clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u,
2333 return_type,
2334 dex_pc,
2335 dex_method_index),
Nicolas Geoffray79041292015-03-26 10:05:54 +00002336 original_invoke_type_(original_invoke_type),
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002337 invoke_type_(invoke_type),
Roland Levillain4c0eb422015-04-24 16:43:49 +01002338 is_recursive_(is_recursive),
Jeff Hao848f70a2014-01-15 13:49:50 -08002339 clinit_check_requirement_(clinit_check_requirement),
2340 string_init_offset_(string_init_offset) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002341
Calin Juravle641547a2015-04-21 22:08:51 +01002342 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
2343 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00002344 // We access the method via the dex cache so we can't do an implicit null check.
2345 // TODO: for intrinsics we can generate implicit null checks.
2346 return false;
2347 }
2348
Nicolas Geoffray79041292015-03-26 10:05:54 +00002349 InvokeType GetOriginalInvokeType() const { return original_invoke_type_; }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002350 InvokeType GetInvokeType() const { return invoke_type_; }
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002351 bool IsRecursive() const { return is_recursive_; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002352 bool NeedsDexCache() const OVERRIDE { return !IsRecursive(); }
Jeff Hao848f70a2014-01-15 13:49:50 -08002353 bool IsStringInit() const { return string_init_offset_ != 0; }
2354 int32_t GetStringInitOffset() const { return string_init_offset_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002355
Roland Levillain4c0eb422015-04-24 16:43:49 +01002356 // Is this instruction a call to a static method?
2357 bool IsStatic() const {
2358 return GetInvokeType() == kStatic;
2359 }
2360
Roland Levillain3e3d7332015-04-28 11:00:54 +01002361 // Remove the art::HLoadClass instruction set as last input by
2362 // art::PrepareForRegisterAllocation::VisitClinitCheck in lieu of
2363 // the initial art::HClinitCheck instruction (only relevant for
2364 // static calls with explicit clinit check).
2365 void RemoveLoadClassAsLastInput() {
Roland Levillain4c0eb422015-04-24 16:43:49 +01002366 DCHECK(IsStaticWithExplicitClinitCheck());
2367 size_t last_input_index = InputCount() - 1;
2368 HInstruction* last_input = InputAt(last_input_index);
2369 DCHECK(last_input != nullptr);
Roland Levillain3e3d7332015-04-28 11:00:54 +01002370 DCHECK(last_input->IsLoadClass()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01002371 RemoveAsUserOfInput(last_input_index);
2372 inputs_.DeleteAt(last_input_index);
2373 clinit_check_requirement_ = ClinitCheckRequirement::kImplicit;
2374 DCHECK(IsStaticWithImplicitClinitCheck());
2375 }
2376
2377 // Is this a call to a static method whose declaring class has an
2378 // explicit intialization check in the graph?
2379 bool IsStaticWithExplicitClinitCheck() const {
2380 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kExplicit);
2381 }
2382
2383 // Is this a call to a static method whose declaring class has an
2384 // implicit intialization check requirement?
2385 bool IsStaticWithImplicitClinitCheck() const {
2386 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kImplicit);
2387 }
2388
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002389 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002390
Roland Levillain4c0eb422015-04-24 16:43:49 +01002391 protected:
2392 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
2393 const HUserRecord<HInstruction*> input_record = HInvoke::InputRecordAt(i);
2394 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck() && (i == InputCount() - 1)) {
2395 HInstruction* input = input_record.GetInstruction();
2396 // `input` is the last input of a static invoke marked as having
2397 // an explicit clinit check. It must either be:
2398 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
2399 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
2400 DCHECK(input != nullptr);
2401 DCHECK(input->IsClinitCheck() || input->IsLoadClass()) << input->DebugName();
2402 }
2403 return input_record;
2404 }
2405
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002406 private:
Nicolas Geoffray79041292015-03-26 10:05:54 +00002407 const InvokeType original_invoke_type_;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002408 const InvokeType invoke_type_;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002409 const bool is_recursive_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01002410 ClinitCheckRequirement clinit_check_requirement_;
Jeff Hao848f70a2014-01-15 13:49:50 -08002411 // Thread entrypoint offset for string init method if this is a string init invoke.
2412 // Note that there are multiple string init methods, each having its own offset.
2413 int32_t string_init_offset_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002414
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002415 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002416};
2417
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002418class HInvokeVirtual : public HInvoke {
2419 public:
2420 HInvokeVirtual(ArenaAllocator* arena,
2421 uint32_t number_of_arguments,
2422 Primitive::Type return_type,
2423 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002424 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002425 uint32_t vtable_index)
Roland Levillain3e3d7332015-04-28 11:00:54 +01002426 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002427 vtable_index_(vtable_index) {}
2428
Calin Juravle641547a2015-04-21 22:08:51 +01002429 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00002430 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01002431 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00002432 }
2433
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002434 uint32_t GetVTableIndex() const { return vtable_index_; }
2435
2436 DECLARE_INSTRUCTION(InvokeVirtual);
2437
2438 private:
2439 const uint32_t vtable_index_;
2440
2441 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
2442};
2443
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002444class HInvokeInterface : public HInvoke {
2445 public:
2446 HInvokeInterface(ArenaAllocator* arena,
2447 uint32_t number_of_arguments,
2448 Primitive::Type return_type,
2449 uint32_t dex_pc,
2450 uint32_t dex_method_index,
2451 uint32_t imt_index)
Roland Levillain3e3d7332015-04-28 11:00:54 +01002452 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002453 imt_index_(imt_index) {}
2454
Calin Juravle641547a2015-04-21 22:08:51 +01002455 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00002456 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01002457 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00002458 }
2459
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002460 uint32_t GetImtIndex() const { return imt_index_; }
2461 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
2462
2463 DECLARE_INSTRUCTION(InvokeInterface);
2464
2465 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002466 const uint32_t imt_index_;
2467
2468 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
2469};
2470
Dave Allison20dfc792014-06-16 20:44:29 -07002471class HNewInstance : public HExpression<0> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002472 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002473 HNewInstance(uint32_t dex_pc, uint16_t type_index, QuickEntrypointEnum entrypoint)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002474 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2475 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002476 type_index_(type_index),
2477 entrypoint_(entrypoint) {}
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002478
2479 uint32_t GetDexPc() const { return dex_pc_; }
2480 uint16_t GetTypeIndex() const { return type_index_; }
2481
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002482 // Calls runtime so needs an environment.
Calin Juravle92a6ed22014-12-02 18:58:03 +00002483 bool NeedsEnvironment() const OVERRIDE { return true; }
2484 // It may throw when called on:
2485 // - interfaces
2486 // - abstract/innaccessible/unknown classes
2487 // TODO: optimize when possible.
2488 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002489
Calin Juravle10e244f2015-01-26 18:54:32 +00002490 bool CanBeNull() const OVERRIDE { return false; }
2491
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002492 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
2493
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002494 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002495
2496 private:
2497 const uint32_t dex_pc_;
2498 const uint16_t type_index_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002499 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002500
2501 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
2502};
2503
Roland Levillain88cb1752014-10-20 16:36:47 +01002504class HNeg : public HUnaryOperation {
2505 public:
2506 explicit HNeg(Primitive::Type result_type, HInstruction* input)
2507 : HUnaryOperation(result_type, input) {}
2508
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002509 int32_t Evaluate(int32_t x) const OVERRIDE { return -x; }
2510 int64_t Evaluate(int64_t x) const OVERRIDE { return -x; }
Roland Levillain9240d6a2014-10-20 16:47:04 +01002511
Roland Levillain88cb1752014-10-20 16:36:47 +01002512 DECLARE_INSTRUCTION(Neg);
2513
2514 private:
2515 DISALLOW_COPY_AND_ASSIGN(HNeg);
2516};
2517
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002518class HNewArray : public HExpression<1> {
2519 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002520 HNewArray(HInstruction* length,
2521 uint32_t dex_pc,
2522 uint16_t type_index,
2523 QuickEntrypointEnum entrypoint)
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002524 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2525 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002526 type_index_(type_index),
2527 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002528 SetRawInputAt(0, length);
2529 }
2530
2531 uint32_t GetDexPc() const { return dex_pc_; }
2532 uint16_t GetTypeIndex() const { return type_index_; }
2533
2534 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00002535 bool NeedsEnvironment() const OVERRIDE { return true; }
2536
Mingyao Yang0c365e62015-03-31 15:09:29 -07002537 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
2538 bool CanThrow() const OVERRIDE { return true; }
2539
Calin Juravle10e244f2015-01-26 18:54:32 +00002540 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002541
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002542 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
2543
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002544 DECLARE_INSTRUCTION(NewArray);
2545
2546 private:
2547 const uint32_t dex_pc_;
2548 const uint16_t type_index_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002549 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002550
2551 DISALLOW_COPY_AND_ASSIGN(HNewArray);
2552};
2553
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002554class HAdd : public HBinaryOperation {
2555 public:
2556 HAdd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2557 : HBinaryOperation(result_type, left, right) {}
2558
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002559 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002560
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002561 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002562 return x + y;
2563 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002564 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002565 return x + y;
2566 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002567
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002568 DECLARE_INSTRUCTION(Add);
2569
2570 private:
2571 DISALLOW_COPY_AND_ASSIGN(HAdd);
2572};
2573
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002574class HSub : public HBinaryOperation {
2575 public:
2576 HSub(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2577 : HBinaryOperation(result_type, left, right) {}
2578
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002579 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002580 return x - y;
2581 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002582 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002583 return x - y;
2584 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002585
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002586 DECLARE_INSTRUCTION(Sub);
2587
2588 private:
2589 DISALLOW_COPY_AND_ASSIGN(HSub);
2590};
2591
Calin Juravle34bacdf2014-10-07 20:23:36 +01002592class HMul : public HBinaryOperation {
2593 public:
2594 HMul(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2595 : HBinaryOperation(result_type, left, right) {}
2596
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002597 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002598
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002599 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x * y; }
2600 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x * y; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002601
2602 DECLARE_INSTRUCTION(Mul);
2603
2604 private:
2605 DISALLOW_COPY_AND_ASSIGN(HMul);
2606};
2607
Calin Juravle7c4954d2014-10-28 16:57:40 +00002608class HDiv : public HBinaryOperation {
2609 public:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002610 HDiv(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
2611 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00002612
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002613 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00002614 // Our graph structure ensures we never have 0 for `y` during constant folding.
2615 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00002616 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00002617 return (y == -1) ? -x : x / y;
2618 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002619
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002620 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002621 DCHECK_NE(y, 0);
2622 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2623 return (y == -1) ? -x : x / y;
2624 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002625
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002626 uint32_t GetDexPc() const { return dex_pc_; }
2627
Calin Juravle7c4954d2014-10-28 16:57:40 +00002628 DECLARE_INSTRUCTION(Div);
2629
2630 private:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002631 const uint32_t dex_pc_;
2632
Calin Juravle7c4954d2014-10-28 16:57:40 +00002633 DISALLOW_COPY_AND_ASSIGN(HDiv);
2634};
2635
Calin Juravlebacfec32014-11-14 15:54:36 +00002636class HRem : public HBinaryOperation {
2637 public:
2638 HRem(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
2639 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
2640
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002641 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002642 DCHECK_NE(y, 0);
2643 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2644 return (y == -1) ? 0 : x % y;
2645 }
2646
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002647 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002648 DCHECK_NE(y, 0);
2649 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2650 return (y == -1) ? 0 : x % y;
2651 }
2652
2653 uint32_t GetDexPc() const { return dex_pc_; }
2654
2655 DECLARE_INSTRUCTION(Rem);
2656
2657 private:
2658 const uint32_t dex_pc_;
2659
2660 DISALLOW_COPY_AND_ASSIGN(HRem);
2661};
2662
Calin Juravled0d48522014-11-04 16:40:20 +00002663class HDivZeroCheck : public HExpression<1> {
2664 public:
2665 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
2666 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
2667 SetRawInputAt(0, value);
2668 }
2669
2670 bool CanBeMoved() const OVERRIDE { return true; }
2671
2672 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2673 UNUSED(other);
2674 return true;
2675 }
2676
2677 bool NeedsEnvironment() const OVERRIDE { return true; }
2678 bool CanThrow() const OVERRIDE { return true; }
2679
2680 uint32_t GetDexPc() const { return dex_pc_; }
2681
2682 DECLARE_INSTRUCTION(DivZeroCheck);
2683
2684 private:
2685 const uint32_t dex_pc_;
2686
2687 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
2688};
2689
Calin Juravle9aec02f2014-11-18 23:06:35 +00002690class HShl : public HBinaryOperation {
2691 public:
2692 HShl(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2693 : HBinaryOperation(result_type, left, right) {}
2694
2695 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x << (y & kMaxIntShiftValue); }
2696 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x << (y & kMaxLongShiftValue); }
2697
2698 DECLARE_INSTRUCTION(Shl);
2699
2700 private:
2701 DISALLOW_COPY_AND_ASSIGN(HShl);
2702};
2703
2704class HShr : public HBinaryOperation {
2705 public:
2706 HShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2707 : HBinaryOperation(result_type, left, right) {}
2708
2709 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x >> (y & kMaxIntShiftValue); }
2710 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x >> (y & kMaxLongShiftValue); }
2711
2712 DECLARE_INSTRUCTION(Shr);
2713
2714 private:
2715 DISALLOW_COPY_AND_ASSIGN(HShr);
2716};
2717
2718class HUShr : public HBinaryOperation {
2719 public:
2720 HUShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2721 : HBinaryOperation(result_type, left, right) {}
2722
2723 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
2724 uint32_t ux = static_cast<uint32_t>(x);
2725 uint32_t uy = static_cast<uint32_t>(y) & kMaxIntShiftValue;
2726 return static_cast<int32_t>(ux >> uy);
2727 }
2728
2729 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
2730 uint64_t ux = static_cast<uint64_t>(x);
2731 uint64_t uy = static_cast<uint64_t>(y) & kMaxLongShiftValue;
2732 return static_cast<int64_t>(ux >> uy);
2733 }
2734
2735 DECLARE_INSTRUCTION(UShr);
2736
2737 private:
2738 DISALLOW_COPY_AND_ASSIGN(HUShr);
2739};
2740
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002741class HAnd : public HBinaryOperation {
2742 public:
2743 HAnd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2744 : HBinaryOperation(result_type, left, right) {}
2745
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002746 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002747
2748 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x & y; }
2749 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x & y; }
2750
2751 DECLARE_INSTRUCTION(And);
2752
2753 private:
2754 DISALLOW_COPY_AND_ASSIGN(HAnd);
2755};
2756
2757class HOr : public HBinaryOperation {
2758 public:
2759 HOr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2760 : HBinaryOperation(result_type, left, right) {}
2761
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002762 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002763
2764 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x | y; }
2765 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x | y; }
2766
2767 DECLARE_INSTRUCTION(Or);
2768
2769 private:
2770 DISALLOW_COPY_AND_ASSIGN(HOr);
2771};
2772
2773class HXor : public HBinaryOperation {
2774 public:
2775 HXor(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2776 : HBinaryOperation(result_type, left, right) {}
2777
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002778 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002779
2780 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x ^ y; }
2781 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x ^ y; }
2782
2783 DECLARE_INSTRUCTION(Xor);
2784
2785 private:
2786 DISALLOW_COPY_AND_ASSIGN(HXor);
2787};
2788
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002789// The value of a parameter in this method. Its location depends on
2790// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07002791class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002792 public:
Calin Juravle10e244f2015-01-26 18:54:32 +00002793 HParameterValue(uint8_t index, Primitive::Type parameter_type, bool is_this = false)
2794 : HExpression(parameter_type, SideEffects::None()), index_(index), is_this_(is_this) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002795
2796 uint8_t GetIndex() const { return index_; }
2797
Calin Juravle10e244f2015-01-26 18:54:32 +00002798 bool CanBeNull() const OVERRIDE { return !is_this_; }
2799
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002800 DECLARE_INSTRUCTION(ParameterValue);
2801
2802 private:
2803 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00002804 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002805 const uint8_t index_;
2806
Calin Juravle10e244f2015-01-26 18:54:32 +00002807 // Whether or not the parameter value corresponds to 'this' argument.
2808 const bool is_this_;
2809
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002810 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
2811};
2812
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002813class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002814 public:
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002815 explicit HNot(Primitive::Type result_type, HInstruction* input)
2816 : HUnaryOperation(result_type, input) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002817
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002818 bool CanBeMoved() const OVERRIDE { return true; }
2819 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002820 UNUSED(other);
2821 return true;
2822 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002823
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002824 int32_t Evaluate(int32_t x) const OVERRIDE { return ~x; }
2825 int64_t Evaluate(int64_t x) const OVERRIDE { return ~x; }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002826
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002827 DECLARE_INSTRUCTION(Not);
2828
2829 private:
2830 DISALLOW_COPY_AND_ASSIGN(HNot);
2831};
2832
David Brazdil66d126e2015-04-03 16:02:44 +01002833class HBooleanNot : public HUnaryOperation {
2834 public:
2835 explicit HBooleanNot(HInstruction* input)
2836 : HUnaryOperation(Primitive::Type::kPrimBoolean, input) {}
2837
2838 bool CanBeMoved() const OVERRIDE { return true; }
2839 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2840 UNUSED(other);
2841 return true;
2842 }
2843
2844 int32_t Evaluate(int32_t x) const OVERRIDE {
2845 DCHECK(IsUint<1>(x));
2846 return !x;
2847 }
2848
2849 int64_t Evaluate(int64_t x ATTRIBUTE_UNUSED) const OVERRIDE {
2850 LOG(FATAL) << DebugName() << " cannot be used with 64-bit values";
2851 UNREACHABLE();
2852 }
2853
2854 DECLARE_INSTRUCTION(BooleanNot);
2855
2856 private:
2857 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
2858};
2859
Roland Levillaindff1f282014-11-05 14:15:05 +00002860class HTypeConversion : public HExpression<1> {
2861 public:
2862 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00002863 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
2864 : HExpression(result_type, SideEffects::None()), dex_pc_(dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002865 SetRawInputAt(0, input);
2866 DCHECK_NE(input->GetType(), result_type);
2867 }
2868
2869 HInstruction* GetInput() const { return InputAt(0); }
2870 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
2871 Primitive::Type GetResultType() const { return GetType(); }
2872
Roland Levillain624279f2014-12-04 11:54:28 +00002873 // Required by the x86 and ARM code generators when producing calls
2874 // to the runtime.
2875 uint32_t GetDexPc() const { return dex_pc_; }
2876
Roland Levillaindff1f282014-11-05 14:15:05 +00002877 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00002878 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00002879
2880 DECLARE_INSTRUCTION(TypeConversion);
2881
2882 private:
Roland Levillain624279f2014-12-04 11:54:28 +00002883 const uint32_t dex_pc_;
2884
Roland Levillaindff1f282014-11-05 14:15:05 +00002885 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
2886};
2887
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00002888static constexpr uint32_t kNoRegNumber = -1;
2889
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002890class HPhi : public HInstruction {
2891 public:
2892 HPhi(ArenaAllocator* arena, uint32_t reg_number, size_t number_of_inputs, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002893 : HInstruction(SideEffects::None()),
2894 inputs_(arena, number_of_inputs),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002895 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002896 type_(type),
Calin Juravle10e244f2015-01-26 18:54:32 +00002897 is_live_(false),
2898 can_be_null_(true) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002899 inputs_.SetSize(number_of_inputs);
2900 }
2901
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00002902 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
2903 static Primitive::Type ToPhiType(Primitive::Type type) {
2904 switch (type) {
2905 case Primitive::kPrimBoolean:
2906 case Primitive::kPrimByte:
2907 case Primitive::kPrimShort:
2908 case Primitive::kPrimChar:
2909 return Primitive::kPrimInt;
2910 default:
2911 return type;
2912 }
2913 }
2914
Calin Juravle10e244f2015-01-26 18:54:32 +00002915 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002916
2917 void AddInput(HInstruction* input);
David Brazdil2d7352b2015-04-20 14:52:42 +01002918 void RemoveInputAt(size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002919
Calin Juravle10e244f2015-01-26 18:54:32 +00002920 Primitive::Type GetType() const OVERRIDE { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002921 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002922
Calin Juravle10e244f2015-01-26 18:54:32 +00002923 bool CanBeNull() const OVERRIDE { return can_be_null_; }
2924 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
2925
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002926 uint32_t GetRegNumber() const { return reg_number_; }
2927
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002928 void SetDead() { is_live_ = false; }
2929 void SetLive() { is_live_ = true; }
2930 bool IsDead() const { return !is_live_; }
2931 bool IsLive() const { return is_live_; }
2932
Calin Juravlea4f88312015-04-16 12:57:19 +01002933 // Returns the next equivalent phi (starting from the current one) or null if there is none.
2934 // An equivalent phi is a phi having the same dex register and type.
2935 // It assumes that phis with the same dex register are adjacent.
2936 HPhi* GetNextEquivalentPhiWithSameType() {
2937 HInstruction* next = GetNext();
2938 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
2939 if (next->GetType() == GetType()) {
2940 return next->AsPhi();
2941 }
2942 next = next->GetNext();
2943 }
2944 return nullptr;
2945 }
2946
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002947 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002948
David Brazdil1abb4192015-02-17 18:33:36 +00002949 protected:
2950 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
2951
2952 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
2953 inputs_.Put(index, input);
2954 }
2955
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002956 private:
David Brazdil1abb4192015-02-17 18:33:36 +00002957 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002958 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002959 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002960 bool is_live_;
Calin Juravle10e244f2015-01-26 18:54:32 +00002961 bool can_be_null_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002962
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002963 DISALLOW_COPY_AND_ASSIGN(HPhi);
2964};
2965
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002966class HNullCheck : public HExpression<1> {
2967 public:
2968 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002969 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002970 SetRawInputAt(0, value);
2971 }
2972
Calin Juravle10e244f2015-01-26 18:54:32 +00002973 bool CanBeMoved() const OVERRIDE { return true; }
2974 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002975 UNUSED(other);
2976 return true;
2977 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002978
Calin Juravle10e244f2015-01-26 18:54:32 +00002979 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002980
Calin Juravle10e244f2015-01-26 18:54:32 +00002981 bool CanThrow() const OVERRIDE { return true; }
2982
2983 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01002984
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002985 uint32_t GetDexPc() const { return dex_pc_; }
2986
2987 DECLARE_INSTRUCTION(NullCheck);
2988
2989 private:
2990 const uint32_t dex_pc_;
2991
2992 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
2993};
2994
2995class FieldInfo : public ValueObject {
2996 public:
Calin Juravle52c48962014-12-16 17:02:57 +00002997 FieldInfo(MemberOffset field_offset, Primitive::Type field_type, bool is_volatile)
2998 : field_offset_(field_offset), field_type_(field_type), is_volatile_(is_volatile) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002999
3000 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003001 Primitive::Type GetFieldType() const { return field_type_; }
Calin Juravle52c48962014-12-16 17:02:57 +00003002 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003003
3004 private:
3005 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01003006 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00003007 const bool is_volatile_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003008};
3009
3010class HInstanceFieldGet : public HExpression<1> {
3011 public:
3012 HInstanceFieldGet(HInstruction* value,
3013 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003014 MemberOffset field_offset,
3015 bool is_volatile)
Nicolas Geoffray2af23072015-04-30 11:15:40 +00003016 : HExpression(field_type, SideEffects::DependsOnSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00003017 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003018 SetRawInputAt(0, value);
3019 }
3020
Calin Juravle10c9cbe2014-12-19 10:50:19 +00003021 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003022
3023 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3024 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
3025 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003026 }
3027
Calin Juravle641547a2015-04-21 22:08:51 +01003028 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3029 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00003030 }
3031
3032 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01003033 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
3034 }
3035
Calin Juravle52c48962014-12-16 17:02:57 +00003036 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003037 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003038 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003039 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003040
3041 DECLARE_INSTRUCTION(InstanceFieldGet);
3042
3043 private:
3044 const FieldInfo field_info_;
3045
3046 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
3047};
3048
3049class HInstanceFieldSet : public HTemplateInstruction<2> {
3050 public:
3051 HInstanceFieldSet(HInstruction* object,
3052 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01003053 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003054 MemberOffset field_offset,
3055 bool is_volatile)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003056 : HTemplateInstruction(SideEffects::ChangesSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00003057 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003058 SetRawInputAt(0, object);
3059 SetRawInputAt(1, value);
3060 }
3061
Calin Juravle641547a2015-04-21 22:08:51 +01003062 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3063 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00003064 }
3065
Calin Juravle52c48962014-12-16 17:02:57 +00003066 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003067 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003068 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003069 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003070 HInstruction* GetValue() const { return InputAt(1); }
3071
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003072 DECLARE_INSTRUCTION(InstanceFieldSet);
3073
3074 private:
3075 const FieldInfo field_info_;
3076
3077 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
3078};
3079
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003080class HArrayGet : public HExpression<2> {
3081 public:
3082 HArrayGet(HInstruction* array, HInstruction* index, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003083 : HExpression(type, SideEffects::DependsOnSomething()) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003084 SetRawInputAt(0, array);
3085 SetRawInputAt(1, index);
3086 }
3087
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003088 bool CanBeMoved() const OVERRIDE { return true; }
3089 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003090 UNUSED(other);
3091 return true;
3092 }
Calin Juravle641547a2015-04-21 22:08:51 +01003093 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3094 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00003095 // TODO: We can be smarter here.
3096 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
3097 // which generates the implicit null check. There are cases when these can be removed
3098 // to produce better code. If we ever add optimizations to do so we should allow an
3099 // implicit check here (as long as the address falls in the first page).
3100 return false;
3101 }
3102
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003103 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003104
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003105 HInstruction* GetArray() const { return InputAt(0); }
3106 HInstruction* GetIndex() const { return InputAt(1); }
3107
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003108 DECLARE_INSTRUCTION(ArrayGet);
3109
3110 private:
3111 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
3112};
3113
3114class HArraySet : public HTemplateInstruction<3> {
3115 public:
3116 HArraySet(HInstruction* array,
3117 HInstruction* index,
3118 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003119 Primitive::Type expected_component_type,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003120 uint32_t dex_pc)
3121 : HTemplateInstruction(SideEffects::ChangesSomething()),
3122 dex_pc_(dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003123 expected_component_type_(expected_component_type),
3124 needs_type_check_(value->GetType() == Primitive::kPrimNot) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003125 SetRawInputAt(0, array);
3126 SetRawInputAt(1, index);
3127 SetRawInputAt(2, value);
3128 }
3129
Calin Juravle77520bc2015-01-12 18:45:46 +00003130 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003131 // We currently always call a runtime method to catch array store
3132 // exceptions.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003133 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003134 }
3135
Calin Juravle641547a2015-04-21 22:08:51 +01003136 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3137 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00003138 // TODO: Same as for ArrayGet.
3139 return false;
3140 }
3141
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003142 void ClearNeedsTypeCheck() {
3143 needs_type_check_ = false;
3144 }
3145
3146 bool NeedsTypeCheck() const { return needs_type_check_; }
3147
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003148 uint32_t GetDexPc() const { return dex_pc_; }
3149
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003150 HInstruction* GetArray() const { return InputAt(0); }
3151 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003152 HInstruction* GetValue() const { return InputAt(2); }
3153
3154 Primitive::Type GetComponentType() const {
3155 // The Dex format does not type floating point index operations. Since the
3156 // `expected_component_type_` is set during building and can therefore not
3157 // be correct, we also check what is the value type. If it is a floating
3158 // point type, we must use that type.
3159 Primitive::Type value_type = GetValue()->GetType();
3160 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
3161 ? value_type
3162 : expected_component_type_;
3163 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003164
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003165 DECLARE_INSTRUCTION(ArraySet);
3166
3167 private:
3168 const uint32_t dex_pc_;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003169 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003170 bool needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003171
3172 DISALLOW_COPY_AND_ASSIGN(HArraySet);
3173};
3174
3175class HArrayLength : public HExpression<1> {
3176 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003177 explicit HArrayLength(HInstruction* array)
3178 : HExpression(Primitive::kPrimInt, SideEffects::None()) {
3179 // Note that arrays do not change length, so the instruction does not
3180 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003181 SetRawInputAt(0, array);
3182 }
3183
Calin Juravle77520bc2015-01-12 18:45:46 +00003184 bool CanBeMoved() const OVERRIDE { return true; }
3185 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003186 UNUSED(other);
3187 return true;
3188 }
Calin Juravle641547a2015-04-21 22:08:51 +01003189 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3190 return obj == InputAt(0);
3191 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003192
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003193 DECLARE_INSTRUCTION(ArrayLength);
3194
3195 private:
3196 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
3197};
3198
3199class HBoundsCheck : public HExpression<2> {
3200 public:
3201 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003202 : HExpression(index->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003203 DCHECK(index->GetType() == Primitive::kPrimInt);
3204 SetRawInputAt(0, index);
3205 SetRawInputAt(1, length);
3206 }
3207
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003208 bool CanBeMoved() const OVERRIDE { return true; }
3209 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003210 UNUSED(other);
3211 return true;
3212 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003213
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003214 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003215
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003216 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01003217
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003218 uint32_t GetDexPc() const { return dex_pc_; }
3219
3220 DECLARE_INSTRUCTION(BoundsCheck);
3221
3222 private:
3223 const uint32_t dex_pc_;
3224
3225 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
3226};
3227
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003228/**
3229 * Some DEX instructions are folded into multiple HInstructions that need
3230 * to stay live until the last HInstruction. This class
3231 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00003232 * HInstruction stays live. `index` represents the stack location index of the
3233 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003234 */
3235class HTemporary : public HTemplateInstruction<0> {
3236 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003237 explicit HTemporary(size_t index) : HTemplateInstruction(SideEffects::None()), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003238
3239 size_t GetIndex() const { return index_; }
3240
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00003241 Primitive::Type GetType() const OVERRIDE {
3242 // The previous instruction is the one that will be stored in the temporary location.
3243 DCHECK(GetPrevious() != nullptr);
3244 return GetPrevious()->GetType();
3245 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00003246
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003247 DECLARE_INSTRUCTION(Temporary);
3248
3249 private:
3250 const size_t index_;
3251
3252 DISALLOW_COPY_AND_ASSIGN(HTemporary);
3253};
3254
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003255class HSuspendCheck : public HTemplateInstruction<0> {
3256 public:
3257 explicit HSuspendCheck(uint32_t dex_pc)
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01003258 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003259
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003260 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003261 return true;
3262 }
3263
3264 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01003265 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
3266 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003267
3268 DECLARE_INSTRUCTION(SuspendCheck);
3269
3270 private:
3271 const uint32_t dex_pc_;
3272
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01003273 // Only used for code generation, in order to share the same slow path between back edges
3274 // of a same loop.
3275 SlowPathCode* slow_path_;
3276
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003277 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
3278};
3279
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003280/**
3281 * Instruction to load a Class object.
3282 */
3283class HLoadClass : public HExpression<0> {
3284 public:
3285 HLoadClass(uint16_t type_index,
3286 bool is_referrers_class,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003287 uint32_t dex_pc)
3288 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3289 type_index_(type_index),
3290 is_referrers_class_(is_referrers_class),
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003291 dex_pc_(dex_pc),
Calin Juravleb1498f62015-02-16 13:13:29 +00003292 generate_clinit_check_(false),
3293 loaded_class_rti_(ReferenceTypeInfo::CreateTop(/* is_exact */ false)) {}
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003294
3295 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003296
3297 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3298 return other->AsLoadClass()->type_index_ == type_index_;
3299 }
3300
3301 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
3302
3303 uint32_t GetDexPc() const { return dex_pc_; }
3304 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003305 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003306
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003307 bool NeedsEnvironment() const OVERRIDE {
3308 // Will call runtime and load the class if the class is not loaded yet.
3309 // TODO: finer grain decision.
3310 return !is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003311 }
3312
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003313 bool MustGenerateClinitCheck() const {
3314 return generate_clinit_check_;
3315 }
3316
3317 void SetMustGenerateClinitCheck() {
3318 generate_clinit_check_ = true;
3319 }
3320
3321 bool CanCallRuntime() const {
3322 return MustGenerateClinitCheck() || !is_referrers_class_;
3323 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003324
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003325 bool CanThrow() const OVERRIDE {
3326 // May call runtime and and therefore can throw.
3327 // TODO: finer grain decision.
3328 return !is_referrers_class_;
3329 }
3330
Calin Juravleacf735c2015-02-12 15:25:22 +00003331 ReferenceTypeInfo GetLoadedClassRTI() {
3332 return loaded_class_rti_;
3333 }
3334
3335 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
3336 // Make sure we only set exact types (the loaded class should never be merged).
3337 DCHECK(rti.IsExact());
3338 loaded_class_rti_ = rti;
3339 }
3340
3341 bool IsResolved() {
3342 return loaded_class_rti_.IsExact();
3343 }
3344
Nicolas Geoffray9437b782015-03-25 10:08:51 +00003345 bool NeedsDexCache() const OVERRIDE { return !is_referrers_class_; }
3346
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003347 DECLARE_INSTRUCTION(LoadClass);
3348
3349 private:
3350 const uint16_t type_index_;
3351 const bool is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003352 const uint32_t dex_pc_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003353 // Whether this instruction must generate the initialization check.
3354 // Used for code generation.
3355 bool generate_clinit_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003356
Calin Juravleacf735c2015-02-12 15:25:22 +00003357 ReferenceTypeInfo loaded_class_rti_;
3358
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003359 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
3360};
3361
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003362class HLoadString : public HExpression<0> {
3363 public:
3364 HLoadString(uint32_t string_index, uint32_t dex_pc)
3365 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3366 string_index_(string_index),
3367 dex_pc_(dex_pc) {}
3368
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003369 bool CanBeMoved() const OVERRIDE { return true; }
3370
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003371 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3372 return other->AsLoadString()->string_index_ == string_index_;
3373 }
3374
3375 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
3376
3377 uint32_t GetDexPc() const { return dex_pc_; }
3378 uint32_t GetStringIndex() const { return string_index_; }
3379
3380 // TODO: Can we deopt or debug when we resolve a string?
3381 bool NeedsEnvironment() const OVERRIDE { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00003382 bool NeedsDexCache() const OVERRIDE { return true; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003383
3384 DECLARE_INSTRUCTION(LoadString);
3385
3386 private:
3387 const uint32_t string_index_;
3388 const uint32_t dex_pc_;
3389
3390 DISALLOW_COPY_AND_ASSIGN(HLoadString);
3391};
3392
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003393/**
3394 * Performs an initialization check on its Class object input.
3395 */
3396class HClinitCheck : public HExpression<1> {
3397 public:
3398 explicit HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Nicolas Geoffraya0466e12015-03-27 15:00:40 +00003399 : HExpression(Primitive::kPrimNot, SideEffects::ChangesSomething()),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003400 dex_pc_(dex_pc) {
3401 SetRawInputAt(0, constant);
3402 }
3403
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003404 bool CanBeMoved() const OVERRIDE { return true; }
3405 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3406 UNUSED(other);
3407 return true;
3408 }
3409
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003410 bool NeedsEnvironment() const OVERRIDE {
3411 // May call runtime to initialize the class.
3412 return true;
3413 }
3414
3415 uint32_t GetDexPc() const { return dex_pc_; }
3416
3417 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
3418
3419 DECLARE_INSTRUCTION(ClinitCheck);
3420
3421 private:
3422 const uint32_t dex_pc_;
3423
3424 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
3425};
3426
3427class HStaticFieldGet : public HExpression<1> {
3428 public:
3429 HStaticFieldGet(HInstruction* cls,
3430 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003431 MemberOffset field_offset,
3432 bool is_volatile)
Nicolas Geoffray2af23072015-04-30 11:15:40 +00003433 : HExpression(field_type, SideEffects::DependsOnSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00003434 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003435 SetRawInputAt(0, cls);
3436 }
3437
Calin Juravle52c48962014-12-16 17:02:57 +00003438
Calin Juravle10c9cbe2014-12-19 10:50:19 +00003439 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003440
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003441 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00003442 HStaticFieldGet* other_get = other->AsStaticFieldGet();
3443 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003444 }
3445
3446 size_t ComputeHashCode() const OVERRIDE {
3447 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
3448 }
3449
Calin Juravle52c48962014-12-16 17:02:57 +00003450 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003451 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
3452 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003453 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003454
3455 DECLARE_INSTRUCTION(StaticFieldGet);
3456
3457 private:
3458 const FieldInfo field_info_;
3459
3460 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
3461};
3462
3463class HStaticFieldSet : public HTemplateInstruction<2> {
3464 public:
3465 HStaticFieldSet(HInstruction* cls,
3466 HInstruction* value,
3467 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003468 MemberOffset field_offset,
3469 bool is_volatile)
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003470 : HTemplateInstruction(SideEffects::ChangesSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00003471 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003472 SetRawInputAt(0, cls);
3473 SetRawInputAt(1, value);
3474 }
3475
Calin Juravle52c48962014-12-16 17:02:57 +00003476 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003477 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
3478 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003479 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003480
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003481 HInstruction* GetValue() const { return InputAt(1); }
3482
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003483 DECLARE_INSTRUCTION(StaticFieldSet);
3484
3485 private:
3486 const FieldInfo field_info_;
3487
3488 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
3489};
3490
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003491// Implement the move-exception DEX instruction.
3492class HLoadException : public HExpression<0> {
3493 public:
3494 HLoadException() : HExpression(Primitive::kPrimNot, SideEffects::None()) {}
3495
3496 DECLARE_INSTRUCTION(LoadException);
3497
3498 private:
3499 DISALLOW_COPY_AND_ASSIGN(HLoadException);
3500};
3501
3502class HThrow : public HTemplateInstruction<1> {
3503 public:
3504 HThrow(HInstruction* exception, uint32_t dex_pc)
3505 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {
3506 SetRawInputAt(0, exception);
3507 }
3508
3509 bool IsControlFlow() const OVERRIDE { return true; }
3510
3511 bool NeedsEnvironment() const OVERRIDE { return true; }
3512
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003513 bool CanThrow() const OVERRIDE { return true; }
3514
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003515 uint32_t GetDexPc() const { return dex_pc_; }
3516
3517 DECLARE_INSTRUCTION(Throw);
3518
3519 private:
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003520 const uint32_t dex_pc_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003521
3522 DISALLOW_COPY_AND_ASSIGN(HThrow);
3523};
3524
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003525class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003526 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003527 HInstanceOf(HInstruction* object,
3528 HLoadClass* constant,
3529 bool class_is_final,
3530 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003531 : HExpression(Primitive::kPrimBoolean, SideEffects::None()),
3532 class_is_final_(class_is_final),
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003533 must_do_null_check_(true),
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003534 dex_pc_(dex_pc) {
3535 SetRawInputAt(0, object);
3536 SetRawInputAt(1, constant);
3537 }
3538
3539 bool CanBeMoved() const OVERRIDE { return true; }
3540
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003541 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003542 return true;
3543 }
3544
3545 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003546 return false;
3547 }
3548
3549 uint32_t GetDexPc() const { return dex_pc_; }
3550
3551 bool IsClassFinal() const { return class_is_final_; }
3552
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003553 // Used only in code generation.
3554 bool MustDoNullCheck() const { return must_do_null_check_; }
3555 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
3556
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003557 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003558
3559 private:
3560 const bool class_is_final_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003561 bool must_do_null_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003562 const uint32_t dex_pc_;
3563
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003564 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
3565};
3566
Calin Juravleb1498f62015-02-16 13:13:29 +00003567class HBoundType : public HExpression<1> {
3568 public:
3569 HBoundType(HInstruction* input, ReferenceTypeInfo bound_type)
3570 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3571 bound_type_(bound_type) {
Calin Juravle61d544b2015-02-23 16:46:57 +00003572 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00003573 SetRawInputAt(0, input);
3574 }
3575
3576 const ReferenceTypeInfo& GetBoundType() const { return bound_type_; }
3577
3578 bool CanBeNull() const OVERRIDE {
3579 // `null instanceof ClassX` always return false so we can't be null.
3580 return false;
3581 }
3582
3583 DECLARE_INSTRUCTION(BoundType);
3584
3585 private:
3586 // Encodes the most upper class that this instruction can have. In other words
3587 // it is always the case that GetBoundType().IsSupertypeOf(GetReferenceType()).
3588 // It is used to bound the type in cases like `if (x instanceof ClassX) {}`
3589 const ReferenceTypeInfo bound_type_;
3590
3591 DISALLOW_COPY_AND_ASSIGN(HBoundType);
3592};
3593
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003594class HCheckCast : public HTemplateInstruction<2> {
3595 public:
3596 HCheckCast(HInstruction* object,
3597 HLoadClass* constant,
3598 bool class_is_final,
3599 uint32_t dex_pc)
3600 : HTemplateInstruction(SideEffects::None()),
3601 class_is_final_(class_is_final),
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003602 must_do_null_check_(true),
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003603 dex_pc_(dex_pc) {
3604 SetRawInputAt(0, object);
3605 SetRawInputAt(1, constant);
3606 }
3607
3608 bool CanBeMoved() const OVERRIDE { return true; }
3609
3610 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
3611 return true;
3612 }
3613
3614 bool NeedsEnvironment() const OVERRIDE {
3615 // Instruction may throw a CheckCastError.
3616 return true;
3617 }
3618
3619 bool CanThrow() const OVERRIDE { return true; }
3620
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003621 bool MustDoNullCheck() const { return must_do_null_check_; }
3622 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
3623
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003624 uint32_t GetDexPc() const { return dex_pc_; }
3625
3626 bool IsClassFinal() const { return class_is_final_; }
3627
3628 DECLARE_INSTRUCTION(CheckCast);
3629
3630 private:
3631 const bool class_is_final_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003632 bool must_do_null_check_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003633 const uint32_t dex_pc_;
3634
3635 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003636};
3637
Calin Juravle27df7582015-04-17 19:12:31 +01003638class HMemoryBarrier : public HTemplateInstruction<0> {
3639 public:
3640 explicit HMemoryBarrier(MemBarrierKind barrier_kind)
3641 : HTemplateInstruction(SideEffects::None()),
3642 barrier_kind_(barrier_kind) {}
3643
3644 MemBarrierKind GetBarrierKind() { return barrier_kind_; }
3645
3646 DECLARE_INSTRUCTION(MemoryBarrier);
3647
3648 private:
3649 const MemBarrierKind barrier_kind_;
3650
3651 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
3652};
3653
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003654class HMonitorOperation : public HTemplateInstruction<1> {
3655 public:
3656 enum OperationKind {
3657 kEnter,
3658 kExit,
3659 };
3660
3661 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
3662 : HTemplateInstruction(SideEffects::None()), kind_(kind), dex_pc_(dex_pc) {
3663 SetRawInputAt(0, object);
3664 }
3665
3666 // Instruction may throw a Java exception, so we need an environment.
3667 bool NeedsEnvironment() const OVERRIDE { return true; }
3668 bool CanThrow() const OVERRIDE { return true; }
3669
3670 uint32_t GetDexPc() const { return dex_pc_; }
3671
3672 bool IsEnter() const { return kind_ == kEnter; }
3673
3674 DECLARE_INSTRUCTION(MonitorOperation);
3675
Calin Juravle52c48962014-12-16 17:02:57 +00003676 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003677 const OperationKind kind_;
3678 const uint32_t dex_pc_;
3679
3680 private:
3681 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
3682};
3683
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003684class MoveOperands : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003685 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01003686 MoveOperands(Location source,
3687 Location destination,
3688 Primitive::Type type,
3689 HInstruction* instruction)
3690 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003691
3692 Location GetSource() const { return source_; }
3693 Location GetDestination() const { return destination_; }
3694
3695 void SetSource(Location value) { source_ = value; }
3696 void SetDestination(Location value) { destination_ = value; }
3697
3698 // The parallel move resolver marks moves as "in-progress" by clearing the
3699 // destination (but not the source).
3700 Location MarkPending() {
3701 DCHECK(!IsPending());
3702 Location dest = destination_;
3703 destination_ = Location::NoLocation();
3704 return dest;
3705 }
3706
3707 void ClearPending(Location dest) {
3708 DCHECK(IsPending());
3709 destination_ = dest;
3710 }
3711
3712 bool IsPending() const {
3713 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
3714 return destination_.IsInvalid() && !source_.IsInvalid();
3715 }
3716
3717 // True if this blocks a move from the given location.
3718 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08003719 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003720 }
3721
3722 // A move is redundant if it's been eliminated, if its source and
3723 // destination are the same, or if its destination is unneeded.
3724 bool IsRedundant() const {
3725 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
3726 }
3727
3728 // We clear both operands to indicate move that's been eliminated.
3729 void Eliminate() {
3730 source_ = destination_ = Location::NoLocation();
3731 }
3732
3733 bool IsEliminated() const {
3734 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
3735 return source_.IsInvalid();
3736 }
3737
Nicolas Geoffray90218252015-04-15 11:56:51 +01003738 bool Is64BitMove() const {
3739 return Primitive::Is64BitType(type_);
3740 }
3741
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003742 HInstruction* GetInstruction() const { return instruction_; }
3743
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003744 private:
3745 Location source_;
3746 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01003747 // The type this move is for.
3748 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003749 // The instruction this move is assocatied with. Null when this move is
3750 // for moving an input in the expected locations of user (including a phi user).
3751 // This is only used in debug mode, to ensure we do not connect interval siblings
3752 // in the same parallel move.
3753 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003754};
3755
3756static constexpr size_t kDefaultNumberOfMoves = 4;
3757
3758class HParallelMove : public HTemplateInstruction<0> {
3759 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003760 explicit HParallelMove(ArenaAllocator* arena)
3761 : HTemplateInstruction(SideEffects::None()), moves_(arena, kDefaultNumberOfMoves) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003762
Nicolas Geoffray90218252015-04-15 11:56:51 +01003763 void AddMove(Location source,
3764 Location destination,
3765 Primitive::Type type,
3766 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003767 DCHECK(source.IsValid());
3768 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003769 if (kIsDebugBuild) {
3770 if (instruction != nullptr) {
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00003771 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003772 if (moves_.Get(i).GetInstruction() == instruction) {
3773 // Special case the situation where the move is for the spill slot
3774 // of the instruction.
3775 if ((GetPrevious() == instruction)
3776 || ((GetPrevious() == nullptr)
3777 && instruction->IsPhi()
3778 && instruction->GetBlock() == GetBlock())) {
3779 DCHECK_NE(destination.GetKind(), moves_.Get(i).GetDestination().GetKind())
3780 << "Doing parallel moves for the same instruction.";
3781 } else {
3782 DCHECK(false) << "Doing parallel moves for the same instruction.";
3783 }
3784 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00003785 }
3786 }
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003787 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Zheng Xuad4450e2015-04-17 18:48:56 +08003788 DCHECK(!destination.OverlapsWith(moves_.Get(i).GetDestination()))
3789 << "Overlapped destination for two moves in a parallel move.";
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003790 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003791 }
Nicolas Geoffray90218252015-04-15 11:56:51 +01003792 moves_.Add(MoveOperands(source, destination, type, instruction));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003793 }
3794
3795 MoveOperands* MoveOperandsAt(size_t index) const {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003796 return moves_.GetRawStorage() + index;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003797 }
3798
3799 size_t NumMoves() const { return moves_.Size(); }
3800
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003801 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003802
3803 private:
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003804 GrowableArray<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003805
3806 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
3807};
3808
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003809class HGraphVisitor : public ValueObject {
3810 public:
Dave Allison20dfc792014-06-16 20:44:29 -07003811 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
3812 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003813
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003814 virtual void VisitInstruction(HInstruction* instruction) { UNUSED(instruction); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003815 virtual void VisitBasicBlock(HBasicBlock* block);
3816
Roland Levillain633021e2014-10-01 14:12:25 +01003817 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003818 void VisitInsertionOrder();
3819
Roland Levillain633021e2014-10-01 14:12:25 +01003820 // Visit the graph following dominator tree reverse post-order.
3821 void VisitReversePostOrder();
3822
Nicolas Geoffray787c3072014-03-17 10:20:19 +00003823 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00003824
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003825 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003826#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003827 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
3828
3829 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
3830
3831#undef DECLARE_VISIT_INSTRUCTION
3832
3833 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07003834 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003835
3836 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
3837};
3838
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003839class HGraphDelegateVisitor : public HGraphVisitor {
3840 public:
3841 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
3842 virtual ~HGraphDelegateVisitor() {}
3843
3844 // Visit functions that delegate to to super class.
3845#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003846 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003847
3848 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
3849
3850#undef DECLARE_VISIT_INSTRUCTION
3851
3852 private:
3853 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
3854};
3855
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003856class HInsertionOrderIterator : public ValueObject {
3857 public:
3858 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
3859
3860 bool Done() const { return index_ == graph_.GetBlocks().Size(); }
3861 HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); }
3862 void Advance() { ++index_; }
3863
3864 private:
3865 const HGraph& graph_;
3866 size_t index_;
3867
3868 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
3869};
3870
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003871class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003872 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00003873 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
3874 // Check that reverse post order of the graph has been built.
3875 DCHECK(!graph.GetReversePostOrder().IsEmpty());
3876 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003877
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003878 bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); }
3879 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003880 void Advance() { ++index_; }
3881
3882 private:
3883 const HGraph& graph_;
3884 size_t index_;
3885
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003886 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003887};
3888
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003889class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003890 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003891 explicit HPostOrderIterator(const HGraph& graph)
David Brazdil10f56cb2015-03-24 18:49:14 +00003892 : graph_(graph), index_(graph_.GetReversePostOrder().Size()) {
3893 // Check that reverse post order of the graph has been built.
3894 DCHECK(!graph.GetReversePostOrder().IsEmpty());
3895 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003896
3897 bool Done() const { return index_ == 0; }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003898 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003899 void Advance() { --index_; }
3900
3901 private:
3902 const HGraph& graph_;
3903 size_t index_;
3904
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003905 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003906};
3907
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01003908class HLinearPostOrderIterator : public ValueObject {
3909 public:
3910 explicit HLinearPostOrderIterator(const HGraph& graph)
3911 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().Size()) {}
3912
3913 bool Done() const { return index_ == 0; }
3914
3915 HBasicBlock* Current() const { return order_.Get(index_ -1); }
3916
3917 void Advance() {
3918 --index_;
3919 DCHECK_GE(index_, 0U);
3920 }
3921
3922 private:
3923 const GrowableArray<HBasicBlock*>& order_;
3924 size_t index_;
3925
3926 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
3927};
3928
3929class HLinearOrderIterator : public ValueObject {
3930 public:
3931 explicit HLinearOrderIterator(const HGraph& graph)
3932 : order_(graph.GetLinearOrder()), index_(0) {}
3933
3934 bool Done() const { return index_ == order_.Size(); }
3935 HBasicBlock* Current() const { return order_.Get(index_); }
3936 void Advance() { ++index_; }
3937
3938 private:
3939 const GrowableArray<HBasicBlock*>& order_;
3940 size_t index_;
3941
3942 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
3943};
3944
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003945// Iterator over the blocks that art part of the loop. Includes blocks part
3946// of an inner loop. The order in which the blocks are iterated is on their
3947// block id.
3948class HBlocksInLoopIterator : public ValueObject {
3949 public:
3950 explicit HBlocksInLoopIterator(const HLoopInformation& info)
3951 : blocks_in_loop_(info.GetBlocks()),
3952 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
3953 index_(0) {
3954 if (!blocks_in_loop_.IsBitSet(index_)) {
3955 Advance();
3956 }
3957 }
3958
3959 bool Done() const { return index_ == blocks_.Size(); }
3960 HBasicBlock* Current() const { return blocks_.Get(index_); }
3961 void Advance() {
3962 ++index_;
3963 for (size_t e = blocks_.Size(); index_ < e; ++index_) {
3964 if (blocks_in_loop_.IsBitSet(index_)) {
3965 break;
3966 }
3967 }
3968 }
3969
3970 private:
3971 const BitVector& blocks_in_loop_;
3972 const GrowableArray<HBasicBlock*>& blocks_;
3973 size_t index_;
3974
3975 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
3976};
3977
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003978inline int64_t Int64FromConstant(HConstant* constant) {
3979 DCHECK(constant->IsIntConstant() || constant->IsLongConstant());
3980 return constant->IsIntConstant() ? constant->AsIntConstant()->GetValue()
3981 : constant->AsLongConstant()->GetValue();
3982}
3983
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003984} // namespace art
3985
3986#endif // ART_COMPILER_OPTIMIZING_NODES_H_